NSMutableDictionary setObject:forKey(没有ARC的ios 6)导致NULL

时间:2012-11-26 16:12:17

标签: ios nsmutabledictionary

以下代码在升级到iOS 6之前正常工作。它也适用于5.1 iPhone模拟器,但在6.0模拟器和设备上失败。

尝试将循环中的setObject:forKey转换为NSMutableDictionary。尝试添加循环(如下面的代码所示),并尝试通过使用数组初始化对象和键导致相同的失败。另一个奇怪的信息是,它有时会起作用,但大部分时间都会失败。要添加的对象是UILocalNotification,键是表示WeekDay的对象(不仅仅是一个简单的字符串)。运行输出如下所示。 UILocalNotifications和键显然不是NULL,但是MutableDictionary中添加的对在大多数情况下对于某些对象都是NULL。大多数情况下,它是最后添加的日期(键),其对象为null。我完全不知道如何打破这一点,提前感谢任何帮助!

WeekDay的复制方法(NSCopying Protocol):

- (id)copyWithZone:(NSZone *)zone
{
    WeekDay * copy = [[WeekDay alloc] initWithDay:self.day];
    return copy;
}

使用setObject的代码段:forKey:

NSMutableDictionary * newAlarmsDictionary = [[NSMutableDictionary alloc] init];
NSArray * theDayKeys = [[_daysEnabledDict allKeys] sortedArrayUsingSelector:@selector(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个通知,通过sat sat sat。放入tempNotifyArray的'alarm'是有效的,但是当添加到字典中时(在这种情况下为一个)是null。

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}}";    

1 个答案:

答案 0 :(得分:4)

此处的问题是您实施-copyWithZone:,但未能实施-isEqual:。在不知道对象的完整结构的情况下,我无法回答应该如何实现,但这是一个很好的基础:

- (BOOL)isEqual:(id)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];
}