Hello DaniWeb, i recently began reading a book called programming in Objective-c by Kochan 5th edition, One thing
i havent gotten the grasp on is Properties and WHEN to use them or if they are even usable on such a simple test program such as in a coffeeShop class for example.

for example:

@interface coffeeShop: NSObject
    NSString *coffee;
    NSString *icedCoffee;

// In my opinion, i believe that those Instance varibles (if they are) go below here as i put them.

@proiperty (strong, nonatomic) NSString *coffee;
@property (strong, nonatomic) NSString *icedCoffee;


@implementation coffeeShop

// Can anyone explain how to correctly synthesize then and What exactly is SYNTHESIZE mean ?


As i commented on the @implementation section, can anyone guide me on how to implement them the correct what and i've seen tutorial where people tend to put an Underscore (_) infront of the Name like NSString *_coffee;
in the @interface section and where as i dont get that.

One more thing, Can someone better explain the use of properties and why they may be great using them for iOS development ?

Thanks DaniWeb.

Recommended Answers

All 2 Replies

@properties are instance variables with predefined getters and setters.
When you define a property ie:
@property (nonatomic, strong)NSString *coffee;
xCode actually creates couple of things:
1. a instance variable _coffee
2. a getter for the instance variable

- (NSString *)coffee{
    return _coffee

3. a setter for the instance variable

- (void)setCoffee:(NSString *)coffee{
    _coffee = coffee

Also in the current version @synthesize is automatically done for you.

I believe in previous versions of xCode @synthesize effectively does steps 2 and 3, and it would go inside of @implementation.

Yes, you do not need to synthesise them if the setters and getters are as simple as above. In recent versions the "_" on the front is for the internal representation of the property. You also do not need the statements on lines 3 & 4.

Properties are the external attributes of you class coffeeShop. When you create an instance of this class you can then set these properties.


coffeeShop *myShop = [[coffeeShop alloc] init];
myShop.coffee = @"arabica";


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.