0

I'm saving an array of type ClassA to NSUserDefaults. ClassA look like this:

class ClassA :NSObject, NSCoding{

    init (descriptionParam: String) {
        self.description = descriptionParam
    }

    var description: String?

    required init(coder aDecoder: NSCoder) {
        if let description = aDecoder.decodeObjectForKey("description") as? String {
            self.description = description
        }
    }

    func encodeWithCoder(aCoder: NSCoder) {
        if let description = self.description {
            aCoder.encodeObject(description, forKey: "description")
        }
    }

}

This is how I'm saving the array to NSUserDefaults:

let myData = NSKeyedArchiver.archivedDataWithRootObject(ClassAManager.classa_array)
userDefaults.setObject(myData, forKey: "classAarray");

I'm doing the following in my initial viewDidLoad():

var classA: AnyObject? = NSUserDefaultsManager.userDefaults.objectForKey("classAarray") as AnyObject?
let classAunpacked = NSKeyedUnarchiver.unarchiveObjectWithData(classA) as [ClassA]

I get the following compile-time error on the second line above (the one with let):

Cannot invoke 'unarchiveObjectWithData' with an argument list of type '(AnyObject?)'

However, if I try to retrieve the array with anything other than AnyObject?, I get other compile time errors. It also seems I can't cast from AnyObject? to [ClassA]. Any ideas how this should be done?

David Berry
  • 40,941
  • 12
  • 84
  • 95
4thSpace
  • 43,672
  • 97
  • 296
  • 475

1 Answers1

1

unarchiveObjectWithData takes an NSData as it's sole argument, not an optional AnyObject. Since the result of unarchive... is also an optional, I'd suggest using:

if let classA = NSUserDefaultsManager.userDefaults.dataForKey("classAarray") {
    if let classAunpacked = NSKeyedUnarchiver.unarchiveObjectWithData(classA) as? [ClassA] {
        // Use classAunpacked here
    }
}
David Berry
  • 40,941
  • 12
  • 84
  • 95
  • No more errors. When I try to view classAunpacked via the debugger, it is just a memory address. I can't see any array items. Is there some way to view it? Also, I have checked /CoreSimulator/Devices/{someid}/data/library/preferences but all I see is com.apple.UIAutomation.plist. Is the plist I'm modifying somewhere else? – 4thSpace Mar 15 '15 at 05:01
  • You should use the nsuserdefaults method dataForKey https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/#//apple_ref/occ/instm/NSUserDefaults/dataForKey: – Leo Dabus Mar 15 '15 at 05:03
  • @LeonardoSavioDabus: Excellent! That works fine. Also, while I can't see the entire array in the debugger, I can view each element by using "po classAunpacked[0].description" for example. Thanks David for a great answer. – 4thSpace Mar 15 '15 at 05:08
  • Any ideas where this plist file that is holding the data is located and what it is named? – 4thSpace Mar 15 '15 at 05:10
  • You shouldn't try to manually edit those plists. You can use removeObjectForKey and reset/delete any value – Leo Dabus Mar 15 '15 at 05:12
  • you can use NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.array to check your keys – Leo Dabus Mar 15 '15 at 05:28
  • I'm not wanting to edit the file. I want to view its contents. – 4thSpace Mar 15 '15 at 06:05