akashivskyy
akashivskyy

Reputation: 45180

Calling isKindOfClass throws an error in Objective-C

I want to implement subscripting in my custom class and thus implemented the following methods:

- (id)objectForKeyedSubscript:(id<NSCopying>)key;
- (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key;

But I have a strange issue, because I've never met it before: [(id)obj isKindOfClass:] throws an ARC Semantic Issue:

No known instance method for selector 'isKindOfClass:'

As far as I remember, I didn't have any problems with id before... Is it a bug with Xcode (I'm using the Xcode 5 Developer Preview 2), or have I forgotten something important?

- (id)objectForKeyedSubscript:(id<NSCopying>)key {
    if(![key isKindOfClass:[NSString class]]) { // error
        ...
    } else {
        ...
    }
}

Upvotes: 3

Views: 3024

Answers (1)

Martin R
Martin R

Reputation: 539705

isKindOfClass: is a method of the NSObject protocol, so you can either declare key as conforming to the protocol

- (id)objectForKeyedSubscript:(id <NSCopying, NSObject> )key { ...

or require key to be derived from NSObject (which conforms to that protocol):

- (id)objectForKeyedSubscript:(NSObject <NSCopying> *)key { ...

Upvotes: 7

Related Questions