iOS development-mutating method sent to immutable object error

Source: Internet
Author: User

When I was working today, I was confronted with such a problem.

It was too careless. Mark,

2014-01-05 11:44:34.762 SOFTWAREAPP[1435:C07] * * * terminating app due to uncaught exception ' Nsinternalinconsistencyexception ', Reason: ' * * * *-[jkdictionary Setobject:forkey:]: Mutating method sent to immutable Object

First throw Call stack:

(0x285a012 0x1e48e7e 0x2859deb 0x36b96 0x18610ef 0x6c8df 0xabd61 0xac0f8 0x1e5c6b0 0x1bc3b 0x1e5c6b0 0x1888765 0x27ddf3f 0 x27dd96f 0x2800734 0x27fff44 0x27ffe1b 0x2a927e3 0x2a92668 0xd8cffc 0x2d5d 0x2551725 0x1)

Libc++abi.dylib:terminate called throwing an exception

Program ended with exit code:0


This is the code that reported the mistake.

[CPP]View Plaincopy
  1. nsdictionary *jsondata = [resultdic objectatindex:i];  
  2.             nsarray  *eachlines = [[jsondata objectforkey:@" Newstime " ]  componentsseparatedbystring:@ ". " ];  
  3.             [jsondata  setvalue:[eachlines objectatindex:0] forkey:@" Newstime " ];  
  4. [Testarr Addobject:jsondata];


... I think it's embarrassing to think about it now.

See reason: The following exception description: Meaning I have a variable corresponding to the method let an immutable call

Mutating method (variable counterpart): Methods that are owned by variables that can be changed after creation, such as Nsmutablearray,nsmutabledictionary

Immutable object (immutable variable): Variables that cannot be changed after they are created: such as Nsarray nsdictionary, etc.


After modification, it runs normally.

[CPP]View Plaincopy
  1. Nsmutabledictionary *jsondata = [Resultdic objectatindex:i];
  2. nsarray *eachlines = [[jsondata objectforkey:@" Newstime " ]  componentsseparatedbystring:@ ". " ];  
  3. [jsondata setvalue:[eachlines objectatindex:0] forkey:@" Newstime " ];  
  4. [Testarr Addobject:jsondata];

iOS development-mutating method sent to immutable object error

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.