Hiding properties from public access

后端 未结 6 723
攒了一身酷
攒了一身酷 2020-12-07 22:53

I\'m trying to declare properties that are for internal use only in a Private category as such:

@interface BarLayer (Private)

@property (readwr         


        
6条回答
  •  一向
    一向 (楼主)
    2020-12-07 23:41

    Actually, with the latest LLVM compiler, this problem can be solved much better. The previously recommended method to hiding as much about your properties as possible was to declare your variables in your .h, prefix them with an _, declare a property in a class extension in the private .m, and @synthesise that property in your @implementation.

    With the latest LLVM (3.0), you can go farther yet, hiding everything about your property, including the backing ivar. Its declaration can be moved to the .m and even omitted there as well in which case it will be synthesized by the compiler (thanks Ivan):

    Car.h:

    @interface Car : NSObject
    
    - (void)drive;
    
    @end
    

    Car.m:

    @interface Car ()
    
    @property (assign) BOOL driving;
    
    @end
    
    @implementation Car
    @synthesize driving;
    
    - (void)drive {
    
        self.driving = YES;
    }
    
    @end
    

提交回复
热议问题