Reputation: 3604
I realize that there are already many property vs. ivar questions on here, but after doing a lot of research I can't seem to find a clear answer.
I understand that when you declare a property like the following, that the compiler automatically synthesizes the backing ivar and the two accessor methods for you:
@property NSString *myString;
What still confuses me is, is myString
an actual instance variable? The reason I ask this is because you can never access it like this:
NSLog(@"Value of myString is: %@", myString);
You either have to use the backing ivar _myString
, or one of the getter methods like [self myString]
or self.myString
. So I'm confused because normally you could just use the variable name plain and simple.
To top it all off, I've been told that you should not refer to myString
as a property, and that the word property should only be used to refer to the two accessor methods that are synthesized for you by the compiler when you use the @property
directive.
Would it really be wrong for you to say "I have a property called myString" , and if that is wrong then what would be the correct way to say it?
Any help clearing this up would be greatly appreciated. I've been struggling with solidifying the idea of properties and ivars being different things all day now.
Upvotes: 2
Views: 119
Reputation: 1
I'm pretty much a noob in objective c, but i understand it as follows:
As any other OOP language objective c has instance vars and getters+setters methods. Basically every getter or setter looks the same, so XCode let you synthesize those automatically using the @property syntax.
In previous versions on XCode you had to declare both the ivar and its @property + @synthesize, but now the compiler does that for you.
This code:
@interface SomeClass : NSObject {
NSString* _myInstanceVar; //declare the ivar
}
@property (non-atomic, strong) myInstanceVar //declare the property+accessors
Is equivalent to this code:
@property (non-atomic, strong) myInstanceVar //declare the ivar+property+accessors
This documentation pretty much sums its up.
Hope i helped in anyway...
Upvotes: 0
Reputation: 11839
Here are answer of your questions -
is myString an actual instance variable? - No
Would it really be wrong for you to say "I have a property called myString" , and if that is wrong then what would be the correct way to say it? - No its not wrong to call it.
So it looks like what actually confuses you is naming conventions, if you go through the naming conventions behind properties -
When you use the @property syntax to declare properties on an object, as described in “Encapsulating Data,” the compiler automatically synthesizes the relevant getter and setter methods (unless you indicate otherwise). If you need to provide your own accessor method implementations for any reason, it’s important to make sure that you use the right method names for a property in order for your methods to be called through dot syntax, for example.
Unless specified otherwise, a getter method should use the same name as the property. For a property called firstName, the accessor method should also be called firstName. The exception to this rule is for Boolean properties, for which the getter method should start with is. For a property called paused, for example, the getter method should be called isPaused.
The setter method for a property should use the form setPropertyName:. For a property called firstName, the setter method should be called setFirstName:; for a Boolean property called paused, the setter method should be called setPaused:.
Check the developer website for detailed description - https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/Conventions/Conventions.html
Upvotes: 1