Skip to content

Commit

Permalink
Merge pull request #570 from Hearst-DD/swift-3
Browse files Browse the repository at this point in the history
Swift 3 support
  • Loading branch information
tristanhimmelman committed Sep 14, 2016
2 parents 9617cda + 6ba230a commit 3ad489f
Show file tree
Hide file tree
Showing 39 changed files with 891 additions and 852 deletions.
32 changes: 15 additions & 17 deletions .travis.yml
@@ -1,5 +1,5 @@
language: objective-c
osx_image: xcode7.3
osx_image: xcode8

env:
global:
Expand All @@ -10,22 +10,20 @@ env:
- OSX_FRAMEWORK_SCHEME="ObjectMapper-Mac"
- TVOS_FRAMEWORK_SCHEME="ObjectMapper-tvOS"
- WATCHOS_FRAMEWORK_SCHEME="ObjectMapper-watchOS"
- IOS_SDK=iphonesimulator9.3
- OSX_SDK=macosx10.11
- TVOS_SDK=appletvsimulator9.2
- WATCHOS_SDK=watchsimulator2.2
- IOS_SDK=iphonesimulator10.0
- OSX_SDK=macosx10.12
- TVOS_SDK=appletvsimulator10.0
- WATCHOS_SDK=watchsimulator3.0
matrix:
- DESTINATION="OS=8.1,name=iPhone 4S" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="YES"
- DESTINATION="OS=8.2,name=iPhone 5" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="YES"
- DESTINATION="OS=8.3,name=iPhone 5S" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="YES"
- DESTINATION="OS=8.4,name=iPhone 6" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="YES"
- DESTINATION="OS=9.0,name=iPhone 6 Plus" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="YES"
- DESTINATION="OS=9.1,name=iPhone 6S" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="YES"
- DESTINATION="OS=9.2,name=iPhone 6S Plus" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="YES"
- DESTINATION="OS=9.3,name=iPhone 6S Plus" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="YES"
- DESTINATION="arch=x86_64" SCHEME="$OSX_FRAMEWORK_SCHEME" SDK="$OSX_SDK" RUN_TESTS="YES"
- DESTINATION="OS=9.2,name=Apple TV 1080p" SCHEME="$TVOS_FRAMEWORK_SCHEME" SDK="$TVOS_SDK" RUN_TESTS="YES"
- DESTINATION="OS=2.2,name=Apple Watch - 38mm" SCHEME="$WATCHOS_FRAMEWORK_SCHEME" SDK="$WATCHOS_SDK" RUN_TESTS="NO"
- DESTINATION="OS=10.0,name=iPhone 5" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="YES"
- DESTINATION="OS=10.0,name=iPhone 5S" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="YES"
- DESTINATION="OS=10.0,name=iPhone 6" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="YES"
- DESTINATION="OS=10.0,name=iPhone 6 Plus" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="YES"
- DESTINATION="OS=10.0,name=iPhone 6S" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="YES"
- DESTINATION="OS=10.0,name=iPhone 6S Plus" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="YES"
- DESTINATION="arch=x86_64" SCHEME="$OSX_FRAMEWORK_SCHEME" SDK="$OSX_SDK" RUN_TESTS="YES"
- DESTINATION="OS=10.0,name=Apple TV 1080p" SCHEME="$TVOS_FRAMEWORK_SCHEME" SDK="$TVOS_SDK" RUN_TESTS="YES"
- DESTINATION="OS=3.0,name=Apple Watch - 38mm" SCHEME="$WATCHOS_FRAMEWORK_SCHEME" SDK="$WATCHOS_SDK" RUN_TESTS="NO"

before_install:
- gem install xcpretty --no-rdoc --no-ri --no-document --quiet
Expand All @@ -50,4 +48,4 @@ script:
fi

