diff --git a/JSON.xcodeproj/project.pbxproj b/JSON.xcodeproj/project.pbxproj index 1881953..f1a6963 100644 --- a/JSON.xcodeproj/project.pbxproj +++ b/JSON.xcodeproj/project.pbxproj @@ -8,12 +8,14 @@ /* Begin PBXBuildFile section */ 04AF95DC1EA5001A0002C72D /* JSON.h in Headers */ = {isa = PBXBuildFile; fileRef = 04AF95DA1EA5001A0002C72D /* JSON.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 04AF95E31EA500490002C72D /* JSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04AF95E21EA500490002C72D /* JSON.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ 04AF95D71EA5001A0002C72D /* JSON.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = JSON.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 04AF95DA1EA5001A0002C72D /* JSON.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JSON.h; sourceTree = ""; }; 04AF95DB1EA5001A0002C72D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 04AF95E21EA500490002C72D /* JSON.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSON.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -47,6 +49,7 @@ isa = PBXGroup; children = ( 04AF95DA1EA5001A0002C72D /* JSON.h */, + 04AF95E21EA500490002C72D /* JSON.swift */, 04AF95DB1EA5001A0002C72D /* Info.plist */, ); path = JSON; @@ -95,6 +98,7 @@ TargetAttributes = { 04AF95D61EA5001A0002C72D = { CreatedOnToolsVersion = 8.3.1; + LastSwiftMigration = 0830; ProvisioningStyle = Automatic; }; }; @@ -131,6 +135,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 04AF95E31EA500490002C72D /* JSON.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -241,6 +246,8 @@ 04AF95E01EA5001A0002C72D /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + APPLICATION_EXTENSION_API_ONLY = YES; + CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; @@ -248,10 +255,12 @@ DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = JSON/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.matrix.JSON; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_VERSION = 3.0; }; name = Debug; @@ -259,6 +268,8 @@ 04AF95E11EA5001A0002C72D /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + APPLICATION_EXTENSION_API_ONLY = YES; + CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; @@ -266,6 +277,7 @@ DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = JSON/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.matrix.JSON; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/JSON.xcodeproj/xcuserdata/kylefang.xcuserdatad/xcschemes/JSON.xcscheme b/JSON.xcodeproj/xcshareddata/xcschemes/JSON.xcscheme similarity index 52% rename from JSON.xcodeproj/xcuserdata/kylefang.xcuserdatad/xcschemes/JSON.xcscheme rename to JSON.xcodeproj/xcshareddata/xcschemes/JSON.xcscheme index 015fcc0..dae2e5b 100644 --- a/JSON.xcodeproj/xcuserdata/kylefang.xcuserdatad/xcschemes/JSON.xcscheme +++ b/JSON.xcodeproj/xcshareddata/xcschemes/JSON.xcscheme @@ -5,6 +5,22 @@ + + + + + + + + + + @@ -35,6 +60,15 @@ savedToolIdentifier = "" useCustomWorkingDirectory = "NO" debugDocumentVersioning = "YES"> + + + + diff --git a/JSON.xcodeproj/xcuserdata/kylefang.xcuserdatad/xcschemes/xcschememanagement.plist b/JSON.xcodeproj/xcuserdata/kylefang.xcuserdatad/xcschemes/xcschememanagement.plist index 10f8a19..4c2e90b 100644 --- a/JSON.xcodeproj/xcuserdata/kylefang.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/JSON.xcodeproj/xcuserdata/kylefang.xcuserdatad/xcschemes/xcschememanagement.plist @@ -4,7 +4,7 @@ SchemeUserState - JSON.xcscheme + JSON.xcscheme_^#shared#^_ orderHint 0 diff --git a/JSON/JSON.swift b/JSON/JSON.swift new file mode 100644 index 0000000..595e4a8 --- /dev/null +++ b/JSON/JSON.swift @@ -0,0 +1,122 @@ +// +// JSON.swift +// JSON +// +// Created by Zhigang Fang on 4/17/17. +// Copyright © 2017 matrix. All rights reserved. +// + +import Foundation +public struct JSON { + + var value: Optional + + public init(value: Optional) { + self.value = value + } + + public subscript(index: Int) -> JSON { + get { + return JSON(value: value.flatMap({ value in + return (value as? NSArray).flatMap({ + if $0.count > index && index >= 0 { + return $0[index] as? NSObject + } + return nil + }) + })) + } + set { + let newValueToSet = newValue.value ?? NSNull() + guard let array = self.value as? NSArray else { + self.value = NSMutableArray(object: newValueToSet) + return + } + let mutableArray: NSMutableArray + if let array = array as? NSMutableArray { + mutableArray = array + } else { + mutableArray = array.mutableCopy() as! NSMutableArray + } + guard (mutableArray.count > index && index >= 0) else { return } + mutableArray.replaceObject(at: index, with: newValueToSet) + self.value = mutableArray + } + } + + public subscript(key: String) -> JSON { + get { + return JSON(value: value.flatMap({ value in + return (value as? NSDictionary).flatMap({$0[key] as? NSObject}) + })) + } + set { + let newValueToSet = newValue.value ?? NSNull() + guard let dictionary = self.value as? NSDictionary else { + self.value = NSMutableDictionary(object: newValueToSet, forKey: key as NSString) + return + } + let mutableDictionary: NSMutableDictionary + if let dictionary = dictionary as? NSMutableDictionary { + mutableDictionary = dictionary + } else { + mutableDictionary = dictionary.mutableCopy() as! NSMutableDictionary + } + mutableDictionary.setValue(newValueToSet, forKeyPath: key as String) + self.value = mutableDictionary + } + } + + public var int: Int? { + get { + return self.value as? Int + } + set { + self.value = newValue.flatMap({ $0 as NSObject}) + } + } + + public var float: Float? { + get { + return self.value as? Float + } + set { + self.value = newValue.flatMap({ $0 as NSObject }) + } + } + + public var string: String? { + get { + return self.value as? String + } + set { + self.value = newValue.flatMap({ $0 as NSObject }) + } + } + + public var bool: Bool? { + get { + return self.value as? Bool + } + set { + self.value = newValue.flatMap({ $0 as NSObject }) + } + } + + public var array: Array? { + return (self.value as? NSArray).map({ array in + array.map({JSON(value: $0 as? NSObject)}) + }) + } + + public var dictionary: Array<(String, JSON)>? { + return (self.value as? NSDictionary).map({ dictionary in + dictionary.flatMap({ key, value in + guard let key = key as? String else { return nil } + + return (key, JSON(value: value as? NSObject)) + }) + }) + } + +}