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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:59:36  来源:igfitidea点击:

Assigning NSInteger property in iPhone App

iphoneobjective-ciosxcodesqlite

提问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

NSIntegeris not a class, it is a basic type like an intor long. On iOS NSIntegeris typedefed to an int, on OS X it is typedefed to a long. Therefore, you shouldn't try to retainan NSInteger. You should change your property declaration to:

NSInteger不是一个类,它是一个像intor一样的基本类型long。在 iOS 上NSIntegertypedef编为int,在 OS X 上被typedef编为long。因此,您不应该尝试retain使用NSInteger. 您应该将您的财产声明更改为:

@property (nonatomic, assign) NSInteger participant_Id;

The same goes for your coffeeIDproperty.

您的coffeeID财产也是如此。