1
  1. I have a model
  2. I want to pass value of my model to other class
  3. I set in userDefault but get error "thread 1 signal sigabrt"

    Alamofire.request("http://mylink.com/login", method: .post, parameters: parameters, encoding: JSONEncoding.default).responseObject { (response: DataResponse<LoginResponse>)
        in
        let loginResponse: AnyObject = response.result.value!
    
    
        UserDefaults.standard.setValue(loginResponse, forKey: "resultLogin")
    
    
    
    }
    

So i want to ask how to set my model value to user default

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Evan Laksana
  • 410
  • 3
  • 7
  • 17
  • 1
    Check your loginResponse. May be your model contain nil value for any key. I have same issue and found that value of one of my key is nil. – Nishant Bhindi Apr 17 '17 at 08:55
  • Which exact line of code causes the error? – rmaddy Apr 17 '17 at 18:11
  • @rmaddy UserDefaults.standard.setValue(loginResponse, forKey: "resultLogin") – Evan Laksana Apr 17 '17 at 23:34
  • 1
    Though not related to your crash, do not use `setValue(forKey:)`. Use `set(forKey:)`. And you can only store specific data types in `UserDefaults`. Please read the documentation for `UserDefaults` to see the value types. Hint: `AnyObject` is not one of the valid types. – rmaddy Apr 17 '17 at 23:36
  • 1
    http://stackoverflow.com/questions/29986957/save-custom-objects-into-nsuserdefaults – Evan Laksana Apr 18 '17 at 07:45

2 Answers2

2

Convert your login response to data and then save to UserDefaults. Otherwise you can also use NSKeyedArchiever after converting the response into Data. Refer to this SO Post

Community
  • 1
  • 1
Md. Ibrahim Hassan
  • 5,359
  • 1
  • 25
  • 45
0

To store a value or object in User default, that value or object must be NSCoding complaint. Here you are setting value of type AnyObject in user default. To make this work,create a model and implement the NSCoding.