• Stars
    star
    307
  • Rank 136,109 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Reflection, Dict2Model, Model2Dict, Archive

image

感谢小饭的翻译 中文文档

###Selfless pay, just to make you faster to conquer swift!

.Reflection

.convert dictionary to model

.convert model to dictionary

.convert plist to model

.One key archive




Reflect


### Reflec .iOS 7.0
.Swift 2.0
.Xcode 7
.Reflection is available under the MIT license.




Installation

you can just drag the Reflect files into your own project, without any third-party library.

Structure Details:
.Coding about archiving
.Reflect reflection core package
.Dict2Model convert dictionary to custom object
.Model2Dict convert custom object to dictionary

Tips

This frame already prepares many examples for lazy guys and it’s easy to see the result when u just use the methord Person1.Convert(),Studetn1.Parse(),Book1.Action()
1.Parse-Parse8: convert dictionary to model
2.Convert1-Convert4: convert model to dictionary
3.Archiver1-Archiver3: One key archive




Core theory

  1. If you wanna use all the functions, all your custom model must inherit from Reflect Class.

  2. It’s better to use implicitly optional type for properties. Try not to use strange type like var v1: [string?]? , var v2: [string!]! and this frame has lots of assertion to help you use data type reasonably.

  3. This frame is based on requirement : parse json data into dictionaries then to model. Limitation: reckon without complictated data like Core Data support; without consideration of Struct.





Reflect

Call the properties of object directly can get the reflection for enumerations:

    p1.properties { (name, type, value) -> Void in
        println("\(name),\(type),\(value)")
    }

In addition, you can call class methods without initiate object while the value has no meaning:

    Person.properties { (name, type, _) -> Void in
        println("\(name),\(type)")
    }

The name is the property name of your model; Type is the type of encapsulated ReflectType data; Value is the value of variable; You can also see the detail of properties by seeing the reference of ReflectType encapsulation. For example, the specific data type, if it is basic data type, if it is array, if it is optional, if it is object transferred from OC and if it is custom class.:

var typeName: String!

/**  analysis type from system  */
var typeClass: Any.Type!

var disposition: MirrorDisposition!

var dispositionDesc: String!

/**  whether it is optional  */
var isOptional: Bool = false

/**  whether it is array  */
var isArray: Bool = false

/**  real type: optional + array  */
var realType: RealType = .None

In addition to above, I also add the function of printing like OC so that you can directly print the object. For example, print Book1(book1 class is in Archiver1.swift of the projcet) println(book1):

println(book1)

the result of console would be:

Reflect.Book1 <0x7a09fb10>: 
{
name: tvb
price: 36.6
}

ignore the field and field reflection of the process of analyzing additional features, subclass needs only rewrite the method.

/**  property mapping  */

//The model's property named 'userModel',get value width key 'user_model' from dictionay
func mappingDict() -> [String: String]? {
    return ["userModel":"user_model"]
}
 
 
/**  property ignore */

//Ignore the 'info' property of the model.
func ignorePropertiesForParse() -> [String]? {
    return ["info"]
}

You also can use the method below to complete converting from string to class

let cls = ClassFromString("Reflect.Person")

attention, the string here include the name space




convert dictionary to model

it’s easy to convert dictionary to model, I already did the cascade im not going to repeat the details:

let stu1 = Student1.parse(dict: Student1Dict)
let stus = Student7.parses(arr: Student7DictArr)

If you property type is Bool, you may encounter UndefinedKey, which is due to Swift's own reasons, you just need to implement the following methods manually resolved:

override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    self.isVip = (value as! Int) != 0
}

parse Plist file ,Please don't take the suffix

let author = Author.parsePlist("Author")




convert model to dictionary

it’s easy to convert model to dictionary, I already did the cascade im not going to repeat the details:

let dict = person3.toDict()




One key archive

Archive has done the cascade and easy for using. It also encapsulates the operation of Caches folder, directly save in Caches folder.

Archive: the name of single model could be empty, but the name of array can’t be empty. Back to the position of archiving.

let path1 =  Book2.save(obj: book2, name: nil)
let path2 =  Book3.save(obj: bookArr, name: "book3")

Read data: please use the same key, if save name wich not used as nill, use nil for read data.

