我的countTextView返回3.我已验证for循环确实被调用了3次。 1在if语句中,2在else语句中。但是当我尝试NSLog出数组时,它只包含1个对象。为什么?将相同的NSDictionary添加到NSMutableArray多个时间问题
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
for (int i = 1; i <= [countTextView.text intValue]; i++) {
NSLog(@"called %i times", i);
// prepare keys and object needed to created a dic to save booking info.
NSArray *keys = [NSArray arrayWithObjects:@"PickUpAddress", @"Time", nil];
NSArray *objects = [NSArray arrayWithObjects:[addressTextView.text stringByReplacingOccurrencesOfString:@"\n" withString:@" "], date , nil];
NSDictionary *statusInfoDic = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
if ([prefs objectForKey:@"StatusInfoArray"] == nil) {
NSArray *statusInfoArray = [NSArray arrayWithObject:statusInfoDic];
[prefs setObject:statusInfoArray forKey:@"StatusInfoArray"];
[prefs synchronize];
NSLog(@"nil called on %i", i);
}
else {
NSMutableArray *currentStatusInfoArray = [NSMutableArray arrayWithArray:[prefs objectForKey:@"StatusInfoArray"]];
[currentStatusInfoArray addObject:@"statusInfoDic"];
[prefs synchronize];
NSLog(@"not nil called on %i ", i);
}
}
NSLog(@"statusInfoArray : %@", [prefs objectForKey:@"StatusInfoArray"]);
呀, else leg addObject是一个no-op。如果你想statusInfoArray是可变的,你应该可以这样创建它。或者在else脚本中使用setObject来替换旧数组。 –