yahoojapan / SwiftyXMLParser

Simple XML Parser implemented in Swift
MIT License
577 stars 89 forks source link

Supporting dynamicMemberLookup #19

Closed kazuhiro4949 closed 5 years ago

kazuhiro4949 commented 6 years ago

Swift4.2 has the following feature. https://github.com/apple/swift-evolution/blob/master/proposals/0195-dynamic-member-lookup.md

It enables us to implement a property dynamically.

@dynamicMemberLookup
struct Foo {
       public subscript(dynamicMember member: String) -> String {
           return member
       }
}

let foo = Foo()
foo.method // -> "method"

If XML.Accessor has the feature, we can access XML elements with a dynamic property.

let str = """
<ResultSet>
    <Result>
        <Hit index=\"1\">
            <Name>Item1</Name>
        </Hit>
        <Hit index=\"2\">
            <Name>Item2</Name>
        </Hit>
    </Result>
</ResultSet>
"""

let xml = try! XML.parse(str)
if let text = xml.ResultSet.Result.Hit[0].Name.text {
    print(text) // -> "Item1" 
}
kazuhiro4949 commented 6 years ago

pull request: https://github.com/yahoojapan/SwiftyXMLParser/pull/20

kazuhiro4949 commented 6 years ago

Branch for Swift 4.2 : https://github.com/yahoojapan/SwiftyXMLParser/tree/swift4.2

I will merge it to master branch after Xcode10 GM is launched.