user1792043
user1792043

Reputation: 23

Objective-C property access: unrecognized selector sent to instance

I am new to objective-c, but coding for many years now. Somehow I don't get warm with Objective-C. I searched google and stackoverflow, but I think my problem is just to simple and stupid that no one has asked this yet.

My Code is based on DateSelectionTitles Example. http://developer.apple.com/library/ios/#samplecode/DateSectionTitles/Introduction/Intro.html

I have an NSManagedObject

@interface Event : NSManagedObject

@property (nonatomic, retain) NSDate * date;
...

// Cache
@property (nonatomic, retain) NSString * primitiveSectionIdentifier;

All prperties are defined in my datamodel, except the primitiveSectionIdentifier (as in the apple example)

But when I call

NSString *tmp = [self primitiveSectionIdentifier];

I get the Exception

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Event primitiveSectionIdentifier]: unrecognized selector sent to instance 0x74850c0'

To put it simple:

Event *foo = [[Event alloc] init];

if (foo.primitiveSectionIdentifier) {
    NSLog(@"YEAH");
}

throws the same exception. So I basically want to check if primitiveSectionIdentifier is nil. But when I access the property, it throws an exception? Do I need to alloc each property before I can check if it has a value?!

Which of the Objective-C basics am I not getting here?

Thanks a lot for responses!

Upvotes: 2

Views: 3298

Answers (2)

Sulthan
Sulthan

Reputation: 130082

There is only one way how this can happen without compiler warnings - you must have written @dynamic primitiveSectionIdentifier; in your implementation file. This means that you don't want to define the method because you believe it is already defined somewhere else.

You are using a NSManagedObject, do you know how it works? You declare methods without implementation (putting @dynamic in the implementation) and when the method is called, it is not found and a special handler [NSObject doesNotRecognizeSelector:] is called instead. This handler checks the Core Data model whether an attribute for the given selector exists and if it doesn't, it throws the exception you are seeing.

So, the problem might the caused by the fact that primitiveSectionIdentifier is not declared in your model.

Upvotes: 2

user1790252
user1790252

Reputation: 506

You are using an older example program, which uses a different style of memory management; if you are compiling under the iOS 5 or 6, that may be causing the problem.

Try

NSLog(@"primitiveSectionIdentifier = %@", self.primitiveSectionIdentifier);

If it doesn't give you the string you are looking for then the problem is likely in that the string object was never initialized and is still set to nil. In that situation, the code would compile, but sending a selector to a nil pointer would throw up an exception.

Upvotes: 1

Related Questions