let arcBook2 = Book2.read(name: nil)
let arr = Book3.read(name: "book3")

delete data:

Book1.delete(name: nil)

additional function: ignore archive field

/**  ignore archive field  */

//ignore archive property named 'icon'
func ignoreCodingPropertiesForCoding() -> [String]? {
    return ["icon"]
}

More Repositories

1

CorePhotoBroswerVC

快速集成高性能照片浏览器,支持本地及网络相册
Objective-C
1,575
star
2

CoreLock

高仿支付宝解锁
Objective-C
971
star
3

CoreModel

Replace CoreData
Objective-C
727
star
4

PhotoBrowser

Photo Browser Terminator
Swift
613
star
5

CFCityPickerVC

城市选取控制器
Swift
544
star
6

CoreStatus

网络状态监听者:可监听2G/3G/4G
Objective-C
220
star
7

CoreLaunch

一键启动动画
Objective-C
214
star
8

CorePagesView

列表滚动视图,性能王者!
Objective-C
189
star
9

CoreRefresh

核心上拉下拉刷新控件,高性能、与众不同!
Objective-C
188
star
10

CoreNavVC

Most Powerfull NavigationController
Objective-C
187
star
11

CFRuntime

重磅推出:Swift版的MJExtension,运行时、反射与一键字典模型互转
Swift
174
star
12

CoreNewFeatureVC

版本新特性
Objective-C
171
star
13

CoreFMDB

One Key Database Operations
Objective-C
157
star
14

Show

信息公示牌
153
star
15

CorePhotoPickerVCManager

大统一的多功能照片选取器,集成拍摄,单选,多选。
Objective-C
150
star
16

CoreJPush

三分钟,五句代码,搞定JPush
Objective-C
125
star
17

CoreArchive

One Key Archive
Objective-C
111
star
18

CoreUmeng

友盟分享封装
Objective-C
103
star
19

CoreHttp

抛弃AFN,网络处理工具类,将网络交互玩于股掌间的利器!
Objective-C
93
star
20

CorePullScale

表格顶部图片下拉放大控件,一键式安装,异常简单
Objective-C
86
star
21

CoreEmotionView

表情键盘
Objective-C
79
star
22

CoreTFManagerVC

强大的键盘躲避管理器,完全解耦版本!
Objective-C
77
star
23

TextField-InputView

UITextField+InputView模式:终结注册类、信息采集类
Swift
68
star
24

CFPPTView

Swift版幻灯,scrollView无限滚动,支持网络和本地图片展示!
Objective-C
66
star
25

CorePPTVC

幻灯终结者:超简单、酷且炫、多样式、自定义!
Objective-C
62
star
26

CoreLabel

为图文混排而设计,超轻量级的Label,轻度使用下是TTTLabel的最佳替换选择
Objective-C
61
star
27

SinaPhotoView

朋友圈相册视图
Swift
57
star
28

CoreCategory

核心分类,基本所有需要用的功能聚集于此!
Objective-C
48
star
29

CFPagesVC

Professional Multi controller management Framework based on Swift
Swift
45
star
30

CoreMediaFuncManagerVC

一句话打电话
Objective-C
39
star
31

CoreBtn

核心按钮,让界面更专业!旨在替代一部份HUD的Btn
Objective-C
36
star
32

TextView-KeyBoard

textView的键盘躲避者,一句代码集成,灵活,好用。
Swift
34
star
33

CorePasswordView

仿支付宝密码视图
Objective-C
32
star
34

CoreHamburgerManagerVC

汉堡菜单,高度解耦,您想怎么搞就怎么搞!
Objective-C
31
star
35

CoreAleetViewManagerVC

高度集成AlertView/ActionSheet,真的只需要一句代码!
Objective-C
26
star
36

CoreSVP

核心提示工具
Objective-C
25
star
37

CoreList

内部研发
Objective-C
21
star
38

ThreeDTouch

3D Touch
Swift
18
star
39

CoreViewNetWorkStausManager

网络状态提示图层管理者,提高你的app的逼格与细节。
Objective-C
17
star
40

CoreException

核心异常捕捉及处理框架
Objective-C
15
star
41

CoreEaseMob

EaseMob
Objective-C
15
star
42

SwipeNavigationVC

