Skip to content

Commit 2bd4754

Browse files
authored
Update usage in swift (#822)
1 parent e31c9a6 commit 2bd4754

File tree

2 files changed

+48
-55
lines changed

2 files changed

+48
-55
lines changed

MJExtensionTests/SwiftModel/MJTester.swift

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,15 @@
77
//
88

99
import Foundation
10+
import MJExtension
1011

1112
@objc(MJTester)
13+
@objcMembers
1214
class MJTester: NSObject {
13-
var isSpecialAgent: Bool { _isSpecialAgent?.boolValue ?? false }
14-
var age: Int { _age?.intValue ?? 0 }
15+
// make sure to use `dynamic` attribute for basic type & must use as Non-Optional & must set initial value
16+
dynamic var isSpecialAgent: Bool = false
17+
dynamic var age: Int = 0
1518

16-
@objc private var _isSpecialAgent: NSNumber?
17-
@objc private var _age: NSNumber?
18-
@objc var name: String?
19-
@objc var identifier: String?
20-
21-
override class func mj_replacedKeyFromPropertyName() -> [AnyHashable : Any]! {
22-
return [
23-
"_isSpecialAgent": "isSpecialAgent",
24-
"_age": "age"
25-
]
26-
}
19+
var name: String?
20+
var identifier: String?
2721
}

README.md

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,34 @@ MJExtension
88

99
[📜✍🏻**Release Notes**: more details](https://github.com/CoderMJLee/MJExtension/releases)
1010

11-
### ‼️ 纯Swift版的JSON与Model转换框架已经开源上架 ‼️
12-
13-
- [KakaJSON](https://github.com/kakaopensource/KakaJSON)
14-
- [中文教程](https://www.cnblogs.com/mjios/p/11352776.html)
15-
- 如果你的项目是用Swift写的Model,墙裂推荐使用[KakaJSON](https://github.com/kakaopensource/KakaJSON)
16-
- 已经对各种常用的数据场景进行了大量的单元测试
17-
- 简单易用、功能丰富、转换快速
18-
19-
20-
21-
### Use the Framework in Swift [关于在Swift中使用MJExtension] ‼️
22-
23-
> Example:
24-
>
25-
> - [Model - MJTester.swift](MJExtensionTests/SwiftModel/MJTester.swift)
26-
>
27-
> - [Usage - SwiftModelTests.swift](MJExtensionTests/SwiftModelTests.swift)
28-
29-
#### ‼️ `@objc` or `objcMembers` attributes should be added to class or property for declaration of Objc accessibility [在 Swift4 之后, 请在属性前加 `@objc` 修饰或在类前增加 `objcMembers`. 以保证 Swift 的属性能够暴露给 Objc 使用. ]‼️
30-
#### ‼️ Use `NSNumber` instead of `Bool`, which is not bridged to `BOOL`. [请勿使用 `Bool` 类型, 因为在 Swift 中并没有桥接该类型, 不能显式的对应 `BOOL`, 请使用 `NSNumber` 替代] ‼️
31-
3211
## Contents
3312

3413
* [Getting Started 【开始使用】](#Getting_Started)
3514
* [Features 【能做什么】](#Features)
3615
* [Installation 【安装】](#Installation)
3716
* [Examples 【示例】](#Examples)
38-
* [JSON -> Model](#JSON_Model)
39-
* [JSONString -> Model](#JSONString_Model)
40-
* [Model contains model](#Model_contains_model)
41-
* [Model contains model-array](#Model_contains_model_array)
42-
* [Model name - JSON key mapping](#Model_name_JSON_key_mapping)
43-
* [JSON array -> model array](#JSON_array_model_array)
44-
* [Model -> JSON](#Model_JSON)
45-
* [Model array -> JSON array](#Model_array_JSON_array)
46-
* [Core Data](#Core_Data)
47-
* [Coding](#Coding)
48-
* [Secure Coding](#SecureCoding)
49-
* [Camel -> underline](#Camel_underline)
50-
* [NSString -> NSDate, nil -> @""](#NSString_NSDate)
51-
* [NSDate -> NSString](#NSDate_NSString)
52-
* [More use cases](#More_use_cases)
17+
* [Usage in Swift](#usage_in_swift)
18+
* [JSON -> Model](#JSON_Model)
19+
* [JSONString -> Model](#JSONString_Model)
20+
* [Model contains model](#Model_contains_model)
21+
* [Model contains model-array](#Model_contains_model_array)
22+
* [Model name - JSON key mapping](#Model_name_JSON_key_mapping)
23+
* [JSON array -> model array](#JSON_array_model_array)
24+
* [Model -> JSON](#Model_JSON)
25+
* [Model array -> JSON array](#Model_array_JSON_array)
26+
* [Core Data](#Core_Data)
27+
* [Coding](#Coding)
28+
* [Secure Coding](#SecureCoding)
29+
* [Camel -> underline](#Camel_underline)
30+
* [NSString -> NSDate, nil -> @""](#NSString_NSDate)
31+
* [NSDate -> NSString](#NSDate_NSString)
32+
* [More use cases](#More_use_cases)
5333

5434
---
5535

56-
# <a id="Getting_Started"></a> Getting Started【开始使用】
36+
## <a id="Getting_Started"></a> Getting Started【开始使用】
5737

58-
## <a id="Features"></a> Features【能做什么】
38+
### <a id="Features"></a> Features【能做什么】
5939
- MJExtension是一套字典和模型之间互相转换的超轻量级框架
6040
* `JSON` --> `Model``Core Data Model`
6141
* `JSONString` --> `Model``Core Data Model`
@@ -66,15 +46,15 @@ MJExtension
6646
* Coding all properties of a model with only one line of code.
6747
* 只需要一行代码,就能实现模型的所有属性进行Coding / SecureCoding(归档和解档)
6848

69-
## <a id="Installation"></a> Installation【安装】
49+
### <a id="Installation"></a> Installation【安装】
7050

71-
### From CocoaPods【使用CocoaPods】
51+
#### From CocoaPods【使用CocoaPods】
7252

7353
```ruby
7454
pod 'MJExtension'
7555
```
7656

77-
### Manually【手动导入】
57+
#### Manually【手动导入】
7858
- Drag all source files under folder `MJExtension` to your project.【将`MJExtension`文件夹中的所有源代码拽入项目中】
7959
- Import the main header file:`#import "MJExtension.h"`【导入主头文件:`#import "MJExtension.h"`
8060

@@ -89,10 +69,29 @@ NSObject+MJProperty.h NSObject+MJProperty.m
8969
NSObject+MJKeyValue.h NSObject+MJKeyValue.m
9070
```
9171

92-
# <a id="Examples"></a> Examples【示例】
72+
## <a id="Examples"></a> Examples【示例】
9373

9474
**Add `MJKeyValue` protocol to your model if needed【如果有需要, 请在模型中加入 `MJKeyValue` 协议】**
9575

76+
### <a id="usage_in_swift"></a> Usage in Swift [关于在Swift中使用MJExtension] ‼️
77+
78+
> Example:
79+
>
80+
> - [Model - MJTester.swift](MJExtensionTests/SwiftModel/MJTester.swift)
81+
>
82+
> - [Usage - SwiftModelTests.swift](MJExtensionTests/SwiftModelTests.swift)
83+
84+
1. `@objc` or `@objcMembers` attributes should be added to class or property for declaration of Objc accessibility [在 Swift4 之后, 请在属性前加 `@objc` 修饰或在类前增加 `@objcMembers`. 以保证 Swift 的属性能够暴露给 Objc 使用. ]
85+
2. If you let `Bool` & `Int` as property type, make sure that using `dynamic` to attribute it. It must be `Non-Optional` type and assign `a default value`.
86+
87+
> 纯Swift版的JSON与Model转换框架已经开源上架
88+
>
89+
> - [KakaJSON](https://github.com/kakaopensource/KakaJSON)
90+
> - [中文教程](https://www.cnblogs.com/mjios/p/11352776.html)
91+
> - 如果你的项目是用Swift写的Model,墙裂推荐使用[KakaJSON](https://github.com/kakaopensource/KakaJSON)
92+
> - 已经对各种常用的数据场景进行了大量的单元测试
93+
> - 简单易用、功能丰富、转换快速
94+
9695
### <a id="JSON_Model"></a> The most simple JSON -> Model【最简单的字典转模型】
9796

9897
```objc

0 commit comments

Comments
 (0)