views:

32

answers:

2

One of the things I've been struggling with whilst breaking into Objective C programming is understanding how to manipulate properties. I'm perhaps out of my comfort zone using a proper coding language as opposed to scripting languages that I'm used to, so the declaring things in header files and implementation files is confusing me somewhat.

Lets say I have a String. I wish to add some text into that string. What do I declare in the header file and what do I do in the implementation file to allow this to work properly, and could somebody explain @property and @synthesize?

Thanks for helping nurture me into an ObjectiveC coder, any help you can give is greatly appreciated.

+1  A: 

@property - declares a property with access and memory modifiers. Properties can be readonly or readwrite, nonatomic or atomic (thread safety), assign/retain/copy managed. Actually, you can declare simple getter and setter method like we did in Tiger era, but declaring a @property will help you to identify all aspects of the prop at any time without checking the implementation.

@synthesize - simplifies the job if you need a simple property without any complex job in getter and setter. It defines a default implementation according to the definition of @property.

At last, your questions about string. Properties won't help here if you are looking for something simple, let's say myObj.string += "abc". It's not obj-c style and with or without property you will do something like myObj.string = [[myObj string] stringByAppendingString:@"abc"] or [[myObj string] appendString:@"abc"] depending on mutable/immutable nature of the string object.

As a bottom line: it's quite big topic to explain everything in a single post. I'd recommend you to read Apple documentation and maybe purchase a book about obj-c. Aaron Hillegass wrote one - a good start for any obj-c and MacOS beginner.

Good luck

Gobra
+1  A: 

In the bad old days before Objective-C 2.0, it was common to write getters and setters for your instance variables e.g.

@interface Foo : NSObject
{
@private 
    id foo;
}
-(void) setFoo: (id) newFoo;
-(id) foo;
@end

@implementation Foo

// need dealloc to release foo too.

-(void) setFoo: (id) newFoo
{
    [newFoo retain];
    [foo release];
    foo = newFoo;
}

-(id) foo
{
   return foo;
}
@end

And that's just in the single threaded environment. There was even more stuff needed for multithreaded apps.

Properties provide a neat shorthand for the above. The @property replaces both of the declarations in the interface as well as giving the caller better hints about the semantics of the getter and setter. It also allows you to @synthesize the accessors so the compiler will generate the code for them automatically (you don't have to @synthesize them, you can provide your own implementations if you want). All of the above can be replaced by

 @interface Foo : NSObject
{
@private 
    id foo;
}
@property (nonatomic, retain) id foo;
@end

@implementation Foo

// need dealloc to release foo too.

@synthesize foo; 
@end

That saves quite a lot of typing but also you can see from the interface that setFoo: will retain its new value and that the property is not safe to use (to set or get) in a multithreaded environment (without some other locking mechanism).

JeremyP