Objective-C set default value for a property

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

    The dictionary suggestion above is a good one. I've sometimes used dedicated configuration objects:

    @interface GoalKeepingAttributes
    @property (nonatomic) int agression
    @property (nonatomic) int ... etc
    @end
    
    @implementation GoalKeepingAttributes
    @end
    

    etc. If needed, this class could have an init method that sets a bunch of values and defaults.

    You could probably use a c-style struct as well, since these are just primitives.

    It's true that this is merely deferring the problem, moving the initialization from the one class to the config class, but:

    • you'd have to do this with a dictionary anyway
    • you can comment the config settings
    • biggest win: the config class is strongly typed, which catches errors at compile time, allows for xcode code completion

    You could split up the configurations into different types (Goalkeeping, Technical, and Mental). Again, it's just splitting up the problem, but that can help keeping things focused.

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