strox
strox

Reputation: 23

The declared property issue in Objective-C

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?

Upvotes: 2

Views: 113

Answers (3)

PengOne
PengOne

Reputation: 48398

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.

Upvotes: 1

Carl Norum
Carl Norum

Reputation: 224864

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

Upvotes: 0

futureelite7
futureelite7

Reputation: 11502

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.

Upvotes: 5

Related Questions