I'm starting to learn objective c, I made a simple person class, i get 6 errors, expected specifier-q开发者_JAVA技巧ualifier-list before '-' token
just wondering if someone can explain to me what I'm doing wrong,
thanks
#import <Foundation/Foundation.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
@interface Person:NSObject
{
NSString *firstName;
NSString *lastName;
-(void) setFName:(NSString *) theFirstName;
-(void) setLName:(NSString *) theLastName;
-(void) printName;
}
@end
@implementation Person
{
-(void) setFName:(NSString *) theFirstName
{
firstName = [[NSString alloc]initWithString: theFirstName];
}
-(void) setLName:(NSString *) theLastName
{
lastName = [[NSString alloc]initWithString: theLastName];
}
-(void) printName
{
NSLog(@"The person's full name is",firstName,lastName);
}
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Person *person = [[Person alloc] init];
[person setFName:@"Amir"];
[person setLName:@"Karimian"];
[person printName];
[person release];
[pool drain];
return 0;
}
Or even simpler:
@interface Person : NSObject
@property (nonatomic, copy) NSString * firstName;
@property (nonatomic, copy) NSString * lastName;
- (NSString *) fullName;
@end
@implementation Person
@synthesize firstName, lastName;
- (NSString *) fullName {
return [NSString stringWithFormat:@"%@ %@", [self firstName], [self lastName]];
}
- (void) dealloc {
[self setFirstName:nil];
[self setLastName:nil];
[super dealloc];
}
@end
Method declaration will be outside of closing brace.
@interface Person:NSObject
{
NSString *firstName;
NSString *lastName;
}
-(void) setFName:(NSString *) theFirstName;
-(void) setLName:(NSString *) theLastName;
-(NSString *) firstName;
-(NSString *) lastName;
-(void) printName;
@end
And in implementation no brace is required.
@implementation Person
// { is not required
-(void) setFName:(NSString *) theFirstName {
// code
}
-(void) setLName:(NSString *) theLastName {
}
// in this way
// } is not required
@end
There are at least two logical errors. In printName
NSLog(@"The person's full name is %@ %@",firstName,lastName);
Your format string was wrong.
And in main
[person setLName:@"Karimian"];
You have user "ser" instead of "set".
In the header file:
@interface Person:NSObject
{
NSString *firstName;
NSString *lastName;
}
@property(retain, nonatomic) NSString * firstName;
@property(retain, nonatomic) NSString * lastName;
-(void) setFName:(NSString *) theFirstName;
-(void) setLName:(NSString *) theLastName;
-(void) printName;
@end
In the implementation use the @synthesize to create your getter and setter accessor methods:
@implementation Person
/* Create the getter and setter properties
which are also called accessor methods
*/
@synthesize firstName = _firstName;
@synthesize lastName = _lastName;
//The other methods
-(void) setFName:(NSString *) theFirstName{
}
-(void) setLName:(NSString *) theLastName{
}
-(void) printName{
}
@end
Also, apart from taskinoor's answer, it's good practice to implement a -(void)dealloc method in your class:
-(void)dealloc {
[firstName release]; //since they have been alloced.
[lastName release];
[super dealloc];
}
Note that if you wanted to use properties, like Dave DeLong, you should be careful about choosing @property(assign)
, @property(copy)
or @property(retain)
and managing whether you release the NSStrings or not.
精彩评论