XYFModel Swift-Objective-C字典模型转换与归档框架

Swift 和 Objective-C 混合开发的时候,数据模型的转换还挺常见的,尤其是做接口求那块。XYFModel-master这个项目就帮了我不少忙,它把字典转模型、模型转字典还有归档这些事儿,全都打包搞定了,写起来清爽,出错率也低。

字典转模型的时候用KVC确实省事,但直接写setValue:forKey:太麻烦?嗯,它支持自定义方法,比如+modelWithDictionary:,一行行赋值看着舒服多了。还可以写个dictionaryRepresentation方法,把模型反序列化回字典,用来转成 JSON 发接口,正好。

归档和解档呢,它也没落下,直接实现NSCoding那套:encodeWithCoder:initWithCoder:,再用NSKeyedArchiver,模型持久化的需求也能搞定。常见场景比如缓存用户数据、保存状态页模型,这些都挺实用。

不过要注意:类型不匹配、键值缺失这些坑别掉进去,最好加点类型判断或者容错逻辑。另外,有些对象像NSManagedObject是不能直接归档的,别硬上。

如果你项目里数据模型转换频繁,又想代码写得安全点、省事点,可以试试这个项目。不管你用 OC 还是 Swift 混着来,思路是互通的。

zip
swift-iOSObjective-C字典转模型模型转字典模型归档接档使用安全并且简单.zip 预估大小:42个文件
folder
XYFModel-master 文件夹
file
XYFModel技术交流群群二维码.png 24KB
folder
XYFModelAndRuntimeDemo 文件夹
folder
Assets.xcassets 文件夹
folder
AppIcon.appiconset 文件夹
file
Contents.json 2KB
file
Contents.json 62B
folder
binding_qq_icon.imageset 文件夹
file
Contents.json 360B
file
binding_qq_icon@2x.png 7KB
file
binding_qq_icon@3x.png 12KB
file
main.m 338B
folder
XYFModel Use Model 文件夹
file
Phone.h 279B
file
Phone.m 171B
file
Person.h 864B
file
Person.m 934B
file
AppDelegate.h 281B
file
AppDelegate.m 2KB
folder
Base.lproj 文件夹
file
LaunchScreen.storyboard 2KB
file
Main.storyboard 2KB
file
ViewController.h 219B
file
Info.plist 1KB
file
ViewController.m 4KB
folder
Runtime Practice 文件夹
file
UIControl+ClickBlock.m 969B
file
Monkey.m 1KB
file
MyClass.h 197B
file
Monkey.h 241B
file
NSObject+Property.h 253B
file
Bird.m 217B
file
MyClass.m 347B
file
UIControl+ClickBlock.h 295B
file
UIImage+image.m 2KB
file
UIImage+image.h 190B
file
Bird.h 191B
file
NSObject+Property.m 458B
file
LICENSE 1KB
file
.DS_Store 6KB
file
XYFModel.podspec 6KB
folder
XYFModel 文件夹
file
NSObject+XYFModel.m 22KB
file
NSObject+XYFModel.h 2KB
folder
XYFModelAndRuntimeDemo.xcodeproj 文件夹
folder
project.xcworkspace 文件夹
file
contents.xcworkspacedata 167B
folder
xcuserdata 文件夹
folder
mac.xcuserdatad 文件夹
file
UserInterfaceState.xcuserstate 70KB
folder
xcshareddata 文件夹
file
IDEWorkspaceChecks.plist 238B
file
project.pbxproj 19KB
folder
xcuserdata 文件夹
folder
mac.xcuserdatad 文件夹
folder
xcschemes 文件夹
file
xcschememanagement.plist 469B
file
.gitignore 1KB
file
README.md 8KB
zip 文件大小:112.91KB