开心人生

用今天的努力----实现我所向往的明天

导航

Using Properties And Synthesize In Objective-C 2.0 For Getters And Setters

Feb 26, 2008--转自 http://otype.de/index.php?id=106

In my last entry First steps in Objective-C I described how to create getters and setters and how to handle objects in Objective-C. Well, since then (about 1 day) a lot happened, a lot was read … and I found a nice way how to do this all without writing too much code which is always bug prone.

Let’s take a short look at a possible header file OTSupport.h:

@interface OTSupport : NSObject {
        float aFloat;
        NSString *aString;
}
property(readwrite, assign) float aFloat; property(readwrite, copy) NSString *aString;- (void)printOutAttributes;
@end

We created two attributes in this class, a float and a NSString variable. Furthermore, we have the prototyping line for a method called printOutAttributes. And right between those two declarations, we find a new way of handling getters and setters: the properties. For each attribute we declare a property whereas NSStrings need to be copied if used later on, floats can simply be assigned.

Let’s have a look at the class file:

#import <Foundation/Foundation.h>
#import <OTSupport.h>
@implementation OTSupport

synthesize aFloat; synthesize aString;
- (id)init{
[super init]; self.aFloat = 5.0; self.aString = @“Just a test string”; return self; }
- (void) printOutAttributes{ NSLog("Printing out attributes:"); NSLog(“Setting aFloat = %.3f”, self.aFloat); NSLog("Setting aString = %”, self.aString); }
@end
Just for the kicks, I created a constructor method called init which is called at initialization of an instance of this class. All it does is setting values for the attributes. Furthermore, we see the declaration of the printOutAttributes method which simply prints out the values of the instance attributes.

We don’t see any getter or setter methods, right? Well, this is handled by the two magical lines “synthesize”. These handle the access to the attributes.

Far easier than describing how this works (I’m not even sure I would really get it right this close to the beginning of diving into Objective-C) is showing the main function which uses the instance and its attributes:

#import <Foundation/Foundation.h>
#import <OTSupport.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// [1] Creating an instance of OTSupport and initializing it
id myObject;
myObject = [[OTSupport alloc] init];
// [2] Calling the printOutAttributes method
[myObject printOutAttributes];
// [3] Using the getter
float af = [myObject aFloat];
NSLog(@“Float value is af = %.2f”, af);

// [4] Using the setter
[myObject setAFloat:9.43];
af = [myObject aFloat];
NSLog(@“Float value is af = %.2f”, af);
[myObject release];
[pool drain];
return 0; }
In [1] we simply create an instance and initialize it (giving values to the attributes).

In [2] we use the printOutAttributes method in order to, as the name already states, print out the instance attributes.

In [3] we take advantage of the getter method, simply asking for the float value.

In [4] we overwrite the original value with a new one and print this one out short after.

A neat way to create getters and setters in Objective-C 2.0.

posted on 2010-07-20 21:49  hai  阅读(318)  评论(0)    收藏  举报