Skip to content

Commit

Permalink
Merge pull request #1 from kerrmarin/master
Browse files Browse the repository at this point in the history
Update to use Swift 3
  • Loading branch information
deanWombourne authored Oct 19, 2016
2 parents 0969348 + 6d48890 commit 362fe76
Show file tree
Hide file tree
Showing 68 changed files with 9,381 additions and 8,436 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0
17 changes: 11 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
\# references:
# references:
# * http://www.objc.io/issue-6/travis-ci.html
# * https://github.com/supermarin/xcpretty#usage

language: objective-c
osx_image: xcode7.3
osx_image: xcode8

before_install:
- gem install cocoapods --pre --no-rdoc --no-ri --no-document --quiet
- pod repo update --silent
- pod --version

env:
- DESTINATION="platform=iOS Simulator,name=iPhone 6,OS=9.1"
- DESTINATION="platform=iOS Simulator,name=iPhone 6,OS=8.4"
- DESTINATION="platform=iOS Simulator,name=iPhone 6,OS=10.0"
- DESTINATION="platform=iOS Simulator,name=iPad Air,OS=9.1"
- DESTINATION="platform=iOS Simulator,name=iPad Air,OS=8.4"
- DESTINATION="platform=iOS Simulator,name=iPad Air,OS=10.0"

script:
- set -o pipefail && xcodebuild test -destination=$DESTINATION -workspace Example/Alamofire-Decodable.xcworkspace -scheme Alamofire-Decodable-Example -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO | xcpretty
- pod lib lint
- set -o pipefail && xcodebuild test -destination "$DESTINATION" -workspace Example/Alamofire-Decodable.xcworkspace -scheme Alamofire-Decodable-Example -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO | xcpretty
- pod lib lint
6 changes: 3 additions & 3 deletions Alamofire-Decodable.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ This pod adds the `responseDecodable` method to Alamofire's Request object to re
s.source = { :git => 'https://github.com/deanWombourne/Alamofire-Decodable.git', :tag => 'v' + s.version.to_s }
s.social_media_url = 'https://twitter.com/deanWombourne'

s.ios.deployment_target = '8.0'
s.ios.deployment_target = '9.0'

s.source_files = 'Alamofire-Decodable/Classes/**/*'

s.dependency 'Alamofire', '~> 3.3'
s.dependency 'Decodable', '~> 0.4.2'
s.dependency 'Alamofire', '~> 4.0'
s.dependency 'Decodable', '~> 0.5.0'

