【问题标题】:How to save mappable object to nsuserdefault?如何将可映射对象保存到 nsuserdefault?
【发布时间】:2019-01-22 13:37:38
【问题描述】:

我想将自定义的整个对象保存到 NSUserDefault ?

这是我的自定义对象..

import UIKit
import ObjectMapper

class SAuthModel: Mappable {

    var status: Int?
    var message: String?
    var result: SResultModel?
    var authToken: String?

    required init?(map: Map) {

    }

    func mapping(map: Map) {
        status       <- map["code"]
        message      <- map["message"]
        authToken    <- map["authToken"]
        result       <- map["data"]
    }
}

class SResultModel: Mappable {

    var name, email, countryCode, mobile, isNotification, promoCode, imageURL, _id, isMobileVerify,otp,faceBookId,googleId,stripeCustId,isProfileSet,created:String?

    required init?(map: Map) {

    }

    func mapping(map: Map) {
        name             <- map["name"]
        email            <- map["email"]
        countryCode      <- map["countryCode"]
        mobile           <- map["mobile"]
        isNotification   <- map["isNotification"]
        promoCode        <- map["promoCode"]
        imageURL         <- map["imageURL"]
        _id              <- map["_id"]
        isMobileVerify   <- map["isMobileVerify"]
        otp              <- map["otp"]
        faceBookId       <- map["faceBookId"]
        googleId         <- map["googleId"]
        stripeCustId     <- map["stripeCustId"]
        isProfileSet     <- map["isProfileSet"]


    }
}

 SAuthService().logInService(parameters:userModel.toJSON()).done{(response) -> Void  in
            SVProgressHUD.dismiss()

            if response.status == 210 {
                guard let responseMessage = response.message else {
                    return
                }
                HHelper.showAlert(responseMessage)
            } else if response.status == 200 {
                USER_DEFAULTS.set(response.result, forKey:ConstantTexts.resultData.localizedString)

                kAppDelegate.pushToHomeViewContoller()
            }
}

我试图在 USER_DEFAULTS 中保存自定义模型对象,但它一直在崩溃。

【问题讨论】:

    标签: ios json swift objectmapper user-defined


    【解决方案1】:

    描述了存储到 UserDefaults 的正确方法in this article

    var userDefaults = UserDefaults.standard
    let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: teams)
    userDefaults.set(encodedData, forKey: "teams")
    

    基本上你必须为它提供一个数据对象...

    【讨论】:

    • 它是“UserDefaults.standard”。
    猜你喜欢
    • 2013-08-31
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 2022-10-22
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 2010-09-10
    相关资源
    最近更新 更多