简体   繁体   中英

Getting error use of undeclared identifier “value” when assigning parameter to variable

When I assign value to age, I get this error:

@interface Person : NSObject
{
    int age;
} 
-(void)setAge;   
@end

I tried to use self.age , yet it did not work

Here is my .m file:

@implementation Person 
-(void)setAge(int)value
{ 
    age = value;
}    
@end

I tried several differnet things. ..I get this error when I type this: age = value; do you know why this is?

You should add:

-(void)setAge:(int)value;

In the header file because the current method specification you have there doesn't have a parameter.

Your method in the implementation should also have the same spec as it's missing a colon currently.

You have actually declared one method ( -setAge ) and implemented another ( -setAge: , note the colon). You should really declare age as a property and avoid explicit ivars as much as possible. Also, I hope you have properly formatted the class in your real code.

@interface Person : NSObject 

@property (nonatomic) int age;

@end

@implementation Person 

-(void)setAge:(int)value
{ 
    _age = value;
}

@end

Note that it is no longer necessary to explicitly @synthesize properties, and they automatically synthesize with an underscored ivar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM