I'm not very sure about this. I think that an "property" is an instance variable that has accessor methods, but I might think wrong...
-
A property is a more abstract concept. An instance variable is literally just a storage slot, like a slot in a struct. Normally other objects are never supposed to access them directly. A property, on the other hand, is an attribute of your object that can be accessed (it sounds vague and it's supposed to). Usually a property will return or set an instance variable, but it could use data from several or none at all. For example:
@interface Person : NSObject { NSString *name; } @property(copy) NSString *name; @property(copy) NSString *firstName; @property(copy) NSString *lastName; @end @implementation Person @synthesize name; - (NSString *)firstName { [[name componentsSeparatedByString:@" "] objectAtIndex:0]; } - (NSString *)lastName { [[name componentsSeparatedByString:@" "] lastObject]; } - (NSString *)setFirstName:(NSString *)newName { NSArray *nameArray = [name componentsSeparatedByString:@" "]; NSArray *newNameArray [[NSArray arrayWithObjects:newName, nil] arrayByAddingObjectsFromArray:[nameArray subarrayWithRange:NSMakeRange(1, [nameArray size]-1)]]; self.name = [newNameArray componentsJoinedByString:@" "]; } - (NSString *)setLastName:(NSString *)newName { NSArray *nameArray = [name componentsSeparatedByString:@" "]; NSArray *newNameArray [[nameArray subarrayWithRange:NSMakeRange(0, [nameArray size]-2)] arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:newName, nil]]; self.name = [newNameArray componentsJoinedByString:@" "]; } @end(Note: The above code is buggy in that it assumes the name already exists and has at least two components (e.g. "Bill Gates" rather than just "Gates"). I felt that fixing those assumptions would make the actual point of the code less clear, so I'm just pointing it out here so nobody innocently repeats those mistakes.)
-
A property is a friendly way of implementing a getter/setter for some value, with additional useful features and syntax. A property can be backed by an instance variable, but you can also define the getter/setter to do something a bit more dynamic, e.g. you might define a lowerCase property on a string which dynamically creates the result rather than returning the value of some member variable.
Here's an example:
// === In your .h === @interface MyObject { NSString *propertyName; } // ... @property (nonatomic, retain) NSString *propertyName; // === In your .m @implementation === @synthesize propertyName /* = otherVarName */;The
@propertyline defines a property calledpropertyNameof typeNSString *. This can be get/set using the following syntax:myObject.propertyName = @"Hello World!"; NSLog("Value: %@", myObject.propertyName);When you assign to or read from
myObject.propertyNameyou are really calling setter/getter methods on the object.The
@synthesizeline tells the compiler to generate these getter/setters for you, using the member variable with the same name of the property to store the value (orotherVarNameif you use the syntax in comments).Along with
@synthesizeyou can still override one of the getter/setters by defining your own. The naming convention for these methods issetPropertyName:for the setter andpropertyName(orgetPropertyName, not standard) for the getter. The other will still be generated for you.In your
@propertyline you can define a number of attributes in parens for the property that can automate things like thread-safety and memory management. By default a property is atomic meaning the compiler will wrap@synthesized get/set calls with appropriate locks to prevent concurrency issues. You can specify thenonatomicattribute to disable this (for example on the iPhone you want to default most properties tononatomic).There are 3 attribute values that control memory management for any
@synthesizedsetters. The first isretainwhich will automatically sendreleaseto old values of the property, andretainto the new values. This is very useful.The second is
copywhich will make a copy of any values passed in rather than retaining them. It is good practice to usecopyfor NSString because a caller could pass in an NSMutableString and change it out from under you.copywill make a new copy of the input which only you have access to.The third is
assignwhich does a straight pointer assign without calling retain/release on the old or new object.Lastly you can also use the
readonlyattribute to disable the setter for the property.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.