Skip to content
Merged
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
36 changes: 18 additions & 18 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/)

[![Travis CI](https://travis-ci.org/SwiftyJSON/SwiftyJSON.svg?branch=master)](https://travis-ci.org/SwiftyJSON/SwiftyJSON)

Expand All @@ -19,7 +19,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 @@ -80,9 +80,9 @@ if let userName = json[999999]["wrong_key"]["wrong_name"].string {
- iOS 7.0+ / OS X 10.9+
- Xcode 7

##Integration
## Integration

####CocoaPods (iOS 8+, OS X 10.9+)
#### CocoaPods (iOS 8+, OS X 10.9+)
You can use [Cocoapods](http://cocoapods.org/) to install `SwiftyJSON`by adding it to your `Podfile`:
```ruby
platform :ios, '8.0'
Expand All @@ -94,13 +94,13 @@ end
```
Note that this requires CocoaPods version 36, and your iOS deployment target to be at least 8.0:

####Carthage (iOS 8+, OS X 10.9+)
#### Carthage (iOS 8+, OS X 10.9+)
You can use [Carthage](https://github.com/Carthage/Carthage) to install `SwiftyJSON` by adding it to your `Cartfile`:
```
github "SwiftyJSON/SwiftyJSON"
```

####Swift Package Manager
#### Swift Package Manager
You can use [The Swift Package Manager](https://swift.org/package-manager) to install `SwiftyJSON` by adding the proper description to your `Package.swift` file:
```swift
import PackageDescription
Expand All @@ -116,7 +116,7 @@ let package = Package(

Note that the [Swift Package Manager](https://swift.org/package-manager) is still in early design and development, for more infomation checkout its [GitHub Page](https://github.com/apple/swift-package-manager)

####Manually (iOS 7+, OS X 10.9+)
#### Manually (iOS 7+, OS X 10.9+)

To use this library in your project manually you may:

Expand All @@ -125,7 +125,7 @@ To use this library in your project manually you may:

## Usage

####Initialization
#### Initialization
```swift
import SwiftyJSON
```
Expand All @@ -141,7 +141,7 @@ if let dataFromString = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allow
}
```

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

If the JSON is:
Expand Down Expand Up @@ -226,7 +226,7 @@ if let name = json["name"].string {
}
```

####Optional getter
#### Optional getter
```swift
//NSNumber
if let id = json["user"]["favourites_count"].number {
Expand Down Expand Up @@ -264,7 +264,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 @@ -283,7 +283,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 @@ -296,7 +296,7 @@ json.arrayObject = [1,2,3,4]
json.dictionary = ["name":"Jack", "age":25]
```

####Raw object
#### Raw object
```swift
let jsonObject: AnyObject = json.object
```
Expand All @@ -315,13 +315,13 @@ if let string = json.rawString() {
//Do something you want
}
```
####Existance
#### Existance
```swift
//shows you whether value specified in JSON or not
if json["name"].isExists()
```

####Literal convertibles
#### Literal convertibles
For more info about literal convertibles: [Swift Literal Convertibles](http://nshipster.com/swift-literal-convertible/)
```swift
//StringLiteralConvertible
Expand Down Expand Up @@ -374,7 +374,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