Skip to content

Instantly share code, notes, and snippets.

@finder39
Last active August 29, 2015 14:13
Show Gist options
  • Save finder39/7b086dcf9a5314bd0e6b to your computer and use it in GitHub Desktop.
Save finder39/7b086dcf9a5314bd0e6b to your computer and use it in GitHub Desktop.
jsonDictionaryHelpers
import Foundation
func getBool(object:AnyObject?) -> Bool? {
if let temp = object as? Bool {
return temp
} else if let temp = object as? NSNumber {
return temp.boolValue
} else if let temp = object as? Int {
return Bool(temp)
} else if let temp = object as? String {
return (temp as NSString).boolValue
} else {
return nil
}
}
// TODO: Currently using getDateFormatter() from Constants singleton. This should be seperated out. Possibly added to ICNetworking singleton?
// Reason for this is because NSDateFormatter is expensive to alloc
func getDate(object:AnyObject?) -> NSDate? {
if let tempObject = getString(object) {
return ICNetworking.getDateFormatter().dateFromString(tempObject)
} else {
return nil
}
}
func getDictionary(object:AnyObject?) -> Dictionary<String, AnyObject>? {
return object as AnyObject? as? Dictionary<String, AnyObject>
}
func getString(object:AnyObject?) -> String? {
if let temp: AnyObject = object as AnyObject? {
if let temp2 = temp as? String {
return temp2
} else if let temp2 = temp as? Float {
// check if it is actually an int
if temp2 == Float(Int(temp2)) {
return String(Int(temp2))
} else {
return "\(temp2)"
}
} else if let temp2 = temp as? Int {
return String(temp2)
} else {
return nil
}
} else {
return nil
}
}
func getDouble(object:AnyObject?) -> Double? {
if let temp: AnyObject = object as AnyObject? {
if let temp2 = temp as? Double {
return temp2
} else if let temp2 = temp as? Int {
return Double(temp2)
} else if let temp2 = temp as? String {
return (temp2 as NSString).doubleValue
} else if let temp2 = temp as? NSNumber {
// NSNumber behaves strangely, 123.456 will become 123.456001281738
return Double(temp2)
}else {
return nil
}
} else {
return nil
}
}
func getFloat(object:AnyObject?) -> Float? {
if let temp: AnyObject = object as AnyObject? {
if let temp2 = temp as? Float {
return temp2
} else if let temp2 = temp as? Int {
return Float(temp2)
} else if let temp2 = temp as? String {
return (temp2 as NSString).floatValue
} else {
return nil
}
} else {
return nil
}
}
func getArray(object:AnyObject?) -> [AnyObject]? {
if let temp: [AnyObject] = object as [AnyObject]? {
return temp
} else {
return nil
}
}
import XCTest
class jsonDictionaryHelpersTests: XCTestCase {
func testGetString() {
// Test string
let stringTest = getString("apple")
if let stringTest = stringTest {
XCTAssertEqual(stringTest, "apple", "String does not remain a String")
} else {
XCTFail("String does not remain a String, returned as nil")
}
// Test Int
let intTest = getString(Int(123))
if let intTest = intTest {
XCTAssertEqual(intTest, "123", "Int is not casted to String")
} else {
XCTFail("Int is not casted to String, returned as nil")
}
// Float test
let floatTest = getString(Float(123.456))
if let floatTest = floatTest {
XCTAssertEqual(floatTest, "123.456", "Float is not casted to String")
} else {
XCTFail("Float is not casted to String, returned as nil")
}
// Float without decimal test
let floatWithoutDecimalTest = getString(Float(123))
if let floatWithoutDecimalTest = floatWithoutDecimalTest {
XCTAssertEqual(floatWithoutDecimalTest, "123", "Float is not casted to String")
} else {
XCTFail("Float is not casted to String, returned as nil")
}
// Double test
let doubleTest = getString(Double(123.456))
if let doubleTest = doubleTest {
XCTAssertEqual(doubleTest, "123.456", "Double is not casted to String")
} else {
XCTFail("Double is not casted to String, returned as nil")
}
// NSNumber test
let nsnumberTest = getString(NSNumber(float: 123.456))
if let nsnumberTest = nsnumberTest {
XCTAssertEqual(nsnumberTest, "123.456", "NSNumber is not casted to String")
} else {
XCTFail("NSNumber is not casted to String, returned as nil")
}
}
func testGetDouble() {
// Test double
let doubleTest = getDouble(Double(123.456))
if let doubleTest = doubleTest {
XCTAssertEqual(doubleTest, Double(123.456), "Double does not remain a Double")
} else {
XCTFail("Double does not remain a Double, returned as nil")
}
// Test Int
let intTest = getDouble(Int(123))
if let intTest = intTest {
XCTAssertEqual(intTest, Double(123), "Int is not casted to Double")
} else {
XCTFail("Int is not casted to Double, returned as nil")
}
// NSNumber test
// NSNumber behaves strangely, 123.456 will become 123.456001281738
/*let nsnumberTest = getDouble(NSNumber(float: 123.456))
if let nsnumberTest = nsnumberTest {
XCTAssertEqual(nsnumberTest, Double(123.456), "NSNumber is not casted to Double")
} else {
XCTFail("NSNumber is not casted to Double, returned as nil")
}
*/
}
func testGetDate() {
let stringTest = getDate("2014-12-01 09:54:38")
if let stringTest = stringTest {
XCTAssertEqual(stringTest, ICNetworking.getDateFormatter().dateFromString("2014-12-01 09:54:38")!, "String is not converted into NSDate object")
} else {
XCTFail("String is not converted into NSDate object, returned as nil")
}
}
func testGetBool() {
// Test string
let stringTest1 = getBool("1")
let stringTest0 = getBool("0")
let stringTestTrue = getBool("true")
let stringTestFalse = getBool("false")
let stringTestTRUE = getBool("TRUE")
let stringTestFALSE = getBool("FALSE")
XCTAssertNotNil(stringTest1)
XCTAssertEqual(stringTest1!, true)
XCTAssertNotNil(stringTest0)
XCTAssertEqual(stringTest0!, false)
XCTAssertNotNil(stringTestTrue)
XCTAssertEqual(stringTestTrue!, true)
XCTAssertNotNil(stringTestFalse)
XCTAssertEqual(stringTestFalse!, false)
XCTAssertNotNil(stringTestTRUE)
XCTAssertEqual(stringTestTRUE!, true)
XCTAssertNotNil(stringTestFALSE)
XCTAssertEqual(stringTestFALSE!, false)
// Test Int
let intTest1 = getBool(1)
let intTest0 = getBool(0)
XCTAssertNotNil(intTest1)
XCTAssertEqual(intTest1!, true)
XCTAssertNotNil(intTest0)
XCTAssertEqual(intTest0!, false)
// Float test
let floatTest1 = getBool(1.0)
let floatTest0 = getBool(0.0)
XCTAssertNotNil(floatTest1)
XCTAssertEqual(floatTest1!, true)
XCTAssertNotNil(floatTest0)
XCTAssertEqual(floatTest0!, false)
// NSNumber test
let numberTest1 = getBool(NSNumber(int: 1))
let numberTest0 = getBool(NSNumber(int: 0))
XCTAssertNotNil(numberTest1)
XCTAssertEqual(numberTest1!, true)
XCTAssertNotNil(numberTest0)
XCTAssertEqual(numberTest0!, false)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment