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]

Unknown's avatar

iOS development with Swift - book: https://manning.com/books/ios-development-with-swift video course: https://www.manning.com/livevideo/ios-development-with-swift-lv

Tagged with:
Posted in Objective C

Leave a Reply

Discover more from Before I forget...

Subscribe now to keep reading and get access to the full archive.

Continue reading