开发者

The declared property issue in Objective-C

开发者 https://www.devze.com 2023-04-03 12:31 出处:网络
My xcode version is 4.2. When I use property to generate getter/setter methods. Here is the code: @interface NewSolutionViewController : UIViewController

My xcode version is 4.2. When I use property to generate getter/setter methods. Here is the code:

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

There is no issue exist. However when I turn into basic data type

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

My program crashed. Do you guys know开发者_如何学编程 the reason?


A basic data type is not an object, and hence does not need to be declared weak.

Use

@property (nonatomic, assign) int a;

to directly assign to the variable.


The problem is that an int is not an NSObject.


Elaborating a bit...

NSObject is the root class of most Objective-C class hierarchies. Read up on Apple's Objects, Classes, and Messaging tutorial.

int is a basic machine type. It doesn't have methods, like an object, so it doesn't respond to messages.


int doesn't respond to retain - you probably want to put an assign in the property declaration for that one.

0

精彩评论

暂无评论...
验证码 换一张
取 消