Swift UnsafeMutablePointer&amp; UnsafeMutablePointer <unsafepointer <sometype。这时候>&GT;

时间:2015-07-09 07:45:59

标签: swift

我尝试使用NSView&#39; getRectsBeingDrawn,而且我不太清楚如何投射所需的变量。

它的定义是:

func getRectsBeingDrawn(rects: UnsafeMutablePointer<UnsafePointer<NSRect>>, count: UnsafeMutablePointer<Int>)

我已将rectscount定义为:

var rects: UnsafeMutablePointer<UnsafePointer<NSRect>> = nil            
var count: UnsafeMutablePointer<Int> = nil

似乎有用。调试器没有为查看指针中的内容提供任何方便的帮助。

rect被调用之后,

getRectsBeingDrawn应该指向一个NSRect数组,但它似乎什么也没有返回。另外我如何得到rects和count指向的数组和int?

我试图从Apple样本中获取一段代码&#34; TableViewPlayground&#34;在swift工作。

- (void)drawSelectionInRect:(NSRect)dirtyRect {
    NSColor *primaryColor = [[NSColor alternateSelectedControlColor] colorWithAlphaComponent:0.5];
    NSColor *secondarySelectedControlColor = [[NSColor secondarySelectedControlColor] colorWithAlphaComponent:0.5];

// Implement our own custom alpha drawing
    switch (self.selectionHighlightStyle) {
        case NSTableViewSelectionHighlightStyleRegular: {
            if (self.selected) {
                if (self.emphasized) {
                    [primaryColor set];
                } else { 
                    [secondarySelectedControlColor set];
                }
                NSRect bounds = self.bounds;
                const NSRect *rects = NULL;
                NSInteger count = 0;
                [self getRectsBeingDrawn:&rects count:&count];
                for (NSInteger i = 0; i < count; i++) {
                    NSRect rect = NSIntersectionRect(bounds, rects[i]);
                    NSRectFillUsingOperation(rect, NSCompositeSourceOver);
                }
            }
            break;
        }
        default: {
            // Do super's drawing
            [super drawSelectionInRect:dirtyRect];
            break;
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

我已经成功地解决了这个问题......我没有为rectscount正确分配内存

我的意思是:

var rects: UnsafeMutablePointer<UnsafePointer<NSRect>> = nil
var count: UnsafeMutablePointer<Int> = nil

最终解决了这个问题的原因是:

var rects: UnsafeMutablePointer<UnsafePointer<NSRect>> = UnsafeMutablePointer<UnsafePointer<NSRect>>.alloc(1)
var count: UnsafeMutablePointer<Int> = UnsafeMutablePointer<Int>.alloc(1)

希望这有助于其他人:)

编辑如果有人有兴趣,以下是上面的swift代码:

override func drawSelectionInRect(dirtyRect: NSRect) {
    var primaryColor = NSColor.alternateSelectedControlColor().colorWithAlphaComponent(0.5)
    var secondarySelectedControlColor = NSColor.secondarySelectedControlColor().colorWithAlphaComponent(0.5)

    switch (self.selectionHighlightStyle){
    case .Regular:
        if (self.selected){
            if self.emphasized{
                primaryColor.set()
            }else{
                secondarySelectedControlColor.set()
            }
            let bounds = self.bounds

            var rects: UnsafeMutablePointer<UnsafePointer<NSRect>> = UnsafeMutablePointer<UnsafePointer<NSRect>>.alloc(1)
            var count: UnsafeMutablePointer<Int> = UnsafeMutablePointer<Int>.alloc(1)

            self.getRectsBeingDrawn(rects, count: count)

            let rectArrayPtr = UnsafeMutableBufferPointer(start: rects, count: count.memory)
            var base = rectArrayPtr.baseAddress

            for var i = 0; i < count.memory; i++ {
                var currentRect = base.memory
                let rect = NSIntersectionRect(bounds, currentRect.memory)
                NSRectFillUsingOperation(rect, .CompositeSourceOver)
                base.successor()
            }

        }
    default:
            super.drawSelectionInRect(dirtyRect)
        }
}

修改

根据进一步的评论,这里采用了一种不那么冗长的方法(同样由下面的Paul Patterson)实现了同样的目标:

http://stackoverflow.com/questions/32536855/using-getrectsbeingdrawn-with-swift