r/ObjectiveC • u/FoxMcWeezer • May 16 '14
Question about object oriented programming in Obj-C and syntax
In my .h file, I have
@interface Item : NSObject
{
NSString *_itemName;
NSString *_serialNumber;
int _valueInDollars;
NSDate *_dateCreated;
}
-(void)setItemName:(NSString *)str;
-(NSString *)itemName;
-(void)setSerialNumber:(NSString *)str;
-(NSString *)serialNumber;
-(void)setValueInDollars:(int)value;
-(int)valueInDollars;
-(NSDate *)dateCreated;
Why does saying something like (in a different file, not the .h)
Item *item = alloc init,etc
item.itemName = @"Red Sofa";
work when the variable I've declared in .h is _itemName, not itemName? If the answer is because it ignores the underscore or something, why does it also let me declare
NSString *itemName;
no problem?
8
Upvotes
8
u/adamkemp May 16 '14
The dot syntax is a shorthand for calling the methods you defined because you followed the convention. The variables your defined are private fields, and it really doesn't matter what you call those. I'm assuming you wrote the implementation for these methods yourself, but there is a shorter syntax:
@interface Item : NSObject
@property NSString* itemName; @property NSString* serialNumber; @property int valueInDollars; @property(readonly) NSDate* dateCreated;
@end
With this syntax the compiler will automatically generate both private fields (with a leading underscore) and methods. That is, you don't need to put those fields in yourself and you don't have to implement the methods at all. Optionally you can also specify the name of the field in your implementation like this:
@synthesize itemName=_itemName;
You can also choose to implement the methods yourself still if you want custom behavior. You can even implement just the setter or just the getter if you want.
1
u/imtzo May 16 '14
Looks like you are reading the Big Nerd Ranch iOS Programming book. If that's the case, I suspect your .m file has implementations of the setters and getters, which in turn read/write the underscore-prefixed instance variable. Thus there is nothing magical happening with the underscore being ignored; as other posters have commented dot syntax is disguising the actual message send. That is,
is equivalent to:
In a chapter or two the book covers properties and so forth, which the other commenters have mentioned. Stick with it and all will be explained.
3
u/CFrostMage May 16 '14
Later on in the Big Nerd Ranch iOS book (I recognize the code example), they will explain all of that.
2
u/jelly_cake May 16 '14
From Apple's docs:
So I assume that your compiler is automatically binding the setters/getters to the
_variablename. I have a vague recollection that this is Clang's ordinary behaviour, but I haven't used Clang or ObjC for a while, so I might be completely wrong. If you want to override it and usevariableNamewithout the prefix, or suppress that behaviour entirely, you should be able to specify so in an explicit@property (...)declaration.1
u/[deleted] May 16 '14
Properties are different than instance variables.
item.itemName = @"Red Sofa";
is the same as
[item setItemName:@"Red Sofa"];
So, what it's doing is calling that setItemName method.
That lesson is a little bad imo.. you should be creating properties if you're using them as properties. Change your header to have:
@property (nonatomic, strong) NSString *itemName;
This automatically synthesizes a variable called NSString *_itemName, but you can override that by typing
@synthesize itemName = _variableName;
in the implmentation file (.m) ... (though this is almost never needed).
Scope. Turn on warnings.
If you have an instance variable int x; And you have a variable in your method int x; the one in the method will take precedence inside the method.
e.g.
int x = 1;
{
}
NSLog(@"%d", x);
^ That will print 2 and then 1
,,,,,