【问题标题】:Objective-C exercises undeclared identifier errorObjective-C 练习未声明的标识符错误
【发布时间】:2014-02-21 20:39:30
【问题描述】:

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

我不断收到“使用未声明的标识符”错误。我试图找出导致错误的原因。

这是 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

【问题讨论】:

    标签: ios objective-c object-code


    【解决方案1】:

    问题在于fullName 是一个方法的名称。应该使用方括号在self 上调用它。

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

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

    或者从@"Hello %@"[self fullName]创建一个字符串,像这样:

    -(void)sayHello {
        [self saySomething:[NSString stringWithFormat:@"Hello %@", [self fullName]]];
    };
    

    【讨论】:

    • 这会返回错误:方法调用的参数太多,预期为 1,有 2 个。
    • 非常感谢,它成功了!您能否解释一下为什么 SayHello 方法只允许 1 个参数?
    • @user3084800 这是因为它是这样声明的:为了允许多个参数,函数需要声明“命名”参数,如-(void)saySomething:(NSString *)greeting toSomeone:(NSString*)target; 或允许可变数量的参数,如在stringWithFormat: 通话中。
    【解决方案2】:

    使用

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

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

    【讨论】:

      【解决方案3】:

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

          -(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];
          }
      

      【讨论】:

      • 我尝试了第二种方法,它自动将名字更正为_firstName,将lastName 更正为_lastName。我在 [self saysomething:@"hello %@", [self fullname]] 上收到错误消息;说明:方法调用的参数太多,预期 1,有 2。
      猜你喜欢
      • 2014-07-30
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多