Using a BOOL property

前端 未结 3 963
忘了有多久
忘了有多久 2020-12-02 05:13

Apple recommends to declare a BOOL property this way:

@property (nonatomic, assign, getter=isWorking) BOOL working;

As I\'m using Objective

相关标签:
3条回答
  • 2020-12-02 05:53

    Apple recommends for stylistic purposes.If you write this code:

    @property (nonatomic,assign) BOOL working;
    

    Then you can not use [object isWorking].
    It will show an error. But if you use below code means

    @property (assign,getter=isWorking) BOOL working;
    

    So you can use [object isWorking] .

    0 讨论(0)
  • 2020-12-02 06:17

    There's no benefit to using properties with primitive types. @property is used with heap allocated NSObjects like NSString*, NSNumber*, UIButton*, and etc, because memory managed accessors are created for free. When you create a BOOL, the value is always allocated on the stack and does not require any special accessors to prevent memory leakage. isWorking is simply the popular way of expressing the state of a boolean value.

    In another OO language you would make a variable private bool working; and two accessors: SetWorking for the setter and IsWorking for the accessor.

    0 讨论(0)
  • 2020-12-02 06:18

    Apple simply recommends declaring an isX getter for stylistic purposes. It doesn't matter whether you customize the getter name or not, as long as you use the dot notation or message notation with the correct name. If you're going to use the dot notation it makes no difference, you still access it by the property name:

    @property (nonatomic, assign) BOOL working;
    
    [self setWorking:YES];         // Or self.working = YES;
    BOOL working = [self working]; // Or = self.working;
    

    Or

    @property (nonatomic, assign, getter=isWorking) BOOL working;
    
    [self setWorking:YES];           // Or self.working = YES;, same as above
    BOOL working = [self isWorking]; // Or = self.working;, also same as above
    
    0 讨论(0)
提交回复
热议问题