Swift 使用userdefault保存并获取模型类的详细信息

Swift 使用userdefault保存并获取模型类的详细信息,swift,mvvm,alamofire,objectmapper,Swift,Mvvm,Alamofire,Objectmapper,我有一个模型对象用户,其中另一个模型对象是图片 "user": { "id": 1, "email": "abc.k@gmail.com", "user_profile_photo": { "id": 997, "user_id": 1, "photo_url": "https://newproduction.s3.amazonaws.com

我有一个模型对象用户,其中另一个模型对象是图片

 "user": {
            "id": 1,
            "email": "abc.k@gmail.com",
     "user_profile_photo": {
                "id": 997,
                "user_id": 1,
                "photo_url": "https://newproduction.s3.amazonaws.com/profile_image/RkUJAczv5nWpUyFTgyTgMLChR.jpeg",
                           }   
         }
我有两个模型类,一个是用户,另一个是图片内部用户。 我将模型用户保存在userdefault中,如下所示

    //Get Response
    loginResponseObj = Mapper<LoginResponse>().map(JSONObject:(response.result.value))

   //Save user Details
   let userData = loginResponseObj.user!
        let data  = NSKeyedArchiver.archivedData(withRootObject: userData)
        UserDefaults.standard.set(data, forKey:"user")
此返回
***

但是当我试图从用户获取图片时,它返回nil


用户模型中

 class User:NSObject,Mappable,NSCoding{

 var email: String?
 var picture: Picture?

required init?(coder aDecoder: NSCoder) {
        self.email = aDecoder.decodeObject(forKey: "email") as? String
    }
    func initWithCoder(aDecoder:NSCoder) -> UserModel
    {
        self.email = aDecoder.decodeObject(forKey: "email") as? String

        return self
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(email, forKey: "email")
    }
class Picture:Mappable,NSCoding{

    var id: String?
    var photoURL: String?

    required init?(coder aDecoder: NSCoder) {
            self.id = aDecoder.decodeObject(forKey: "id") as? String
            self.photoURL = aDecoder.decodeObject(forKey: "photoURL") as? String
        }
        func initWithCoder(aDecoder:NSCoder) -> Picture
        {
            self.id = aDecoder.decodeObject(forKey: "id") as? String
            self.photoURL = aDecoder.decodeObject(forKey: "photoURL") as? String

            return self
        }

        func encode(with aCoder: NSCoder) {
            aCoder.encode(id, forKey: "id")


 }
class User:NSObject,Mappable,NSCoding{

 var email: String?
 var picture: Picture?

required init?(coder aDecoder: NSCoder) {
        self.email = aDecoder.decodeObject(forKey: "email") as? String
        self.picture = aDecoder.decodeObject(forKey: "picture") as? Picture
    }
    func initWithCoder(aDecoder:NSCoder) -> UserModel
    {
        self.email = aDecoder.decodeObject(forKey: "email") as? String
        self.picture = aDecoder.decodeObject(forKey: "picture") as? Picture
        return self
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(email, forKey: "email")
        aCoder.encode(picture, forKey: "picture")
    }
}
class Picture: NSObject,Mappable,NSCoding {

var id: String?
var photoURL: String?

required init?(coder aDecoder: NSCoder) {
    self.id = aDecoder.decodeObject(forKey: "id") as? String
    self.photoURL = aDecoder.decodeObject(forKey: "photoURL") as? String
}
func initWithCoder(aDecoder:NSCoder) -> Picture
{
    self.id = aDecoder.decodeObject(forKey: "id") as? String
    self.photoURL = aDecoder.decodeObject(forKey: "photoURL") as? String

    return self
}

func encode(with aCoder: NSCoder) {
    aCoder.encode(id, forKey: "id")
    aCoder.encode(photoURL, forKey: "photoURL")
}
} 在图片中模型

 class User:NSObject,Mappable,NSCoding{

 var email: String?
 var picture: Picture?

required init?(coder aDecoder: NSCoder) {
        self.email = aDecoder.decodeObject(forKey: "email") as? String
    }
    func initWithCoder(aDecoder:NSCoder) -> UserModel
    {
        self.email = aDecoder.decodeObject(forKey: "email") as? String

        return self
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(email, forKey: "email")
    }
class Picture:Mappable,NSCoding{

    var id: String?
    var photoURL: String?

    required init?(coder aDecoder: NSCoder) {
            self.id = aDecoder.decodeObject(forKey: "id") as? String
            self.photoURL = aDecoder.decodeObject(forKey: "photoURL") as? String
        }
        func initWithCoder(aDecoder:NSCoder) -> Picture
        {
            self.id = aDecoder.decodeObject(forKey: "id") as? String
            self.photoURL = aDecoder.decodeObject(forKey: "photoURL") as? String

            return self
        }

        func encode(with aCoder: NSCoder) {
            aCoder.encode(id, forKey: "id")


 }
class User:NSObject,Mappable,NSCoding{

 var email: String?
 var picture: Picture?

required init?(coder aDecoder: NSCoder) {
        self.email = aDecoder.decodeObject(forKey: "email") as? String
        self.picture = aDecoder.decodeObject(forKey: "picture") as? Picture
    }
    func initWithCoder(aDecoder:NSCoder) -> UserModel
    {
        self.email = aDecoder.decodeObject(forKey: "email") as? String
        self.picture = aDecoder.decodeObject(forKey: "picture") as? Picture
        return self
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(email, forKey: "email")
        aCoder.encode(picture, forKey: "picture")
    }
}
class Picture: NSObject,Mappable,NSCoding {

var id: String?
var photoURL: String?

required init?(coder aDecoder: NSCoder) {
    self.id = aDecoder.decodeObject(forKey: "id") as? String
    self.photoURL = aDecoder.decodeObject(forKey: "photoURL") as? String
}
func initWithCoder(aDecoder:NSCoder) -> Picture
{
    self.id = aDecoder.decodeObject(forKey: "id") as? String
    self.photoURL = aDecoder.decodeObject(forKey: "photoURL") as? String

    return self
}

func encode(with aCoder: NSCoder) {
    aCoder.encode(id, forKey: "id")
    aCoder.encode(photoURL, forKey: "photoURL")
}
} 注意:我正在使用MVVM模式和对象映射器


那么,我如何在用户模型中获得用户的全部详细信息,包括photo\u url(user.Picture.photo\u url)

 class User:NSObject,Mappable,NSCoding{

 var email: String?
 var picture: Picture?

required init?(coder aDecoder: NSCoder) {
        self.email = aDecoder.decodeObject(forKey: "email") as? String
    }
    func initWithCoder(aDecoder:NSCoder) -> UserModel
    {
        self.email = aDecoder.decodeObject(forKey: "email") as? String

        return self
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(email, forKey: "email")
    }
class Picture:Mappable,NSCoding{

    var id: String?
    var photoURL: String?

    required init?(coder aDecoder: NSCoder) {
            self.id = aDecoder.decodeObject(forKey: "id") as? String
            self.photoURL = aDecoder.decodeObject(forKey: "photoURL") as? String
        }
        func initWithCoder(aDecoder:NSCoder) -> Picture
        {
            self.id = aDecoder.decodeObject(forKey: "id") as? String
            self.photoURL = aDecoder.decodeObject(forKey: "photoURL") as? String

            return self
        }

        func encode(with aCoder: NSCoder) {
            aCoder.encode(id, forKey: "id")


 }
class User:NSObject,Mappable,NSCoding{

 var email: String?
 var picture: Picture?

required init?(coder aDecoder: NSCoder) {
        self.email = aDecoder.decodeObject(forKey: "email") as? String
        self.picture = aDecoder.decodeObject(forKey: "picture") as? Picture
    }
    func initWithCoder(aDecoder:NSCoder) -> UserModel
    {
        self.email = aDecoder.decodeObject(forKey: "email") as? String
        self.picture = aDecoder.decodeObject(forKey: "picture") as? Picture
        return self
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(email, forKey: "email")
        aCoder.encode(picture, forKey: "picture")
    }
}
class Picture: NSObject,Mappable,NSCoding {

var id: String?
var photoURL: String?

required init?(coder aDecoder: NSCoder) {
    self.id = aDecoder.decodeObject(forKey: "id") as? String
    self.photoURL = aDecoder.decodeObject(forKey: "photoURL") as? String
}
func initWithCoder(aDecoder:NSCoder) -> Picture
{
    self.id = aDecoder.decodeObject(forKey: "id") as? String
    self.photoURL = aDecoder.decodeObject(forKey: "photoURL") as? String

    return self
}

func encode(with aCoder: NSCoder) {
    aCoder.encode(id, forKey: "id")
    aCoder.encode(photoURL, forKey: "photoURL")
}
图片中模型

 class User:NSObject,Mappable,NSCoding{

 var email: String?
 var picture: Picture?

required init?(coder aDecoder: NSCoder) {
        self.email = aDecoder.decodeObject(forKey: "email") as? String
    }
    func initWithCoder(aDecoder:NSCoder) -> UserModel
    {
        self.email = aDecoder.decodeObject(forKey: "email") as? String

        return self
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(email, forKey: "email")
    }
class Picture:Mappable,NSCoding{

    var id: String?
    var photoURL: String?

    required init?(coder aDecoder: NSCoder) {
            self.id = aDecoder.decodeObject(forKey: "id") as? String
            self.photoURL = aDecoder.decodeObject(forKey: "photoURL") as? String
        }
        func initWithCoder(aDecoder:NSCoder) -> Picture
        {
            self.id = aDecoder.decodeObject(forKey: "id") as? String
            self.photoURL = aDecoder.decodeObject(forKey: "photoURL") as? String

            return self
        }

        func encode(with aCoder: NSCoder) {
            aCoder.encode(id, forKey: "id")


 }
class User:NSObject,Mappable,NSCoding{

 var email: String?
 var picture: Picture?

required init?(coder aDecoder: NSCoder) {
        self.email = aDecoder.decodeObject(forKey: "email") as? String
        self.picture = aDecoder.decodeObject(forKey: "picture") as? Picture
    }
    func initWithCoder(aDecoder:NSCoder) -> UserModel
    {
        self.email = aDecoder.decodeObject(forKey: "email") as? String
        self.picture = aDecoder.decodeObject(forKey: "picture") as? Picture
        return self
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(email, forKey: "email")
        aCoder.encode(picture, forKey: "picture")
    }
}
class Picture: NSObject,Mappable,NSCoding {

var id: String?
var photoURL: String?

required init?(coder aDecoder: NSCoder) {
    self.id = aDecoder.decodeObject(forKey: "id") as? String
    self.photoURL = aDecoder.decodeObject(forKey: "photoURL") as? String
}
func initWithCoder(aDecoder:NSCoder) -> Picture
{
    self.id = aDecoder.decodeObject(forKey: "id") as? String
    self.photoURL = aDecoder.decodeObject(forKey: "photoURL") as? String

    return self
}

func encode(with aCoder: NSCoder) {
    aCoder.encode(id, forKey: "id")
    aCoder.encode(photoURL, forKey: "photoURL")
}
从用户默认设置获取详细信息

guard let data = UserDefaults.standard.object(forKey: "user") as? Data
            else
        {
            return User()

        }
        return (NSKeyedUnarchiver.unarchiveObject(with: data) as? User)!
上述代码将返回用户型号 使用此模型访问其变量

print(user.email)
print(user.picture.photoURL)

如何将用户转换为数据,使用NSCoding?您的图片模型是否符合NSCoding?用户模型符合,但图片模型与用户模型不符合?请确保您的图片模型也符合NSCoding,并且应该没有问题。请参阅我的更新问题现在我可以使用UserDefaults获取User.email,但当我尝试获取图片时,它会返回NIL请参阅下面的答案