EVReflection
Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift
Install / Use
/learn @evermeer/EVReflectionREADME
EVReflection
<!--- [](https://circleci.com/gh/evermeer/EVReflection) [](https://travis-ci.org/evermeer/EVReflection) -->General information
At this moment the master branch is tested with Swift 4.2 and 5.0 beta If you want to continue using EVReflection in an older version, then use the corresponding branch. Run the unit tests to see EVReflection in action.
Please update to cocoapods 1.7.0 for now you could do that using
sudo gem install cocoapods --pre
This is required for libraries that want to support other swift versions besides 5.0 For more information see the cocoapods blog
EVReflection is used in EVCloudKitDao and EVWordPressAPI
In most cases EVReflection is very easy to use. Just take a look the section It's easy to use. But if you do want to do non standard specific things, then EVReflection will offer you an extensive range of functionality.
Available extensions
There are extension available for using EVReflection with XMLDictionairy, Realm, CloudKit, Alamofire and Moya with RxSwift or ReactiveSwift
- XML
- CloudKit
- CoreData
- Realm
- Alamofire
- AlamofireXML
- Moya
- MoyaXML
- MoyaRxSwift
- MoyaRxSwiftXML
- MoyaReactiveSwift
- MoyaReactiveSwiftXML
All these extens can be installed by adding something like this in your podfile:
pod 'EVReflection/MoyaRxSwift'
For Carthage there is not (yet) an extension for all above extension. If needed, please let me know. For carthage you can use:
github "evermeer/EVReflection"
Index
- Main features of EVReflection
- It's easy to use
- If you have XML instead of JSON
- Using EVReflection in your own App
- More Sample code
- Extending existing objects
- Conversion options
- Automatic keyword mapping for Swift keywords
- Automatic keyword mapping PascalCase or camelCase to snake_case
- Custom keyword mapping
- Custom property converters
- Custom object converter
- Custom type converters
- Encoding and Decoding
- Skip the serialization or deserialization of specific values
- Property validators
- Print options
- Deserialization class level validations
- What to do when you use object inheritance
- Known issues
- License
- My other libraries
Main features of EVReflection:
- Parsing objects based on NSObject to and from a dictionary. (also see the XML and .plist samples!)
- Parsing objects to and from a JSON string.
- Support NSCoding function encodeWithCoder and decodeObjectWithCoder
- Supporting Printable, Hashable and Equatable while using all properties.
- Mapping objects from one type to an other
- Support for property mapping, converters, validators and key cleanup
It's easy to use:
Defining an object. You only have to set EVObject as it's base class (or extend an NSObject with EVReflectable):
class User: EVObject {
var id: Int = 0
var name: String = ""
var friends: [User]? = []
}
Parsing JSON to an object:
let json:String = "{\"id\": 24, \"name\": \"Bob Jefferson\", \"friends\": [{\"id\": 29, \"name\": \"Jen Jackson\"}]}"
let user = User(json: json)
Parsing JSON to an array of objects:
let json:String = "[{\"id\": 27, \"name\": \"Bob Jefferson\"}, {\"id\": 29, \"name\": \"Jen Jackson\"}]"
let array = [User](json: json)
Parsing from and to a dictionary:
let dict = user.toDictionary()
let newUser = User(dictionary: dict)
XCTAssert(user == newUser, "Pass")
Saving and loading an object to and from a file:
user.saveToTemp("temp.dat")
let result = User(fileNameInTemp: "temp.dat")
XCTAssert(theObject == result, "Pass")
Mapping object to another type:
let administrator: Administrator = user.mapObjectTo()
If you have XML instead of JSON
If you want to do the same but you have XML, then you can achieve that using the XML subspec 'pod EVReflection/XML' It is a simple way to parse XML. With that your code will look like this:
let xml = "<user><id>27</id><name>Bob</name><friends><user><id>20</id><name>Jen</name></user></friends></user>"
let user = User(xmlString: xml)
Using EVReflection in your own App
'EVReflection' is available through the dependency manager CocoaPods. You do have to use cocoapods version 0.36 or later
You can just add EVReflection to your workspace by adding the following 2 lines to your Podfile:
use_frameworks!
pod "EVReflection"
You can also use the Swift2.2 or Swift2.3 version of EVReflection. You can get that version by using the podfile command:
use_frameworks!
pod "EVReflection"', :git => 'https://github.com/evermeer/EVReflection.git', :branch => 'Swift2.2'
Version 0.36 of cocoapods will make a dynamic framework of all the pods that you use. Because of that it's only supported in iOS 8.0 or later. When usi
