Gorib Developer
Gorib Developer

Reputation: 597

How to save json data on userdefaults in swift 4

enter image description here

This is my model class

class UserRoot : Mappable {
    var success : Bool!
    var user : UserDetails!
    var error = ""

    required init?(map: Map) {

    }
    func mapping(map: Map) {
        success <- map["success"]
        user <- map["user"]
        error <- map["error"]

    }
}

after successfully login i want to save this data on user defaults so that when a user have to not give login credential again. Here is my code

class Default : NSObject{
    static func saveToSharedPrefs(user: UserDetails!) {
        let d = UserDefaults.standard
        if user != nil {
            d.set(Mapper().toJSONString(user, prettyPrint: false) , forKey: "USERDETAILS")
        } else {
            d.set(nil, forKey: "USERDETAILS")
        }
        d.synchronize()
    }
}

Upvotes: 4

Views: 8325

Answers (4)

Pramod More
Pramod More

Reputation: 1250

Using ObjectMapper pod.

class GooglePlace : Mappable {

    var id = ""
    var addressTitle = ""
    var formattedAddress = ""
    var latitude : Double?
    var longitude : Double?

 required init?(map: Map) {

    }

    func mapping(map: Map) {
        addressTitle <- map["name"]
        formattedAddress <- map["formatted_address"]
        latitude <- map["geometry.location.lat"]
        longitude <- map["geometry.location.lng"]
        id <- map["id"]
    }

}

var placesesSearchHistory : [GooglePlace]? = []

if let jsonString = placesesSearchHistory?.toJSONString(prettyPrint: true) {
//Store in UserDefaults
      UserDefaults.standard.set(jsonString, forKey: "addressSearchHistory")

} 


//retrieve from UserDefaults
 if let dataArrayString = (UserDefaults.standard.string(forKey: "addressSearchHistory")) as? String {

 if let dataObject = Mapper<GooglePlace>().mapArray(JSONString: dataArrayString)  {
                placesesSearchHistory = dataObject
            }
        }

Upvotes: 0

Mahendra
Mahendra

Reputation: 8904

Make sure your model class is inherited from NSObject class otherwise it will crash at run time.

To store data:

let data = NSKeyedArchiver.archivedData(withRootObject: <Your model class>)
UserDefaults.standard.set(data, forKey: "userDetails")

To retrive and convert data back

if let data = UserDefaults.standard.value(forKey: "userDetails") as? Data {
    if let dict = NSKeyedUnarchiver.unarchiveObject(with: data) as? <Your model class> {
           print(dict)
     }
}

Upvotes: 3

Kazi Abdullah Al Mamun
Kazi Abdullah Al Mamun

Reputation: 1481

In swift 4 Better use JSONEncoder to encode your Swift object to JSON and JSONDecoder to decode your JSON to Swift object, Confirm Codable protocol to your Model class before encode and decode. You can follow this answer from stack overflow

Upvotes: 3

Ramon Vasconcelos
Ramon Vasconcelos

Reputation: 1466

You can use Data to store this json in the user defaults like this:

let myData = NSKeyedArchiver.archivedData(withRootObject: myJson)
UserDefaults.standard.set(myData, forKey: "userJson")

let recovedUserJsonData = UserDefaults.standard.object(forKey: "userJson")
let recovedUserJson = NSKeyedUnarchiver.unarchiveObject(with: recovedUserJsonData)

Edit

you can not store mapper in NSUserDefault, you can only store NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary.

NSKeyedArchiver converts your mapper into NSData which you can store into userdefault.

Upvotes: 1

Related Questions