菜鸟教程小白 发表于 2022-12-13 15:28:41

ios - 集合 <__NSArrayM> 在枚举时发生了变异。


                                            <p><p></p><div class="snippet"data-lang="js"data-hide="false"data-console="true"data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>Application Specific Information:
*** Terminating app due to uncaught exception &#39;NSGenericException&#39;, reason: &#39;*** Collection &lt;__NSArrayM: 0x17204b070&gt; was mutated while being enumerated.&#39;
Last Exception Backtrace:
0   CoreFoundation                      0x0000000186acefe0 __exceptionPreprocess + 124
1   libobjc.A.dylib                     0x0000000185530538 objc_exception_throw + 56
2   CoreFoundation                      0x0000000186acea30 __NSFastEnumerationMutationHandler + 128
3   UIKit                               0x000000018cc22f04 - + 220
4   UIKit                               0x000000018cc22d78 - + 88
5   UIKit                               0x000000018d1d1d38 - + 64
6   UIKit                               0x000000018d1d556c _UIGestureRecognizerSendTargetActions + 124
7   UIKit                               0x000000018cd8f470 _UIGestureRecognizerSendActions + 252
8   UIKit                               0x000000018cc31380 - + 720
9   UIKit                               0x000000018d1c5810 _UIGestureEnvironmentUpdate + 988
10UIKit                               0x000000018d1c53e0 - + 404
11UIKit                               0x000000018d1c468c - + 268
12UIKit                               0x000000018cc2f70c - + 3164
13UIKit                               0x000000018cc0033c - + 340
14UIKit                               0x000000018d3fa014 __dispatchPreprocessedEventFromEventQueue + 2400
15UIKit                               0x000000018d3f4770 __handleEventQueue + 4268
16UIKit                               0x000000018d3f4b9c __handleHIDEventFetcherDrain + 148
17CoreFoundation                      0x0000000186a7d42c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
18CoreFoundation                      0x0000000186a7cd9c __CFRunLoopDoSources0 + 540
19CoreFoundation                      0x0000000186a7a9a8 __CFRunLoopRun + 744
20CoreFoundation                      0x00000001869aada4 CFRunLoopRunSpecific + 424
21GraphicsServices                  0x0000000188414074 GSEventRunModal + 100
22UIKit                               0x000000018cc65058 UIApplicationMain + 208
23MyApp                            0x000000010001d0ac main (main.m:16)
24libdyld.dylib                     0x00000001859b959c start + 4</code></pre>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这个错误可能有以下几种原因:</p>

<p><strong>第一个原因</strong></p>

<p>您可能正在尝试使用(内部)<code>For Each Loop</code> 更新或删除数组中的元素。</p>

<ul>
<li><p>不要更新或删除 <code>For Each Loop</code> 中的元素。</p></li>
<li><p>如果您不想对数组进行数据操作,请使用普通的 for 循环,例如:</p>

<pre><code>int i
for (i=0; i &lt; array.count; i++) {
   // update or remove operation
}
</code></pre> </li>
</ul>

<p><strong>第二个原因</strong></p>

<p>您正在尝试修改(更新或删除)非可变数组中的元素。您的错误消息表明,您正在使用 Objective-C 编程语言。</p>

<p>在 Objective-C 中,有两种集合:可变的和不可变的。</p>

<ul>
<li><code>NSArray</code> 是不可变的集合类型,而</li>
<li><code>NSMutableArray</code> 是可变类型的集合</li>
</ul>

<p>使用 <code>NSMutableArray</code> 对数组执行数据操作(更新或删除元素)。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 集合 &lt;__NSArrayM&gt; 在枚举时发生了变异。,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44648610/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44648610/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 集合 &lt;__NSArrayM&gt; 在枚举时发生了变异。