ios – 如何在swift中创建UnsafeMutablePointer对象

前端之家收集整理的这篇文章主要介绍了ios – 如何在swift中创建UnsafeMutablePointer对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要调用UIScrollViewDelegate方法,我不知道如何创建UnsafeMutablePointer对象.

let pointer:UnsafeMutablePointer<CGPoint>  = CGPoint(x: 0,y: 1) as! UnsafeMutablePointer<CGPoint>

self.scrollViewWillEndDragging(self.collectionView,withVelocity: CGPoint(x: 0,y: 1),targetContentOffset: pointer)

func scrollViewWillEndDragging(scrollView: UIScrollView,withVelocity velocity: CGPoint,targetContentOffset: UnsafeMutablePointer<CGPoint>) 
{  
     //Some code here
}

解决方法

import Foundation
var point = CGPoint(x: 10.0,y: 20.0)
let p = withUnsafeMutablePointer(&point) { (p) -> UnsafeMutablePointer<CGPoint> in
    return p
}

print(p.memory.x,p.memory.y) // 10.0 20.0
point.x = 100.0
print(p.memory.x,p.memory.y) // 100.0 20.0

借助斯威夫特的句法糖

import Foundation
var point = CGPoint(x: 10.0,y: 20.0)
let p = withUnsafeMutablePointer(&point) { $0 }

print(p.dynamicType) // UnsafeMutablePointer<CGPoint>

猜你在找的Xcode相关文章