gpt4 book ai didi

cocoa - NSPathControl 和 NSPathControlItem 是满是错误还是我? (优胜美地)

转载 作者:行者123 更新时间:2023-11-28 09:34:56 24 4
gpt4 key购买 nike

Apple 将 NSPathControl 更改为与 Yosemite 中的 NSPathControlItem 一起使用。

但从我的角度来看,这些新类(class)根本不起作用。我试图在我的数据结构中显示自定义路径,但我在使用常规文件路径时遇到了类似的问题。是我还是苹果?

这是我的代码:

第一个片段的工作原理与此相同,它将显示一条路径。但这就是所有有效的方法。

//MARK: notifications
func selectionDidChange(notification : NSNotification)
{
if let item = notification.object as? Group
{
//get "path" components
var components : [String] = [item.title ?? "a"]
var ancestor : Group? = item.parent
while (ancestor != nil)
{
components.append(ancestor?.title ?? "b")
ancestor = ancestor?.parent
}
components.append("")

//convert to url
let path = ("MyScheme:/" + "/".join(components.reverse()))
pathControl?.URL = NSURL(string: path.stringByAddingPe
}
}

单击路径的任何部分以尝试从 NSPathControlItem 中获取任何属性根本不起作用。一切都返回零。

@IBAction func select(sender : AnyObject)
{
println(sender.clickedPathItem??.title)
println(sender.clickedPathItem??.URL)
}

如果我尝试使用 NSPathControlItem 构建路径,我无法设置任何属性(标题、url)。

    pathComponent.URL = NSURL(string: path.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)
//let url : NSURL? = NSURL(string: "/")
//let path = NSURL(fileURLWithPath: "/")
//pathComponent.URL = path as NSURL
pathComponent.attributedTitle = NSAttributedString(string: "/atttributes")
self.pathControl?.pathItems = [pathComponent]
println(pathComponent.description)

NSPathControlItem 也不应该被子类化。

这是怎么回事?

编辑

据我所知,NSPathControlItem 存在问题。创建 NSPathControlItem 的辅助函数。

    func pathItem(title: String, imageName: String) -> NSPathControlItem
{
let item = NSPathControlItem()
item.title = title
item.image = NSImage(named: imageName)
return item
}

创建 NSPathControlItem 并打印出它们的标题的测试函数。

    var pathItems : [NSPathControlItem] = []
for title in ["a","b","c"]
{
pathItems.append(self.pathItem(title, imageName: NSImageNameFolder))
}

for item in pathItems
{
println(item.title)
}

预期的输出是三行,a、b、c。我得到零,b,零。

如果您直接在 NSPathControl 上设置 pathItems,它将起作用。

self.pathControl?.pathItems = [
self.pathItem("a", imageName: NSImageNameFolder),
self.pathItem("b", imageName: NSImageNameFolder),
self.pathItem("c", imageName: NSImageNameFolder)]

但是,如果您间接设置 pathItems,一切都会变糟。

self.pathControl?.pathItems = pathItems //array of NSPathControl (see above)

编辑2

我又看了一眼。我在 pathItem 函数中配置了一个 NSPathControlItem。这里我设置了标题。如果我设置 attributedTitle 没有区别。使用 lldb 检查项目显示正确的(属性)标题值。

但是当我将 NSPathControlItem 的数组分配给 NSPathControl 时,标题的值为“”并且 attributedTitle 未初始化。

最佳答案

NSPathControlItem 严重损坏。从 10.11.3 开始,它包含以下方法,由 Hopper 反编译:

int -[NSPathControlItem release](int arg0) {
[arg0->_secretCell autorelease];
objc_assign_ivar(0x0, arg0, *_OBJC_IVAR_$_NSPathControlItem._secretCell);
rax = [[arg0 super] release];
return rax;
}

这显然应该是一个标准的 dealloc 覆盖,它释放 ivar,将其归零,然后调用 super。但是,由于某些可能与非法药物有关的未知原因,它是一个 release 覆盖。这意味着一旦其中一个对象被释放,即使在一些无害的操作过程中,它也会自毁并变得无法使用。

我创建了一些代码来使用运行时来消除错误的 release 覆盖并替换为适当的 dealloc 覆盖。这几乎没有经过测试,使用风险自负:

extension NSPathControl {
class func fix() {
let itemClass = NSPathControlItem.self
let superclass: AnyClass = class_getSuperclass(itemClass)

let releaseSelector = Selector("release")
let releaseMethod = class_getInstanceMethod(itemClass, releaseSelector)
let superReleaseIMP = class_getMethodImplementation(superclass, releaseSelector)

if method_getImplementation(releaseMethod) != superReleaseIMP {
method_setImplementation(releaseMethod, superReleaseIMP)

let ivars = class_copyIvarList(itemClass, nil)
var offsets: [Int] = []
var cursor = ivars
while cursor.memory != nil {
let ivar = cursor.memory
if String.fromCString(ivar_getTypeEncoding(ivar))?.hasPrefix("@") == true {
offsets.append(ivar_getOffset(ivar))
}
cursor++
}
free(ivars)

let superDeallocIMP = class_getMethodImplementation(superclass, "dealloc")

let dealloc: @convention(block) UnsafeMutablePointer<Int8> -> Void = { obj in
for offset in offsets {
let ivarPtr = UnsafeMutablePointer<COpaquePointer>(obj + offset)
let unmanaged = Unmanaged<AnyObject>.fromOpaque(ivarPtr.memory)
unmanaged.release()
ivarPtr.memory = nil
}

typealias DeallocF = @convention(c) (UnsafeMutablePointer<Int8>, Selector) -> Void
let superDeallocF = unsafeBitCast(superDeallocIMP, DeallocF.self)
superDeallocF(obj, "dealloc")
}
let deallocIMP = imp_implementationWithBlock(unsafeBitCast(dealloc, AnyObject.self))

class_addMethod(itemClass, "dealloc", deallocIMP, "v@:")
}
}
}

关于cocoa - NSPathControl 和 NSPathControlItem 是满是错误还是我? (优胜美地),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28284277/

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