菜鸟教程小白 发表于 2022-12-12 23:51:18

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


                                            <p><p>以下代码在升级到 iOS 6 之前可以运行。它也可以在 5.1 iPhone 模拟器中运行,但在 6.0 模拟器和设备上会失败。</p>

<p>尝试在 NSMutableDictionary 的循环中设置对象:forKey。已尝试在循环中添加(如下面的代码所示),并尝试使用对象和键的数组进行初始化,这会导致相同的失败。另一个奇怪的信息是,有时它有效,但大部分时间都失败了。添加的对象是 UILocalNotification,键是表示 WeekDay 的对象(不仅仅是一个简单的字符串)。运行的输出如下图所示。 UILocalNotifications 和键显然不是 NULL,但 MutableDictionary 中添加的对在大多数情况下对于某些对象具有 NULL。大多数情况下,它是最后添加的日期(键),其对象为空。我完全不知道这是如何破坏的,在此先感谢您的帮助!</p>

<p>WeekDay 的复制方法(NSCopying 协议(protocol)):</p>

<pre><code>- (id)copyWithZone:(NSZone *)zone
{
    WeekDay * copy = [ initWithDay:self.day];
    return copy;
}
</code></pre>

<p>使用 setObject:forKey: 的代码片段</p>

<pre><code>NSMutableDictionary * newAlarmsDictionary = [ init];
NSArray * theDayKeys = [ sortedArrayUsingSelector:@selector(compare:)];

NSMutableArray * tempNotifyArray = [ init];
UILocalNotification * theAlarm = nil;
WeekDay * theWeekDay = nil;

for (int i=0; i &lt; ; i++) {

    if ([] boolValue] == TRUE) {

      theWeekDay = ;

      NSDate * now = ;

... deleted lines setting up fire date for UILocalNotification, not significant to problem ...

      theAlarm = [ init];
      theAlarm.fireDate = itemDate;
      theAlarm.repeatInterval = NSWeekCalendarUnit;
      theAlarm.timeZone = ;
      theAlarm.soundName = UILocalNotificationDefaultSoundName;
      theAlarm.applicationIconBadgeNumber = 0;

      ;
      ;
      ;
      }
    }
}
NSLog(@&#34;--Debug: tempNotifyArray---- %@ -------------&#34;, tempNotifyArray);
NSLog(@&#34;--Debug: newAlarmsDictionary ====== %@ =============&#34;, newAlarmsDictionary);
</code></pre>

<p>这是代码片段末尾的两个 NSlog 语句的输出。这个特殊的运行增加了 4 个通知,从周三到周六。放入 tempNotifyArray 的“警报”是有效的,但在添加到字典时(在本例中为一个)为空。</p>

<pre><code>2012-11-26 11:07:01.087 MedTrack --Debug: tempNotifyArray---- (

&#34;&lt;UIConcreteLocalNotification: 0x7277940&gt;{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}}&#34;,


&#34;&lt;UIConcreteLocalNotification: 0x8883280&gt;{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}}&#34;,


&#34;&lt;UIConcreteLocalNotification: 0x75c6590&gt;{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}}&#34;,


&#34;&lt;UIConcreteLocalNotification: 0x75c83e0&gt;{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}}&#34;
</code></pre>

<p>) -------------</p>

<pre><code>2012-11-26 11:07:01.097 MedTrack --Debug: newAlarmsDictionary ====== {


&#34; 6 (Sat)&#34; = (null);


&#34; 3 (Wed)&#34; = &#34;&lt;UIConcreteLocalNotification: 0x7277940&gt;{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}}&#34;;


&#34; 4 (Thu)&#34; = &#34;&lt;UIConcreteLocalNotification: 0x8883280&gt;{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}}&#34;;


&#34; 5 (Fri)&#34; = &#34;&lt;UIConcreteLocalNotification: 0x75c6590&gt;{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}}&#34;;   
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这里的问题是你实现了<code>-copyWithZone:</code>,但是你没有实现<code>-isEqual:</code>。在不了解对象的完整结构的情况下,我无法回答应该如何实现,但这里有一个很好的基础:</p>

<pre><code>- (BOOL)isEqual:(id)otherObject;
{
    if (]) {
      WeekDay *otherWeekDay= (WeekDay *)otherObject;
      if (self.day != ) return NO;
      if (self.name != ) return NO;
      return YES;
    }
    return NO;
}

- (NSUInteger) hash;
{
    return self.day ^ ;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - NSMutableDictionary setObject :forKey (ios 6 without ARC) results in NULL,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/13568682/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/13568682/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - NSMutableDictionary setObject :forKey (ios 6 without ARC) results in NULL