objective-cIf both getter and setter of a certain attribute need to be rewritten
Need to add @synthesize propertyName = _propertyName;
As for why there are many detailed article analyzes on the Internet, you can check it out
If you rewrite the getter and setter methods at the same time, the system will not automatically generate this member variable for you, so of course an error will be reported saying that this member variable is not recognized. First manually generate member variables, and then rewrite the getter and setter methods at the same time.
BOOL _carOwner;
#pragma mark - getters and setters
-(void)setCarOwner:(BOOL)carOwner
{
_carOwner = carOwner;
// 自定义操作
[[NSUserDefaults standardUserDefaults] setBool:_carOwner forKey:@"KeyIsCarOwner"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (BOOL)carOwner {
// 自定义操作
id result = [[NSUserDefaults standardUserDefaults] valueForKey:@"KeyIsCarOwner"];
if (result) {
return [[[NSUserDefaults standardUserDefaults] valueForKey:@"KeyIsCarOwner"] boolValue];
}
return _carOwner;
}
objective-c
If bothgetter
andsetter
of a certain attribute need to be rewrittenNeed to add
@synthesize propertyName = _propertyName;
As for why there are many detailed article analyzes on the Internet, you can check it out
If you rewrite the getter and setter methods at the same time, the system will not automatically generate this member variable for you, so of course an error will be reported saying that this member variable is not recognized. First manually generate member variables, and then rewrite the getter and setter methods at the same time.
Because you wrote it wrong. (:з ∠) is written as self-> name = name;
I just tried it and it’s exactly what you said. Don't know why. After adding @synthesize name = _name; syntax, no error will be reported.
Just use self.name, the effect is the same anyway