iOS安全之【拦截请求,实现特定目的 】(修改HTTPHeaderFieldMJExtension === ![Carthage compatible](https://github.com/Carthage/Carthage) ![podversion](https://img.shields.io/cocoapods/v/MJExtension.svg) - A fast, convenient and nonintrusive conversion framework between JSON and model. - 转换速度快、使用简单方便的字典转模型框架 📜✍🏻**Release Notes**: more details ### ‼️ 纯Swift版的JSON与Model转换框架已经开源上架 ‼️ - KakaJSON - 中文教程 - 如果你的项目是用Swift写的Model,墙裂推荐使用KakaJSON - 已经对各种常用的数据场景进行了大量的单元测试 - 简单易用、功能丰富、转换快速 ### 关于在Swift中使用MJExtension ‼️ #### ‼️ `@objc` attributes should be added to class and property for declaration of Objc accessibility [在 Swift4 之后, 请在属性前加 `@objc` 修饰. 以保证 Swift 的属性能够暴露给 Objc 使用. ]‼️ #### ‼️ Use `NSNumber` instead of `Bool`, which is not bridged to `BOOL`. [请勿使用 `Bool` 类型, 因为在 Swift 中并没有桥接该类型, 不能显式的对应 `BOOL`, 请使用 `NSNumber` 替代] ‼️ ## Contents * [Getting Started 【开始使用】](#Getting_Started) * [Features 【能做什么】](#Features) * [Installation 【安装】](#Installation) * [Examples 【示例】](#Examples) * [JSON -> Model](#JSON_Model) * [JSONString -> Model](#JSONString_Model) * [Model contains model](#Model_contains_model) * [Model contains model-array](#Model_contains_model_array) * [Model name - JSON key mapping](#Model_name_JSON_key_mapping) * [JSON array -> model array](#JSON_array_model_array) * [Model -> JSON](#Model_JSON) * [Model array -> JSON array](#Model_array_JSON_array) * [Core Data](#Core_Data) * [Coding](#Coding) * [Camel -> underline](#Camel_underline) * [NSString -> NSDate, nil -> @""](#NSString_NSDate) * [NSDate -> NSString](#NSDate_NSString) * [More use cases](#More_use_cases) --- # <a id="Getting_Started"></a> Getting Started【开始使用】 ## <a id="Features"></a> Features【能做什么】 - MJExtension是一套字典和模型之间互相转换的超轻量级框架 * `JSON` --> `Model`、`Core Data Model` * `JSONString` --> `Model`、`Core Data Model` * `Model`、`Core Data Model` --> `JSON` * `JSON Array` --> `Model Array`、`Core Data Model Array` * `JSONString` --> `Model Array`、`Core Data Model Array` * `Model Array`、`Core Data Model Array` --> `JSON Array` * Coding all properties of a model with only one line of code. * 只需要一行代码,就能实现模型的所有属性进行Coding(归档和解档) ## <a id="Installation"></a> Installation【安装】 ### From CocoaPods【使用CocoaPods】 ```ruby pod 'MJExtension' ``` ### Manually【手动导入】 - Drag all source files under folder `MJExtension` to your project.【将`MJExtension`文件夹中的所有源代码拽入项目中】 - Import the main header file:`#import "MJExtension.h"`【导入主头文件:`#import "MJExtension.h"`】 ```objc MJExtension.h MJConst.h MJConst.m MJFoundation.h MJFoundation.m MJProperty.h MJProperty.m MJType.h MJType.m NSObject+MJCoding.h NSObject+MJCoding.m NSObject+MJProperty.h NSObject+MJProperty.m NSObject+MJKeyValue.h NSObject+MJKeyValue.m ``` # <a id="Examples"></a> Examples【示例】 **Add `MJKeyValue` protocol to your model if needed【如果有需要, 请在模型中加入 `MJKeyValue` 协议】** ### <a id="JSON_Model"></a> The most simple JSON -> Model【最简单的字典转模型】 ```objc typedef enum { SexMale, SexFemale } Sex; @interface User : NSObject @property (copy, nonatomic) NSString *name; @property (copy, nonatomic) NSString *icon; @property (assign, nonatomic) unsigned int age; @property (copy, nonatomic) NSString *height; @property (strong, nonatomic) NSNumber *money; @property (assign, nonatomic) Sex sex; @property (assign, nonatomic, getter=isGay) BOOL gay; @end /***********************************************/ NSDictionary *dict = @{ @"name" : @"Jack", @"icon" : @"lufy.png", @"age" : @20, @"height" : @"1.55", @"money" : @100.9, @"sex" : @(SexFemale), @"gay" : @"true" // @"gay" : @"1" // @"gay" : @"NO" }; // JSON -> User User *user = [User mj_objectWithKeyValues:dict]; NSLog(@"name=%@, icon=%@, age=%zd, height=%@, money=%@, sex=%d, gay=%d", user.name, user.icon, user.age, user.height, user.money, user.sex, user.gay); // name=Jack, icon=lufy.png, age=20, height=1.550000, money=100.9, sex=1 ``` ### <a id="JSONString_Model"></a> JSONString -> Model【JSON字符串转模型】 ```objc // 1.Define a JSONString NSString *jsonString = @"{\"name\":\"Jack\", \"icon\":\"lufy.png\", \"age\":20}"; // 2.JSONString -> User User *user = [User mj_objectWithKeyValues:jsonString]; // 3.Print user's properties NSLog(@"name=%@, icon=%@, age=%d", user.name, user.icon, user.age); // name=Jack, icon=lufy.png, age=20 ``` ### <a id="Model_contains_model"></a> Model contains model【模型中嵌套模型】 ```objc @interface Status : NSObject @property (copy, nonatomic) NSString *text; @property (strong, nonatomic) User *user; @property (strong, nonatomic) Status *retweetedStatus; @end /***********************************************/ NSDictionary *dict = @{ @"text" : @"Agree!Nice weather!", @"user" : @{ @"name" : @"Jack", @"icon" : @"lufy.png" }, @"retweetedStatus" : @{ @"text" : @"Nice weather!", @"user" : @{ @"name" : @"Rose", @"icon" : @"nami.png" } } }; // JSON -> Status Status *status = [Status mj_objectWithKeyValues:dict]; NSString *text = status.text; NSString *name = status.user.name; NSString *icon = status.user.icon; NSLog(@"text=%@, name=%@, icon=%@", text, name, icon); // text=Agree!Nice weather!, name=Jack, icon=lufy.png NSString *text2 = status.retweetedStatus.text; NSString *name2 = status.retweetedStatus.user.name; NSString *icon2 = status.retweetedStatus.user.icon; NSLog(@"text2=%@, name2=%@, icon2=%@", text2, name2, icon2); // text2=Nice weather!, name2=Rose, icon2=nami.png ``` ### <a id="Model_contains_model_array"></a> Model contains model-array【模型中有个数组属性,数组里面又要装着其他模型】 ```objc @interface Ad : NSObject @property (copy, nonatomic) NSString *image; @property (copy, nonatomic) NSString *url; @end @interface StatusResult : NSObject /** Contatins status model */ @property (strong, nonatomic) NSMutableArray *statuses; /** Contatins ad model */ @property (strong, nonatomic) NSArray *ads; @property (strong, nonatomic) NSNumber *totalNumber; @end /***********************************************/ // Tell MJExtension what type of model will be contained in statuses and ads. [StatusResult mj_setupObjectClassInArray:^NSDictionary *{ return @{ @"statuses" : @"Status", // @"statuses" : [Status class], @"ads" : @"Ad" // @"ads" : [Ad class] }; }]; // Equals: StatusResult.m implements +mj_objectClassInArray method. NSDictionary *dict = @{ @"statuses" : @[ @{ @"text" : @"Nice weather!", @"user" : @{ @"name" : @"Rose", @"icon" : @"nami.png" } }, @{ @"text" : @"Go camping tomorrow!", @"user" : @{ @"name" : @"Jack", @"icon" : @"lufy.png" } }
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。