Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#SwiftyJSON [中文介绍](http://tangplin.github.io/swiftyjson/)
# SwiftyJSON [中文介绍](http://tangplin.github.io/swiftyjson/)

SwiftyJSON makes it easy to deal with JSON data in Swift.

Expand All @@ -17,7 +17,7 @@ SwiftyJSON makes it easy to deal with JSON data in Swift.
- [Literal convertibles](#literal-convertibles)
1. [Work with Alamofire](#work-with-alamofire)

##Why is the typical JSON handling in Swift NOT good?
## Why is the typical JSON handling in Swift NOT good?
Swift is very strict about types. But although explicit typing is good for saving us from mistakes, it becomes painful when dealing with JSON and other areas that are, by nature, implicit about types.

Take the Twitter API for example. Say we want to retrieve a user's "name" value of some tweet in Swift (according to Twitter's API https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline).
Expand Down Expand Up @@ -84,7 +84,7 @@ if let userName = json[999999]["wrong_key"]["wrong_name"].string{
- iOS 7.0+ / Mac OS X 10.9+
- Xcode 6.1

##Integration
## Integration

You can use [Carthage](https://github.com/Carthage/Carthage) to install `SwiftyJSON` by adding
`github "SwiftyJSON/SwiftyJSON" >= 2.1.2` to your `Cartfile`
Expand All @@ -96,15 +96,15 @@ CocoaPods is now supported for Swift yet. But to use this library in your projec

## Usage

####Initialization
#### Initialization
```swift
let json = JSON(data: dataFromNetworking)
```
```swift
let json = JSON(jsonObject)
```

####Subscript
#### Subscript
```swift
//With a int from JSON supposed to an Array
let name = json[0].double
Expand All @@ -130,7 +130,7 @@ let name = json[1]["list"][2]["name"].string
//With a Hard Way
let name = json[[1,"list",2,"name"]].string
```
####Loop
#### Loop
```swift
//If json is .Dictionary
for (key: String, subJson: JSON) in json {
Expand All @@ -145,7 +145,7 @@ for (index: String, subJson: JSON) in json {
//Do something you want
}
```
####Error
#### Error
Use subscript to get/set value in Array or Dicitonary

* If json is an array, the app may crash with "index out-of-bounds."
Expand Down Expand Up @@ -187,7 +187,7 @@ let name = json["name"].string {
}
```

####Optional getter
#### Optional getter
```swift
//NSNumber
if let id = json["user"]["favourites_count"].number {
Expand Down Expand Up @@ -225,7 +225,7 @@ if let id = json["user"]["id"].int {
}
...
```
####Non-optional getter
#### Non-optional getter
Non-optional getter is named `xxxValue`
```swift
//If not a Number or nil, return 0
Expand All @@ -244,7 +244,7 @@ let list: Array<JSON> = json["list"].arrayValue
let user: Dictionary<String, JSON> = json["user"].dictionaryValue
```

####Setter
#### Setter
```swift
json["name"] = JSON("new-name")
json[0] = JSON(1)
Expand All @@ -257,7 +257,7 @@ json.array = [1,2,3,4]
json.dictionary = ["name":"Jack", "age":25]
```

####Raw object
#### Raw object
```swift
let jsonObject: AnyObject = json.object
```
Expand All @@ -276,7 +276,7 @@ if let string = json.rawString() {
//Do something you want
}
```
####Literal convertibles
#### Literal convertibles
More info about the literal convertibles: [Swift Literal Convertibles](http://nshipster.com/swift-literal-convertible/)
```swift
//StringLiteralConvertible
Expand Down Expand Up @@ -329,7 +329,7 @@ json["list",3,"what"] = "that"
let path = ["list",3,"what"]
json[path] = "that"
```
##Work with Alamofire
## Work with Alamofire

SwiftyJSON nicely wraps the result of the Alamofire JSON response handler:
```swift
Expand Down