Swift - NSKeyedUnarchiver unarchiveObjectWithData in NSMutableArray

7.8k views Asked by At
var objects: AnyObject? = NSKeyedUnarchiver.unarchiveObjectWithData(data)

How to turn objects into NSMutableArray ? I archived NSMutableArray.

1

There are 1 answers

5
Martin R On BEST ANSWER

Just downcast the result to NSMutableArray:

if let objects = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? NSMutableArray {
    // ...
} else {
    // failed
}

If the archived object is an (immutable) NSArray then you have to create a mutable copy:

if let array = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? NSArray {
    let objects = NSMutableArray(array: array)
    // ...
}