@interface parent : NSObject
@property(strong,nonatomic,readonly) NSString* familyName;
@end
@implementation parent
- (instancetype)init{
self = [super init];
if (self) {
}
return self;
}
@end
@interface child : parent
@property(strong,nonatomic) NSString* familyName;
@end
@implementation child
//@synthesize familyName = _familyName;
- (instancetype)init{
self = [super init];
if (self) {
}
return self;
}
//- (void)setFamilyName:(NSString *)familyName{
// _familyName = familyName;
//}
@end
如上,child重载了父类只读属性familyName,child重载后变成了可读可写权限。然后在调用时如下,会出现崩溃。
child *c = [[child alloc]init];
c.familyName = @"fff";
NSLog(@"%@",c.familyName);
原因是因为familyName在父类只读,所以编译器只自动生成了getter方法。child修改了读写权限后,要手动添加settter才行,并且还有调用@synthesize生成变量。
网友评论