Swift NSMutableArray再添加一个数组
问题是我第一次从 WebServices
获取数据,所以我已经在 TableView
上显示了此数据,然后用户滚动了下tableview然后再次调用 WebSevices
并将此数据再次添加到数组
中,但是当我尝试在nsmutable类型数组应用中再次添加数据时崩溃
The Problem is First time I get data from WebServices
so I have show this data on TableView
Then user scroller down tableview then again call WebSevices
and add this data to again in array
but when I try to add data again in nsmutable type array app is crashing
这是我的代码。有什么办法吗?
Here is my code. Any solution?
第一次数据加载正在工作
1st time Data load is Working
var ary_mutable: NSMutableArray!
ary_mutable = NSMutableArray()
ary_mutable=jsonResult as AnyObject as! NSMutableArray
self.tbl_T.reloadData();
self.tbl_T.delegate=self;
self.tbl_T.dataSource=self;
第二次加载数据并添加旧数组而无法正常工作
2nd Time data load and add with old array not Working
var myArray :NSArray!
myArray = jsonResult as AnyObject as! NSArray
ary_mutable.addObject(myArray.objectAtIndex(0))
此错误
[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
我也尝试过此代码,但不起作用
I have Try this code also But Not Working
ary_mutable.addObject(myArray)
您应该创建一个可变数组从一成不变。
You should create a mutable array from immutable. This:
ary_mutable=jsonResult as AnyObject as! NSMutableArray
不会这样做,它只是从一种类型到另一种类型的静态转换。您必须构造一个新的NSMutableArray,然后用所需的值填充它。
does not do it, it is just a static cast from one type to another. You have to construct a new NSMutableArray and then fill it with required values.
将代码更改为
var ary_mutable = NSMutableArray()
// everytime you receive a new data
ary_mutable.addObjectsFromArray(jsonResult as! [AnyObject])