Swift - NSKeyedUnarchiver unarchiveObjectWithData in NSMutableArray

Bogdan Bogdanov picture Bogdan Bogdanov · Nov 22, 2014 · Viewed 7.5k times · Source
var objects: AnyObject? = NSKeyedUnarchiver.unarchiveObjectWithData(data)

How to turn objects into NSMutableArray ? I archived NSMutableArray.

Answer

Martin R picture Martin R · Nov 22, 2014

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)
    // ...
}