我正在創建一個應用程序,並且我有一個具有相當多屬性的類,我想知道是否可以給它們一個默認值。因爲如果我做一個init方法,這是一個很大的工作類型應有盡有,有錯字的一大機會,它只是沒有很好的編碼...Objective-C設置屬性的默認值
這是我的課怎麼樣子:
// 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;
那麼,在實現,我做這樣的事情:
@synthesize aerialAbility = _aerialAbility;
,我想知道,是否有可能做到這一點:
@interface MyClass : NSObject
@property (nonatomic) int someProperty;
@end
@implementation MyClass
@synthesize someProperty = _someProperty = 10;
@end
我知道這是行不通的,這根本不對,但我想知道是否有辦法做這樣的事情。
像Java中,您可以:
class MyClass
{
private int someProperty = 10;
public int getSomeProperty(){return someProperty;}
public void setSomeProperty(int someProperty){this.someProperty = someProperty;}
}
這是正確的 - 初始化是一個很好的地方爲伊娃或財產設置初始價值。更具體地說,指定的初始化程序(該類的任何其他初始化程序將調用的初始化程序)是您應該設置這些值的那個初始化程序。 – Caleb 2012-03-22 21:02:39
感謝您的建議,但是,我試圖避免在初始化程序中放置30個以上的作業,並且課程無法真正分離(當然,它總是可以的,但這隻會讓我的課堂設計變得更糟)。 – 2012-03-22 21:24:08
爲什麼要避免我不明白。 init是爲此目的而創建的,或者你可以使用一個你必須在init中設置的字典...... – chikuba 2012-03-22 21:29:10