2012-09-05 34 views
5

我在SQLite中有一個表我想在該表中插入數據。該表具有respond_id,participant_id,answer_text,answer_option_update_date_time。 respond_id和participant_id是整數。當我將任何事情分配給participant_id時,它會發出錯誤,對象無法設置爲屬性。在iPhone App中分配NSInteger屬性

@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]; 
} 

當我將值賦給participant_id時,它會給出錯誤。

回答

34

NSInteger不是一個類,它是一個基本類型,如intlong。在iOS上NSIntegertypedef編輯爲int,在OS X上它是typedef編輯爲long。因此,您不應該嘗試retainNSInteger。您應該將您的財產聲明更改爲:

@property (nonatomic, assign) NSInteger participant_Id; 

您的coffeeID屬性也是如此。

+0

感謝它爲我工作 –

+2

如果你確實希望它是一個對象,你可以將它包裝在一個'NSNumber'中。 – QED

+0

沒問題。人們常常認爲「NSInteger」和「NSUInteger」是類。 – mttrb