Using
your header (.h) file looks something like this:
@property tells the compiler that you intend to access the corresponding member variable through "setter" and "getter" functions, i.e. functions which change the value of the variable or return its value. The warnings you're seeing are because you have not defined the required functions.your header (.h) file looks something like this:
@property(readwrite, assign) myPhoto;
Because you've told the compiler that those variables should be accessible through setter and getter functions, you need to define those functions. There are two ways of doing this. The easiest is to use
@synthesize, by adding the following to your implementation (.m) file:@synthesize myPhoto;
The
@synthesize line causes the - myPhoto, - setMyPhoto methods to be generated automatically at compile time. You may also define those methods manually, but doing so is usually unnecessary.
Let's have look on following the Album class example to use properties.
Here's what it looked like before:
{
NSString* albumTitle;
NSString* albumAuther;
}
- (NSString*) albumTitle;
- (NSString*) albumAuther;
- (void) setalbumTitle: (NSString*)input;
- (void) setalbumAuther: (NSString*)input;
@end
Here's what it looks like once converted to properties:
@interface AlbumController : UIViewController
{
NSString* albumTitle;
NSString* albumAuther;
}
@property(retain)NSString* albumTitle;
@property(retain)NSString* albumAuther;
@end
The @property is an Objective-C directive which declares the property. The "retain" in the parenthesis specifies that the setter should retain the input value, and the rest of the line simply specifies the type and the name of the property.
Insted of "retain" you can also use following some other keyword.
- assign - does nothing to reference count simply points your variable to the data
- retain - points your variable to data and adds 1 to reference count, data is guaranteed to be there while your variable is still alive
- copy - makes a copy of data, points your variable at it and makes the retain count 1
assign - the ivar is set by doing a simple assignment. Implementation:
- (void) setFoo:(NSString *)newFoo {
foo = newFoo;
}
retain - the ivar is sent the retain message before doing the assignment. Implementation:
- (void) setFoo:(NSString *)newFoo {
if (foo != newFoo) {
[foo release];
foo = [newFoo retain];
}
}
copy - the ivar is sent the copy message before doing the assignment. Implementation:
- (void) setFoo:(NSString *)newFoo {
if (foo != newFoo) {
[foo release];
foo = [newFoo copy];
}
}
|

