Update README.md

This commit is contained in:
Kazuhiro Hayashi 2018-08-18 14:37:53 +09:00 committed by GitHub
parent c649b35507
commit fcc6a96354
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 11 deletions

View File

@ -144,7 +144,7 @@ xml = XML.parse(data) // -> XML.Accessor
### 2. Access child Elements ### 2. Access child Elements
```swift ```swift
let element = xml["ResultSet"] // -> XML.Accessor let element = xml.ResultSet // -> XML.Accessor
``` ```
### 3. Access grandchild Elements ### 3. Access grandchild Elements
@ -167,18 +167,18 @@ let element = xml.ResultSet.Result // -> <Result><Hit index=\"1\"><Name>Item1</N
``` ```
### 4. Access specific grandchild Element ### 4. Access specific grandchild Element
```swift ```swift
let element = xml["ResultSet", "Result", "Hit", 1] // -> <Hit index=\"2\"><Name>Item2</Name></Hit> let element = xml.ResultSet.Result.Hit[1] // -> <Hit index=\"2\"><Name>Item2</Name></Hit>
``` ```
### 5. Access attribute in Element ### 5. Access attribute in Element
```swift ```swift
if let attributeValue = xml["ResultSet", "Result", "Hit", 1].attributes?["index"] { if let attributeValue = xml.ResultSet.Result.Hit[1].attributes?["index"] {
print(attributeValue) // -> 2 print(attributeValue) // -> 2
} }
``` ```
### 6. Access text in Element ### 6. Access text in Element
+ with optional binding + with optional binding
```swift ```swift
if let text = xml["ResultSet", "Result", "Hit", 1, "Name"].text { if let text = xml.ResultSet.Result.Hit[1].Name.text {
print(text) // -> Item2 print(text) // -> Item2
} }
``` ```
@ -188,7 +188,7 @@ struct Entity {
var name = "" var name = ""
} }
let entity = Entity() let entity = Entity()
entity.name ?= xml["ResultSet", "Result", "Hit", 1, "Name"].text // assign if it has text entity.name ?= xml.ResultSet.Result.Hit[1].Name.text // assign if it has text
``` ```
+ convert Int and assign + convert Int and assign
```swift ```swift
@ -196,7 +196,7 @@ struct Entity {
var name: Int = 0 var name: Int = 0
} }
let entity = Entity() let entity = Entity()
entity.name ?= xml["ResultSet", "Result", "Hit", 1, "Name"].int // assign if it has Int entity.name ?= xml.ResultSet.Result.Hit[1].Name.int // assign if it has Int
``` ```
and there are other syntax sugers, bool, url and double. and there are other syntax sugers, bool, url and double.
+ assign text into Array + assign text into Array
@ -205,23 +205,23 @@ struct Entity {
var names = [String]() var names = [String]()
} }
let entity = Entity() let entity = Entity()
entity.names ?<< xml["ResultSet", "Result", "Hit", 1, "Name"].text // assign if it has text entity.names ?<< xml.ResultSet.Result.Hit[1].Name.text // assign if it has text
``` ```
### Check error ### Check error
```swift ```swift
print(xml["ResultSet", "Result", "TypoKey"]) // -> "TypoKey not found." print(xml.ResultSet.Result.TypoKey) // -> "TypoKey not found."
``` ```
### Access as SequenceType ### Access as SequenceType
+ for-in + for-in
```swift ```swift
for element in xml["ResultSet", "Result", "Hit"] { for element in xml.ResultSet.Result.Hit {
print(element.text) print(element.text)
} }
``` ```
+ map + map
```swift ```swift
xml["ResultSet", "Result", "Hit"].map { $0["Name"].text } xml.ResultSet.Result.Hit.map { $0.Name.text }
``` ```
## Work with Alamofire ## Work with Alamofire
@ -235,7 +235,7 @@ Alamofire.request(.GET, "https://itunes.apple.com/us/rss/topgrossingapplications
.responseData { response in .responseData { response in
if let data = response.data { if let data = response.data {
let xml = XML.parse(data) let xml = XML.parse(data)
print(xml["feed", "entry", 0, "title"].text) // outputs the top title of iTunes app raning. print(xml.feed.entry[0].title.text) // outputs the top title of iTunes app raning.
} }
} }
``` ```