以下代码在升级到 iOS 6 之前可以运行。它也可以在 5.1 iPhone 模拟器中运行,但在 6.0 模拟器和设备上会失败。
尝试在 NSMutableDictionary 的循环中设置对象:forKey。已尝试在循环中添加(如下面的代码所示),并尝试使用对象和键的数组进行初始化,这会导致相同的失败。另一个奇怪的信息是,有时它有效,但大部分时间都失败了。添加的对象是 UILocalNotification,键是表示 WeekDay 的对象(不仅仅是一个简单的字符串)。运行的输出如下图所示。 UILocalNotifications 和键显然不是 NULL,但 MutableDictionary 中添加的对在大多数情况下对于某些对象具有 NULL。大多数情况下,它是最后添加的日期(键),其对象为空。我完全不知道这是如何破坏的,在此先感谢您的帮助!
WeekDay 的复制方法(NSCopying 协议(protocol)):
- (id)copyWithZoneNSZone *)zone
{
WeekDay * copy = [[WeekDay alloc] initWithDay:self.day];
return copy;
}
使用 setObject:forKey: 的代码片段
NSMutableDictionary * newAlarmsDictionary = [[NSMutableDictionary alloc] init];
NSArray * theDayKeys = [[_daysEnabledDict allKeys] sortedArrayUsingSelectorselector(compare];
NSMutableArray * tempNotifyArray = [[NSMutableArray alloc] init];
UILocalNotification * theAlarm = nil;
WeekDay * theWeekDay = nil;
for (int i=0; i < [theDayKeys count]; i++) {
if ([[_daysEnabledDict objectForKey:[theDayKeys objectAtIndex:i]] boolValue] == TRUE) {
theWeekDay = [theDayKeys objectAtIndex:i];
NSDate * now = [NSDate date];
... deleted lines setting up fire date for UILocalNotification, not significant to problem ...
theAlarm = [[UILocalNotification alloc] init];
theAlarm.fireDate = itemDate;
theAlarm.repeatInterval = NSWeekCalendarUnit;
theAlarm.timeZone = [NSTimeZone localTimeZone];
theAlarm.soundName = UILocalNotificationDefaultSoundName;
theAlarm.applicationIconBadgeNumber = 0;
[newAlarmsDictionary setObject:theAlarm forKey:theWeekDay];
[tempNotifyArray addObject:theAlarm];
[theAlarm release];
}
}
}
NSLog(@"--Debug: tempNotifyArray---- %@ -------------", tempNotifyArray);
NSLog(@"--Debug: newAlarmsDictionary ====== %@ =============", newAlarmsDictionary);
这是代码片段末尾的两个 NSlog 语句的输出。这个特殊的运行增加了 4 个通知,从周三到周六。放入 tempNotifyArray 的“警报”是有效的,但在添加到字典时(在本例中为一个)为空。
2012-11-26 11:07:01.087 MedTrack[9728:11303] --Debug: tempNotifyArray---- (
"<UIConcreteLocalNotification: 0x7277940>{fire date = Wednesday, November 28, 2012, 11:06:00 AM Eastern Standard Time, time zone = America/Toronto (EST) offset -18000, repeat interval = NSWeekCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Wednesday, November 28, 2012, 11:06:00 AM Eastern Standard Time, user info = {\n Temp = Fred;\n}}",
"<UIConcreteLocalNotification: 0x8883280>{fire date = Thursday, November 29, 2012, 11:06:00 AM Eastern Standard Time, time zone = America/Toronto (EST) offset -18000, repeat interval = NSWeekCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Thursday, November 29, 2012, 11:06:00 AM Eastern Standard Time, user info = {\n Temp = Fred;\n}}",
"<UIConcreteLocalNotification: 0x75c6590>{fire date = Friday, November 30, 2012, 11:06:00 AM Eastern Standard Time, time zone = America/Toronto (EST) offset -18000, repeat interval = NSWeekCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Friday, November 30, 2012, 11:06:00 AM Eastern Standard Time, user info = {\n Temp = Fred;\n}}",
"<UIConcreteLocalNotification: 0x75c83e0>{fire date = Saturday, December 1, 2012, 11:06:00 AM Eastern Standard Time, time zone = America/Toronto (EST) offset -18000, repeat interval = NSWeekCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, December 1, 2012, 11:06:00 AM Eastern Standard Time, user info = {\n Temp = Fred;\n}}"
) -------------
2012-11-26 11:07:01.097 MedTrack[9728:11303] --Debug: newAlarmsDictionary ====== {
"[WeekDay] 6 (Sat)" = (null);
"[WeekDay] 3 (Wed)" = "<UIConcreteLocalNotification: 0x7277940>{fire date = Wednesday, November 28, 2012, 11:06:00 AM Eastern Standard Time, time zone = America/Toronto (EST) offset -18000, repeat interval = NSWeekCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Wednesday, November 28, 2012, 11:06:00 AM Eastern Standard Time, user info = {\n Temp = Fred;\n}}";
"[WeekDay] 4 (Thu)" = "<UIConcreteLocalNotification: 0x8883280>{fire date = Thursday, November 29, 2012, 11:06:00 AM Eastern Standard Time, time zone = America/Toronto (EST) offset -18000, repeat interval = NSWeekCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Thursday, November 29, 2012, 11:06:00 AM Eastern Standard Time, user info = {\n Temp = Fred;\n}}";
"[WeekDay] 5 (Fri)" = "<UIConcreteLocalNotification: 0x75c6590>{fire date = Friday, November 30, 2012, 11:06:00 AM Eastern Standard Time, time zone = America/Toronto (EST) offset -18000, repeat interval = NSWeekCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Friday, November 30, 2012, 11:06:00 AM Eastern Standard Time, user info = {\n Temp = Fred;\n}}";
这里的问题是你实现了-copyWithZone:
,但是你没有实现-isEqual:
。在不了解对象的完整结构的情况下,我无法回答应该如何实现,但这里有一个很好的基础:
- (BOOL)isEqualid)otherObject;
{
if ([otherObject isKindOfClass:[self class]]) {
WeekDay *otherWeekDay= (WeekDay *)otherObject;
if (self.day != [otherWeekDay day]) return NO;
if (self.name != [otherWeekDay name]) return NO;
return YES;
}
return NO;
}
- (NSUInteger) hash;
{
return self.day ^ [self.name hash];
}
关于ios - NSMutableDictionary setObject :forKey (ios 6 without ARC) results in NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13568682/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |