Skip to content

Commit

Permalink
Add SequenceType methods
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeTangPL committed Oct 2, 2014
1 parent 54dc5d2 commit 919d4b6
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 12 deletions.
6 changes: 4 additions & 2 deletions Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
Expand All @@ -292,7 +293,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand All @@ -318,6 +319,7 @@
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = YES;
ENABLE_NS_ASSERTIONS = NO;
Expand All @@ -329,7 +331,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
Expand Down
5 changes: 3 additions & 2 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ class ViewController: UITableViewController {
switch self.json {
case .Sequence(let array):
cell.textLabel?.text = "\(row)"
cell.detailTextLabel?.text = JSON(object:array[row]).description
cell.detailTextLabel?.text = self.json.arrayValue.description
case .Mapping(let dictionary):
let key: AnyObject = (dictionary as NSDictionary).allKeys[row]
let value = self.json[key as String]
cell.textLabel?.text = "\(key)"
cell.detailTextLabel?.text = value.stringValue
cell.detailTextLabel?.text = value.description
default:
cell.textLabel?.text = ""
cell.detailTextLabel?.text = self.json.description
}

Expand Down
84 changes: 76 additions & 8 deletions Source/SwiftyJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,54 @@ extension JSON {
}
}

// MARK: - SequenceType
extension JSON: SequenceType{

public var count: Int {
get {
switch self {
case .Sequence(let array):
return array.count
case .Mapping(let dictionary):
return dictionary.count
default:
return 0
}
}
}
/**
When .Sequence return GeneratorOf<(index, JSON(object:element))>
When .Mapping return GeneratorOf<(key, JSON(value))>
*/
public func generate() -> GeneratorOf <(String, JSON)> {
switch self {
case .Sequence(let array):
var generate = array.generate()
var index: Int = -1
return GeneratorOf<(String, JSON)> {
if let element: AnyObject = generate.next() {
return ("\(index++)", JSON(object: element))
} else {
return nil
}
}
case .Mapping(let dictionary):
var generate = dictionary.generate()
return GeneratorOf<(String, JSON)> {
if let (key: String, value: AnyObject) = generate.next() {
return (key, JSON(object: value))
} else {
return nil
}
}
default:
return GeneratorOf<(String, JSON)> {
return nil
}
}
}
}

// MARK: - Subscript
extension JSON {

Expand Down Expand Up @@ -229,8 +277,7 @@ extension JSON: Printable, DebugPrintable {
// MARK: - Sequence: Array<JSON>
extension JSON {

//Optional array
//It's slow
//Optional Array<JSON>
public var array: Array<JSON>? {
get {
switch self {
Expand All @@ -246,20 +293,30 @@ extension JSON {
}
}

//Non-optional array
//It's slow
//Non-optional Array<JSON>
public var arrayValue: Array<JSON> {
get {
return self.array ?? []
}
}

//Optional Array<AnyObject>
public var arrayObjects: Array<AnyObject>? {
get {
switch self {
case .Sequence(let array):
return array
default:
return nil
}
}
}
}

// MARK: - Mapping: Dictionary<String, JSON>
extension JSON {

//Optional dictionary
//It's slow
//Optional Dictionary<String, JSON>
public var dictionary: Dictionary<String, JSON>? {
get {
switch self {
Expand All @@ -275,13 +332,24 @@ extension JSON {
}
}

//Non-optional dictionary
//It's slow
//Non-optional Dictionary<String, JSON>
public var dictionaryValue: Dictionary<String, JSON> {
get {
return self.dictionary ?? [:]
}
}

//Optional Dictionary<String, AnyObject>
public var dictionaryObjects: Dictionary<String, AnyObject>? {
get {
switch self {
case .Mapping(let dictionary):
return dictionary
default:
return nil
}
}
}
}

//MARK: - Scalar: Bool
Expand Down
17 changes: 17 additions & 0 deletions Tests/SwiftyJSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,23 @@ class SwiftyJSONTests: XCTestCase {
XCTAssert(user_dictionary_name_profile_image_url == NSURL(string: "http://a0.twimg.com/profile_images/730275945/oauth-dancer_normal.jpg"))
}

func testSequenceType() {
let json = JSON(data:self.testData)
XCTAssertEqual(json.count, 3)
for (_, aJson) in json {
XCTAssertEqual(aJson, json[0])
break
}

var index = 0
let keys = (json[1].dictionaryObjects! as NSDictionary).allKeys as Array<String>
for (aKey, aJson) in json[1] {
XCTAssertEqual(aKey, keys[index])
XCTAssertEqual(aJson, json[1][keys[index]])
break
}
}

func testJSONNumberCompare() {
XCTAssertEqual(JSON(object: 12376352.123321), JSON(object: 12376352.123321))
XCTAssertGreaterThan(JSON(object: 20.211), JSON(object: 20.112))
Expand Down

0 comments on commit 919d4b6

Please sign in to comment.