-1

I want to save my data into NSUSerDefaults in Swift3 in:

func a() {
    let userDefaults = UserDefaults.standard
    let sessionData = NSKeyedArchiver.archivedData(withRootObject: session)
    userDefaults.set(session, forKey: "SpotifySession")
    userDefaults.synchronize()
}

but it crashes in

NSKeyedArchiver.archivedData(withRootObject: session)

How can I fix that?

Doe
  • 431
  • 1
  • 8
  • 16
  • 2
    What is `session`? – Aaron Sep 17 '16 at 23:49
  • NSUserDefaults supports the following data types: NSString, NSNumber, NSDate, NSArray, NSDictionary and NSData http://stackoverflow.com/questions/37980432/swift-3-saving-and-retrieving-custom-object-from-userdefaults – MAhipal Singh Feb 14 '17 at 14:00

2 Answers2

1

I think you have not encode and decode method in your session class. If your session is custom class then you should write the following two methods, without that archive won't work.

Code :

Objective C Version:

- (void)encodeWithCoder:(NSCoder *)encoder {
 [encoder encodeObject:self.session_property1 forKey:@"session_property1"];
    [encoder encodeObject:self.session_property2 forKey:@"session_property2"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    if((self = [super init])) {
        //decode properties, other class vars
        self.ession_property1 = [decoder decodeObjectForKey:@"ession_property1"];
        self.ession_property2 = [decoder decodeObjectForKey:@"ession_property2"];
}
    return self;
}

Here session_property1 and session_property2 are considered as string, if your variable is other than string you need to format it accordingly.

After these in your cut on class, if you will use archiveData method as you did, then it will archive the data and then you can set those data to NSUserDefault

Swift Version :

required init(coder decoder: NSCoder) {
    self.state = decoder.decodeObject(forKey: "state") as? String
    self.country = decoder.decodeObject(forKey: "country") as? String
    self.zip = decoder.decodeObject(forKey: "zip") as? String
    self.locality = decoder.decodeObject(forKey: "locality") as? String
    self.city = decoder.decodeObject(forKey: "city") as? String
    self.latitude = decoder.decodeObject(forKey: "latitude") as? Double
    self.longitude = decoder.decodeObject(forKey: "longitude") as? Double
}

func encode(with coder: NSCoder) {
    coder.encode(state, forKey: "state")
    coder.encode(country, forKey: "country")
    coder.encode(zip, forKey: "zip")
    coder.encode(locality, forKey: "locality")
    coder.encode(city, forKey: "city")
    coder.encode(latitude, forKey: "latitude")
    coder.encode(longitude, forKey: "longitude")
} 

I hope it will help you. Happy coding ...

Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
  • you write solution in objective-c and the question is asking for swift coding version – Amr Angry Feb 14 '17 at 13:17
  • 1
    @AmrAngry, Thanks for reminding me. I was just giving the hint with objective C by that time. But no worries now, I have updated my answer now with Swift. – Janmenjaya Feb 14 '17 at 13:26
-1

I think, this is the right way to save data in NSUserDefaults:

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject("Coding Explorer", forKey: "userNameKey")
Jeff B
  • 8,572
  • 17
  • 61
  • 140
Dmitriy Greh
  • 684
  • 8
  • 17
  • 1
    Saving data in NSUserDefaults has already been explained hundreds of times, your answer adds nothing. Moreover, OP is specifically asking for a Swift 3 version, which you don't give... – Eric Aya Feb 14 '17 at 13:33