You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -19,7 +19,7 @@ SwiftyJSON makes it easy to deal with JSON data in Swift.
19
19
- [Literal convertibles](#literal-convertibles)
20
20
1.[Work with Alamofire](#work-with-alamofire)
21
21
22
-
##Why is the typical JSON handling in Swift NOT good?
22
+
##Why is the typical JSON handling in Swift NOT good?
23
23
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.
24
24
25
25
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).
@@ -80,9 +80,9 @@ if let userName = json[999999]["wrong_key"]["wrong_name"].string {
80
80
- iOS 7.0+ / OS X 10.9+
81
81
- Xcode 7
82
82
83
-
##Integration
83
+
##Integration
84
84
85
-
####CocoaPods (iOS 8+, OS X 10.9+)
85
+
####CocoaPods (iOS 8+, OS X 10.9+)
86
86
You can use [Cocoapods](http://cocoapods.org/) to install `SwiftyJSON`by adding it to your `Podfile`:
87
87
```ruby
88
88
platform :ios, '8.0'
@@ -94,13 +94,13 @@ end
94
94
```
95
95
Note that this requires CocoaPods version 36, and your iOS deployment target to be at least 8.0:
96
96
97
-
####Carthage (iOS 8+, OS X 10.9+)
97
+
####Carthage (iOS 8+, OS X 10.9+)
98
98
You can use [Carthage](https://github.com/Carthage/Carthage) to install `SwiftyJSON` by adding it to your `Cartfile`:
99
99
```
100
100
github "SwiftyJSON/SwiftyJSON"
101
101
```
102
102
103
-
####Swift Package Manager
103
+
####Swift Package Manager
104
104
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:
105
105
```swift
106
106
importPackageDescription
@@ -116,7 +116,7 @@ let package = Package(
116
116
117
117
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)
118
118
119
-
####Manually (iOS 7+, OS X 10.9+)
119
+
####Manually (iOS 7+, OS X 10.9+)
120
120
121
121
To use this library in your project manually you may:
122
122
@@ -125,7 +125,7 @@ To use this library in your project manually you may:
125
125
126
126
## Usage
127
127
128
-
####Initialization
128
+
####Initialization
129
129
```swift
130
130
importSwiftyJSON
131
131
```
@@ -141,7 +141,7 @@ if let dataFromString = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allow
141
141
}
142
142
```
143
143
144
-
####Subscript
144
+
####Subscript
145
145
```swift
146
146
//Getting a double from a JSON Array
147
147
let name = json[0].double
@@ -168,7 +168,7 @@ let name = json[].string
168
168
let keys:[SubscriptType] = [1,"list",2,"name"]
169
169
let name = json[keys].string
170
170
```
171
-
####Loop
171
+
####Loop
172
172
```swift
173
173
//If json is .Dictionary
174
174
for (key,subJson):(String, JSON) in json {
@@ -183,7 +183,7 @@ for (index,subJson):(String, JSON) in json {
183
183
//Do something you want
184
184
}
185
185
```
186
-
####Error
186
+
####Error
187
187
Use a subscript to get/set a value in an Array or Dictionary
188
188
189
189
If the JSON is:
@@ -226,7 +226,7 @@ if let name = json["name"].string {
226
226
}
227
227
```
228
228
229
-
####Optional getter
229
+
####Optional getter
230
230
```swift
231
231
//NSNumber
232
232
iflet id = json["user"]["favourites_count"].number {
@@ -264,7 +264,7 @@ if let id = json["user"]["id"].int {
264
264
}
265
265
...
266
266
```
267
-
####Non-optional getter
267
+
####Non-optional getter
268
268
Non-optional getter is named `xxxValue`
269
269
```swift
270
270
//If not a Number or nil, return 0
@@ -283,7 +283,7 @@ let list: Array<JSON> = json["list"].arrayValue
283
283
let user: Dictionary<String, JSON> = json["user"].dictionaryValue
284
284
```
285
285
286
-
####Setter
286
+
####Setter
287
287
```swift
288
288
json["name"] =JSON("new-name")
289
289
json[0] =JSON(1)
@@ -296,7 +296,7 @@ json.arrayObject = [1,2,3,4]
296
296
json.dictionary= ["name":"Jack", "age":25]
297
297
```
298
298
299
-
####Raw object
299
+
####Raw object
300
300
```swift
301
301
let jsonObject: AnyObject= json.object
302
302
```
@@ -315,13 +315,13 @@ if let string = json.rawString() {
315
315
//Do something you want
316
316
}
317
317
```
318
-
####Existance
318
+
####Existance
319
319
```swift
320
320
//shows you whether value specified in JSON or not
321
321
if json["name"].isExists()
322
322
```
323
323
324
-
####Literal convertibles
324
+
####Literal convertibles
325
325
For more info about literal convertibles: [Swift Literal Convertibles](http://nshipster.com/swift-literal-convertible/)
0 commit comments