0
我目前正在編程簽入應用程序。我正在嘗試使用NSDictionary將所有簽入詳細信息收集到NSUserDefaults。我有這個代碼塊;如何收集兩個NSDictionary並添加到NSUserDefaults?
NSMutableDictionary *oldcheckin =[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"MissionID"];
if([oldcheckin valueForKey:@"CheckinID"]) // I don't know how to take last object CheckinID value
{
int number = [[oldcheckin valueForKey:@"CheckinID"] intValue] +1;
NSMutableDictionary *newcheckin= [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%d",number], @"CheckinID",dateRightLabel.text, @"Date", noteLabel.text, @"Note",typeLabel.text, @"TypeID",projectLabel.text, @"ProjectID",noteLabel.text, @"Note", longitute, @"Longitute",latitude, @"Latitude",nil];
}
else
{
[[NSUserDefaults standardUserDefaults] setObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"1", @"CheckinID",dateRightLabel.text, @"Date", noteLabel.text, @"Note",typeLabel.text, @"TypeID",projectLabel.text, @"ProjectID",noteLabel.text, @"Note", longitute, @"Longitute",latitude, @"Latitude",nil] forKey:@"MissionID"];
NSDictionary * myDictionary = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"MissionID"];
NSLog(@"%@",myDictionary);
}
[[NSUserDefaults standardUserDefaults] synchronize];
很容易將第一個NSDictionary添加到NSUserDefaults。然而,在NSDictionary值之前很難採用新的簽入值收集。例如,我首次使用上面給出的代碼塊將這些值添加到NSUserDefaults。
{
CheckinID = 1;
Date = "2016.09.19 21:15";
Latitude = "37.33233141";
Longitute = "-122.03121860";
Note = "test";
ProjectID = "2";
TypeID = "1";
}
當我想添加其他登記入住信息,我需要把舊登記入住信息,並與新的登記入住信息收集。例如;
{
CheckinID = 1;
Date = "2016.09.19 21:15";
Latitude = "37.33233141";
Longitute = "-122.03121860";
Note = "test";
ProjectID = "2";
TypeID = "1";
},
{
CheckinID = 2;
Date = "2016.09.20 21:15";
Latitude = "35.33233141";
Longitute = "120.03121860";
Note = "test2";
ProjectID = "5";
TypeID = "4";
}
這樣的事情,我需要採取每個檢查後的細節。順便說一下,CheckinID將作爲最後一個checkinID遞增。你可以幫我嗎?謝謝。
@vadian你是對的。我更新了我的答案。謝謝指出。 –
謝謝@ Teja-Nandamuri。有用 :) –