Skip to content

Commit

Permalink
Migrate samples to Swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Hardiman authored and AliSoftware committed Oct 23, 2016
1 parent fff8afd commit 45a2cea
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Examples/Swift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
Expand Down
62 changes: 31 additions & 31 deletions Examples/Swift/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ class MainViewController: UIViewController {

installTextStub(self.installTextStubSwitch)
installImageStub(self.installImageStubSwitch)
OHHTTPStubs.onStubActivation { (request: NSURLRequest, stub: OHHTTPStubsDescriptor, response: OHHTTPStubsResponse) in
print("[OHHTTPStubs] Request to \(request.URL!) has been stubbed with \(stub.name)")
OHHTTPStubs.onStubActivation { (request: URLRequest, stub: OHHTTPStubsDescriptor, response: OHHTTPStubsResponse) in
print("[OHHTTPStubs] Request to \(request.url!) has been stubbed with \(stub.name)")
}
}

////////////////////////////////////////////////////////////////////////////////
// MARK: - Global stubs activation

@IBAction func toggleStubs(sender: UISwitch) {
OHHTTPStubs.setEnabled(sender.on)
self.delaySwitch.enabled = sender.on
self.installTextStubSwitch.enabled = sender.on
self.installImageStubSwitch.enabled = sender.on
@IBAction func toggleStubs(_ sender: UISwitch) {
OHHTTPStubs.setEnabled(sender.isOn)
self.delaySwitch.isEnabled = sender.isOn
self.installTextStubSwitch.isEnabled = sender.isOn
self.installImageStubSwitch.isEnabled = sender.isOn

let state = sender.on ? "and enabled" : "but disabled"
let state = sender.isOn ? "and enabled" : "but disabled"
print("Installed (\(state)) stubs: \(OHHTTPStubs.allStubs)")
}

Expand All @@ -52,30 +52,30 @@ class MainViewController: UIViewController {
// MARK: - Text Download and Stub


@IBAction func downloadText(sender: UIButton) {
sender.enabled = false
@IBAction func downloadText(_ sender: UIButton) {
sender.isEnabled = false
self.textView.text = nil

let urlString = "http://www.opensource.apple.com/source/Git/Git-26/src/git-htmldocs/git-commit.txt?txt"
let req = NSURLRequest(URL: NSURL(string: urlString)!)
let req = URLRequest(url: URL(string: urlString)!)

NSURLConnection.sendAsynchronousRequest(req, queue: NSOperationQueue.mainQueue()) { (_, data, _) in
sender.enabled = true
if let receivedData = data, receivedText = NSString(data: receivedData, encoding: NSASCIIStringEncoding) {
NSURLConnection.sendAsynchronousRequest(req, queue: OperationQueue.main) { (_, data, _) in
sender.isEnabled = true
if let receivedData = data, let receivedText = NSString(data: receivedData, encoding: String.Encoding.ascii.rawValue) {
self.textView.text = receivedText as String
}
}
}

weak var textStub: OHHTTPStubsDescriptor?
@IBAction func installTextStub(sender: UISwitch) {
if sender.on {
@IBAction func installTextStub(_ sender: UISwitch) {
if sender.isOn {
// Install

textStub = stub(isExtension("txt")) { _ in
let stubPath = OHPathForFile("stub.txt", self.dynamicType)
return fixture(stubPath!, headers: ["Content-Type":"text/plain"])
.requestTime(self.delaySwitch.on ? 2.0 : 0.0, responseTime:OHHTTPStubsDownloadSpeedWifi)
textStub = stub(condition: isExtension("txt")) { _ in
let stubPath = OHPathForFile("stub.txt", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type":"text/plain"])
.requestTime(self.delaySwitch.isOn ? 2.0 : 0.0, responseTime:OHHTTPStubsDownloadSpeedWifi)
}
textStub?.name = "Text stub"
} else {
Expand All @@ -88,30 +88,30 @@ class MainViewController: UIViewController {
////////////////////////////////////////////////////////////////////////////////
// MARK: - Image Download and Stub

@IBAction func downloadImage(sender: UIButton) {
sender.enabled = false
@IBAction func downloadImage(_ sender: UIButton) {
sender.isEnabled = false
self.imageView.image = nil

let urlString = "http://images.apple.com/support/assets/images/products/iphone/hero_iphone4-5_wide.png"
let req = NSURLRequest(URL: NSURL(string: urlString)!)
let req = URLRequest(url: URL(string: urlString)!)

NSURLConnection.sendAsynchronousRequest(req, queue: NSOperationQueue.mainQueue()) { (_, data, _) in
sender.enabled = true
NSURLConnection.sendAsynchronousRequest(req, queue: OperationQueue.main) { (_, data, _) in
sender.isEnabled = true
if let receivedData = data {
self.imageView.image = UIImage(data: receivedData)
}
}
}

weak var imageStub: OHHTTPStubsDescriptor?
@IBAction func installImageStub(sender: UISwitch) {
if sender.on {
@IBAction func installImageStub(_ sender: UISwitch) {
if sender.isOn {
// Install

imageStub = stub(isExtension("png") || isExtension("jpg") || isExtension("gif")) { _ in
let stubPath = OHPathForFile("stub.jpg", self.dynamicType)
return fixture(stubPath!, headers: ["Content-Type":"image/jpeg"])
.requestTime(self.delaySwitch.on ? 2.0 : 0.0, responseTime: OHHTTPStubsDownloadSpeedWifi)
imageStub = stub(condition: isExtension("png") || isExtension("jpg") || isExtension("gif")) { _ in
let stubPath = OHPathForFile("stub.jpg", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type":"image/jpeg"])
.requestTime(self.delaySwitch.isOn ? 2.0 : 0.0, responseTime: OHHTTPStubsDownloadSpeedWifi)
}
imageStub?.name = "Image stub"
} else {
Expand Down
12 changes: 11 additions & 1 deletion Examples/Swift/OHHTTPStubsDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,12 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0700;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = AliSoftware;
TargetAttributes = {
099F74061AE2D049001108A5 = {
CreatedOnToolsVersion = 6.3;
LastSwiftMigration = 0800;
ProvisioningStyle = Manual;
};
};
Expand Down Expand Up @@ -261,8 +262,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 Down Expand Up @@ -308,8 +311,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 @@ -328,6 +333,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.3;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 2.3;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
Expand All @@ -338,25 +344,29 @@
isa = XCBuildConfiguration;
baseConfigurationReference = FE789C901264C7AF3BDF48CB /* Pods-OHHTTPStubsDemo.debug.xcconfig */;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = "$(SRCROOT)/Supporting Files/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.alisoftware.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
099F74281AE2D049001108A5 /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 0BFB1DB3496791F522727353 /* Pods-OHHTTPStubsDemo.release.xcconfig */;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = "$(SRCROOT)/Supporting Files/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.alisoftware.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
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 = "0700"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
74 changes: 37 additions & 37 deletions Examples/Swift/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions OHHTTPStubs/Sources/Swift/OHHTTPStubsSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,15 @@
* - Returns: The `OHHTTPStubsResponse` instance that will stub with the given status code
* & headers, and use the file content as the response body.
*/
#if swift(>=3.0)
public func fixture(filePath: String, status: Int32 = 200, headers: [AnyHashable: Any]?) -> OHHTTPStubsResponse {
return OHHTTPStubsResponse(fileAtPath: filePath, statusCode: status, headers: headers)
}
#else
public func fixture(filePath: String, status: Int32 = 200, headers: [NSObject: AnyObject]?) -> OHHTTPStubsResponse {
return OHHTTPStubsResponse(fileAtPath: filePath, statusCode: status, headers: headers)
}
#endif

/**
* Helper to call the stubbing function in a more concise way?
Expand Down

0 comments on commit 45a2cea

Please sign in to comment.