2012-01-08 24 views
0

解決標識錯誤後,我想,我現在已經產生了以下錯誤,但我看不到的地方我已經錯了...錯誤:語義問題:無效的參數類型「的NSString *」來單目表達式

用我學到的,這應該工作....但相反,會產生錯誤..奇怪的是,我碰到的每個例子是不同的?那麼是否有正確的方法來使用函數?

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

它的工作?爲什麼? : -/

任何想法? :-)

+0

你是否在'.m'文件中的getStudentCategory方法中有任何代碼?更多信息將有所幫助。 – jtbandes 2012-01-08 21:29:25

+0

更新信息... – pst007x 2012-01-09 11:53:47

回答

0

編譯器的錯誤信息會告訴你未聲明的標識符是什麼。它是否告訴你「NSString」未申報?如果是這樣,請確保您在使用前先導入<Foundation/Foundation.h><Cocoa/Cocoa.h>

+0

我Student.m頂我:#進口<基金會/ Foundation.h> #進口「Student.h」感謝 – pst007x 2012-01-09 10:36:40

+0

使用未聲明的標識符IN「getStudentCategory」 – pst007x 2012-01-09 11:55:12

+0

更新的信息.. 。 – pst007x 2012-01-09 11:55:53

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開頭的一行是什麼。看起來你正試圖在另一個方法中定義一個方法,而你不能那樣做。

+0

我的理解功能: - (NSString *)getStudentCategory {// DEFINE FUNCTION getStudentCategory}我想定義函數... mmmm? : -/ – pst007x 2012-01-09 20:06:33

+0

@ pst007x:您不能在另一個方法(或函數)內定義方法(或函數)。 – mipadi 2012-01-09 20:26:14

+0

- (NSString *)getStudentCategory { // FUNCTION CODE } //這會產生相同的錯誤? – pst007x 2012-01-09 20:39:14

相關問題