@interface Person : NSObject
{
NSString *name;
int age;
}
- (NSString *)name; //get method
- (void)setName:(NSString *)value;
- (int) age; //get method;
- (void)setAge:(int)age;
- (BOOL)canLegallyVote;
//another way to creat property
//property declarations
@property int age;
@property (copy) NSString *name;
@property (readonly) BOOL canLegallyVote;
- (void)castBallot;
@end
// implementation file
@implementation Person;
- (int) age {return age;}
- (void)setAge:(int) value
{
age=value;
}
- (NSString *) name {return name;}
- (void)setName:(NSString *) value
{
name=value;
}
//calling method
- (BOOL)canLegallyVote {
return (self age>=18);
}
//antoher way to write it
@synthesize age;
@synthesize name;
- (BOOL)canLegallyVote{
return(age >18);
}
- (void)castBallot {
if([self canLegallyVote]){
//do voting stuff
}else {
NSLog(@"I'm not allowed to vote yet!");
}
}
}
@end
// Autoreleasing example
- (NSString *)fullName{
NSString *result;
result=[[NSString alloc] initWithFormat:"%@ %@" , firstName, lastName];
[result autorelease];
return result;
}