end
73 changes: 41 additions & 32 deletions Alamofire-Decodable/Classes/Request+responseDecodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@ import Decodable
/**
Errors returned from decoding the response
*/
public enum DecodableResponseError: ErrorType {
case network(error: ErrorType)
case serialization(error: ErrorType)
case decoding(error: ErrorType)
public enum DecodableResponseError: Error {
case network(error: Error)
case serialization(error: Error)
case decoding(error: Error)
}


/**
Add a responseDecodable method to Alamo fire's `Request` to return already decoded objects.
Add a responseDecodable method to Alamofire's `DataRequest` to return already decoded objects.
Also add a method which does the same to arrays of decodable, because we can't yet define in Swift that
an array of `Decodable` is also itself `Decodable`.
*/
extension Request {
extension DataRequest {

/**
Provide a `Response<T, DecodableResponseError>` where `T` is something `Decodable`.
- parameter completionHandler: This function is passed the `Response` after decoding is complete
*/
public func responseDecodable<T: Decodable>(completionHandler: Response<T, DecodableResponseError> -> Void) -> Self {
@discardableResult
public func responseDecodable<T: Decodable>(completionHandler: @escaping (DataResponse<T>) -> Void) -> Self {

let responseSerializer: ResponseSerializer<T, DecodableResponseError> = Request.makeResponseSerializer()
let responseSerializer: DataResponseSerializer<T> = DataRequest.decodableResponseSerializer()

return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
}
Expand All @@ -40,37 +41,41 @@ extension Request {
- parameter partial: If this is true then individual items in the array which fail to be decoded into an instance of `T` are skipped. If false, then the entire response fails on the first invalid item.
- parameter completionHandler: This function is passed the `Response` after decoding is complete
*/
public func responseDecodable<T: Decodable>(partial: Bool = true, completionHandler: Response<[T], DecodableResponseError> -> Void) -> Self {
@discardableResult
public func responseDecodable<T: Decodable>(partial: Bool = true, completionHandler: @escaping (DataResponse<[T]>) -> Void) -> Self {

let responseSerializer: ResponseSerializer<[T], DecodableResponseError> = Request.makeResponseSerializer(partial: partial)
let responseSerializer: DataResponseSerializer<[T]> = DataRequest.decodableResponseSerializer(partial: partial)

return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
}

/**
Internal helper to make the response serializer for a `Decodable`
*/
static func makeResponseSerializer<T: Decodable>() -> ResponseSerializer<T, DecodableResponseError> {
return ResponseSerializer<T, DecodableResponseError> { request, response, data, error in
static func decodableResponseSerializer<T: Decodable>() -> DataResponseSerializer<T> {
return DataResponseSerializer<T> { request, response, data, error in
guard error == nil else {
return .Failure(.network(error: error!))
return .failure(DecodableResponseError.network(error: error!))
}

let JSONResponseSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, data, error)
// Use Alamofire's existing JSON serializer to extract the data, passing the error as nil, as it has already been handled.
let result = Request.serializeResponseJSON(options: .allowFragments,
response:response,
data:data,
error:nil)

switch result {

case .Success(let value):
case .success(let value):
do {
let responseObject = try T.decode(value)
return .Success(responseObject)
return .success(responseObject)
} catch let e {
return .Failure(.serialization(error: e))
return .failure(DecodableResponseError.serialization(error: e))
}

case .Failure(let error):
return .Failure(.decoding(error: error))
case .failure(let error):
return .failure(DecodableResponseError.decoding(error: error))
}
}

Expand All @@ -79,21 +84,25 @@ extension Request {
/**
Internal helper to make the response serializer for a collection of `Decodable`s
*/
static func makeResponseSerializer<T: Decodable>(partial partial: Bool) -> ResponseSerializer<[T], DecodableResponseError> {
return ResponseSerializer<[T], DecodableResponseError> { request, response, data, error in
static func decodableResponseSerializer<T: Decodable>(partial: Bool) -> DataResponseSerializer<[T]> {
return DataResponseSerializer<[T]> { request, response, data, error in
guard error == nil else {
return .Failure(.network(error: error!))
return .failure(DecodableResponseError.network(error: error!))
}

let JSONResponseSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, data, error)
// Use Alamofire's existing JSON serializer to extract the data, passing the error as nil, as it has already been handled.
let result = Request.serializeResponseJSON(options: .allowFragments,
response:response,
data:data,
error:nil)

switch result {

case .Success(let value):
case .success(let value):
guard let values = value as? Array<AnyObject> else {
let error = TypeMismatchError(expectedType: Array<AnyObject>.self, receivedType: value.dynamicType, object: value)
return .Failure(.serialization(error: error))
let metadata = DecodingError.Metadata(object: value)
let error = DecodingError.typeMismatch(expected: Array<AnyObject>.self, actual: type(of: value), metadata)
return .failure(DecodableResponseError.serialization(error: error))
}

do {
Expand All @@ -109,13 +118,13 @@ extension Request {
}
}
}
return .Success(responseObject)
return .success(responseObject)
} catch let e {
return .Failure(.serialization(error: e))
return .failure(DecodableResponseError.serialization(error: e))
}

case .Failure(let error):
return .Failure(.decoding(error: error))
case .failure(let error):
return .failure(DecodableResponseError.decoding(error: error))
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

### Unreleased
- Migrate to Swift 3
- Update Alamofire to version 4.0
- Update Decodable to version 0.5
- Drop support for iOS 8

### 1.0.1
- Better version control for dependencies in the podspec
Expand Down
23 changes: 18 additions & 5 deletions Example/Alamofire-Decodable.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0720;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = CocoaPods;
TargetAttributes = {
607FACCF1AFB9204008FA782 = {
Expand Down Expand Up @@ -309,7 +309,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
A918FD6D72DC99FDFB7A4771 /* [CP] Copy Pods Resources */ = {
Expand Down Expand Up @@ -339,7 +339,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
BC71730434ED5B5E017559C4 /* [CP] Embed Pods Frameworks */ = {
Expand Down Expand Up @@ -421,8 +421,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -445,7 +447,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.3;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand All @@ -466,8 +468,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -483,9 +487,10 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.3;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
VALIDATE_PRODUCT = YES;
};
name = Release;
Expand All @@ -494,25 +499,31 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 79D0DB92A5765C1BB30FD804 /* Pods-Alamofire-Decodable_Example.debug.xcconfig */;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = "Alamofire-Decodable/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
607FACF11AFB9204008FA782 /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 214573CCF0A5B1D9DD900B0E /* Pods-Alamofire-Decodable_Example.release.xcconfig */;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = "Alamofire-Decodable/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -532,6 +543,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Alamofire-Decodable_Example.app/Alamofire-Decodable_Example";
};
name = Debug;
Expand All @@ -548,6 +560,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Alamofire-Decodable_Example.app/Alamofire-Decodable_Example";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
13 changes: 6 additions & 7 deletions Example/Alamofire-Decodable/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,29 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func applicationDidFinishLaunching(_ application: UIApplication) {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Expand Down
4 changes: 2 additions & 2 deletions Example/Alamofire-Decodable/Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ struct Post {


extension Post: Decodable {

static func decode(json: AnyObject) throws -> Post {
static func decode(_ json: Any) throws -> Post {
return try Post(userId: json => "userId",
id: json => "id",
title: json => "title",
Expand Down
Loading

0 comments on commit 362fe76

Please sign in to comment.