Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 12-02-2008, 05:21 PM   #4 (permalink)
rames44
Mobile Geek
 
Join Date: Aug 2008
Location: Florida, USA
Posts: 365
Send a message via AIM to rames44 Send a message via Yahoo to rames44
Default Getters, setters and properties for the newbie, Part IV

I like the "dotted" notation, but I want to have a read-only property - in other words, a getter but no setter.

Quite simple. When you declare the property, do it as:
Code:
@property (nonatomic, readonly) PropertyType propertyName;
Given this,
Code:
    something = obj.propertyName;
will work but
Code:
    obj.propertyName = something;
won't.

If you want to explicitly denote that something is read/write you can use the readwrite keyword, but this is the default, so it's rarely used.

What's the nonatomic bit you keep using?

By default, when Objective C synthesizes getters and setters, it makes the worst-case assumption - that these methods are going to get used in a multi-threaded system. As such, it generates extra internal code to manage the case when two threads call one of these methods at the same time, or that one thread is calling a setter as another is calling a getter.

Unless you're explicitly using multiple threads in your application, all calls to your object will be made from the same thread, and so you don't have this particular issue. By including nonatomic in the property declaration, you cause Objective C to generate simpler, more efficient getters and setters.

Of course, if you are coding a multithreaded app, you have to watch out for this kind of thing. If an object's properties can be accessed from multiple threads at the same time, then you can get the more robust code by leaving the nonatomic keyword off.

I like to code my member variables with a prefix so that I don't confuse them with local variables. What does this do to my property names?

Let's suppose that you like to use an underscore prefix on member variables, as shown below:
Code:
@interface MyClass: NSObject
{
    NSString *_text;
}

-(void) init;
-(void) modifyText:(NSString*)text;
@end

//MyClass.m file
@implementation MyClass

- (void)init
{
    _text = @"some text";
}

-(void) modifyText:(NSString *)text
{
    // do something with "text" and "_text"
}
@end
Now you'd like to expose the "_text" value as a property, but without the underscore.
You write the property declaration in the same way as normal, but with the "public" name, not the variable name
Code:
@property(nonatomic, retain) NSString *text;
since this part of the declaration is telling the world what property names can be used in the rest of the software.

In the implementation, however, you write:
Code:
@synthesize text = _text;
instead of just
Code:
@synthesize text;
This tells Objective C to synthesize a property named "text", but to wire it to the variable named "_text" instead of one named "text".

I like my boolean properties to be isValue instead of just value, but I still want the setter to be value.

No problem. The property declaration allows you to manually specify the name of the getter and setter if you want. In this case you can write
Code:
@property (nonatomic, assign, getter=isValue) boolean value;
Now you can access the property as:
Code:
    if (obj.isValue) ...
    obj.value = YES;
There is a corresponding "setter=name" directive as well if you have need to rename the setter.

Are retain and assign my only options?

No, there is a third option - copy. This specifies that, instead of retaining the object passed in, the setter will make a copy of the object (implicitly retaining it at the same time). This is only valid for object types (not for int's, boolean's, etc.), and the object that is passed in must implement the NSCopying protocol.

"Copy" has some special implications if you're using it on objects that you want to be mutable - the "copy" operation usually returns an immutable object. This is covered in more detail in The Objective-C 2.0 Programming Language: Declared Properties
rames44 is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 158,821
Threads: 89,206
Posts: 380,633
Top Poster: BrianSlick (7,129)
Welcome to our newest member, socbaybot
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:19 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.