且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从子类中的超类覆盖属性

更新时间:2022-05-17 08:54:46

只有超类才能访问ivar _species 。您的子类应如下所示:

Only the superclass has access to the ivar _species. Your subclass should look like this:

- (NSString *)species {
    NSString *value = [super species];
    if (!value) {
        self.species = @"Homo sapiens";
    }

    return [super species];
}

如果当前未设置值,则将值设置为默认值。另一个选择是:

That sets the value to a default if it isn't currently set at all. Another option would be:

- (NSString *)species {
    NSString *result = [super species];
    if (!result) {
        result = @"Home sapiens";
    }

    return result;
}

如果没有值,则不会更新该值。它只是根据需要返回默认值。

This doesn't update the value if there is no value. It simply returns a default as needed.