0

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

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

在以下 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) 

它有效吗?为什么?:-/

有任何想法吗?:-)

4

2 回答 2

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是什么。看起来您正在尝试在另一个方法中定义一个方法,但您不能这样做。

于 2012-01-09T19:03:31.843 回答
0

来自编译器的错误消息会告诉您未声明的标识符是什么。它是在告诉你“ NSString”是未声明的吗?如果是这样,请确保您正在导入<Foundation/Foundation.h><Cocoa/Cocoa.h>在使用它之前。

于 2012-01-09T01:35:20.513 回答