Reflection provides an API for advanced reflection at runtime including dynamic construction of types.
import Reflection
structPerson{varfirstName:StringvarlastName:Stringvarage:Int}
// Reflects the instance properties of type `Person`
letprops=tryproperties(Person.self)varperson=Person(firstName:"John", lastName:"Smith", age:35)
// Retrieves the value of `person.firstName`
letfirstName:String=tryget("firstName", from: person)
// Sets the value of `person.age`
tryset(36, key:"age", for:&person)
// Creates a `Person` from a dictionary
letfriend:Person=tryconstruct(dictionary:["firstName":"Sarah","lastName":"Gates","age":28])import PackageDescription
letpackage=Package(
dependencies:[.Package(url:"https://github.com/Zewo/Reflection.git", majorVersion:0, minor:15),])// `Reflection` can be extended for higher-level packages to do mapping and serializing.
// Here is a simple `Mappable` protocol that allows deserializing of arbitrary nested structures.
import Reflection
typealiasMappableDictionary=[String:Any]enumError:ErrorProtocol{case missingRequiredValue(key:String)}protocolMappable{init(dictionary:MappableDictionary)throws}extensionMappable{init(dictionary:MappableDictionary)throws{self=tryconstruct{ property iniflet value =dictionary[property.key]{iflet type = property.type as?Mappable.Type,let value = value as?MappableDictionary{returntry type.init(dictionary: value)}else{return value
}}else{throwError.missingRequiredValue(key: property.key)}}}}structPerson:Mappable{varfirstName:StringvarlastName:Stringvarage:IntvarphoneNumber:PhoneNumber}structPhoneNumber:Mappable{varnumber:Stringvartype:String}letdictionary=["firstName":"Jane","lastName":"Miller","age":54,"phoneNumber":["number":"924-555-0294","type":"work"]asMappableDictionary]asMappableDictionaryletperson=tryPerson(dictionary: dictionary)If you need any help you can join our Slack and go to the #help channel. Or you can create a Github issue in our main repository. When stating your issue be sure to add enough details, specify what module is causing the problem and reproduction steps.
The entire Zewo code base is licensed under MIT. By contributing to Zewo you are contributing to an open and engaged community of brilliant Swift programmers. Join us on Slack to get to know us!
This project is released under the MIT license. See LICENSE for details.
