Objective-C中声明的属性问题

时间:2011-09-09 03:31:16

标签: objective-c

我的xcode版本是4.2。 当我使用property生成getter / setter方法时。 这是代码:

 @interface NewSolutionViewController : UIViewController
 @property(nonatomic, weak) NSString a; 

不存在任何问题。但是当我变成基本数据类型

  @interface NewSolutionViewController : UIViewController
  @property(nonatomic, weak) int a; 

我的程序崩溃了。你们知道原因吗?

3 个答案:

答案 0 :(得分:5)

基本数据类型不是对象,因此不需要声明为弱。

使用

@property (nonatomic, assign) int a;

直接分配给变量。

答案 1 :(得分:1)

问题是int不是NSObject


详细说明......

NSObject是大多数Objective-C类层次结构的根类。阅读Apple的Objects, Classes, and Messaging教程。

int是基本的机器类型。它没有方法,如对象,因此它不响应消息。

答案 2 :(得分:0)

int没有回复retain - 您可能希望在该属性声明中添加assign

相关问题