2015-09-12 22 views
0

我试图在Swift应用中使用NSView调用getRectsBeingDrawn(_:count:),但无法理解如何解开'返回'值 - 该方法的签名特别神秘。我通过count变量获得预期的矩形数量,但我不知道如何访问数组中的矩形。 This question解决了同样的问题,并提出了一个解决方案,但它不适合我 - 我无法访问NSRect结构。使用getRectsBeingDrawn:与Swift

func decideWhatToRedraw() { 

    let r1 = CGRect(x: 0, y: 0, width: 10, height: 20) 
    let r2 = CGRect(x: 0, y: 100, width: 35, height: 15) 

    setNeedsDisplayInRect(r1) 
    setNeedsDisplayInRect(r2) 
} 

override func drawRect(dirtyRect: NSRect) { 
    var rects: UnsafeMutablePointer<UnsafePointer<NSRect>> = UnsafeMutablePointer<UnsafePointer<NSRect>>.alloc(1) 
    var count: Int = 0 
    getRectsBeingDrawn(rects, count: &count) 

    // count -> 2 
    // But how to get the rects? 
} 

回答

1

这是你想要什么:

var rects = UnsafePointer<NSRect>() 
var count = Int() 
getRectsBeingDrawn(&rects, count: &count) 

for i in 0 ..< count { 

    let rect = rects[i] 

    // do things with 'rect' here 

} 

你让两个变量rectscount,并通过引用他们两个,所以他们得到信息填满。

调用getRectsBeingDrawn后,rects指向count矩形,您可以使用下标访问矩形,就像数组一样。

+0

王牌! - 谢谢。我会更新我提到的问题,指出这个更直接(和成功)的方法。 –

+0

Swift 4:'init()不可用:使用nil literal' – adib