Dictionary reflection


Reflection and UnWrapping now has Dictionary support

Swift example:

let temp:Temp = Temp([0:"test",3:"testing",5:"more testing"])//create a dict
let xml:XML = Reflection.toXML(temp) //reflect the dict to xml
Swift.print(xml.XMLString)//print the xml

let newInstance:Temp = Temp.unWrap(xml)!//unwrap the xml to dict
newInstance.someDict.forEach{
    Swift.print("key: \($0.0) value: \($0.1)")//print key,value
}

class Temp{
    var someDict:[Int:String]
    init(_ someDict:[Int:String]){
        self.someDict = someDict
    }
}
extension Temp:UnWrappable{
    static func unWrap<T>(xml:XML) -> T? {
        let someDict:[Int:String] = unWrap(xml,"someDict")
        return Temp(someDict) as? T
    }
}

XML output:

<Temp>
	<someDict type="Dictionary">
		<item>
			<key type="Int">5</key>
			<value type="String">more testing</value>
		</item>
		<item>
			<key type="Int">0</key>
			<value type="String">test</value>
		</item>
		<item>
			<key type="Int">3</key>
			<value type="String">testing</value>
		</item>
	</someDict>
</Temp>

Array with Dictionary reflection:

let temp:Temp = Temp()
let xml:XML = Reflection.toXML(temp)
Swift.print("xml.xmlString: " + "\(xml.xmlString)")
class Temp{
    let temp:[[String:String]] = [["a":"b"],["1":"2"]]
}
<Temp>
	<temp type="Array">
		<item type="Dictionary">
			<item>
				<key type="String">a</key>
				<value type="String">b</value>
			</item>
		</item>
		<item type="Dictionary">
			<item>
				<key type="String">1</key>
				<value type="String">2</value>
			</item>
		</item>
	</temp>
</Temp>

Note:

Storing key and value in separate nodes is important because key can have different types and value can have nested nodes. Aka complex content. As can key when I think about it, as long as it extends the Hashable protocol.

Related Posts

Startup Oslo

“Kulturhuset” is a new place in the startup scene in Oslo. It’s “free” and can be used by anyone to collaborate or work on different projects.

Infinite Tree List

My notes on Infinite tree list

Protocol Inheritance

My notes on Protocol Inheritance

Protocol Ambiguity

How to differentiate protocol ambiguity

The Ultimate Xcode Workflow

Spend zero time managing dependencies

Faster Xcode With Spm

How you can speed up compile times in XCode with Swift Package Manager

Spm And Ci Travis

My notes on Swift PM + CI Travis

Spm And Nested Frameworks

My notes on Swift package manager + XCode + Nested frameworks

Xcode And Spm

Here is how you use Swift package manager in your XCode app projects

Carthage And Nested Frameworks

A few workflows concerning Carthage and nested framework