Search code examples
iosswiftnsmutablearraynsarray

how to add two mutable array? Swift


In my app I want to implement pull down to refresh. So when scroll view scroll down its load more data. Now first time when page loads I save data in videoArray array, and when scroll down I got data in videoArray2 array so how can I add value of videoArray2 to videoArray? both are nsmuttablearray.

Here is my code:

var videoArray : NSMutableArray = NSMutableArray()
var videoArray2 : NSMutableArray = NSMutableArray()

First time I stored value in this array

self.videoArray = (response.result.value)?.valueForKey("posts") as! NSMutableArray

Second time I stored value in this array

self.videoArray2 = (response.result.value)?.valueForKey("posts") as! NSMutableArray

This is how I tried to append

for enumerator in self.videoArray2
{
    self.videoArray.addObject(enumerator)
}

but its throwing error like

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'

So how can I solve this?


Solution

  • let videoArray = (response.result.value)?.valueForKey("posts") as! NSArray
    self.videoArray = NSMutableArray(array:videoArray)
    

    and the same for the second array