notifications:
email: false
email: false
72 changes: 39 additions & 33 deletions ObjectMapper.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Expand Up @@ -9,11 +9,11 @@
},
"DVTSourceControlWorkspaceBlueprintIdentifierKey" : "A42BB54B-7EE9-41B5-B851-0FC3BB5A9811",
"DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : {
"58AAB0051E2B4EEDF1845A552012E6D0EBAD9127" : "ObjectMapper",
"58AAB0051E2B4EEDF1845A552012E6D0EBAD9127" : "ObjectMapper\/",
"95438028B10BBB846574013D29F154A00556A9D1" : "ObjectMapperCarthage\/Checkouts\/Nimble"
},
"DVTSourceControlWorkspaceBlueprintNameKey" : "ObjectMapper",
"DVTSourceControlWorkspaceBlueprintVersion" : 203,
"DVTSourceControlWorkspaceBlueprintVersion" : 204,
"DVTSourceControlWorkspaceBlueprintRelativePathToProjectKey" : "ObjectMapper.xcworkspace",
"DVTSourceControlWorkspaceBlueprintRemoteRepositoriesKey" : [
{
Expand Down
103 changes: 52 additions & 51 deletions ObjectMapper/Core/FromJSON.swift
Expand Up @@ -29,152 +29,153 @@
internal final class FromJSON {

/// Basic type
class func basicType<FieldType>(inout field: FieldType, object: FieldType?) {
class func basicType<FieldType>(_ field: inout FieldType, object: FieldType?) {
if let value = object {
field = value
}
}

/// optional basic type
class func optionalBasicType<FieldType>(inout field: FieldType?, object: FieldType?) {
class func optionalBasicType<FieldType>(_ field: inout FieldType?, object: FieldType?) {
field = object
}

/// Implicitly unwrapped optional basic type
class func optionalBasicType<FieldType>(inout field: FieldType!, object: FieldType?) {
class func optionalBasicType<FieldType>(_ field: inout FieldType!, object: FieldType?) {
field = object
}

/// Mappable object
class func object<N: BaseMappable>(inout field: N, map: Map) {
class func object<N: BaseMappable>(_ field: inout N, map: Map) {
if map.toObject {
Mapper(context: map.context).map(map.currentValue, toObject: field)
} else if let value: N = Mapper(context: map.context).map(map.currentValue) {
_ = Mapper(context: map.context).map(JSONObject: map.currentValue, toObject: field)
} else if let value: N = Mapper(context: map.context).map(JSONObject: map.currentValue) {
field = value
}
}

/// Optional Mappable Object
class func optionalObject<N: BaseMappable>(inout field: N?, map: Map) {
if let field = field where map.toObject && map.currentValue != nil {
Mapper(context: map.context).map(map.currentValue, toObject: field)

class func optionalObject<N: BaseMappable>(_ field: inout N?, map: Map) {
if let field = field , map.toObject && map.currentValue != nil {
_ = Mapper(context: map.context).map(JSONObject: map.currentValue, toObject: field)
} else {
field = Mapper(context: map.context).map(map.currentValue)
field = Mapper(context: map.context).map(JSONObject: map.currentValue)
}
}

/// Implicitly unwrapped Optional Mappable Object
class func optionalObject<N: BaseMappable>(inout field: N!, map: Map) {
if let field = field where map.toObject && map.currentValue != nil {
Mapper(context: map.context).map(map.currentValue, toObject: field)
class func optionalObject<N: BaseMappable>(_ field: inout N!, map: Map) {
if let field = field , map.toObject && map.currentValue != nil {
_ = Mapper(context: map.context).map(JSONObject: map.currentValue, toObject: field)
} else {
field = Mapper(context: map.context).map(map.currentValue)
field = Mapper(context: map.context).map(JSONObject: map.currentValue)
}
}

/// mappable object array
class func objectArray<N: BaseMappable>(inout field: Array<N>, map: Map) {
if let objects = Mapper<N>(context: map.context).mapArray(map.currentValue) {
class func objectArray<N: BaseMappable>(_ field: inout Array<N>, map: Map) {
if let objects = Mapper<N>(context: map.context).mapArray(JSONObject: map.currentValue) {
field = objects
}
}

/// optional mappable object array
class func optionalObjectArray<N: BaseMappable>(inout field: Array<N>?, map: Map) {
if let objects: Array<N> = Mapper(context: map.context).mapArray(map.currentValue) {

class func optionalObjectArray<N: BaseMappable>(_ field: inout Array<N>?, map: Map) {
if let objects: Array<N> = Mapper(context: map.context).mapArray(JSONObject: map.currentValue) {
field = objects
} else {
field = nil
}
}

/// Implicitly unwrapped optional mappable object array
class func optionalObjectArray<N: BaseMappable>(inout field: Array<N>!, map: Map) {
if let objects: Array<N> = Mapper(context: map.context).mapArray(map.currentValue) {
class func optionalObjectArray<N: BaseMappable>(_ field: inout Array<N>!, map: Map) {
if let objects: Array<N> = Mapper(context: map.context).mapArray(JSONObject: map.currentValue) {
field = objects
} else {
field = nil
}
}

/// mappable object array
class func twoDimensionalObjectArray<N: BaseMappable>(inout field: Array<Array<N>>, map: Map) {
if let objects = Mapper<N>(context: map.context).mapArrayOfArrays(map.currentValue) {
class func twoDimensionalObjectArray<N: BaseMappable>(_ field: inout Array<Array<N>>, map: Map) {
if let objects = Mapper<N>(context: map.context).mapArrayOfArrays(JSONObject: map.currentValue) {
field = objects
}
}

/// optional mappable 2 dimentional object array
class func optionalTwoDimensionalObjectArray<N: BaseMappable>(inout field: Array<Array<N>>?, map: Map) {
field = Mapper(context: map.context).mapArrayOfArrays(map.currentValue)
class func optionalTwoDimensionalObjectArray<N: BaseMappable>(_ field: inout Array<Array<N>>?, map: Map) {
field = Mapper(context: map.context).mapArrayOfArrays(JSONObject: map.currentValue)
}

/// Implicitly unwrapped optional 2 dimentional mappable object array
class func optionalTwoDimensionalObjectArray<N: BaseMappable>(inout field: Array<Array<N>>!, map: Map) {
field = Mapper(context: map.context).mapArrayOfArrays(map.currentValue)
class func optionalTwoDimensionalObjectArray<N: BaseMappable>(_ field: inout Array<Array<N>>!, map: Map) {
field = Mapper(context: map.context).mapArrayOfArrays(JSONObject: map.currentValue)
}

/// Dctionary containing Mappable objects
class func objectDictionary<N: BaseMappable>(inout field: Dictionary<String, N>, map: Map) {
class func objectDictionary<N: BaseMappable>(_ field: inout Dictionary<String, N>, map: Map) {
if map.toObject {
Mapper<N>(context: map.context).mapDictionary(map.currentValue, toDictionary: field)
_ = Mapper<N>(context: map.context).mapDictionary(JSONObject: map.currentValue, toDictionary: field)
} else {
if let objects = Mapper<N>(context: map.context).mapDictionary(map.currentValue) {
if let objects = Mapper<N>(context: map.context).mapDictionary(JSONObject: map.currentValue) {
field = objects
}
}
}

/// Optional dictionary containing Mappable objects
class func optionalObjectDictionary<N: BaseMappable>(inout field: Dictionary<String, N>?, map: Map) {
if let field = field where map.toObject && map.currentValue != nil {
Mapper(context: map.context).mapDictionary(map.currentValue, toDictionary: field)
class func optionalObjectDictionary<N: BaseMappable>(_ field: inout Dictionary<String, N>?, map: Map) {
if let field = field , map.toObject && map.currentValue != nil {
_ = Mapper(context: map.context).mapDictionary(JSONObject: map.currentValue, toDictionary: field)
} else {
field = Mapper(context: map.context).mapDictionary(map.currentValue)
field = Mapper(context: map.context).mapDictionary(JSONObject: map.currentValue)
}
}

/// Implicitly unwrapped Dictionary containing Mappable objects
class func optionalObjectDictionary<N: BaseMappable>(inout field: Dictionary<String, N>!, map: Map) {
if let field = field where map.toObject && map.currentValue != nil {
Mapper(context: map.context).mapDictionary(map.currentValue, toDictionary: field)
class func optionalObjectDictionary<N: BaseMappable>(_ field: inout Dictionary<String, N>!, map: Map) {
if let field = field , map.toObject && map.currentValue != nil {
_ = Mapper(context: map.context).mapDictionary(JSONObject: map.currentValue, toDictionary: field)
} else {
field = Mapper(context: map.context).mapDictionary(map.currentValue)
field = Mapper(context: map.context).mapDictionary(JSONObject: map.currentValue)
}
}

/// Dictionary containing Array of Mappable objects
class func objectDictionaryOfArrays<N: BaseMappable>(inout field: Dictionary<String, [N]>, map: Map) {
if let objects = Mapper<N>(context: map.context).mapDictionaryOfArrays(map.currentValue) {
class func objectDictionaryOfArrays<N: BaseMappable>(_ field: inout Dictionary<String, [N]>, map: Map) {
if let objects = Mapper<N>(context: map.context).mapDictionaryOfArrays(JSONObject: map.currentValue) {
field = objects
}
}

/// Optional Dictionary containing Array of Mappable objects
class func optionalObjectDictionaryOfArrays<N: BaseMappable>(inout field: Dictionary<String, [N]>?, map: Map) {
field = Mapper<N>(context: map.context).mapDictionaryOfArrays(map.currentValue)
class func optionalObjectDictionaryOfArrays<N: BaseMappable>(_ field: inout Dictionary<String, [N]>?, map: Map) {
field = Mapper<N>(context: map.context).mapDictionaryOfArrays(JSONObject: map.currentValue)
}

/// Implicitly unwrapped Dictionary containing Array of Mappable objects
class func optionalObjectDictionaryOfArrays<N: BaseMappable>(inout field: Dictionary<String, [N]>!, map: Map) {
field = Mapper<N>(context: map.context).mapDictionaryOfArrays(map.currentValue)
class func optionalObjectDictionaryOfArrays<N: BaseMappable>(_ field: inout Dictionary<String, [N]>!, map: Map) {
field = Mapper<N>(context: map.context).mapDictionaryOfArrays(JSONObject: map.currentValue)
}

/// mappable object Set
class func objectSet<N: BaseMappable>(inout field: Set<N>, map: Map) {
if let objects = Mapper<N>(context: map.context).mapSet(map.currentValue) {
class func objectSet<N: BaseMappable>(_ field: inout Set<N>, map: Map) {
if let objects = Mapper<N>(context: map.context).mapSet(JSONObject: map.currentValue) {
field = objects
}
}

/// optional mappable object array
class func optionalObjectSet<N: BaseMappable>(inout field: Set<N>?, map: Map) {
field = Mapper(context: map.context).mapSet(map.currentValue)
class func optionalObjectSet<N: BaseMappable>(_ field: inout Set<N>?, map: Map) {
field = Mapper(context: map.context).mapSet(JSONObject: map.currentValue)
}

/// Implicitly unwrapped optional mappable object array
class func optionalObjectSet<N: BaseMappable>(inout field: Set<N>!, map: Map) {
field = Mapper(context: map.context).mapSet(map.currentValue)
}

class func optionalObjectSet<N: BaseMappable>(_ field: inout Set<N>!, map: Map) {
field = Mapper(context: map.context).mapSet(JSONObject: map.currentValue)
}
}
38 changes: 19 additions & 19 deletions ObjectMapper/Core/Map.swift
Expand Up @@ -38,9 +38,9 @@ public protocol MapContext {
public final class Map {
public let mappingType: MappingType

public internal(set) var JSONDictionary: [String: AnyObject] = [:]
public internal(set) var JSON: [String: Any] = [:]
public internal(set) var isKeyPresent = false
public var currentValue: AnyObject?
public var currentValue: Any?
public var context: MapContext?
var currentKey: String?
var keyIsNested = false
Expand All @@ -50,9 +50,9 @@ public final class Map {
/// Counter for failing cases of deserializing values to `let` properties.
private var failedCount: Int = 0

public init(mappingType: MappingType, JSONDictionary: [String: AnyObject], toObject: Bool = false, context: MapContext? = nil) {
public init(mappingType: MappingType, JSON: [String: Any], toObject: Bool = false, context: MapContext? = nil) {
self.mappingType = mappingType
self.JSONDictionary = JSONDictionary
self.JSON = JSON
self.toObject = toObject
self.context = context
}
Expand All @@ -61,7 +61,7 @@ public final class Map {
/// The Key paramater can be a period separated string (ex. "distance.value") to access sub objects.
public subscript(key: String) -> Map {
// save key and value associated to it
let nested = key.containsString(".")
let nested = key.contains(".")
return self[key, nested: nested, ignoreNil: false]
}

Expand All @@ -70,7 +70,7 @@ public final class Map {
}

public subscript(key: String, ignoreNil ignoreNil: Bool) -> Map {
let nested = key.containsString(".")
let nested = key.contains(".")
return self[key, nested: nested, ignoreNil: ignoreNil]
}

Expand All @@ -82,13 +82,13 @@ public final class Map {
// check if a value exists for the current key
// do this pre-check for performance reasons
if nested == false {
let object = JSONDictionary[key]
let object = JSON[key]
let isNSNull = object is NSNull
isKeyPresent = isNSNull ? true : object != nil
currentValue = isNSNull ? nil : object
} else {
// break down the components of the key that are separated by .
(isKeyPresent, currentValue) = valueFor(ArraySlice(key.componentsSeparatedByString(".")), dictionary: JSONDictionary)
(isKeyPresent, currentValue) = valueFor(ArraySlice(key.components(separatedBy: ".")), dictionary: JSON)
}

// update isKeyPresent if ignoreNil is true
Expand All @@ -105,7 +105,7 @@ public final class Map {
return currentValue as? T
}

public func valueOr<T>(@autoclosure defaultValue: () -> T) -> T {
public func valueOr<T>( _ defaultValue: @autoclosure() -> T) -> T {
return value() ?? defaultValue()
}

Expand All @@ -119,9 +119,9 @@ public final class Map {
failedCount += 1

// Returns dummy memory as a proxy for type `T`
let pointer = UnsafeMutablePointer<T>.alloc(0)
pointer.dealloc(0)
return pointer.memory
let pointer = UnsafeMutablePointer<T>.allocate(capacity: 0)
pointer.deallocate(capacity: 0)
return pointer.pointee
}
}

Expand All @@ -132,7 +132,7 @@ public final class Map {
}

/// Fetch value from JSON dictionary, loop through keyPathComponents until we reach the desired object
private func valueFor(keyPathComponents: ArraySlice<String>, dictionary: [String: AnyObject]) -> (Bool, AnyObject?) {
private func valueFor(_ keyPathComponents: ArraySlice<String>, dictionary: [String: Any]) -> (Bool, Any?) {
// Implement it as a tail recursive function.
if keyPathComponents.isEmpty {
return (false, nil)
Expand All @@ -142,10 +142,10 @@ private func valueFor(keyPathComponents: ArraySlice<String>, dictionary: [String
let object = dictionary[keyPath]
if object is NSNull {
return (true, nil)
} else if let dict = object as? [String: AnyObject] where keyPathComponents.count > 1 {
} else if let dict = object as? [String: Any] , keyPathComponents.count > 1 {
let tail = keyPathComponents.dropFirst()
return valueFor(tail, dictionary: dict)
} else if let array = object as? [AnyObject] where keyPathComponents.count > 1 {
} else if let array = object as? [Any] , keyPathComponents.count > 1 {
let tail = keyPathComponents.dropFirst()
return valueFor(tail, array: array)
} else {
Expand All @@ -157,7 +157,7 @@ private func valueFor(keyPathComponents: ArraySlice<String>, dictionary: [String
}

/// Fetch value from JSON Array, loop through keyPathComponents them until we reach the desired object
private func valueFor(keyPathComponents: ArraySlice<String>, array: [AnyObject]) -> (Bool, AnyObject?) {
private func valueFor(_ keyPathComponents: ArraySlice<String>, array: [Any]) -> (Bool, Any?) {
// Implement it as a tail recursive function.

if keyPathComponents.isEmpty {
Expand All @@ -166,16 +166,16 @@ private func valueFor(keyPathComponents: ArraySlice<String>, array: [AnyObject])

//Try to convert keypath to Int as index
if let keyPath = keyPathComponents.first,
let index = Int(keyPath) where index >= 0 && index < array.count {
let index = Int(keyPath) , index >= 0 && index < array.count {

let object = array[index]

if object is NSNull {
return (true, nil)
} else if let array = object as? [AnyObject] where keyPathComponents.count > 1 {
} else if let array = object as? [Any] , keyPathComponents.count > 1 {
let tail = keyPathComponents.dropFirst()
return valueFor(tail, array: array)
} else if let dict = object as? [String: AnyObject] where keyPathComponents.count > 1 {
} else if let dict = object as? [String: Any] , keyPathComponents.count > 1 {
let tail = keyPathComponents.dropFirst()
return valueFor(tail, dictionary: dict)
} else {
Expand Down

0 comments on commit 3ad489f

Please sign in to comment.