Skip to content

Commit 45a2cea

Browse files
Dave HardimanAliSoftware
authored andcommitted
Migrate samples to Swift 3
1 parent fff8afd commit 45a2cea

File tree

6 files changed

+87
-71
lines changed

6 files changed

+87
-71
lines changed

Examples/Swift/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1313

1414
var window: UIWindow?
1515

16-
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
16+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
1717
// Override point for customization after application launch.
1818
return true
1919
}

Examples/Swift/MainViewController.swift

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ class MainViewController: UIViewController {
2828

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

3636
////////////////////////////////////////////////////////////////////////////////
3737
// MARK: - Global stubs activation
3838

39-
@IBAction func toggleStubs(sender: UISwitch) {
40-
OHHTTPStubs.setEnabled(sender.on)
41-
self.delaySwitch.enabled = sender.on
42-
self.installTextStubSwitch.enabled = sender.on
43-
self.installImageStubSwitch.enabled = sender.on
39+
@IBAction func toggleStubs(_ sender: UISwitch) {
40+
OHHTTPStubs.setEnabled(sender.isOn)
41+
self.delaySwitch.isEnabled = sender.isOn
42+
self.installTextStubSwitch.isEnabled = sender.isOn
43+
self.installImageStubSwitch.isEnabled = sender.isOn
4444

45-
let state = sender.on ? "and enabled" : "but disabled"
45+
let state = sender.isOn ? "and enabled" : "but disabled"
4646
print("Installed (\(state)) stubs: \(OHHTTPStubs.allStubs)")
4747
}
4848

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

5454

55-
@IBAction func downloadText(sender: UIButton) {
56-
sender.enabled = false
55+
@IBAction func downloadText(_ sender: UIButton) {
56+
sender.isEnabled = false
5757
self.textView.text = nil
5858

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

62-
NSURLConnection.sendAsynchronousRequest(req, queue: NSOperationQueue.mainQueue()) { (_, data, _) in
63-
sender.enabled = true
64-
if let receivedData = data, receivedText = NSString(data: receivedData, encoding: NSASCIIStringEncoding) {
62+
NSURLConnection.sendAsynchronousRequest(req, queue: OperationQueue.main) { (_, data, _) in
63+
sender.isEnabled = true
64+
if let receivedData = data, let receivedText = NSString(data: receivedData, encoding: String.Encoding.ascii.rawValue) {
6565
self.textView.text = receivedText as String
6666
}
6767
}
6868
}
6969

7070
weak var textStub: OHHTTPStubsDescriptor?
71-
@IBAction func installTextStub(sender: UISwitch) {
72-
if sender.on {
71+
@IBAction func installTextStub(_ sender: UISwitch) {
72+
if sender.isOn {
7373
// Install
7474

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

91-
@IBAction func downloadImage(sender: UIButton) {
92-
sender.enabled = false
91+
@IBAction func downloadImage(_ sender: UIButton) {
92+
sender.isEnabled = false
9393
self.imageView.image = nil
9494

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

98-
NSURLConnection.sendAsynchronousRequest(req, queue: NSOperationQueue.mainQueue()) { (_, data, _) in
99-
sender.enabled = true
98+
NSURLConnection.sendAsynchronousRequest(req, queue: OperationQueue.main) { (_, data, _) in
99+
sender.isEnabled = true
100100
if let receivedData = data {
101101
self.imageView.image = UIImage(data: receivedData)
102102
}
103103
}
104104
}
105105

106106
weak var imageStub: OHHTTPStubsDescriptor?
107-
@IBAction func installImageStub(sender: UISwitch) {
108-
if sender.on {
107+
@IBAction func installImageStub(_ sender: UISwitch) {
108+
if sender.isOn {
109109
// Install
110110

111-
imageStub = stub(isExtension("png") || isExtension("jpg") || isExtension("gif")) { _ in
112-
let stubPath = OHPathForFile("stub.jpg", self.dynamicType)
113-
return fixture(stubPath!, headers: ["Content-Type":"image/jpeg"])
114-
.requestTime(self.delaySwitch.on ? 2.0 : 0.0, responseTime: OHHTTPStubsDownloadSpeedWifi)
111+
imageStub = stub(condition: isExtension("png") || isExtension("jpg") || isExtension("gif")) { _ in
112+
let stubPath = OHPathForFile("stub.jpg", type(of: self))
113+
return fixture(filePath: stubPath!, headers: ["Content-Type":"image/jpeg"])
114+
.requestTime(self.delaySwitch.isOn ? 2.0 : 0.0, responseTime: OHHTTPStubsDownloadSpeedWifi)
115115
}
116116
imageStub?.name = "Image stub"
117117
} else {

Examples/Swift/OHHTTPStubsDemo.xcodeproj/project.pbxproj

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,12 @@
145145
isa = PBXProject;
146146
attributes = {
147147
LastSwiftUpdateCheck = 0700;
148-
LastUpgradeCheck = 0700;
148+
LastUpgradeCheck = 0800;
149149
ORGANIZATIONNAME = AliSoftware;
150150
TargetAttributes = {
151151
099F74061AE2D049001108A5 = {
152152
CreatedOnToolsVersion = 6.3;
153+
LastSwiftMigration = 0800;
153154
ProvisioningStyle = Manual;
154155
};
155156
};
@@ -261,8 +262,10 @@
261262
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
262263
CLANG_WARN_EMPTY_BODY = YES;
263264
CLANG_WARN_ENUM_CONVERSION = YES;
265+
CLANG_WARN_INFINITE_RECURSION = YES;
264266
CLANG_WARN_INT_CONVERSION = YES;
265267
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
268+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
266269
CLANG_WARN_UNREACHABLE_CODE = YES;
267270
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
268271
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
@@ -308,8 +311,10 @@
308311
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
309312
CLANG_WARN_EMPTY_BODY = YES;
310313
CLANG_WARN_ENUM_CONVERSION = YES;
314+
CLANG_WARN_INFINITE_RECURSION = YES;
311315
CLANG_WARN_INT_CONVERSION = YES;
312316
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
317+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
313318
CLANG_WARN_UNREACHABLE_CODE = YES;
314319
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
315320
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
@@ -328,6 +333,7 @@
328333
IPHONEOS_DEPLOYMENT_TARGET = 8.3;
329334
MTL_ENABLE_DEBUG_INFO = NO;
330335
SDKROOT = iphoneos;
336+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
331337
SWIFT_VERSION = 2.3;
332338
TARGETED_DEVICE_FAMILY = "1,2";
333339
VALIDATE_PRODUCT = YES;
@@ -338,25 +344,29 @@
338344
isa = XCBuildConfiguration;
339345
baseConfigurationReference = FE789C901264C7AF3BDF48CB /* Pods-OHHTTPStubsDemo.debug.xcconfig */;
340346
buildSettings = {
347+
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
341348
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
342349
INFOPLIST_FILE = "$(SRCROOT)/Supporting Files/Info.plist";
343350
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
344351
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
345352
PRODUCT_BUNDLE_IDENTIFIER = "com.alisoftware.$(PRODUCT_NAME:rfc1034identifier)";
346353
PRODUCT_NAME = "$(TARGET_NAME)";
354+
SWIFT_VERSION = 3.0;
347355
};
348356
name = Debug;
349357
};
350358
099F74281AE2D049001108A5 /* Release */ = {
351359
isa = XCBuildConfiguration;
352360
baseConfigurationReference = 0BFB1DB3496791F522727353 /* Pods-OHHTTPStubsDemo.release.xcconfig */;
353361
buildSettings = {
362+
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
354363
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
355364
INFOPLIST_FILE = "$(SRCROOT)/Supporting Files/Info.plist";
356365
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
357366
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
358367
PRODUCT_BUNDLE_IDENTIFIER = "com.alisoftware.$(PRODUCT_NAME:rfc1034identifier)";
359368
PRODUCT_NAME = "$(TARGET_NAME)";
369+
SWIFT_VERSION = 3.0;
360370
};
361371
name = Release;
362372
};

Examples/Swift/OHHTTPStubsDemo.xcodeproj/xcshareddata/xcschemes/OHHTTPStubsDemo.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0700"
3+
LastUpgradeVersion = "0800"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Examples/Swift/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 37 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

OHHTTPStubs/Sources/Swift/OHHTTPStubsSwift.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,15 @@
7777
* - Returns: The `OHHTTPStubsResponse` instance that will stub with the given status code
7878
* & headers, and use the file content as the response body.
7979
*/
80+
#if swift(>=3.0)
81+
public func fixture(filePath: String, status: Int32 = 200, headers: [AnyHashable: Any]?) -> OHHTTPStubsResponse {
82+
return OHHTTPStubsResponse(fileAtPath: filePath, statusCode: status, headers: headers)
83+
}
84+
#else
8085
public func fixture(filePath: String, status: Int32 = 200, headers: [NSObject: AnyObject]?) -> OHHTTPStubsResponse {
8186
return OHHTTPStubsResponse(fileAtPath: filePath, statusCode: status, headers: headers)
8287
}
88+
#endif
8389

8490
/**
8591
* Helper to call the stubbing function in a more concise way?

0 commit comments

Comments
 (0)