1

我目前正在自学Objective-C作为第一语言。我理解其中的困难,但我是一个坚持不懈的人。我已经开始在 Apple Objective-C 文档上做练习了。我的目标是让我的程序注销我的名字和姓氏,而不是通用的 Hello World 问候语。

我不断收到 Use of Undeclared identifier 错误。我试图找出导致错误的原因。

这是 introClass.h

    #import <UIKit/UIKit.h>

    @interface XYZperson : NSObject

    @property NSString *firstName;
    @property NSString *lastName;
    @property NSDate *dateOfBirth;
    - (void)sayHello;
    - (void)saySomething:(NSString *)greeting;
    + (instancetype)person;
    -(int)xYZPointer;
    -(NSString *)fullName;
    @end

这是 IntroClass.m

#import "IntroClass.h"

@implementation XYZperson
-(NSString *)fullName
{
    return[NSString stringWithFormat:@" %@ %@", self.firstName, self.lastName];
}

-(void)sayHello
{
    [self saySomething:@"Hello %@", fullName]; //use of undeclared identifier "fullName"
};

-(void)saySomething:(NSString *)greeting
{
    NSLog(@"%@", greeting);
}

+(instancetype)person{
   return [[self alloc] init];
};

- (int)xYZPointer {
    int someInteger;
    if (someInteger != nil){
        NSLog(@"its alive");
    }
    return someInteger;
};


@end
4

3 回答 3

2

问题在于fullName方法的名称。应该self用方括号调用它。

由于saySomething:需要一个参数,因此您需要 (1) 删除@"Hello %@"调用的部分,如下所示:

-(void)sayHello {
    [self saySomething:[self fullName]];
};

@"Hello %@"或从and制作单个字符串[self fullName],如下所示:

-(void)sayHello {
    [self saySomething:[NSString stringWithFormat:@"Hello %@", [self fullName]]];
};
于 2014-01-29T20:00:13.363 回答
1

您正在传回一串名字和姓氏,但我没有看到您为它们设置值的任何地方。正如其他人指出的那样尝试

    -(void)sayHello
    {
         _firstName = [NSString stringWithFormat:@"John"];
         _lastName = [NSString stringWithFormat:@"Doe"];

         //if you want to see what's happening through out your code, NSLog it like
        NSLog(@"_firstName: %@ ...", _firstName);
        NSLog(@"_lastName: %@ ...", _lastName);

        NSString *strReturned = [self fullName];
        NSString *concatStr = [NSString stringWithFormat:@"Hello %@", strReturned];

        NSLog(@"strReturned: %@ ...", strReturned);
        NSLog(@"concatStr: %@ ...", concatStr);

        [self saySomething:concatStr]; 
    };

    -(NSString *)fullName
    {
        return[NSString stringWithFormat:@" %@ %@", self.firstName, self.lastName];
    }
于 2014-01-29T20:09:08.283 回答
0

利用

[self saySomething:@"Hello %@", self.fullName]];

或者

[self saySomething:@"Hello %@", [self fullName]];
于 2014-01-29T20:07:15.297 回答