關於可變數組的一點小知識,可變數組小知識

來源:互聯網
上載者:User

關於可變數組的一點小知識,可變數組小知識

最近在使用iOS的可變數組時出現了一個錯誤,報錯資訊為:

reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'

字面的意思就是可變數組的方法發送給了一個不可變的對象。

聲明和初始化的代碼如下:

//聲明NSMutableArray *dataArray;//初始化dataArray = [NSMutableArray array];

通過分析dataArray,發現只有在向伺服器請求資料後,對dataArray進行了賦值,想來問題就是應該出現這裡了。

[manager POST:requestUrl       parameters:jsonDictionary          success:^(AFHTTPRequestOperation *operation, id responseObject) {              NSLog(@"json: %@", responseObject);                            dataArray = responseObject;                            [self.tableView reloadData];                        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {              NSLog(@"%@", error);                                      }];

所以問題應該是responseObject這個傳回值是一個不可變的數組,在賦值後,dataArray也變成了不可變的數組。

如果想繼續使用,做些處理就可以了,例如:

dataArray = [NSMutableArray arrayWithArray:dataArray];

 

 

參考:http://stackoverflow.com/questions/22381384/ios-error-nscfarray-removeobjectatindex-mutating-method-sent-to-immutable

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.