emailconfirmed, nsInternRO, nsInternRW, Administrators
3,356
edits
(created) |
m (→Beispiel) |
||
Line 23: | Line 23: | ||
== Beispiel == | == Beispiel == | ||
=== Interface (file.h) === | |||
<source lang="objc"> | |||
@interface MyClass:NSObject { | |||
NSString *name; | |||
int age; | |||
id delegate; | |||
} | |||
@property (nonatomic, retain) id delegate; | |||
// Hinweis: name und age würden normalerweise auch einfacher als properties verwendet! | |||
-(NSString*)name; | |||
-(void)setNewName:(NSString*)newName; | |||
-(int)age; | |||
-(void)setAge:(int)newAge; | |||
@end | |||
</source> | |||
=== Implementation (file.m) === | |||
<source lang="objc"> | |||
#import "file.h" | |||
@implementation MyClass | |||
@synthesize delegate; | |||
-(id)init { | |||
self = [super init]; | |||
if(self) { | |||
[self setName:@"Barnie"]; | |||
[self setAge:30]; | |||
} | |||
} | |||
-(NSString*)name { return name; } | |||
-(void)setName:(NSString*)newName { | |||
if(newName != name) { | |||
[name release]; | |||
[newName retain]; | |||
name = newName; | |||
} | |||
} | |||
-(int)age { return age; } | |||
-(void)setAge:(int)newAge { age = newAge; } | |||
-(void)dealloc { | |||
[delegate release]; // retain-property! | |||
[self setName:nil]; // release wird im Setter gesendet! | |||
// age braucht keinen release weil int ist kein Objekt! | |||
} | |||
@end | |||
</source> | |||
Fragen: | |||
* was ist ein Accessor? | |||
* was passiert im dealloc bei: <tt>name = nil;</tt>? | |||
* wo ist der Unterschied: | |||
** [self setName:nil]; | |||
** self.name = nil; | |||
** name = nil; | |||
== Regeln == | == Regeln == |