Question

I'm currently working with the latest version of Xcode (v5.0.2) and my app has a deployment target of iOS 7.0.

My problem is that no matter in what class, i can set a property in the header

ex: @property (strong, nonatomic) NSString *firstName;

in the implementation file, I can synthesize it just fine

ex: @synthesize firstName;

My problem is that I cannot use _firstName in the code. I have used the automatic ivars in other projects just fine (the property name preceded with the underscore).

Xcode complains and tells me to change it to the name of the property without the underscore. I don't understand why this project isn't creating the ivars with the underscore for me automatically. I know that in older versions of Objective-C, you had to do this yourself but you don't for iOS 7.

Thanks!

Was it helpful?

Solution

you can just skip the @synthesize completely, and it will create underscored ivars. If you do @synthesize without specifying the variable name (@synthesize firstName = _firstName;), it will create variables without underscores.

OTHER TIPS

@synthesize create an ivar for your property

In this line

@synthsize firstName = otherName;

where firstName is your property name while otherName is your ivar name

In your line

@synthesize firstName; === @synthesize firstName = firstName;

So The property and ivar are the same which is firstName

If you didn't write any @synthesize

then the compiler will put

@synthesize firstName = _firstName;

In which firstName is the property name and _firstName is the ivar name

Don't bother using the @synthesize (unless it's an NSManagedObject subclass). But, it's rare that you should use the direct ivar anyway. Outside of your init methods you should always use self.firstName.

Skip the @synthesize and it will be fine in your case. Since you use a new XCode version you do not really need this in this case. Please take a look at similar question (and this one).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top