ios 在 iPhone 应用程序中分配 NSInteger 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12275423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assigning NSInteger property in iPhone App
提问by Nazia Jan
I have a table in SQLite I want to insert data in that table. The table has responses_id, participant_id, answer_text, answer_option_update_date_time. responses_id and participant_id are integers. When I am assigning any thing to participant_id it gives an error, object can not set to property.
我在 SQLite 中有一个表,我想在该表中插入数据。该表有responses_id、participant_id、answer_text、answer_option_update_date_time。响应 ID 和参与者 ID 是整数。当我将任何东西分配给参与者_id 时,它会出现错误,对象无法设置为属性。
@interface Coffee : NSObject {
NSInteger coffeeID;
NSInteger participant_Id;
NSString*question_Id;
NSString*answer_option;
NSString*answer_text;
NSString*update_date_time;
//Intrnal variables to keep track of the state of the object.
}
@property (nonatomic, readonly) NSInteger coffeeID;
@property (nonatomic, retain) NSInteger participant_Id;
@property (nonatomic, copy) NSString *question_Id;
@property (nonatomic, copy) NSString *answer_option;
@property (nonatomic, copy) NSString *answer_text;
@property (nonatomic, copy) NSString *update_date_time;
- (void)save_Local {
CereniaAppDelegate *appDelegate = (CereniaAppDelegate *)[[UIApplication sharedApplication] delegate];
Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:0];
coffeeObj.participant_Id=mynumber;
NSString*question="1";
coffeeObj.question_Id=question;
coffeeObj.answer_option=selectionAnswerOption;
coffeeObj.answer_text=professionTextField.text;
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:MM:SS"];
NSString* str = [formatter stringFromDate:date];
UPDATE_DATE_TIME=str;
coffeeObj.update_date_time=UPDATE_DATE_TIME;
//Add the object
[appDelegate addCoffee:coffeeObj];
}
When I am assigning a value to participant_id it gives an error.
当我为participant_id 赋值时,它给出了一个错误。
回答by mttrb
NSInteger
is not a class, it is a basic type like an int
or long
. On iOS NSInteger
is typedef
ed to an int
, on OS X it is typedef
ed to a long
. Therefore, you shouldn't try to retain
an NSInteger
. You should change your property declaration to:
NSInteger
不是一个类,它是一个像int
or一样的基本类型long
。在 iOS 上NSInteger
被typedef
编为int
,在 OS X 上被typedef
编为long
。因此,您不应该尝试retain
使用NSInteger
. 您应该将您的财产声明更改为:
@property (nonatomic, assign) NSInteger participant_Id;
The same goes for your coffeeID
property.
您的coffeeID
财产也是如此。