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

发布于 2024-12-25 08:41:23 字数 2101 浏览 2 评论 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) 

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

有什么想法吗? :-)

After resolving an identifier error, I think, I have now generated the following errors, but I cannot see where i have gone wrong...

With what I have learnt, this should work.... but instead generates errors.. the weird thing is every example I come across is different? So is there a right way to use a function?

Found in the following Student.h file

Error: Semantic Issue: Invalid argument type 'NSString *' to unary expression

Error: Parse Issue: Expected expression

I am VERY new to coding, with limited experience, so any advice to resolving this would be appreciated... and a learning curve...

Thanks

Student.h (looks like this)

#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 (looks like this)

#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 (looks like this)

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

}

I removed from Student.m:

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

and it worked? why? :-/

Any ideas? :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

謸气贵蔟 2025-01-01 08:41:23

好吧,这里有一个问题:

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

Well, here's a problem:

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

I don't know what the line beginning with - (NSString *)getStudentCategory is. It looks like you're trying to define a method inside of another method, and you can't do that.

少钕鈤記 2025-01-01 08:41:23

编译器的错误消息将告诉您未声明的标识符是什么。它是否告诉您“NSString”未声明?如果是这样,请确保在使用之前导入

The error message from the compiler will tell you what the undeclared identifier is. Is it telling you that "NSString" is undeclared? If so, make sure you're importing <Foundation/Foundation.h> or <Cocoa/Cocoa.h> before you use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文