Craig Grummitt – Before I forget

Discoveries in Swift and iOS development.

Getters and Setters in Objective C

·

Doing a lot of Objective C lately, thought it worthwhile to write a few key differences with Actionscript. To begin with today – getters and setters!

Properties in Objective C are automatically given setters and getters eg:

[sourcecode language=”objc”]@property (strong,nonatomic) NSString *name;[/sourcecode]

is automatically given:

the getter:

[sourcecode language=”objc”]
-(NSString *)name
{
return _name;
}
[/sourcecode]

the setter:

[sourcecode language=”objc”]
-(void)setName:(NSString *)name
{
_name = name;
}
[/sourcecode]

You can replace these in your code. If you replace both, you need to add the line:

[sourcecode language=”objc”]@synthesize name = _name;[/sourcecode]


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *