-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changed for Swift 3 compliance #50
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,42 +12,42 @@ Supported objects: | |
|
||
import Foundation | ||
|
||
public class Serializable: NSObject { | ||
private class SortedDictionary: NSMutableDictionary { | ||
var dictionary = [String: AnyObject]() | ||
open class Serializable: NSObject { | ||
fileprivate class SortedDictionary : NSMutableDictionary { | ||
var _dictionary = [String: AnyObject]() | ||
|
||
override var count: Int { | ||
return dictionary.count | ||
override var count : Int { | ||
return _dictionary.count | ||
} | ||
|
||
override func keyEnumerator() -> NSEnumerator { | ||
let sortedKeys: NSArray = dictionary.keys.sort() | ||
let sortedKeys : NSArray = _dictionary.keys.sorted() as NSArray | ||
return sortedKeys.objectEnumerator() | ||
} | ||
|
||
override func setValue(value: AnyObject?, forKey key: String) { | ||
dictionary[key] = value | ||
override func setValue(_ value: Any?, forKey key: String) { | ||
_dictionary[key] = value as AnyObject? | ||
} | ||
|
||
override func objectForKey(aKey: AnyObject) -> AnyObject? { | ||
override func object(forKey aKey: Any) -> Any? { | ||
if let key = aKey as? String { | ||
return dictionary[key] | ||
return _dictionary[key] | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
|
||
public func formatKey(key: String) -> String { | ||
open func formatKey(_ key: String) -> String { | ||
return key | ||
} | ||
|
||
public func formatValue(value: AnyObject?, forKey: String) -> AnyObject? { | ||
open func formatValue(_ value: AnyObject?, forKey: String) -> AnyObject? { | ||
return value | ||
} | ||
|
||
func setValue(dictionary: NSDictionary, value: AnyObject?, forKey: String) { | ||
func setValue(_ dictionary: NSDictionary, value: AnyObject?, forKey: String) { | ||
dictionary.setValue(formatValue(value, forKey: forKey), forKey: formatKey(forKey)) | ||
} | ||
|
||
|
@@ -56,44 +56,54 @@ public class Serializable: NSObject { | |
|
||
- returns: The class as an NSDictionary. | ||
*/ | ||
public func toDictionary() -> NSDictionary { | ||
open func toDictionary() -> NSDictionary { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cyclomatic Complexity Violation: Function should have complexity 10 or less: currently complexity equals 17 (cyclomatic_complexity) |
||
let propertiesDictionary = SortedDictionary() | ||
let mirror = Mirror(reflecting: self) | ||
for (propName, propValue) in mirror.children { | ||
if let propValue: AnyObject = self.unwrap(propValue) as? AnyObject, propName = propName { | ||
if let propValue: AnyObject = self.unwrap(propValue) as AnyObject?, let propName = propName { | ||
if let serializablePropValue = propValue as? Serializable { | ||
setValue(propertiesDictionary, value: serializablePropValue.toDictionary(), forKey: propName) | ||
} else if let arrayPropValue = propValue as? [Serializable] { | ||
let subArray = arrayPropValue.toNSDictionaryArray() | ||
setValue(propertiesDictionary, value: subArray, forKey: propName) | ||
} else if propValue is Int || propValue is Double || propValue is Float || propValue is Bool { | ||
var subArray = [NSDictionary]() | ||
for item in arrayPropValue { | ||
subArray.append(item.toDictionary()) | ||
} | ||
setValue(propertiesDictionary, value: subArray as AnyObject?, forKey: propName) | ||
} else if propValue is Int || propValue is Double || propValue is Float { | ||
setValue(propertiesDictionary, value: propValue, forKey: propName) | ||
} else if let dataPropValue = propValue as? NSData { | ||
setValue(propertiesDictionary, | ||
value: dataPropValue.base64EncodedStringWithOptions(.Encoding64CharacterLineLength), forKey: propName) | ||
} else if let datePropValue = propValue as? NSDate { | ||
setValue(propertiesDictionary, value: datePropValue.timeIntervalSince1970, forKey: propName) | ||
} else if let dataPropValue = propValue as? Data { | ||
setValue(propertiesDictionary, value: dataPropValue.base64EncodedString(options: .lineLength64Characters) as AnyObject?, forKey: propName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line Length Violation: Line should be 150 characters or less: currently 158 characters (line_length) |
||
} else if let datePropValue = propValue as? Date { | ||
setValue(propertiesDictionary, value: datePropValue.timeIntervalSince1970 as AnyObject?, forKey: propName) | ||
} else if let boolPropValue = propValue as? Bool { | ||
setValue(propertiesDictionary, value: boolPropValue as AnyObject?, forKey: propName) | ||
} else { | ||
setValue(propertiesDictionary, value: propValue, forKey: propName) | ||
} | ||
} else if let propValue: Int8 = propValue as? Int8 { | ||
setValue(propertiesDictionary, value: NSNumber(char: propValue), forKey: propName!) | ||
} else if let propValue: Int16 = propValue as? Int16 { | ||
setValue(propertiesDictionary, value: NSNumber(short: propValue), forKey: propName!) | ||
} else if let propValue: Int32 = propValue as? Int32 { | ||
setValue(propertiesDictionary, value: NSNumber(int: propValue), forKey: propName!) | ||
} else if let propValue: Int64 = propValue as? Int64 { | ||
setValue(propertiesDictionary, value: NSNumber(longLong: propValue), forKey: propName!) | ||
} else if let propValue: UInt8 = propValue as? UInt8 { | ||
setValue(propertiesDictionary, value: NSNumber(unsignedChar: propValue), forKey: propName!) | ||
} else if let propValue: UInt16 = propValue as? UInt16 { | ||
setValue(propertiesDictionary, value: NSNumber(unsignedShort: propValue), forKey: propName!) | ||
} else if let propValue: UInt32 = propValue as? UInt32 { | ||
setValue(propertiesDictionary, value: NSNumber(unsignedInt: propValue), forKey: propName!) | ||
} else if let propValue: UInt64 = propValue as? UInt64 { | ||
setValue(propertiesDictionary, value: NSNumber(unsignedLongLong: propValue), forKey: propName!) | ||
} else if isEnum(propValue) { | ||
setValue(propertiesDictionary, value: "\(propValue)", forKey: propName!) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration. (statement_position) |
||
else if let propValue:Int8 = propValue as? Int8 { | ||
setValue(propertiesDictionary, value: NSNumber(value: propValue as Int8), forKey: propName!) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration. (statement_position) |
||
else if let propValue:Int16 = propValue as? Int16 { | ||
setValue(propertiesDictionary, value: NSNumber(value: propValue as Int16), forKey: propName!) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration. (statement_position) |
||
else if let propValue:Int32 = propValue as? Int32 { | ||
setValue(propertiesDictionary, value: NSNumber(value: propValue as Int32), forKey: propName!) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration. (statement_position) |
||
else if let propValue:Int64 = propValue as? Int64 { | ||
setValue(propertiesDictionary, value: NSNumber(value: propValue as Int64), forKey: propName!) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration. (statement_position) |
||
else if let propValue:UInt8 = propValue as? UInt8 { | ||
setValue(propertiesDictionary, value: NSNumber(value: propValue as UInt8), forKey: propName!) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration. (statement_position) |
||
else if let propValue:UInt16 = propValue as? UInt16 { | ||
setValue(propertiesDictionary, value: NSNumber(value: propValue as UInt16), forKey: propName!) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration. (statement_position) |
||
else if let propValue:UInt32 = propValue as? UInt32 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Colon Violation: Colons should be next to the identifier when specifying a type. (colon) |
||
setValue(propertiesDictionary, value: NSNumber(value: propValue as UInt32), forKey: propName!) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Statement Position Violation: Else and catch should be on the same line, one space after the previous declaration. (statement_position) |
||
else if let propValue:UInt64 = propValue as? UInt64 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Colon Violation: Colons should be next to the identifier when specifying a type. (colon) |
||
setValue(propertiesDictionary, value: NSNumber(value: propValue as UInt64), forKey: propName!) | ||
} | ||
} | ||
|
||
|
@@ -105,12 +115,12 @@ public class Serializable: NSObject { | |
|
||
- returns: The class as JSON, wrapped in NSData. | ||
*/ | ||
public func toJson(prettyPrinted: Bool = false) -> NSData? { | ||
open func toJson(_ prettyPrinted : Bool = false) -> Data? { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Colon Violation: Colons should be next to the identifier when specifying a type. (colon) |
||
let dictionary = self.toDictionary() | ||
|
||
if NSJSONSerialization.isValidJSONObject(dictionary) { | ||
if JSONSerialization.isValidJSONObject(dictionary) { | ||
do { | ||
let json = try NSJSONSerialization.dataWithJSONObject(dictionary, options: (prettyPrinted ? .PrettyPrinted: NSJSONWritingOptions())) | ||
let json = try JSONSerialization.data(withJSONObject: dictionary, options: (prettyPrinted ? .prettyPrinted : JSONSerialization.WritingOptions())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line Length Violation: Line should be 150 characters or less: currently 161 characters (line_length) |
||
return json | ||
} catch let error as NSError { | ||
print("ERROR: Unable to serialize json, error: \(error)") | ||
|
@@ -125,9 +135,9 @@ public class Serializable: NSObject { | |
|
||
- returns: The class as a JSON string. | ||
*/ | ||
public func toJsonString(prettyPrinted: Bool = false) -> String? { | ||
open func toJsonString(_ prettyPrinted : Bool = false) -> String? { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Colon Violation: Colons should be next to the identifier when specifying a type. (colon) |
||
if let jsonData = self.toJson(prettyPrinted) { | ||
return NSString(data: jsonData, encoding: NSUTF8StringEncoding) as String? | ||
return NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue) as String? | ||
} | ||
|
||
return nil | ||
|
@@ -139,9 +149,10 @@ public class Serializable: NSObject { | |
|
||
- returns: The unwrapped object. | ||
*/ | ||
func unwrap(any: Any) -> Any? { | ||
func unwrap(_ any:Any) -> Any? { | ||
|
||
let mi = Mirror(reflecting: any) | ||
if mi.displayStyle != .Optional { | ||
if mi.displayStyle != .optional { | ||
return any | ||
} | ||
|
||
|
@@ -151,6 +162,6 @@ public class Serializable: NSObject { | |
} | ||
|
||
func isEnum(any: Any) -> Bool { | ||
return Mirror(reflecting: any).displayStyle == .Enum | ||
return Mirror(reflecting: any).displayStyle == .enum | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable Name Violation: Variable name should only contain alphanumeric characters: '_dictionary' (variable_name)