2
votes

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

All seems to be ok, but finally when I drop some files from Finder I get error:

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

Here is my test project: https://www.dropbox.com/s/1mgcg2dysvs292u/SimpleDrag.zip?dl=0

What I exactly need in my app: allow user to drag and drop multiple files and folder into some tree list and then display them to user. Also save all this this into some file, so it can be loaded again with all user dragged files and folders.

A final result I want to have like this:

enter image description here

1

1 Answers

3
votes

The description property of NSObject is read-only, and is generally set by providing a getter in the implementation file:

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

You can't set it, either via key-value coding or by direct assignment:

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

In -[ChildNode copyWithZone:] an attempt is made to do the latter of the two, and that's what causes the warning to be logged to the console.

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

This begs the question why do you get the warning in your app, and not in the sample app? From what I can tell no ChildNode instance is ever sent a copyWithZone: message in the sample app, whereas this does happen in your app, immediately after the drop. Of course there's a second question here as well: why do Apple explicitly include the description key-path when it can't be set this way? - unfortunately I can't help you with that.


A really handy way of trying to trap errors that don't actually cause exceptions is to add an All Exceptions breakpoint. If you do this in your sample app you'll see that the app freezes at the line that's causing the problem, giving you a better chance of figuring out the issue.

enter image description here