From 6aa99bbb0dc90e335dde797c72b854cc9db274a4 Mon Sep 17 00:00:00 2001 From: Daniel Firsht Date: Wed, 13 Apr 2016 12:45:48 -0500 Subject: [PATCH] IBM-Swift/Kitura#361 upgraded to compile with 4/12 binaries --- Source/LclJSONSerialization.swift | 20 ++--- Source/NSExtension.swift | 85 ++++++++++++++++++++ Source/SwiftyJSON.swift | 112 +++++++++++++-------------- SwiftyJSON.xcodeproj/project.pbxproj | 16 ++++ 4 files changed, 167 insertions(+), 66 deletions(-) create mode 100644 Source/NSExtension.swift diff --git a/Source/LclJSONSerialization.swift b/Source/LclJSONSerialization.swift index aee2c7c5..f964508d 100644 --- a/Source/LclJSONSerialization.swift +++ b/Source/LclJSONSerialization.swift @@ -29,9 +29,9 @@ public class LclJSONSerialization { private static let NULL = NSString(string: "null") - public class func isValidJSONObject(obj: Any) -> Bool { + public class func isValidJSONObject(_ obj: Any) -> Bool { // TODO: - revisit this once bridging story gets fully figured out - func isValidJSONObjectInternal(obj: Any) -> Bool { + func isValidJSONObjectInternal(_ obj: Any) -> Bool { // object is Swift.String or NSNull if obj is String || obj is Int || obj is Bool || obj is NSNull { return true @@ -97,13 +97,13 @@ public class LclJSONSerialization { return isValidJSONObjectInternal(obj) } - public class func dataWithJSONObject(obj: Any, options: NSJSONWritingOptions) throws -> NSData + public class func dataWithJSONObject(_ obj: Any, options: NSJSONWritingOptions) throws -> NSData { let result = NSMutableData() try writeJson(obj, options: options) { (str: NSString?) in if let str = str { - result.appendBytes(str.cStringUsingEncoding(NSUTF8StringEncoding), length: str.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)) + result.appendBytes(str.cStringUsingEncoding(NSUTF8StringEncoding)!, length: str.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)) } } @@ -111,7 +111,7 @@ public class LclJSONSerialization { } /* Helper function to enable writing to NSData as well as NSStream */ - private static func writeJson(obj: Any, options opt: NSJSONWritingOptions, writer: (NSString?) -> Void) throws { + private static func writeJson(_ obj: Any, options opt: NSJSONWritingOptions, writer: (NSString?) -> Void) throws { let prettyPrint = opt.rawValue & NSJSONWritingOptions.PrettyPrinted.rawValue != 0 let padding: NSString? = prettyPrint ? NSString(string: "") : nil @@ -119,7 +119,7 @@ public class LclJSONSerialization { } /* Write out a JSON value (simple value, object, or array) */ - private static func writeJsonValue(obj: Any, padding: NSString?, writer: (NSString?) -> Void) throws { + private static func writeJsonValue(_ obj: Any, padding: NSString?, writer: (NSString?) -> Void) throws { if obj is String { writer("\"") writer((obj as! String).bridge()) @@ -153,7 +153,7 @@ public class LclJSONSerialization { } /* Write out a dictionary as a JSON object */ - private static func writeJsonObject(pairs: Array, padding: NSString?, writer: (NSString?) -> Void) throws { + private static func writeJsonObject(_ pairs: Array, padding: NSString?, writer: (NSString?) -> Void) throws { let (nestedPadding, startOfLine, endOfLine) = setupPadding(padding) let nameValueSeparator = NSString(string: padding != nil ? ": " : ":") @@ -186,7 +186,7 @@ public class LclJSONSerialization { } /* Write out an array as a JSON Array */ - private static func writeJsonArray(obj: Array, padding: NSString?, writer: (NSString?) -> Void) throws { + private static func writeJsonArray(_ obj: Array, padding: NSString?, writer: (NSString?) -> Void) throws { let (nestedPadding, startOfLine, endOfLine) = setupPadding(padding) writer("[") @@ -209,7 +209,7 @@ public class LclJSONSerialization { Note: if padding is nil, then all padding, newlines etc., are suppressed */ - private static func setupPadding(padding: NSString?) -> (NSString?, NSString?, NSString?) { + private static func setupPadding(_ padding: NSString?) -> (NSString?, NSString?, NSString?) { let nestedPadding: NSString? let startOfLine: NSString? let endOfLine: NSString? @@ -229,7 +229,7 @@ public class LclJSONSerialization { return (nestedPadding, startOfLine, endOfLine) } - private static func createWriteError(reason: String) -> NSError { + private static func createWriteError(_ reason: String) -> NSError { let userInfo: [String: Any] = [NSLocalizedDescriptionKey: JSON_WRITE_ERROR, NSLocalizedFailureReasonErrorKey: reason] return NSError(domain: LclErrorDomain, code: 1, userInfo: userInfo) diff --git a/Source/NSExtension.swift b/Source/NSExtension.swift new file mode 100644 index 00000000..40be2a92 --- /dev/null +++ b/Source/NSExtension.swift @@ -0,0 +1,85 @@ +// +// NSExtension.swift +// SwiftyJSON +// +// Created by Daniel Firsht on 4/13/16. +// +// + +import Foundation + +#if os(Linux) +extension NSNumber { + convenience init(value: Bool) { + self.init(bool: value) + } + + convenience init(value: Double) { + self.init(double: value) + } + + convenience init(value: Float) { + self.init(float: value) + } + + convenience init(value: Int) { + self.init(integer: value) + } + + convenience init(value: UInt) { + self.init(unsignedLong: value) + } + + convenience init(value: Int8) { + self.init(char: value) + } + + convenience init(value: UInt8) { + self.init(unsignedChar: value) + } + + convenience init(value: Int16) { + self.init(short: value) + } + + convenience init(value: UInt16) { + self.init(unsignedShort: value) + } + + convenience init(value: Int32) { + self.init(int: value) + } + + convenience init(value: UInt32) { + self.init(unsignedInt: value) + } + + convenience init(value: Int64) { + self.init(longLong: value) + } + + convenience init(value: UInt64) { + self.init(unsignedLongLong: value) + } + + var intValue:Int {return self.longValue} + + var uintValue:UInt {return self.unsignedLongValue} + + var int8Value:Int8 {return self.charValue} + + var uint8Value:UInt8 {return self.unsignedCharValue} + + var int16Value:Int16 {return self.shortValue} + + var uint16Value:UInt16 {return self.unsignedShortValue} + + var int32Value:Int32 {return self.intValue} + + var uint32Value:UInt32 {return self.unsignedIntValue} + + var int64Value:Int64 {return self.longLongValue} + + var uint64Value:UInt64 {return self.unsignedLongLongValue} +} +#endif \ No newline at end of file diff --git a/Source/SwiftyJSON.swift b/Source/SwiftyJSON.swift index a056d9ac..8a283393 100644 --- a/Source/SwiftyJSON.swift +++ b/Source/SwiftyJSON.swift @@ -81,7 +81,7 @@ public struct JSON { self.init(object) } catch let aError as NSError { if error != nil { - error.pointee = aError + error?.pointee = aError } self.init(NSNull()) } @@ -97,7 +97,7 @@ public struct JSON { #if os(Linux) return string.bridge().dataUsingEncoding(NSUTF8StringEncoding).flatMap({JSON(data: $0)}) ?? JSON(NSNull()) #else - return string.data(usingEncoding: NSUTF8StringEncoding).flatMap({JSON(data: $0)}) ?? JSON(NSNull()) + return string.data(using: NSUTF8StringEncoding).flatMap({JSON(data: $0)}) ?? JSON(NSNull()) #endif } @@ -205,7 +205,7 @@ public struct JSON { } } - private func setObjectHelper(newValue: Any) -> (Type, Any) { + private func setObjectHelper(_ newValue: Any) -> (Type, Any) { var type: Type var value: Any @@ -272,7 +272,7 @@ public struct JSON { return (type, value) } - private func convertToKeyValues(pairs: [Any]) -> [(String, Any)] { + private func convertToKeyValues(_ pairs: [Any]) -> [(String, Any)] { var result = [(String, Any)]() for pair in pairs { let pairMirror = Mirror(reflecting: pair) @@ -351,7 +351,7 @@ public struct JSON { public static var nullJSON: JSON { get { return null } } public static var null: JSON { get { return JSON(NSNull() as AnyObject) } } #if os(Linux) - internal static func stringFromNumber(number: NSNumber) -> String { + internal static func stringFromNumber(_ number: NSNumber) -> String { let type = CFNumberGetType(unsafeBitCast(number, to: CFNumber.self)) switch(type) { case kCFNumberFloat32Type: @@ -1077,7 +1077,7 @@ extension JSON: Swift.BooleanType { } set { if let newValue = newValue { - self.object = NSNumber(bool: newValue) + self.object = NSNumber(value: newValue) } else { self.object = NSNull() } @@ -1113,7 +1113,7 @@ extension JSON: Swift.BooleanType { } } set { - self.object = NSNumber(bool: newValue) + self.object = NSNumber(value: newValue) } } #endif @@ -1208,15 +1208,15 @@ extension JSON { } #else let decimal = NSDecimalNumber(string: self.object as? String) - if decimal == NSDecimalNumber.notA() { // indicates parse error + if decimal == NSDecimalNumber.notANumber() { // indicates parse error return NSDecimalNumber.zero() } return decimal #endif case .Number, .Bool: - return self.object as? NSNumber ?? NSNumber(int: 0) + return self.object as? NSNumber ?? NSNumber(value: 0) default: - return NSNumber(double: 0.0) + return NSNumber(value: 0.0) } } set { @@ -1292,7 +1292,7 @@ extension JSON { } set { if let newValue = newValue { - self.object = NSNumber(double: newValue) + self.object = NSNumber(value: newValue) } else { self.object = NSNull() } @@ -1304,7 +1304,7 @@ extension JSON { return self.numberValue.doubleValue } set { - self.object = NSNumber(double: newValue) + self.object = NSNumber(value: newValue) } } @@ -1314,7 +1314,7 @@ extension JSON { } set { if let newValue = newValue { - self.object = NSNumber(float: newValue) + self.object = NSNumber(value: newValue) } else { self.object = NSNull() } @@ -1326,17 +1326,17 @@ extension JSON { return self.numberValue.floatValue } set { - self.object = NSNumber(float: newValue) + self.object = NSNumber(value: newValue) } } public var int: Int? { get { - return self.number?.longValue + return self.number?.intValue } set { if let newValue = newValue { - self.object = NSNumber(integer: newValue) + self.object = NSNumber(value: newValue) } else { self.object = NSNull() } @@ -1345,20 +1345,20 @@ extension JSON { public var intValue: Int { get { - return self.numberValue.integerValue + return self.numberValue.intValue } set { - self.object = NSNumber(integer: newValue) + self.object = NSNumber(value: newValue) } } public var uInt: UInt? { get { - return self.number?.unsignedLongValue + return self.number?.uintValue } set { if let newValue = newValue { - self.object = NSNumber(unsignedLong: newValue) + self.object = NSNumber(value: newValue) } else { self.object = NSNull() } @@ -1367,20 +1367,20 @@ extension JSON { public var uIntValue: UInt { get { - return self.numberValue.unsignedLongValue + return self.numberValue.uintValue } set { - self.object = NSNumber(unsignedLong: newValue) + self.object = NSNumber(value: newValue) } } public var int8: Int8? { get { - return self.number?.charValue + return self.number?.int8Value } set { if let newValue = newValue { - self.object = NSNumber(char: newValue) + self.object = NSNumber(value: newValue) } else { self.object = NSNull() } @@ -1389,20 +1389,20 @@ extension JSON { public var int8Value: Int8 { get { - return self.numberValue.charValue + return self.numberValue.int8Value } set { - self.object = NSNumber(char: newValue) + self.object = NSNumber(value: newValue) } } public var uInt8: UInt8? { get { - return self.number?.unsignedCharValue + return self.number?.uint8Value } set { if let newValue = newValue { - self.object = NSNumber(unsignedChar: newValue) + self.object = NSNumber(value: newValue) } else { self.object = NSNull() } @@ -1411,20 +1411,20 @@ extension JSON { public var uInt8Value: UInt8 { get { - return self.numberValue.unsignedCharValue + return self.numberValue.uint8Value } set { - self.object = NSNumber(unsignedChar: newValue) + self.object = NSNumber(value: newValue) } } public var int16: Int16? { get { - return self.number?.shortValue + return self.number?.int16Value } set { if let newValue = newValue { - self.object = NSNumber(short: newValue) + self.object = NSNumber(value: newValue) } else { self.object = NSNull() } @@ -1433,20 +1433,20 @@ extension JSON { public var int16Value: Int16 { get { - return self.numberValue.shortValue + return self.numberValue.int16Value } set { - self.object = NSNumber(short: newValue) + self.object = NSNumber(value: newValue) } } public var uInt16: UInt16? { get { - return self.number?.unsignedShortValue + return self.number?.uint16Value } set { if let newValue = newValue { - self.object = NSNumber(unsignedShort: newValue) + self.object = NSNumber(value: newValue) } else { self.object = NSNull() } @@ -1455,20 +1455,20 @@ extension JSON { public var uInt16Value: UInt16 { get { - return self.numberValue.unsignedShortValue + return self.numberValue.uint16Value } set { - self.object = NSNumber(unsignedShort: newValue) + self.object = NSNumber(value: newValue) } } public var int32: Int32? { get { - return self.number?.intValue + return self.number?.int32Value } set { if let newValue = newValue { - self.object = NSNumber(int: newValue) + self.object = NSNumber(value: newValue) } else { self.object = NSNull() } @@ -1477,20 +1477,20 @@ extension JSON { public var int32Value: Int32 { get { - return self.numberValue.intValue + return self.numberValue.int32Value } set { - self.object = NSNumber(int: newValue) + self.object = NSNumber(value: newValue) } } public var uInt32: UInt32? { get { - return self.number?.unsignedIntValue + return self.number?.uint32Value } set { if let newValue = newValue { - self.object = NSNumber(unsignedInt: newValue) + self.object = NSNumber(value: newValue) } else { self.object = NSNull() } @@ -1499,20 +1499,20 @@ extension JSON { public var uInt32Value: UInt32 { get { - return self.numberValue.unsignedIntValue + return self.numberValue.uint32Value } set { - self.object = NSNumber(unsignedInt: newValue) + self.object = NSNumber(value: newValue) } } public var int64: Int64? { get { - return self.number?.longLongValue + return self.number?.int64Value } set { if let newValue = newValue { - self.object = NSNumber(longLong: newValue) + self.object = NSNumber(value: newValue) } else { self.object = NSNull() } @@ -1521,20 +1521,20 @@ extension JSON { public var int64Value: Int64 { get { - return self.numberValue.longLongValue + return self.numberValue.int64Value } set { - self.object = NSNumber(longLong: newValue) + self.object = NSNumber(value: newValue) } } public var uInt64: UInt64? { get { - return self.number?.unsignedLongLongValue + return self.number?.uint64Value } set { if let newValue = newValue { - self.object = NSNumber(unsignedLongLong: newValue) + self.object = NSNumber(value: newValue) } else { self.object = NSNull() } @@ -1543,10 +1543,10 @@ extension JSON { public var uInt64Value: UInt64 { get { - return self.numberValue.unsignedLongLongValue + return self.numberValue.uint64Value } set { - self.object = NSNumber(unsignedLongLong: newValue) + self.object = NSNumber(value: newValue) } } } @@ -1662,8 +1662,8 @@ public func <(lhs: JSON, rhs: JSON) -> Bool { } } -private let trueNumber = NSNumber(bool: true) -private let falseNumber = NSNumber(bool: false) +private let trueNumber = NSNumber(value: true) +private let falseNumber = NSNumber(value: false) private let trueObjCType = String(trueNumber.objCType) private let falseObjCType = String(falseNumber.objCType) diff --git a/SwiftyJSON.xcodeproj/project.pbxproj b/SwiftyJSON.xcodeproj/project.pbxproj index c6cf5bba..66260d11 100644 --- a/SwiftyJSON.xcodeproj/project.pbxproj +++ b/SwiftyJSON.xcodeproj/project.pbxproj @@ -29,6 +29,13 @@ 9C459F041A9103C1008C9A41 /* DictionaryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8B66C8B19E51D6500540692 /* DictionaryTests.swift */; }; 9C459F051A9103C1008C9A41 /* ArrayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8B66C8D19E52F4200540692 /* ArrayTests.swift */; }; 9C7DFC661A9102BD005AA3F7 /* SwiftyJSON.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9C7DFC5B1A9102BD005AA3F7 /* SwiftyJSON.framework */; }; + A2FA767A1CBEAA7700F7BAA1 /* NSExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2FA76791CBEAA7700F7BAA1 /* NSExtension.swift */; }; + A2FA767B1CBEAA7700F7BAA1 /* NSExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2FA76791CBEAA7700F7BAA1 /* NSExtension.swift */; }; + A2FA767C1CBEAA7700F7BAA1 /* NSExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2FA76791CBEAA7700F7BAA1 /* NSExtension.swift */; }; + A2FA767D1CBEAA7700F7BAA1 /* NSExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2FA76791CBEAA7700F7BAA1 /* NSExtension.swift */; }; + A2FA767E1CBEAA7700F7BAA1 /* NSExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2FA76791CBEAA7700F7BAA1 /* NSExtension.swift */; }; + A2FA767F1CBEAA7700F7BAA1 /* NSExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2FA76791CBEAA7700F7BAA1 /* NSExtension.swift */; }; + A2FA76801CBEAA7700F7BAA1 /* NSExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2FA76791CBEAA7700F7BAA1 /* NSExtension.swift */; }; A819C49719E1A7DD00ADCC3D /* LiteralConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A819C49619E1A7DD00ADCC3D /* LiteralConvertibleTests.swift */; }; A819C49919E1B10300ADCC3D /* RawRepresentableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A819C49819E1B10300ADCC3D /* RawRepresentableTests.swift */; }; A819C49F19E2EE5B00ADCC3D /* SubscriptTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A819C49E19E2EE5B00ADCC3D /* SubscriptTests.swift */; }; @@ -99,6 +106,7 @@ 9C459EF61A9103B1008C9A41 /* Info-OSX.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Info-OSX.plist"; sourceTree = ""; }; 9C7DFC5B1A9102BD005AA3F7 /* SwiftyJSON.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftyJSON.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 9C7DFC651A9102BD005AA3F7 /* SwiftyJSON OSX Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "SwiftyJSON OSX Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; + A2FA76791CBEAA7700F7BAA1 /* NSExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSExtension.swift; sourceTree = ""; }; A819C49619E1A7DD00ADCC3D /* LiteralConvertibleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LiteralConvertibleTests.swift; sourceTree = ""; }; A819C49819E1B10300ADCC3D /* RawRepresentableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RawRepresentableTests.swift; sourceTree = ""; }; A819C49E19E2EE5B00ADCC3D /* SubscriptTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SubscriptTests.swift; sourceTree = ""; }; @@ -207,6 +215,7 @@ 2E4FEFE019575BE100351305 /* SwiftyJSON.h */, A8491E1D19CD6DAE00CCFAE6 /* SwiftyJSON.swift */, 2E4FEFDE19575BE100351305 /* Supporting Files */, + A2FA76791CBEAA7700F7BAA1 /* NSExtension.swift */, ); path = Source; sourceTree = ""; @@ -529,6 +538,7 @@ buildActionMask = 2147483647; files = ( 3F2EC3221C6B463C0070FCCE /* LclJSONSerialization.swift in Sources */, + A2FA767A1CBEAA7700F7BAA1 /* NSExtension.swift in Sources */, A8491E1E19CD6DAE00CCFAE6 /* SwiftyJSON.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -539,6 +549,7 @@ files = ( A87080E819E439DA00CDE086 /* NumberTests.swift in Sources */, A87080E419E3C2A600CDE086 /* SequenceTypeTests.swift in Sources */, + A2FA767B1CBEAA7700F7BAA1 /* NSExtension.swift in Sources */, A86BAA0E19EBC32B009EAAEB /* PerformanceTests.swift in Sources */, A819C49919E1B10300ADCC3D /* RawRepresentableTests.swift in Sources */, A819C49F19E2EE5B00ADCC3D /* SubscriptTests.swift in Sources */, @@ -557,6 +568,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + A2FA767F1CBEAA7700F7BAA1 /* NSExtension.swift in Sources */, 7236B4EE1BAC14150020529B /* SwiftyJSON.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -565,6 +577,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + A2FA767C1CBEAA7700F7BAA1 /* NSExtension.swift in Sources */, 9C459EF51A910361008C9A41 /* SwiftyJSON.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -575,6 +588,7 @@ files = ( 9C459EFB1A9103C1008C9A41 /* SequenceTypeTests.swift in Sources */, 9C459F001A9103C1008C9A41 /* ComparableTests.swift in Sources */, + A2FA767D1CBEAA7700F7BAA1 /* NSExtension.swift in Sources */, 9C459F021A9103C1008C9A41 /* NumberTests.swift in Sources */, 9C459EFF1A9103C1008C9A41 /* RawRepresentableTests.swift in Sources */, 9C459EFA1A9103C1008C9A41 /* BaseTests.swift in Sources */, @@ -595,6 +609,7 @@ files = ( A8580F801BCF69A000DA927B /* PerformanceTests.swift in Sources */, A8580F811BCF69A000DA927B /* BaseTests.swift in Sources */, + A2FA76801CBEAA7700F7BAA1 /* NSExtension.swift in Sources */, A8580F821BCF69A000DA927B /* SequenceTypeTests.swift in Sources */, A8580F831BCF69A000DA927B /* PrintableTests.swift in Sources */, A8580F841BCF69A000DA927B /* SubscriptTests.swift in Sources */, @@ -613,6 +628,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + A2FA767E1CBEAA7700F7BAA1 /* NSExtension.swift in Sources */, E4D7CCE01B9465A700EE7221 /* SwiftyJSON.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0;