Reputation: 1012
I am using pods with Swift 4
pod 'SWXMLHash', '~> 4.0.0'
pod 'Alamofire', '~> 4.5'
When I am parsing XML with below code, getting the error:
Type 'XMLIndexer' does not conform to protocol 'Sequence'
Code:
Alamofire.request("https://itunes.apple.com/us/rss/topgrossingapplications/limit=10/xml").response { response in
debugPrint(response)
guard let data = response.data else {
return
}
let xml = SWXMLHash.parse(data)
let nodes = xml["feed"]["entry"]
for node in nodes {
print(node["title"].text)
}
}
I am trying to access 'entry' tag list from above iTunes XML URL. If there is any method to access and initialize the list of entries in class/struct, please help.
Upvotes: 3
Views: 751
Reputation: 3020
Another solution is to use Fuzi, which is based on the Ono framework, which had great support.
The following snippet will print the titles:
Alamofire.request("https://itunes.apple.com/us/rss/topgrossingapplications/limit=10/xml").responseString { response in
guard let xml = try? XMLDocument(string: response.value ?? "") else {
return
}
guard let feed = xml.root else {
return
}
for entry in feed.children(tag: "entry") {
let title = entry.firstChild(tag: "title")?.stringValue ?? ""
print(title)
}
}
Upvotes: 1
Reputation: 3020
According to the documentation you need to add all
:
Alamofire.request("https://itunes.apple.com/us/rss/topgrossingapplications/limit=10/xml").response { response in
debugPrint(response)
guard let data = response.data else {
return
}
let xml = SWXMLHash.parse(data)
let nodes = xml["feed"]["entry"]
for node in nodes.all {
print(node["title"]?.text)
}
}
Upvotes: 3