如何在swift中转换NSValue中的CGPoint?

Dha*_*esh 20 cgpoint ios nsvalue swift

正如我的问题标题说我怎么能转换CGPointNSValues所以我可以存储然后进入数组在swift中.

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

  // CGPoint converted to NSValue
     CGPoint point = CGPointMake(9, 9);
     NSValue *pointObj = [NSValue valueWithCGPoint:point];
Run Code Online (Sandbox Code Playgroud)

但有人能告诉我,我怎么能在swift中做到这一点?

提前致谢.

der*_*ida 38

尝试类似的东西:

let point = CGPointMake(9, 9)
var pointObj = NSValue(CGPoint: point)
Run Code Online (Sandbox Code Playgroud)


Ste*_*ton 11

正如你想象的那样:

let point = CGPoint(x: 9.0, y: 9.0)
let pointObj = NSValue(CGPoint: point)
let newPoint = pointObj.CGPointValue()
Run Code Online (Sandbox Code Playgroud)


Abi*_*ern 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]
Run Code Online (Sandbox Code Playgroud)