Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
Better number types. #8
Browse files Browse the repository at this point in the history
  • Loading branch information
calebd committed Nov 20, 2015
1 parent 8d61c0a commit 0a8306f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
7 changes: 4 additions & 3 deletions Alexander/Deprecated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ extension JSON {
return stringValue
}

@available(*, deprecated, message = "Use intValue instead.")
@available(*, deprecated, message = "Use integerValue instead.")
public var int: Int? {
return intValue
return integerValue
}

@available(*, deprecated, message = "Use doubleValue instead.")
public var double: Double? {
return object as? Double
return doubleValue
}

@available(*, deprecated, message = "Use boolValue instead.")
Expand Down
24 changes: 21 additions & 3 deletions Alexander/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

enum AlexanderError: ErrorType {
public enum Error: ErrorType {
case InvalidObject
}

Expand Down Expand Up @@ -53,10 +53,22 @@ public struct JSON {
return object as? String
}

public var intValue: Int? {
public var integerValue: Int? {
return object as? Int
}

public var unsignedIntegerValue: UInt? {
return object as? UInt
}

public var doubleValue: Double? {
return object as? Double
}

public var floatValue: Float? {
return object as? Float
}

public var boolValue: Bool? {
return object as? Bool
}
Expand Down Expand Up @@ -85,7 +97,7 @@ extension JSON {
if NSJSONSerialization.isValidJSONObject(object) {
return try NSJSONSerialization.dataWithJSONObject(object, options: options)
}
throw AlexanderError.InvalidObject
throw Error.InvalidObject
}
}

Expand All @@ -99,3 +111,9 @@ extension JSON: CustomDebugStringConvertible {
return "Invalid JSON."
}
}

extension JSON {
public var CGFloatValue: CGFloat? {
return object as? CGFloat
}
}
10 changes: 5 additions & 5 deletions Tests/AlexanderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ final class AlexanderTests: XCTestCase {

let JSON = Alexander.JSON(object: dictionary)

XCTAssertEqual(JSON["double"]?.double, 9823.212)
XCTAssertEqual(JSON["int"]?.intValue, 42)
XCTAssertEqual(JSON["double"]?.doubleValue, 9823.212)
XCTAssertEqual(JSON["int"]?.integerValue, 42)
XCTAssertEqual(JSON["string"]?.stringValue, "Caleb")
XCTAssertEqual(JSON["bool"]?.boolValue, true)

XCTAssertEqual(JSON["array"]?.array?.count, 2)
XCTAssertEqual(JSON["array"]?[0]?.double, 1234)
XCTAssertEqual(JSON["array"]?[1]?.double, 4.212)
XCTAssertEqual(JSON["array"]?[0]?.doubleValue, 1234)
XCTAssertEqual(JSON["array"]?[1]?.doubleValue, 4.212)

XCTAssertEqual(JSON["object"]?.dictionary?.count, 2)
XCTAssertEqual(JSON["object"]?["double"]?.double, 877.2323)
XCTAssertEqual(JSON["object"]?["double"]?.doubleValue, 877.2323)
XCTAssertEqual(JSON["object"]?["string"]?.stringValue, "Jon")

XCTAssertNil(JSON["null"])
Expand Down

0 comments on commit 0a8306f

Please sign in to comment.