3

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))

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 Answers 2

7

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])
Sign up to request clarification or add additional context in comments.

2 Comments

This Code Replace old Data . But Requirement is (Old Array Data) + (New Array Data) Means First time load data now Count is 15 then again load 5 more data now array count is 20 Like this type please help me
Big Big Thankyouuuuuuuuuu its Working
2

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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.