目标c中的一个简单的人类

时间:2010-12-20 15:43:39

标签: iphone objective-c

我开始学习目标c,我做了一个简单的人类,我得到6个错误, ' - '标记之前的预期说明符限定符列表

只是想知道是否有人可以向我解释我做错了什么,

谢谢

#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;
}

4 个答案:

答案 0 :(得分:6)

甚至更简单:

@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

答案 1 :(得分:5)

方法声明将在闭括号之外。

@interface Person:NSObject
{
    NSString *firstName;
    NSString *lastName;
}

-(void) setFName:(NSString *) theFirstName;
-(void) setLName:(NSString *) theLastName;
-(NSString *) firstName;
-(NSString *) lastName;
-(void) printName;

@end

在实施过程中,不需要支撑。

@implementation Person
// { is not required
-(void) setFName:(NSString *) theFirstName {
    // code
}

-(void) setLName:(NSString *) theLastName {

}

// in this way

// } is not required
@end

至少有两个逻辑错误。在printName中

NSLog(@"The person's full name is %@ %@",firstName,lastName);

您的格式字符串错误。

在主要

[person setLName:@"Karimian"];

您有用户“ser”而不是“set”。

答案 2 :(得分:1)

在头文件中:

@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

在实现中使用@synthesize创建getter和setter访问器 方法:

@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

答案 3 :(得分:0)

此外,除了taskinoor的答案,最好在你的班级中实现 - (void)dealloc方法:

-(void)dealloc {
    [firstName release]; //since they have been alloced.
    [lastName release];
    [super dealloc];
}

请注意,如果您想使用Dave DeLong等属性,则应谨慎选择@property(assign)@property(copy)@property(retain)并管理是否释放NSStrings。< / p>