美文网首页iOS
iOS中@Synthesized现在还有啥用

iOS中@Synthesized现在还有啥用

作者: 码农二哥 | 来源:发表于2016-11-07 15:39 被阅读58次

    Most Properties Are Backed by Instance Variables

    You Can Customize Synthesized Instance Variable Names

    As mentioned earlier, the default behavior for a writeable property is to use an instance variable called _propertyName.

    If you wish to use a different name for the instance variable, you need to direct the compiler to synthesize the variable using the following syntax in your implementation:

    @implementation YourClass
    @synthesize propertyName = instanceVariableName;
    ...
    @end
    

    For example:

    @synthesize firstName = ivar_firstName;

    In this case, the property will still be called firstName, and be accessible through firstName and setFirstName: accessor methods or dot syntax, but it will be backed by an instance variable called ivar_firstName.

    Important: If you use @synthesize without specifying an instance variable name, like this:

    @synthesize firstName;
    
    • the instance variable will bear the same name as the property.
    • In this example, the instance variable will also be called firstName, without an underscore.

    References

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW2

    相关文章

      网友评论

        本文标题:iOS中@Synthesized现在还有啥用

        本文链接:https://www.haomeiwen.com/subject/eheouttx.html