2013-04-09 36 views
2

我得到的- [NSDictionary的initWithObjects:forKeys:]:對象的計數(0)鍵的計數不同(2)

錯誤 - [NSDictionary的initWithObjects:forKeys:]:對象的計數(0 )不同於計數鍵(2)'

這裏是我的代碼保存plist。

@interface setting : UIViewController{ 
    UIDatePicker *datePicker; 
    IBOutlet UILabel * morningtime; 
    UIDatePicker *afternoonpicker; 
    NSString*morningtime1; 
    NSString*afternoontime1; 
    IBOutlet UILabel *afternoontime; 
} 
@property (nonatomic,retain) IBOutlet UIDatePicker *datePicker; 
@property (strong, nonatomic) IBOutlet UIDatePicker *afternoonpicker; 

@property (nonatomic, retain) IBOutlet NSString *morningtime1; 

@property (nonatomic, retain) IBOutlet NSString *afternoontime1; 
@property (nonatomic, retain) IBOutlet UILabel *morningtime; 

@property (nonatomic, retain) IBOutlet UILabel *afternoontime; 
@property (weak, nonatomic) IBOutlet UIButton *morning; 




- (IBAction)savetext:(id)sender { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
    // get documents path 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    // get the path to our Data/plist file 
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"]; 

    self.morningtime1 = morningtime.text; 
    self.afternoontime1 = afternoontime.text; 
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: morningtime1, afternoontime1, nil] forKeys:[NSArray arrayWithObjects: @"Morning", @"Afternoon", nil]]; 

    NSString *error = nil; 
    // create NSData from dictionary 
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error]; 

    // check is plistData exists 
    if(plistData) 
    { 
     // write plistData to our Data.plist file 
     [plistData writeToFile:plistPath atomically:YES]; 
    } 
    else 
    { 
     NSLog(@"Error in saveData: %@", error); 
    } 
} 
+4

爲什麼'NSString'屬性標記爲IBOutlet?這沒有理由。 – rmaddy 2013-04-09 18:38:34

回答

8

morningtime1幾乎肯定nil這裏,過早結束了數組列表。

如果您在此處使用了新的數組文本語法:

@[morningtime1, afternoontime1]; 

,你會得到一個崩潰,因爲它是非法的分配零到NSArray元素。

+1

爲什麼不''afternoontime1'零? – 2013-04-09 18:33:50

+0

因爲對象的數量是零。 (下午time1也可能是零,但你永遠不會那麼遠) – 2013-04-09 18:34:52

+0

那麼,'afternoontime1'可能也是零,但是如果數組數爲0,這意味着列表中的第一個項目正在終止列表。 – 2013-04-09 18:34:58

0

這條線:

NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: morningtime1, afternoontime1, nil] forKeys:[NSArray arrayWithObjects: @"Morning", @"Afternoon", nil]]; 

應該是:

NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: self.morningtime1, self.afternoontime1, nil] forKeys:[NSArray arrayWithObjects: @"Morning", @"Afternoon", nil]]; 

換句話說,參考屬性,而不是實例變量。如你所知,伊法爾從未設置過(您設置的是實際設置生成的伊瓦爾的屬性,名稱爲_morningtime1)。

旁註:

擺脫任何明確的實例變量和@synthesize線的屬性。這將避免這種混淆。

+0

@downvoter - 爲什麼? 'morningtime1' ivar是零,因爲它從未設置。通過更改代碼來使用該屬性,問題就消失了。 – rmaddy 2013-04-09 18:35:59

+0

我不是你的downvoter,我同意這些都是好的做法,但這裏沒有保證使用伊娃參考是問題。 – 2013-04-09 18:46:55