Declaring ivars and using @property?


#1

I noticed that the example codes in the book (as below) tend to both declare the instance variables AND use @property/@synthesize for the same ones. Is there a reason for this?

Right after figure 19.3:

#import <Foundation/Foundation.h>

@interface Asset: NSObject

{
NSString *lable;
unsigned int resaleValue;
}

@property (strong) NSString *label;
@property unsigned int resaleValue:
@end


#2

The reason: examples are stressing the fact that a property is backed by an instance variable.
But these days, Objective-C compiler is taking care of declaring the property instance variable for you; you only need to declare the property.


#3

thanks :slight_smile: i just wanted to make sure before assuming.


#4

I am confused about @property and how it should/shouldn’t be used in Ch19 challenge. To make this thing work, is @property (strong) necessary?

I thought @property & @synthesize simply created accessors/getters for object variables like a float, or maybe a string, but does that work for an array? How do you set an array equal to something? Or see what the “value” of an array is?

Also, could someone shed some light on the importance of “strong” is in the @property line.

Also, tried to find some stuff on it in documentation–nothing obvious popped out at me.

Thanks


#5

I am confused about @property and how it should/shouldn’t be used in Ch19 challenge. To make this thing work, is @property (strong) necessary?

I thought @property & @synthesize simply created accessors/getters for object variables like a float, or maybe a string, but does that work for an array? How do you set an array equal to something? Or see what the “value” of an array is?

Also, could someone shed some light on the importance of “strong” is in the @property line.

Also, tried to find some stuff on it in documentation–nothing obvious popped out at me.

Thanks


#6

Read: Programming with Objective-C, especially the section entitled Encapsulating Data.

developer.apple.com/library/mac … ObjectiveC


#7

Thank you! couldn’t find any of this stuff with Xcode 5.0. Probably could use that reference on Chapter 16 thread.