NSInteger,NSNumber "property 'x' with 'retain' attribute must be of object type "
I am working an application in which data is being populated from an sqlite database. All database related stuff is done in the appdelegate
class. I have used NSMutable
array to hold objects.
I have used a separate NSObject
class for properties.
I am getting the error: property 'x' with 'retain' attribute must be of object type
.
My appdelegate.m
file's code is as:
NSString *amovieName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
NSInteger amovieId = sqlite3_column_int(compiledStatement, 1);
//problem is here the
//value of movieId is coming from database.
//But error: "must be of object type" is puzzling me.
//I am assuming to use NSNumber here.
my NSObject file's code is as:
in .h file-
NSInteger movieId;
its property as:
@property (nonatomic, retain) NSInteger movieId;
and in .m file- 开发者_StackOverflow社区
@synthesize movieId;
then I have just initialize as:
-(id)initWithmovieName:(NSString *)mN movieId:(NSInteger)mId
{
self.movieName=mN;
self.movieId=mId;
return self;
}
I found another way as: assigning value in a NSnumber object .then type caste in NSInteger.for ex;
NSNumber aNSNumbermovieID = sqlite3_column_int(compiledStatement, 1);
NSInteger amovieId = [aNSNumbermovieID integerValue];
but still I am getting the same errors(property 'x' with 'retain' attribute must be of object type).
Any suggestion?
NSInteger is a scalar and not an object. So you shouldn't retain it, it should be assigned. Changing your property will clear up the warning message. You don't need to do the NSNumber stuff that you added in.
@property (nonatomic, assign) NSInteger movieId;
It's a little confusing since NSInteger sounds like a class, but it's just a typedef for int. Apple introduced it as part of 64-bit support so that they can typedef it to the appropriately sized integer for the processor the code is being compiled for.
Just use NSNumber
and you can do:
@property (nonatomic, retain) NSNumber *movieId;
I think the error is because of the @property
retain
for NSInteger
. Assign
is for primitive values like BOOL
, NSInteger
or double
. For objects use retain
or copy
, depending on if you want to keep a reference to the original object or make a copy of it.
Here NSInteger
is clearly not an object so you should try assign
instead of retain
精彩评论