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

1.3k Views Asked by At

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? :-)

2

There are 2 best solutions below

2
On

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.

4
On

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.