Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON转模型 提供模型更新接口 #814

Closed
songhongkang opened this issue Jun 8, 2021 · 4 comments
Closed

JSON转模型 提供模型更新接口 #814

songhongkang opened this issue Jun 8, 2021 · 4 comments

Comments

@songhongkang
Copy link

能否提供一个更新模型的update的接口?

@wolfcon
Copy link
Collaborator

wolfcon commented Jun 9, 2021

Update? 这个你得手动处理吧.

  1. 不知道你说的是否涉及本地存储, 如果本地存储的话, 那么你应该写一个转换类, 这时候涉及到更新模型版本号的问题(参照数据库升级), 这个就相对复杂了, 根据具体业务不同, 逻辑也不同. 这里还是建议自己在存储模块增加一个升级检测模块
  2. 如果不涉及到本地存储, 那么你直接更新模型即可, 为什么需要 Update API?

@songhongkang
Copy link
Author

// 1.Define a JSONString
NSString *jsonString = @"{"name":"Jack", "icon":"lufy.png", "age":20}";

// 2.JSONString -> User
User *user = [User mj_objectWithKeyValues:jsonString];

// 3.jsonString中的age变成21
NSString *jsonString = @"{"age":21}";

// 4.更新user对象
[user updateFromJson:jsonString]

能否提供一个updateFromJson的接口

我们项目中,有许多需要模型更新,目前做法都是手动更新,希望MJ大神能帮小老弟一下!

@songhongkang
Copy link
Author

如果不涉及到本地存储, 那么你直接更新模型即可, 为什么需要 Update API?

不涉及本地存储,自己手动更新,要写很多代码,要把字典所有的key对应的value全部更新到模型上去呀?如果这些代码能MJ写,项目中代码就会简洁不少,如果自己手写那么重复的代码,就不好嘛

目前俺更新就是手动更新,有点代码冗余

handyjson也提供了update接口,希望mj也能提供一个

@songhongkang
Copy link
Author

阅读源码发现MJ做了更新功能
// 1.Define a JSONString
NSString *jsonString = @"{"name":"Jack", "icon":"lufy.png", "age":20}";
// 2.JSONString -> User
User *user = [User mj_objectWithKeyValues:jsonString];
// 3.jsonString中的age变成21
NSString *jsonString1 = @"{"age":21}";
// 4.更新模型
[user mj_setKeyValues:jsonString1];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants