Tôi đang tạo ứng dụng và tôi có một lớp học với nhiều thuộc tính và tôi đã tự hỏi liệu có thể cung cấp cho họ giá trị mặc định hay không. Bởi vì nếu tôi thực hiện một phương pháp init, đó là rất nhiều công việc phải gõ tất cả mọi thứ, có một cơ hội lớn của typo và nó chỉ là không đẹp mã ...Mục tiêu-C đặt giá trị mặc định cho một thuộc tính
Đây là cách lớp học của tôi trông giống như:
// 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;
Vì vậy, sau đó, trong việc thực hiện, tôi đang làm một cái gì đó như:
@synthesize aerialAbility = _aerialAbility;
Và tôi đã tự hỏi nếu nó sẽ có thể để làm điều này:
@interface MyClass : NSObject
@property (nonatomic) int someProperty;
@end
@implementation MyClass
@synthesize someProperty = _someProperty = 10;
@end
Tôi biết điều này sẽ không hoạt động, và điều này không đúng chút nào, nhưng tôi đã tự hỏi liệu có cách nào để làm điều gì đó như thế này không.
Giống như trong java bạn có thể:
class MyClass
{
private int someProperty = 10;
public int getSomeProperty(){return someProperty;}
public void setSomeProperty(int someProperty){this.someProperty = someProperty;}
}
Đúng vậy - initializer là phạt tiền nơi để thiết lập giá trị ban đầu cho một ivar hoặc tài sản. Cụ thể hơn, initializer được chỉ định (initializer mà bất kỳ initializer khác cho lớp sẽ gọi) là một trong những nơi bạn nên thiết lập các giá trị đó. – Caleb
Cảm ơn các đề xuất, tuy nhiên, tôi đang cố gắng tránh đặt 30+ bài tập trong trình khởi tạo và lớp học không thể chia ra được (dĩ nhiên, nó luôn có thể, nhưng nó sẽ làm cho thiết kế lớp của tôi tồi tệ hơn). –
tại sao muốn tránh điều đó tôi không nhận được. Các init được thực hiện cho mục đích này, hoặc bạn có thể sử dụng một từ điển mà bạn sẽ phải thiết lập trong init anyway ... – chikuba