Objective-C set default value for a property

前端 未结 7 614
感情败类
感情败类 2021-02-03 20:30

I\'m making an app, and I have a class with quite a lot properties and I was wondering if it was possible to give them a default value. Because if I make an init method, it\'s a

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-03 21:04

    Yes, you can override getter in case to set default value before property was inited.

    For example, define property in .h file:

    @interface MySegmentedControl : UISegmentedControl
    @property (assign, nonatomic) CGFloat systemFontOfSize;
    @end
    

    and override getter and set default value under implementation in .m file:

    @implementation MySegmentedControl    
    -(CGFloat) systemFontOfSize
    {
        return _systemFontOfSize ? _systemFontOfSize : 17.0f;
    }    
    @end
    

提交回复
热议问题