I’m using xcode 4.5.2 and LLVM 4.1. I am expecting that I do not need to synthesize a class property nor declare an instance variable, but I’m getting errors stating that these expectations are wrong.
My class:
@interface Test : NSManagedObject
@property (strong, nonatomic) NSString *string;
@property (strong, nonatomic) NSString *number;
@end
@implementation Test
- (NSString*)string {
return _string;
}
@end
1) Use of undeclared identifier ‘string’
I’ve also seen in WWDC 2010 Session 144 the following:
return self->string;
But that is giving an error with a suggestion to use dot notation.
2) Property ‘string’ found on object of type ‘Test *’; did you mean to access it with the “.” operator?
I’m also getting a warning for number:
3) Property ‘number’ requires method ‘number’ to be defined – use @synthesize, @dynamic or provide a method implementation in this class implementation
I am entirely lost as to what is going on here. It’s starting to boil my blood just a little bit. What am I missing?
First of all, dot notation is your friend!
Secondly, when you try to use
->you must access the ivar. In the newest versions of Xcode and LLVM, an ivar with an underscore is created for you if you do not specify one yourself using@synthesize.So you would do
self->_string. You access the property when you use the.which is recommended. This also is probably why you’re getting a warning onnumber, sinceself->numberdoesn’t exist.Edit: In response to the fact that you’re using
NSManagedObjectI’d definitely recommend using properties (like Todd recommended). NSManagedObjects expect you to use@dynamicand since Core Data does a lot of stuff under the hood, Apple recommends you not change that.If you want a custom getter/setter, I defer to another SO question which uses the
primitivemethods:And of course, Apple Docs on the matter. And a related question/solution that pertains to iOS 6.