简体   繁体   中英

How to save json data on userdefaults in swift 4

在此输入图像描述

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()
    }
}

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

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 .

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

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
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM