20

正如我的问题标题所说,我怎样才能转换CGPoint成这样我就可以在swiftNSValues中存储 Into array In 。

在 Objective-C 中,我们可以这样做:

  // CGPoint converted to NSValue
     CGPoint point = CGPointMake(9, 9);
     NSValue *pointObj = [NSValue valueWithCGPoint:point];

但是任何人都可以告诉我,我怎样才能迅速做到这一点?

提前致谢。

4

5 回答 5

38

尝试这样的事情:

let point = CGPointMake(9, 9)
var pointObj = NSValue(CGPoint: point)
于 2014-10-08T08:12:16.233 回答
12

和你想象的完全一样:

let point = CGPoint(x: 9.0, y: 9.0)
let pointObj = NSValue(CGPoint: point)
let newPoint = pointObj.CGPointValue()
于 2014-10-08T08:14:02.227 回答
7

如果您不打算在 Objective-C 中使用数组,并且可以将其保留为 Swift 数组,那么您不需要将点转换为 NSValue,您可以直接将点添加到数组中:

let point1 = CGPoint(x: 9.0, y: 9.0)
let point2 = CGPoint(x: 10.0, y: 10.0)

let pointArray = [point1, point2] // pointArray is inferred to be of type [CGPoint]

let valuesArray = pointsArray as [NSValue]
于 2015-01-01T12:16:15.677 回答
1

迅速 4

let point = NSValue.init(cgPoint: mask.position)
于 2018-01-31T06:06:31.573 回答
1

迅速 3

let pointObj =  NSValue(cgPoint: CGPoint(x:9, y:9))

https://developer.apple.com/reference/foundation/nsvalue/1624531-init

于 2016-08-14T23:13:38.160 回答