我有一个模型对象User,另一个模型对象是Picture。
"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")
当我尝试从userdefaults获取数据时,我正在获取用户模型,但内部细节为零。
从userdefault获取Userdetails代码位于
之下guard let data = UserDefaults.standard.object(forKey: "user") as? Data
else
{
return UserModel()
}
return (NSKeyedUnarchiver.unarchiveObject(with: data) as? UserModel)!
此返回**<ABC.User: 0x7f84f740c440>**
但是当我尝试从用户获取图片时,它返回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")
}
} 注意:我使用的是MVVM模式和对象映射器
那么,我怎样才能获得用户的全部细节,包括photo_url(User.Picture.photo_url)?
答案 0 :(得分:0)
在用户模型
中[TestClass]
public class ExampleTest {
public interface IInterface {
void Method(string p, out StringBuilder builder);
}
public class MyClass {
private IInterface p;
public MyClass(IInterface p) {
this.p = p;
}
public void Act() {
var builder = new StringBuilder();
p.Method("String", out builder);
}
}
[TestMethod]
public void Should_Ignore_Out() {
//Arrange
var mock = new Mock<IInterface>();
var sut = new MyClass(mock.Object);
//Act
sut.Act();
//Assert
var builder = new StringBuilder();
mock.Verify(x => x.Method("String", out builder), Times.Once);
}
}
在图片模型
中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)!