Skip to content

Commit

Permalink
Merge branch 'xcode6.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeTangPL committed Oct 19, 2014
2 parents 7df35e7 + 08a51a9 commit 156c319
Show file tree
Hide file tree
Showing 20 changed files with 761 additions and 376 deletions.
2 changes: 2 additions & 0 deletions Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
INFOPLIST_FILE = Example/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -353,6 +354,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
INFOPLIST_FILE = Example/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
2 changes: 1 addition & 1 deletion Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
let viewController = navigationController.topViewController as ViewController

if let file = NSBundle(forClass:AppDelegate.self).pathForResource("SwiftyJSONTests", ofType: "json") {
let data = NSData(contentsOfFile: file)
let data = NSData(contentsOfFile: file)!
let json = JSON(data:data)
viewController.json = json
} else {
Expand Down
52 changes: 52 additions & 0 deletions Example/Images.xcassets/LaunchImage.launchimage/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"images" : [
{
"orientation" : "portrait",
"idiom" : "iphone",
"extent" : "full-screen",
"minimum-system-version" : "7.0",
"scale" : "2x"
},
{
"extent" : "full-screen",
"idiom" : "iphone",
"subtype" : "retina4",
"filename" : "Default@2x.png",
"minimum-system-version" : "7.0",
"orientation" : "portrait",
"scale" : "2x"
},
{
"orientation" : "portrait",
"idiom" : "ipad",
"extent" : "full-screen",
"minimum-system-version" : "7.0",
"scale" : "1x"
},
{
"orientation" : "landscape",
"idiom" : "ipad",
"extent" : "full-screen",
"minimum-system-version" : "7.0",
"scale" : "1x"
},
{
"orientation" : "portrait",
"idiom" : "ipad",
"extent" : "full-screen",
"minimum-system-version" : "7.0",
"scale" : "2x"
},
{
"orientation" : "landscape",
"idiom" : "ipad",
"extent" : "full-screen",
"minimum-system-version" : "7.0",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 14 additions & 4 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ class ViewController: UITableViewController {

switch self.json.type {
case .Array:
cell.textLabel?.text = "\(row)"
cell.textLabel.text = "\(row)"
cell.detailTextLabel?.text = self.json.arrayValue.description
case .Dictionary:
let key: AnyObject = (self.json.object as NSDictionary).allKeys[row]
let value = self.json[key as String]
cell.textLabel?.text = "\(key)"
cell.textLabel.text = "\(key)"
cell.detailTextLabel?.text = value.description
default:
cell.textLabel?.text = ""
cell.textLabel.text = ""
cell.detailTextLabel?.text = self.json.description
}

Expand All @@ -67,7 +67,16 @@ class ViewController: UITableViewController {
// MARK: - Navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if let nextController = segue.destinationViewController.topViewController as? ViewController {

var object: AnyObject
switch UIDevice.currentDevice().systemVersion.compare("8.0.0", options: NSStringCompareOptions.NumericSearch) {
case .OrderedSame, .OrderedDescending:
object = segue.destinationViewController.topViewController
case .OrderedAscending:
object = segue.destinationViewController
}

if let nextController = object as? ViewController {

if let indexPath = self.tableView.indexPathForSelectedRow() {
var row = indexPath.row
Expand All @@ -85,6 +94,7 @@ class ViewController: UITableViewController {
print("")
}
nextController.json = nextJson
print(nextJson)
}
}
}
Expand Down
116 changes: 59 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,6 @@ But while dealing with things that naturally implicit about types such as JSON,

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)

```JSON

[
{
......
"text": "just another test",
......
"user": {
"name": "OAuth Dancer",
"favourites_count": 7,
"entities": {
"url": {
"urls": [
{
"expanded_url": null,
"url": "http://bit.ly/oauth-dancer",
"indices": [
0,
26
],
"display_url": null
}
]
}
......
},
"in_reply_to_screen_name": null,
},
......]

```

The code would look like this:

```swift
Expand Down Expand Up @@ -138,22 +106,43 @@ let json = JSON(jsonObject)

####Subscript
```swift
let name = json[0]["name"].stringValue
//With a int from JSON supposed to an Array
let name = json[0].double
```
```swift
//With a string from JSON supposed to an Dictionary
let name = json["name"].stringValue
```
```swift
//With an array like path to the element
let path = [1,"list",2,"name"]
let name = json[path].string
//Just the same
let name = json[1]["like"][2]["name"].string
```
```swift
//With a literal array to the element
let name = json[1,"list",2,"name"].string
//Just the same
let name = json[1]["like"][2]["name"].string
```
```swift
//With a Hard Way
let name = json[[1,"list",2,"name"]].string
```

####Loop
```swift
//If json is .Dictionary
for (key: String, subJson: JSON) in json {
//Do something you want
//Do something you want
}
```
*The first element always String even the JSON's object is Array*
```swift

//If json is .Array
//The `index` is 0..<json.count's string value
for (index: String, subJson: JSON) in json {
//Do something you want
//Do something you want
}
```
####Error
Expand All @@ -168,39 +157,35 @@ It will never happen in SwiftyJSON
```swift
let json = JSON(["name", "age"])
let name = json[999].string {
//Do something you want
//Do something you want
} else {
println(json[999].error) // "Array[999] is out of bounds"
println(json[999].error) // "Array[999] is out of bounds"
}
```
```swift
let json = JSON(["name":"Jack", "age": 25])
let name = json["address"].string {
//Do something you want
//Do something you want
} else {
println(json["address"].error) // "Dictionary["address"] does not exist"
println(json["address"].error) // "Dictionary["address"] does not exist"
}
```
```swift
let json = JSON(12345)
let age = json[0].string {
//Do something you want
//Do something you want
} else {
println(json[0]) // "Array[0] failure, It is not an array"
println(json[0].error) // "Array[0] failure, It is not an array"
println(json[0]) // "Array[0] failure, It is not an array"
println(json[0].error) // "Array[0] failure, It is not an array"
}

let name = json["name"].string {
//Do something you want
//Do something you want
} else {
println(json["name"]) // "Dictionary[\"name"] failure, It is not an dictionary"
println(json["name"].error) // "Dictionary[\"name"] failure, It is not an dictionary"
println(json["name"]) // "Dictionary[\"name"] failure, It is not an dictionary"
println(json["name"].error) // "Dictionary[\"name"] failure, It is not an dictionary"
}
```
```swift
json["name"] = JSON("new-name")
json["0"] = JSON("new-name")
```

####Optional getter
```swift
Expand Down Expand Up @@ -261,24 +246,34 @@ let user: Dictionary<String, JSON> = json["user"].dictionaryValue

####Setter
```swift
json["name"] = JSON("new-name")
json[0] = JSON(1)
```
```swift
json["id"].int = 1234567890
json["coordinate"].double = 8766.766
json["name"].string = "Jack"
json.array = [1,2,3,4]
json.dictionary = ["name":"Jack", "age":25]
```

####Raw object
```swift
let jsonObject: AnyObject = json.object
```
```swift
if let jsonObject: AnyObject = json.toRaw
if let jsonObject: AnyObject = json.rawValue
```
```swift
if let json = JSON.fromRaw(object) {
//object can be converted to JSON
} else {
//object can not be converted to JSON
//convert the JSON to raw NSData
if let data = json.rawData() {
//Do something you want
}
```
```swift
//convert the JSON to raw String
if let string = json.rawString() {
//Do something you want
}
```
####Literal convertibles
Expand Down Expand Up @@ -326,7 +321,14 @@ json["name"] = "Mike"
json["age"] = "25" //It's OK to set String
json["address"] = "L.A." // Add the "address": "L.A." in json
```

```swift
//Array & Dictionary
var json:JSON = ["name":"Jack", "age": 25, "list":["a","b","c",["what":"this"]]]
json["list"][3]["what"] = "that"
json["list",3,"what"] = "that"
let path = ["list",3,"what"]
json[path] = "that"
```
##Work with Alamofire

To use Alamofire and SwiftyJSON, try [Alamofire-SwiftyJSON](https://github.com/SwiftyJSON/Alamofire-SwiftyJSON).

0 comments on commit 156c319

Please sign in to comment.