Swift NSMutableArray add one more array

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?

  • How to make your push notification Open a certain view controller?
  • What is the maximum length of a Push Notification alert text?
  • Objective-C and Swift URL encoding
  • Xcode - Importing different header file with same name based on Target
  • MVVMCross iOS: how to bind a command with Custom transition when switching to different View model
  • iPhone collecting CoreMotion data in the background. (longer than 10 mins)
  • 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))
    

    Got This Error

    [__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
    

    I have Try this code also But Not Working

    ary_mutable.addObject(myArray)
    

    2 Solutions Collect From Internet About “Swift NSMutableArray add one more array”

    You should create a mutable array from immutable. This:

    ary_mutable=jsonResult as AnyObject as! 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.

    Change your code to

    var ary_mutable = NSMutableArray()
    
    // everytime you receive a new data
    ary_mutable.addObjectsFromArray(jsonResult as! [AnyObject])
    

    replace this code:

    var myArray :NSArray!
    myArray = jsonResult as AnyObject as! NSArray
    ary_mutable.addObject(myArray.objectAtIndex(0))
    

    with this code:

    var myArray : NSMutableArray!
    myArray = jsonResult as AnyObject as! NSMutableArray
    ary_mutable.addObjectsFromArray(myArray as [AnyObject]);