gpt4 book ai didi

ios - 如何快速创建 UnsafeMutablePointer 对象

转载 作者:搜寻专家 更新时间:2023-10-30 23:06:27 25 4
gpt4 key购买 nike

我需要调用 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

借助 Swift 的语法糖

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

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

关于ios - 如何快速创建 UnsafeMutablePointer<CGPoint> 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35309372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com