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?
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.
精彩评论