• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - NSMutableDictionary setObject :forKey (ios 6 without ARC) results in NULL

[复制链接]
菜鸟教程小白 发表于 2022-12-12 23:51:18 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

以下代码在升级到 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}}";    



Best Answer-推荐答案


这里的问题是你实现了-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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap