Objective-C set default value for a property

前端 未结 7 613
感情败类
感情败类 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: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.

提交回复
热议问题