Pergunta

I have a custom class that I want to be able to put on the pasteboard. Here is the code I have so far

- (NSArray *)writableTypesForPasteboard:(NSPasteboard *)pasteboard {

    static NSArray *writableTypes = nil;



    if (!writableTypes) {

        writableTypes=[NSArray arrayWithObjects:(NSString *)kUTTypeXML, nil];

     }

    return writableTypes;

}

- (id)pasteboardPropertyListForType:(NSString *)type {

    return [NSKeyedArchiver archivedDataWithRootObject:[MyClass class]];

}
Foi útil?

Solução

This part isn't right:

- (id)pasteboardPropertyListForType:(NSString *)type {
    return [NSKeyedArchiver archivedDataWithRootObject:[MyClass class]];
}

It looks like you're trying to archive the actual MyClass class, where you should actually be archiving the MyClass object, i.e. a specific instance of MyClass rather than the class itself. Also, you should probably be checking the type that's passed in to make sure that you're offering the data for the right thing. So, it should look something like:

- (id)pasteboardPropertyListForType:(NSString *)type {
    if ([type isEqualToString:kUTTypeXML]) {
        return [NSKeyedArchiver archivedDataWithRootObject:self.someInstanceOfMyClass];
    }
    return nil;
}

Finally, make sure that MyClass implements the NSCoding protocol so that you can create an archive containing the object's data.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top