Swift UnsafeMutablePointer & UnsafeMutablePointer<UnsafePointer<SomeType>>

Adrian Sluyters

I'm trying to use NSView's getRectsBeingDrawn and I'm not too sure as to how to cast the vars that it needs.

The definition of it is:

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

I've defined both rects and count as:

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

which seems to work. The debugger doesn't come up with anything handy for seeing what's at the pointers so that's no help.

rect should point to an array of NSRect after getRectsBeingDrawn is called, but it seems to return nothing. Also how do I get the array and int which rects and count are pointing to?

I'm trying to get a piece of code from the Apple sample "TableViewPlayground" to work in 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;
        }
    }
}
Adrian Sluyters

I've managed to suss this out... I wasn't allocating the memory properly for rects and count

What I had was:

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

What ultimately fixed the issue was:

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

Hope this helps someone else :)

edit If anyone is interested, here's what the swift code for the above turned out to be:

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)
        }
}

edit

As per a further comment, the same has been achieved here with a less verbose approach (thanks to Paul Patterson below):

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert UnsafeMutablePointer to UnsafePointer

Swift get value from UnsafeMutablePointer<Void> using UnsafePointer<String>

How to create UnsafeMutablePointer<UnsafeMutablePointer> in swift

UnsafeMutablePointer to expected argument type UnsafeMutablePointer<_> in Swift 3

Assigning UnsafeMutablePointer value to UnsafePointer without unsafeBitCast

UnsafeMutablePointer<CFTypeRef> in Swift 3

Error in UnsafeMutablePointer in swift3

Swift 2 - UnsafeMutablePointer<Void> to object

Using UnsafeMutablePointer and CFRunLoopObserverContext in swift 2

How to use UnsafeMutablePointer in Swift 3?

Cast a Swift struct to UnsafeMutablePointer<Void>

What is UnsafeMutablePointer<Unmanaged<CMSampleBuffer>?> in Swift?

How to use UnsafeMutablePointer<OpaquePointer> in Swift?

UnsafeMutablePointer<Bytef> swift3

Swift: Cannot convert value of type 'UnsafeMutablePointer' to expected argument type 'UnsafeMutablePointer'

Cannot convert value of type 'UnsafePointer<UnsafeMutablePointer<Float>>?' to expected argument type 'UnsafePointer<Float>?'

In Swift 3.1, UnsafeMutablePointer.initialize(from:) is deprecated

Swift convert Data to UnsafeMutablePointer<Int8>

Cast a swift dictionary ( [String:AnyObject] ) to a UnsafeMutablePointer

Convert UnsafeMutableRawPointer to UnsafeMutablePointer<T> in swift 3

How to create UnsafeMutablePointer<CGPoint> Object in swift

How to cast self to UnsafeMutablePointer<Void> type in swift

Swift convert string to UnsafeMutablePointer<Int8>

Cannot assign value of type UnsafeMutablePointer ObjCBool in Swift

How to dealloc UnsafeMutablePointer referenced from Swift struct

Memset to UnsafeMutablePointer<UInt8> in swift

Swift: Interoperability of UnsafeMutablePointer.deallocate(capacity:) with free()

UnsafeMutablePointer<Int8> from String in Swift

Can´t get the SecKey UnsafeMutablePointer in iOS Swift