首页 文章

将文件拖放到NSOutlineView中

提问于
浏览
2

我'm trying to implement simple drag and drop operation into NSOutlineView Based on Apple'的例子 - https://developer.apple.com/library/mac/samplecode/SourceView/Introduction/Intro.html

一切似乎都没问题,但最后当我从Finder中删除一些文件时,我得到错误:

[<ChildNode 0x60800005a280> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key description.') was raised during a dragging session

这是我的测试项目:https://www.dropbox.com/s/1mgcg2dysvs292u/SimpleDrag.zip?dl=0

在我的应用程序中我真正需要的是:允许用户将多个文件和文件夹拖放到某个树列表中,然后将它们显示给用户 . 还将所有这些保存到某个文件中,因此可以使用所有用户拖动的文件和文件夹再次加载它 .

我希望得到的最终结果如下:

enter image description here

1 回答

  • 3

    NSObjectdescription 属性是只读的,通常通过在实现文件中提供getter来设置:

    - (NSString *)description {
        return [self urlString]; // Using urlString solely for demo purposes.
    }
    

    您无法通过键值编码或直接分配来设置它:

    self.description = [self urlString]; // Xcode error: 'Assignment to readonly property'
    [self setValue:[self urlString] forKey:@"description"];
    

    在_948788中,尝试执行后两者,这就是导致警告记录到控制台的原因 .

    // -------------------------------------------------------------------------------
    //  copyWithZone:zone
    //   -------------------------------------------------------------------------------
    - (id)copyWithZone:(NSZone *)zone
    {
        id newNode = [[[self class] allocWithZone:zone] init];
    
        // One of the keys in mutableKeys is 'description'... 
        // ...but it's readonly! (it's defined in the NSObject protocol)
        for (NSString *key in [self mutableKeys])
        {
            [newNode setValue:[self valueForKey:key] forKey:key];
        }
    
        return newNode;
    }
    

    这就引出了一个问题,为什么你在应用程序中得到警告,而不是在示例应用程序中?据我所知,没有 ChildNode 实例在示例应用程序中发送了 copyWithZone: 消息,而这确实发生在您的应用程序中,在删除后立即发生 . 当然,这里还有第二个问题:为什么Apple明确地包含 description 关键路径,因为它可以帮助你 .


    尝试捕获实际上不会导致异常的错误的一种非常方便的方法是添加一个 All Exceptions 断点 . 如果您在示例应用程序中执行此操作,则会导致问题,从而使您更有可能找出问题所在 .

    enter image description here

相关问题