• <legend id='gsSB1'><style id='gsSB1'><dir id='gsSB1'><q id='gsSB1'></q></dir></style></legend>
  • <tfoot id='gsSB1'></tfoot>

        • <bdo id='gsSB1'></bdo><ul id='gsSB1'></ul>

        <small id='gsSB1'></small><noframes id='gsSB1'>

        <i id='gsSB1'><tr id='gsSB1'><dt id='gsSB1'><q id='gsSB1'><span id='gsSB1'><b id='gsSB1'><form id='gsSB1'><ins id='gsSB1'></ins><ul id='gsSB1'></ul><sub id='gsSB1'></sub></form><legend id='gsSB1'></legend><bdo id='gsSB1'><pre id='gsSB1'><center id='gsSB1'></center></pre></bdo></b><th id='gsSB1'></th></span></q></dt></tr></i><div id='gsSB1'><tfoot id='gsSB1'></tfoot><dl id='gsSB1'><fieldset id='gsSB1'></fieldset></dl></div>

        错误:从 JSON 文件发送到 NSMutableArray 的不可变对象的变异方法

        时间:2024-04-14
          <tbody id='yqOKH'></tbody>

        <small id='yqOKH'></small><noframes id='yqOKH'>

        <tfoot id='yqOKH'></tfoot>

              <legend id='yqOKH'><style id='yqOKH'><dir id='yqOKH'><q id='yqOKH'></q></dir></style></legend>
            1. <i id='yqOKH'><tr id='yqOKH'><dt id='yqOKH'><q id='yqOKH'><span id='yqOKH'><b id='yqOKH'><form id='yqOKH'><ins id='yqOKH'></ins><ul id='yqOKH'></ul><sub id='yqOKH'></sub></form><legend id='yqOKH'></legend><bdo id='yqOKH'><pre id='yqOKH'><center id='yqOKH'></center></pre></bdo></b><th id='yqOKH'></th></span></q></dt></tr></i><div id='yqOKH'><tfoot id='yqOKH'></tfoot><dl id='yqOKH'><fieldset id='yqOKH'></fieldset></dl></div>
                • <bdo id='yqOKH'></bdo><ul id='yqOKH'></ul>
                  本文介绍了错误:从 JSON 文件发送到 NSMutableArray 的不可变对象的变异方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这似乎是一个相当普遍的问题,但我查看的解决方案并不能解决该错误.我正在尝试从 JSON 文件中读取 NSMutableArray.我看到的许多建议涉及使用 mutableCopy[NSMutableArray arrayWithArray:] 但这两种解决方案都不能解决使用调用 replaceObjectAtIndex:withObject 时的问题: 见下文.如果您对如何解决此问题有任何建议,请告诉我.

                  This seems to be a fairly common problem, but the solutions that I have looked at do not solve the error. I am trying to read an NSMutableArray from a JSON file. Many of the suggestions I have seen involve using mutableCopy or [NSMutableArray arrayWithArray:] but both of these solutions do not fix the problem when using the call replaceObjectAtIndex:withObject: seen below. Please let me know if you have any advice on how to solve this problem.

                  我还想补充一点,清单列表是 NSMutableArray 对象的 NSMutableArray.

                  I would also like to add that the inventory list is an NSMutableArray of NSMutableArray objects.

                  确切的错误是:

                  Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
                  reason: '-[__NSCFArray replaceObjectAtIndex:withObject:]: 
                  mutating method sent to immutable object'
                  

                  我在实现文件的顶部定义了如下属性:

                  I have the property defined as follows at the top of my implementation file:

                  NSMutableArray *inventoryData;
                  

                  我正在尝试从 JSON 文件中读取它,如下所示:

                  I am trying to read it from a JSON file as follows:

                  - (void)readJSON
                  {
                      //Code to get dictionary full of saves from JSON file (overworld.json) - includes the file path on the ipad as well as
                      //the dictionary itself
                      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                      NSString *documentsDirectory = [paths objectAtIndex:0];
                      NSString *localPath = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:@"savedPaintGameData.json"]];
                      NSString *filePath = [localPath mutableCopy];
                      NSError *e = nil;
                  
                      // Read data from file saved previously - read the raw data from the path, then parse it into a dictionary using JSONObjectWithData
                      NSData *RawJSON = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&e];
                  
                      if (RawJSON == nil) {
                          [self saveGameInitialize];
                      } else {
                          NSMutableDictionary *localDictionary = [[NSMutableDictionary alloc] initWithDictionary:[NSJSONSerialization JSONObjectWithData:RawJSON options:NSJSONReadingAllowFragments error:&e]];
                          NSMutableDictionary *savedDataDictionary = [localDictionary mutableCopy];
                  
                          //inventoryData = [[savedDataDictionary objectForKey:@"inventory"] mutableCopy];
                          inventoryData = [NSMutableArray arrayWithArray:[savedDataDictionary objectForKey:@"inventory"]];
                      }
                  }
                  

                  然后我尝试替换 NSMutableArray 的给定索引处的对象,如下所示:

                  I am then trying to replace an object at the given index of the NSMutableArray as seen here:

                  - (void)setInventoryData: (NSString *) colorKey: (int) change
                  {
                      // Check if inventory already contains the paint and change the amount
                      bool foundPaint = false;
                      int newAmount = 100;    // Magic number prevents crashing @ removal check
                      for (int i = 0; i < [inventoryData count]; i++) {
                          NSMutableArray *object = [inventoryData objectAtIndex:i];
                          if ([[object objectAtIndex:0] isEqualToString:colorKey]) {
                              newAmount = [[object objectAtIndex:1] integerValue] + change;
                              [[inventoryData objectAtIndex:i] replaceObjectAtIndex:1 withObject:[NSNumber numberWithInt:newAmount]];
                              foundPaint = true;
                              break;
                          }
                      }
                  
                      if (newAmount == 0) {
                          [self removeInventoryColor:colorKey];
                      }
                  }
                  

                  推荐答案

                  问题似乎与您工作的深度有关...您正在创建的容器的可变版本仅适用于该级别".您稍后将索引到该级别(即访问更深一层的容器),该级别仍然是不可变的.首次反序列化 JSON 时尝试传递 NSJSONReadingMutableContainers 选项:

                  The issue appears to be surround the depth at which you are working... the mutable versions of containers you are creating only apply to that "level". You are later indexing into that level (i.e. accessing a container one level deeper) which is still immutable. Try passing the NSJSONReadingMutableContainers option when you first unserialize the JSON:

                  NSUInteger jsonReadingOptions = NSJSONReadingAllowFragments | NSJSONReadingMutableContainers;
                  NSMutableDictionary *localDictionary = [[NSMutableDictionary alloc] initWithDictionary:[NSJSONSerialization JSONObjectWithData:RawJSON options:jsonReadinOptions error:&e]];
                  

                  这篇关于错误:从 JSON 文件发送到 NSMutableArray 的不可变对象的变异方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 android webview 中使用弹出窗口 下一篇:自定义弹出窗口显示时的 SwiftUI 导航栏外观和功能

                  相关文章

                  <i id='1aHjX'><tr id='1aHjX'><dt id='1aHjX'><q id='1aHjX'><span id='1aHjX'><b id='1aHjX'><form id='1aHjX'><ins id='1aHjX'></ins><ul id='1aHjX'></ul><sub id='1aHjX'></sub></form><legend id='1aHjX'></legend><bdo id='1aHjX'><pre id='1aHjX'><center id='1aHjX'></center></pre></bdo></b><th id='1aHjX'></th></span></q></dt></tr></i><div id='1aHjX'><tfoot id='1aHjX'></tfoot><dl id='1aHjX'><fieldset id='1aHjX'></fieldset></dl></div>
                  <legend id='1aHjX'><style id='1aHjX'><dir id='1aHjX'><q id='1aHjX'></q></dir></style></legend>
                  1. <tfoot id='1aHjX'></tfoot>

                      <small id='1aHjX'></small><noframes id='1aHjX'>

                      • <bdo id='1aHjX'></bdo><ul id='1aHjX'></ul>