iOS開發-mutating method sent to immutable object錯誤

來源:互聯網
上載者:User

iOS開發-mutating method sent to immutable object錯誤

今天幹活的時候,遇到了這樣一個問題..

實在是太粗心了。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 0x27dd96f 0x2800734 0x27fff44 0x27ffe1b 0x2a927e3 0x2a92668 0xd8cffc 0x2d5d 0x2551725 0x1)

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

Program ended with exit code: 0

 

這是報錯處的代碼。

 

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

     

    …現在想想都覺得丟人阿。

     

    看reason:後面的異常說明:意思是我把一個可變數對應的方法讓一個不可變數來調用

    mutating method(可變數對應的方法):是那些在建立後可以被更改的變數所擁有的method,比如NSMutableArray,NSMutableDictionary 等

    immutable object(不可改變的變數):就是那些被建立後不能被改變的變數:比如 NSArray NSDictionary等

     

    修改後,正常運行。

     

     

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.