2013-03-07 48 views
0

新建OBJ-C和Cocoa这里递增一个NSNumber

我想只是递增的方法的变量,并使用C++的,我只想用变量++的术语,但没有按没有工作的NSNumber,所以我想出了

player1Points = [NSNumber numberWithInt :([player1Points intValue] + 1)];

我很想只是重新声明player1Points作为头一个int,但我想保持@synthesize和@property,这样我就不用写获取和设置程序。

有没有更简单的方法来写这段代码?

+2

您可以使用带'int'属性的'@ synthesize'和'@ property'。 – 2013-03-07 23:23:11

+0

看看[NSNumber类参考](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html) – 2013-03-08 12:03:24

回答

7

你仍然可以把它声明NSInteger的,属性可以是原始类型,以及:

@property (nonatomic,assign) NSInteger player1Points; 

您仍然可以合成它。

另外,还有一个新的语法,这将使使用NSNumber的更舒适:

player1Points = @(player1Points.integerValue+1); 
+0

像冠军一样工作,谢谢 – nukerebel 2013-03-08 01:47:55

1

可以使用原语的属性,就像这样:

@property (nonatomic, assign) int player1Points; 
0

与所有的答案同意,另一种选择(尽管可能是编码过度),是使用称为增加(或其他)的实例方法在NSNumber上创建一个类别。所以你可以在你的应用程序的任何地方有类似[player1Points increase];的东西。