How to convert CGPoint in NSValue in swift?

前端 未结 5 1640
北海茫月
北海茫月 2020-12-11 00:34

As my question title says how can I convert CGPoint into NSValues so I can store then Into array In swift.

In objective-C

相关标签:
5条回答
  • 2020-12-11 01:00

    swift 3

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

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

    0 讨论(0)
  • 2020-12-11 01:12

    swift 4

    let point = NSValue.init(cgPoint: mask.position)
    
    0 讨论(0)
  • 2020-12-11 01:15

    Exactly as you'd imagine:

    let point = CGPoint(x: 9.0, y: 9.0)
    let pointObj = NSValue(CGPoint: point)
    let newPoint = pointObj.CGPointValue()
    
    0 讨论(0)
  • 2020-12-11 01:20

    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]
    
    0 讨论(0)
  • 2020-12-11 01:24

    Try something like that:

    let point = CGPointMake(9, 9)
    var pointObj = NSValue(CGPoint: point)
    
    0 讨论(0)
提交回复
热议问题