【问题标题】:ERROR: Semantic Issue: Invalid argument type 'NSString *' to unary expression错误:语义问题:一元表达式的参数类型“NSString *”无效
【发布时间】:2012-01-08 21:25:32
【问题描述】:

在解决了一个标识符错误之后,我想,我现在产生了以下错误,但是我看不到我哪里出错了......

根据我所学到的,这应该可以工作....但反而会产生错误..奇怪的是我遇到的每个示例都不一样?那么函数有没有正确的使用方法呢?

在以下 Student.h 文件中找到

错误:语义问题:一元表达式的参数类型“NSString *”无效

错误:解析问题:预期的表达式

我对编码非常陌生,经验有限,所以任何解决此问题的建议都将不胜感激......以及学习曲线......

谢谢

Student.h(看起来像这样)

#import < Foundation/Foundation.h >

@interface Student : NSObject {
@private

NSString *name;
NSString *gender; 
NSString *getStudentCategory;
int age;
}
@property (nonatomic,retain) NSString *name;
@property (nonatomic,retain) NSString *gender;
@property (nonatomic) int age;

- (NSString *)getStudentCategory;

@end

Student.m(看起来像这样)

#import < Foundation/Foundation.h >
#import "Student.h"

@implementation Student
@synthesize name,gender,age;

- (id)init
{
self = [super init];
if (self) { 

    - (NSString *)getStudentCategory  //*ERROR: Semantic Issue: Invalid argument type 'NSString *' to unary expression*
    {
        NSString *studentCategory;
    if (age <=12) 
        studentCategory = @"Primary School Student.";
    else if (age >=13 && age <=17)            //*Error: Parse Issue: Expected expression*
        studentCategory = @"Secondary School Student.";
    else if (age >=18)                       //*Error: Parse Issue: Expected expression*
        studentCategory = @"College Student.";
    }
return self;
}

@end   

main.m(看起来像这样)

#import < Foundation/Foundation.h >
#import "Student.h"

int main (int argc, const char * argv[])
{
@autoreleasepool {

    Student *pupil =[[Student alloc] init];

        pupil.name = @"john";
        pupil.gender = @"male";
        pupil.age = 20; 

        NSLog([NSString stringWithFormat:@"Name: %@, Gender: %@, age %d",pupil.name,pupil.gender,pupil.age]); 

        NSLog([pupil getStudentCategory]);

    }
return 0;

}

我从 Student.m 中删除:

- (id)init
{
self = [super init];
if (self) 

它成功了吗?为什么? :-/

有什么想法吗? :-)

【问题讨论】:

  • .m 文件中的getStudentCategory 方法中有代码吗?更多信息会有所帮助。

标签: objective-c nsstring undeclared-identifier


【解决方案1】:

好吧,这里有一个问题:

- (id)init
{
self = [super init];
if (self) { 
    // WHAT IS THIS CHICANERY?!
    - (NSString *)getStudentCategory  //*ERROR: Semantic Issue: Invalid argument type 'NSString *' to unary expression*
    {
        NSString *studentCategory;
    if (age <=12) 
        studentCategory = @"Primary School Student.";
    else if (age >=13 && age <=17)            //*Error: Parse Issue: Expected expression*
        studentCategory = @"Secondary School Student.";
    else if (age >=18)                       //*Error: Parse Issue: Expected expression*
        studentCategory = @"College Student.";
    }
return self;
}

我不知道以- (NSString *)getStudentCategory 开头的行是什么。看起来您正试图在另一个方法中定义一个方法,但您不能这样做。

【讨论】:

  • 据我了解 FUNCTION: -(NSString *)getStudentCategory { //DEFINE FUNCTION getStudentCategory } 我正在尝试定义函数... mmmm? :-/
  • @pst007x: 你不能在另一个方法(或函数)中定义一个方法(或函数)。
  • - (NSString *)getStudentCategory { //FUNCTION CODE } //这会产生同样的错误吗?
  • 我删除了:- (id)init { self = [super init];如果(自我)//它有效吗?为什么? :-/
【解决方案2】:

编译器的错误信息会告诉你未声明的标识符是什么。它是否告诉您“NSString”未声明?如果是这样,请确保在使用之前导入 &lt;Foundation/Foundation.h&gt;&lt;Cocoa/Cocoa.h&gt;

【讨论】:

  • 在我的 Student.m 的顶部我有:#import #import "Student.h" 谢谢
  • 在“getStudentCategory”中使用未声明的标识符
猜你喜欢
  • 2012-08-18
  • 1970-01-01
  • 2014-05-19
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 2021-04-18
相关资源
最近更新 更多