Objective-C set default value for a property

前端 未结 7 609
感情败类
感情败类 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
    
    0 讨论(0)
  • 2021-02-03 21:13

    There is no built-in Java-like way of initializing synthesized properties or ivars in Objective C. However, since your properties look almost identical, you might want to consider making them @dynamic instead of synthesizing them.

    For sure, you would need to write two scary-looking methods (here is a nice and clean example for you), but in return you get a uniform way of storing your properties as objects inside NSMutableDictionary. This opens up several interesting alternatives not available with plain ivars: you could defer initialization of your properties until they are needed, you could provide default values for unset properties, or you could initialize your properties "wholesale" by filling in the dictionary with values for their keys.

    0 讨论(0)
  • 2021-02-03 21:16

    For such a large number of attributes like that, I'd be inclined to store the data as a dictionary rather than individual properties, and I would store the defaults in a property list. NSDictionary objects can be initialised with a property list easily.

    If using a dictionary is not to your tastes, I'd still store the defaults in a property list, and in the designated initialiser, I would loop over the items in the property list and apply them to self using key-value coding. You should note that this is only appropriate for trusted data, not user-supplied data, as it could otherwise be hijacked to set other properties that you aren't expecting.

    0 讨论(0)
  • 2021-02-03 21:16

    Another possibility would be to override the default getters for the properties. In your getters, you can look to see if the values were initialized and, if not, return your default value. (That would work for some property types but not for others, clearly - you need the default value to be one that indicates that no value was set.)

    0 讨论(0)
  • 2021-02-03 21:22
    - (id)init
    {
        self = [super init];
        if (self != nil) {
            [self commonInit];
        }
    
        return self;
    }
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self != nil) {
            [self commonInit];
        }
    
        return self;
    }
    
    -(instancetype)initWithCoder:(NSCoder *)aDecoder { //nib init
        self = [super initWithCoder:aDecoder];
        if (self != nil) {
            [self commonInit];
        }
        return self;
    }
    

    You can set default value and do default logic in commonInit function if the object is a view. If it's not view, you can do it in the init function in my opinion.

    0 讨论(0)
  • 2021-02-03 21:23

    I have never seen this behavior before but I'm pretty sure this is what the init step is for when allocation an object, that is setting variables and initializing the object.

    -(id)init {
         if (self = [super init])  {
           self.someProperty = 10;
         }
         return self;
    }
    

    And the call it like this:

    MyClass* test = [[MyClass alloc] init];
    

    Notice that you can have more than one init function which allows you to have a few different sets of default values.

    What @synthesize does is tell the precompiler that it should generate the code for the set/get, not set the value of the property. The '=" just tells the precomplier that, even though the name of the variable and the property are not the same, they should be connected.

    Also, as a personal opinion (not realated to the question at all), this object seems way to big and you might be able to split it up in some way or do like other person suggested. Maybe this class could inherit from a few other classes to give it the different properties it needs? As I said, it just a suggestion since I don't know what your other code looks like :)

    0 讨论(0)
提交回复
热议问题