Skip to content

Commit

Permalink
Merge pull request SwiftyJSON#16 from tangplin/master
Browse files Browse the repository at this point in the history
add first and last in JSONValue, add string to double, int etc.
  • Loading branch information
aemaeth-me committed Jul 7, 2014
2 parents 3dca8c0 + 45611ec commit 88e05ba
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion SwiftyJSON/SwiftyJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ enum JSONValue {
case JObject(Dictionary<String,JSONValue>)
case JInvalid(NSError)


var string: String? {
switch self {
case .JString(let value):
Expand All @@ -57,15 +56,21 @@ enum JSONValue {
switch self {
case .JNumber(let value):
return value.doubleValue
case .JString(let value):
return (value as NSString).doubleValue
default:
return nil
}
}

var integer: Int? {
switch self {
case .JBool(let value):
return Int(value)
case .JNumber(let value):
return value.integerValue
case .JString(let value):
return (value as NSString).integerValue
default:
return nil
}
Expand All @@ -75,10 +80,15 @@ enum JSONValue {
switch self {
case .JBool(let value):
return value
case .JNumber(let value):
return value.boolValue
case .JString(let value):
return (value as NSString).boolValue
default:
return nil
}
}

var array: Array<JSONValue>? {
switch self {
case .JArray(let value):
Expand All @@ -87,6 +97,7 @@ enum JSONValue {
return nil
}
}

var object: Dictionary<String, JSONValue>? {
switch self {
case .JObject(let value):
Expand All @@ -96,6 +107,30 @@ enum JSONValue {
}
}

var first: JSONValue? {
switch self {
case .JArray(let jsonArray) where jsonArray.count > 0:
return jsonArray[0]
case .JObject(let jsonDictionary) where jsonDictionary.count > 0 :
let (_, value) = jsonDictionary[jsonDictionary.startIndex]
return value
default:
return nil
}
}

var last: JSONValue? {
switch self {
case .JArray(let jsonArray) where jsonArray.count > 0:
return jsonArray[jsonArray.count-1]
case .JObject(let jsonDictionary) where jsonDictionary.count > 0 :
let (_, value) = jsonDictionary[jsonDictionary.endIndex]
return value
default:
return nil
}
}

init (_ data: NSData!){
if let value = data{
var error:NSError? = nil
Expand Down

0 comments on commit 88e05ba

Please sign in to comment.