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 lot of work to type everything, there's a big chance of typo's and it's just not nice coding...
This is how my class looks like:
// Goalkeeping attributes @property (nonatomic) int aerialAbility; @property (nonatomic) int commandOfArea; @property (nonatomic) int communication; @property (nonatomic) int eccentricity; @property (nonatomic) int handling; @property (nonatomic) int kicking; @property (nonatomic) int oneOnOnes; @property (nonatomic) int reflexes; @property (nonatomic) int rushingOut; @property (nonatomic) int tendencyToPunch; @property (nonatomic) int throwing; // Technical attributes @property (nonatomic) int corners; @property (nonatomic) int crossing; @property (nonatomic) int dribbling; @property (nonatomic) int finishing; @property (nonatomic) int firstTouch; @property (nonatomic) int freeKickTaking; @property (nonatomic) int heading; @property (nonatomic) int longShots; @property (nonatomic) int longThrows; @property (nonatomic) int marking; @property (nonatomic) int passing; @property (nonatomic) int penaltyTaking; @property (nonatomic) int tackling; @property (nonatomic) int technique; // Mental attributes @property (nonatomic) int aggression; @property (nonatomic) int anticipation; @property (nonatomic) int bravery; @property (nonatomic) int composure; @property (nonatomic) int concentration; @property (nonatomic) int creativity; @property (nonatomic) int decisions; @property (nonatomic) int determination; @property (nonatomic) int flair; @property (nonatomic) int influence; @property (nonatomic) int offTheBall; @property (nonatomic) int positioning; @property (nonatomic) int teamwork; @property (nonatomic) int workRate; // Physical attributes @property (nonatomic) int acceleration; @property (nonatomic) int agility; @property (nonatomic) int balance; @property (nonatomic) int jumping; @property (nonatomic) int naturalFitness; @property (nonatomic) int pace; @property (nonatomic) int stamina; @property (nonatomic) int strength;
So then, in the implementation, I'm doing something like:
@synthesize aerialAbility = _aerialAbility;
And I was wondering if it would be possible to do this:
@interface MyClass : NSObject @property (nonatomic) int someProperty; @end @implementation MyClass @synthesize someProperty = _someProperty = 10; @end
I know this won't work, and this doesn't right at all, but I was wondering if there's a way to do something like this.
Like in java you can:
class MyClass { private int someProperty = 10; public int getSomeProperty(){return someProperty;} public void setSomeProperty(int someProperty){this.someProperty = someProperty;} }
Right-click the control that you want to change, and then click Properties or press F4. Click the All tab in the property sheet, locate the Default Value property, and then enter your default value. Press CTRL+S to save your changes.
The DefaultValue property specifies text or an expression that's automatically entered in a control or field when a new record is created. For example, if you set the DefaultValue property for a text box control to =Now(), the control displays the current date and time.
You can assign the default value using the DefaultValueAttribute attribute, as shown below.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With