在解决了一个标识符错误之后,我想,我现在已经产生了以下错误,但是我看不到我哪里出错了......
根据我所学到的,这应该可以工作....但会产生错误..奇怪的是我遇到的每个示例都不一样?那么有没有正确的方法来使用函数呢?
在以下 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)
它有效吗?为什么?:-/
有任何想法吗?:-)