├── TestModel.swift ├── ViewController.swift ├── README.md ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── AppDelegate.swift ├── Info.plist ├── Base.lproj ├── Main.storyboard └── LaunchScreen.storyboard └── QdaiBaseModel.swift /TestModel.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TestModel.swift 3 | // Qdai2 4 | // 5 | // Created by 口贷网 on 16/1/25. 6 | // Copyright © 2016年 1211. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class TestModel: QdaiBaseModel { 12 | 13 | var a: String? 14 | var b: NSArray? 15 | var c: NSDictionary? 16 | 17 | } 18 | -------------------------------------------------------------------------------- /ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // AAJsonToModel 4 | // 5 | // Created by 口贷网 on 16/1/25. 6 | // Copyright © 2016年 Afer. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | override func viewDidLoad() { 14 | super.viewDidLoad() 15 | // Do any additional setup after loading the view, typically from a nib. 16 | } 17 | 18 | override func didReceiveMemoryWarning() { 19 | super.didReceiveMemoryWarning() 20 | // Dispose of any resources that can be recreated. 21 | } 22 | 23 | 24 | } 25 | 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AAJsonToModel 2 | ### Json 转模型 模型转字典 支持直接显示json里面的中文更加清晰 3 | 4 | * 使用方法 继承QdaiBaseModel 5 | 6 | ```objective-c 7 | class TestModel: QdaiBaseModel { 8 | var a: String? 9 | var arr: NSArray? 10 | var dic: Bmodel? 11 | override init(info: NSDictionary?) { 12 | super.init(info: info) 13 | self.setValue(Bmodel.init(info: info!["dic"] as? NSDictionary), forKeyPath: "dic") 14 | } 15 | } 16 | 17 | class Bmodel: QdaiBaseModel { 18 | var b: String? 19 | var c: NSArray? 20 | } 21 | 22 | let aaa: NSDictionary = ["a": "123", "arr":["1", "2"], "dic":["b":"123", "c":["c", "cc"]]] 23 | let tModel = TestModel.init(info: aaa) 24 | print(tModel) 25 | 26 | print(tModel.toDictionary()) // Model 转字典 此版本暂时不支持 ` 27 | ``` 28 | -------------------------------------------------------------------------------- /Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // AAJsonToModel 4 | // 5 | // Created by 口贷网 on 16/1/25. 6 | // Copyright © 2016年 Afer. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 18 | 19 | 20 | let diccc = ["a":"12312", "b":["12","2","3"], "c":["1":"2"]] 21 | let model = TestModel(info: diccc) 22 | print(model) 23 | print(model.b![0]) 24 | 25 | print(model.toDictionary()) 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | return true 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /QdaiBaseModel.swift: -------------------------------------------------------------------------------- 1 | // 2 | // QdaiBaseModel.swift 3 | // Qdai2 4 | // 5 | // Created by 口贷网 on 16/1/14. 6 | // Copyright © 2016年 1211. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class QdaiBaseModel: NSObject { 12 | 13 | 14 | lazy var mirror: Mirror = {Mirror(reflecting: self)}() 15 | 16 | init(info: NSDictionary?) { 17 | super.init() 18 | for p in mirror.children { 19 | 20 | let va = info![p.label!] 21 | let ke = p.label! 22 | 23 | if (va is NSArray || va is NSMutableArray) { 24 | self.setValue(va, forKeyPath: ke) 25 | } else { 26 | self.setValue(QdaiBaseModel.anyObjectToString((va as AnyObject?)!), forKeyPath: ke) 27 | } 28 | 29 | } 30 | } 31 | 32 | func toDictionary() -> NSMutableDictionary { 33 | let mdic: NSMutableDictionary = NSMutableDictionary() 34 | for p in mirror.children { 35 | mdic.setValue( self.valueForKeyPath(p.label!) , forKeyPath: p.label!) 36 | } 37 | 38 | return mdic 39 | } 40 | 41 | override var description: String { 42 | var restr = "############## \(self.dynamicType) description ###############\n" 43 | for p in mirror.children { 44 | restr = restr + "\(p.label!) = \(p.value)\n" + "----------------------\n" 45 | } 46 | restr = restr + "######################## description end #######################\n" 47 | return restr 48 | } 49 | 50 | class func anyObjectToString(any: AnyObject?) -> String { 51 | if any is NSNumber { 52 | return NSString(format: "%@", any as! NSNumber) as String 53 | } else if any is String { 54 | return any as! String 55 | } else if any is NSString { 56 | return any as! String 57 | } else { 58 | print("ERROR: Type is error") 59 | return "" 60 | } 61 | } 62 | } 63 | --------------------------------------------------------------------------------