gpt4 book ai didi

Swift UnsafeMutablePointer & UnsafeMutablePointer>

转载 作者:搜寻专家 更新时间:2023-11-01 05:44:18 33 4
gpt4 key购买 nike

我正在尝试使用 NSView 的 getRectsBeingDrawn,但我不太确定如何转换它需要的变量。

它的定义是:

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

我已经将 rectscount 定义为:

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

这似乎有效。调试器没有提供任何方便的东西来查看指针中的内容,所以这没有帮助。

在调用 getRectsBeingDrawn 之后,

rect 应该指向一个NSRect 数组,但它似乎没有返回任何内容。另外,如何获取 rects 和 count 指向的数组和 int?

我正在尝试从 Apple 示例“TableViewPlayground”中获取一段代码以快速工作。

- (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;
}
}
}

最佳答案

我已经设法弄清楚了...我没有为 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)

希望这对其他人有帮助:)

edit 如果有人感兴趣,下面是上面的 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

关于Swift UnsafeMutablePointer & UnsafeMutablePointer<UnsafePointer<SomeType>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31311166/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com