Error: Mutating method sent to immutable object for NSMutableArray from JSON file

前端 未结 1 350
名媛妹妹
名媛妹妹 2021-01-21 07:57

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 su

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-21 08:56

    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]];
    

    0 讨论(0)
提交回复
热议问题