How to convert CGPoint in NSValue in swift?

混江龙づ霸主 提交于 2019-11-28 11:55:04

Try something like that:

let point = CGPointMake(9, 9)
var pointObj = NSValue(CGPoint: point)

Exactly as you'd imagine:

let point = CGPoint(x: 9.0, y: 9.0)
let pointObj = NSValue(CGPoint: point)
let newPoint = pointObj.CGPointValue()
Abizern

If you aren't planning on using the array in Objective-C, and can keep it as a Swift Array, then you don't need to turn the point into an NSValue, you can just add the point to the array directly:

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]

swift 3

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

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

swift 4

let point = NSValue.init(cgPoint: mask.position)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!