向右滑动退出栈顶控制器
Objective-C
15
star
43

CorePickerView

极简选取器,一安一取,简单方便!
Objective-C
13
star
44

ShiDianFontFit

全自动的屏幕字体适配
Objective-C
13
star
45

CoreBlurView

第二牛B的模糊视图(含图片模糊)
Objective-C
12
star
46

CoreReg

正则封装
Objective-C
11
star
47

CorePageControl

高度自定义的pageControl
Objective-C
10
star
48

CFSegmentControl

双版本分段选取控件
Swift
9
star
49

CoreWeb

原生WebView与H5混编,高效,简单!
8
star
50

CoreExtend

Charlin私有框架:核心扩展
Objective-C
7
star
51

CoreSDWebImage

自用图片加载模型,基于SDWebImage封装
Objective-C
6
star
52

CFSnapKit

CFSnapKit
Swift
6
star
53

CoreFormView

表格
Objective-C
6
star
54

AppLaunchADView

AppLaunchADView
Swift
5
star
55

Resource

5
star
56

SwiftExtension

SwiftExtension
Swift
5
star
57

CoreToast

基于CRToast的封装,更好用
Objective-C
5
star
58

CoreCalendar

日历
Objective-C
5
star
59

CoreJazzHandsVC

手势帧动画
Objective-C
5
star
60

KRVideoPlayerVC

KRVideoPlayer 修改版
Objective-C
5
star
61

CoreCapture

仿微信视频拍摄
Objective-C
4
star
62

CoreAddressBookVC

通讯录
Objective-C
4
star
63

CoreTransition

Push切换
Objective-C
4
star
64

CoreGuideView

CoreGuideView
Objective-C
4
star
65

CoreKXMovie

编译KXMovie
C
3
star
66

ShiDianHttp

基于Volley的框架(浓郁的iOS风格)
Java
3
star
67

LocationManager

LocationManager
Swift
3
star
68

AnimTabVC

AnimTabVC
Swift
3
star
69

CoreRAC

集成RAC
Objective-C
3
star
70

CoreMasonry

Masonry
Objective-C
3
star
71

CoreShineLabel

淡入淡出的Label
Objective-C
3
star
72

CountDownLabel

考试系统倒计时
Swift
3
star
73

CoreVideoView

CoreVideoView
Objective-C
3
star
74

Snapkit

Snapkit
Objective-C
2
star
75

ImagePicker

ImagePicker
Objective-C
2
star
76

CoreYYWebImage

CoreYYWebImage
Objective-C
2
star
77

WeChatApp

微信小程序笔记
2
star
78

CorePopView

CorePopView
Objective-C
2
star
79

CorePaperButton

CorePaperButton
Objective-C
2
star
80

CorePopoverController

PopoverController
Objective-C
2
star
81

CoreWebViewController

CoreWebViewController
Objective-C
2
star
82

CoreTipView

CoreTipView
Objective-C
2
star
83

Pop

Pop动画引擎
Objective-C++
2
star
84

ShiDianPay

ShiDianPay
C
2
star
85

CoreKeyChain

KeyChain
Objective-C
2
star
86

YeahComponent

YeahComponent
Objective-C
1
star
87

ShiDianPlayerView

ShiDianPlayerView
Objective-C
1
star
88

CFPhotoBroswer

Powefull PhotoBroswer For Swift
Swift
1
star
89

MapView

MapView
Swift
1
star
90

CoreLBXScan

CoreLBXScan
Objective-C
1
star
91

CoreJHAnim

CoreJHAnim
Objective-C
1
star
92

CoreTextView

CoreTextView
Objective-C
1
star
93

NewcomerGuide

NewcomerGuide
Swift
1
star
94

CoreAsyncDisplayKit

CoreAsyncDisplayKit
Objective-C++
1
star
95

CoreMorphingLabel

仿iOS8的Label
Objective-C
1
star
96

CoreIV

CoreIV
Objective-C
1
star
97

ShiDianUM

ShiDianUM
Objective-C
1
star
98

ShiDianWebViewController

Objective-C
1
star
99

CoreUMengMobClick

CoreUMengMobClick
Objective-C
1
star
100

ShiDianRefresh

ShiDianRefresh
Objective-C
1
star