[英]How to convert CGPoint in NSValue in swift?
As my question title says how can I convert CGPoint
into NSValues
so I can store then Into array In swift . 正如我的问题标题所述,如何将
CGPoint
转换为NSValues
以便然后将其存储到swift中的数组中。
In objective-C we can do it like this: 在Objective-C中,我们可以这样做:
// CGPoint converted to NSValue
CGPoint point = CGPointMake(9, 9);
NSValue *pointObj = [NSValue valueWithCGPoint:point];
But can anybody tell me that how can I do this in swift? 但是有人能告诉我如何迅速做到这一点吗?
Thanks In Advance. 提前致谢。
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()
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: 如果您不打算在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]
swift 3 迅捷3
let pointObj = NSValue(cgPoint: CGPoint(x:9, y:9))
https://developer.apple.com/reference/foundation/nsvalue/1624531-init https://developer.apple.com/reference/foundation/nsvalue/1624531-init
迅捷4
let point = NSValue.init(cgPoint: mask.position)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.