Objective-C设置属性的默认值 [英] Objective-C set default value for a property

查看:831
本文介绍了Objective-C设置属性的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,并且我有一个具有很多属性的类,我想知道是否可以为它们提供默认值。因为如果我使用init方法,则要键入所有内容需要进行大量工作,因此输入错字的可能性很大,而且编码也不太好...



我的班级看起来像这样:

  //守门员属性
@property(非原子)int airAbility;
@property(nonatomic)int commandOfArea;
@属性(非原子)int通讯;
@property(nonatomic)int eccentricity;
@property(非原子)int处理;
@属性(非原子)int踢;
@property(nonatomic)int oneOnOnes;
@属性(非原子)int反射;
@property(nonatomic)int rushingOut;
@property(nonatomic)int trendToPunch;
@property(nonatomic)int抛出;

//技术属性
@property(非原子)int角;
@属性(非原子)int穿越;
@property(nonatomic)int盘带;
@property(非原子)int精加工;
@property(非原子)int firstTouch;
@property(nonatomic)int freeKickTaking;
属性(非原子)int标题;
@property(非原子)int longShots;
@property(nonatomic)int longThrows;
@属性(非原子)int标记;
@property(nonatomic)int通过;
@property(nonatomic)int惩罚
@属性(非原子)int跟踪;
@property(nonatomic)int技术;

//心理属性
@property(nonatomic)int agression;
@属性(非原子)int预期;
@property(nonatomic)int bravey;
@property(nonatomic)int镇静;
@属性(非原子)int浓度;
@property(nonatomic)int创意;
@属性(非原子)int决策;
属性(非原子)int确定;
@property(nonatomic)int天赋;
@属性(非原子)int影响;
@property(nonatomic)int offTheBall;
@property(非原子)int定位;
@property(nonatomic)int团队合作;
@property(nonatomic)int workRate;

//物理属性
@property(nonatomic)int加速;
@属性(非原子)int敏捷性;
@属性(非原子)int余额;
@property(nonatomic)int跳跃;
@property(nonatomic)int naturalFitness;
@property(nonatomic)int步伐;
@property(nonatomic)int耐力;
@属性(非原子)int强度;

因此,在实现中,我正在做类似的事情:

  @synthesize aircraftAbility = _aerialAbility; 

我想知道是否有可能做到这一点:

  @interface MyClass:NSObject 
@属性(非原子)int someProperty;

@end

@implementation MyClass
@synthesize someProperty = _someProperty = 10;
@end

我知道这行不通,而且在全部,但我想知道是否有一种方法可以做到这一点。



就像在Java中一样,您可以:

  class MyClass 
{
private int someProperty = 10;
public int getSomeProperty(){返回someProperty;}
public void setSomeProperty(int someProperty){this.someProperty = someProperty;}
}


解决方案

我以前从未见过这种行为,但是我很确定这是分配时的初始化步骤一个对象,即设置变量并初始化该对象。

 -(id)init {
if(self = [super init]){
self。 someProperty = 10;
}
返回自我;
}

像这样调用:

  MyClass * test = [[MyClass alloc] init]; 

请注意,您可以拥有多个init函数,该函数可以让您拥有一些不同的默认设置价值观。



@synthesize的作用是告诉预编译器它应该为set / get生成代码,而不是设置属性值。

另外,作为一个个人,即使变量和属性的名称不同,也应将它们连接起来。意见(根本没有意识到这个问题),该对象似乎很大,您可以以某种方式将其拆分或像其他人建议的那样进行操作。也许该类可以从其他几个类继承而来,正如我所说,这只是一个建议,因为我不知道您的其他代码是什么样的:)


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;}
}

解决方案

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 :)

这篇关于Objective-C设置属性的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