From 12ae1df92f5e5ceb26a37fac2afc07ee878326d6 Mon Sep 17 00:00:00 2001 From: Reid Chatham Date: Thu, 25 Feb 2016 12:11:56 -0800 Subject: [PATCH 01/12] Adding error reporting for failed network request --- Podfile.lock | 4 +- Polyglot/Polyglot.swift | 65 +++++++++++-------- PolyglotSample.xcodeproj/project.pbxproj | 7 +- .../xcschemes/PolyglotSample.xcscheme | 2 +- PolyglotSample/Base.lproj/Main.storyboard | 17 +++-- PolyglotSample/Info.plist | 7 +- PolyglotSample/ViewController.swift | 3 +- PolyglotTests/Info.plist | 2 +- PolyglotTests/PolyglotTests.swift | 2 +- 9 files changed, 68 insertions(+), 41 deletions(-) diff --git a/Podfile.lock b/Podfile.lock index 853bf12..da68258 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -1,6 +1,6 @@ PODS: - Nocilla (0.10.0) - - Polyglot (0.4.0) + - Polyglot (0.5.0) DEPENDENCIES: - Nocilla (~> 0.9) @@ -12,6 +12,6 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: Nocilla: ae0a2b05f3087b473624ac2b25903695df51246a - Polyglot: 29a83191af1d56f1af28f195ff2cd748814dab47 + Polyglot: 9357934dadad89a6d68fc6f82047677349bce3df COCOAPODS: 0.39.0 diff --git a/Polyglot/Polyglot.swift b/Polyglot/Polyglot.swift index 9ee78b1..f43c4b0 100644 --- a/Polyglot/Polyglot.swift +++ b/Polyglot/Polyglot.swift @@ -77,66 +77,79 @@ public enum Language: String { Responsible for translating text. */ public class Polyglot { - + let session: Session - + /// The language to be translated from. It will automatically detect the language if you do not set this. public var fromLanguage: Language? - + /// The language to translate to. public var toLanguage: Language - - + + /** - - parameter clientId: Microsoft Translator client ID. - - parameter clientSecret: Microsoft Translator client secret. - */ + - parameter clientId: Microsoft Translator client ID. + - parameter clientSecret: Microsoft Translator client secret. + */ public init(clientId: String, clientSecret: String) { session = Session(clientId: clientId, clientSecret: clientSecret) toLanguage = Language.English } - + /** - Translates a given piece of text. - - - parameter text: The text to translate. - - parameter callback: The code to be executed once the translation has completed. - */ - public func translate(text: String, callback: ((translation: String) -> (Void))) { + Translates a given piece of text. + + - parameter text: The text to translate. + - parameter callback: The code to be executed once the translation has completed. + */ + public func translate(text: String, callback: ((translation: String?, error: TranslationError?) -> (Void))) { session.getAccessToken { token in self.fromLanguage = text.language let toLanguageComponent = "&to=\(self.toLanguage.rawValue.urlEncoded!)" let fromLanguageComponent = (self.fromLanguage != nil) ? "&from=\(self.fromLanguage!.rawValue.urlEncoded!)" : "" let urlString = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=\(text.urlEncoded!)\(toLanguageComponent)\(fromLanguageComponent)" - + let request = NSMutableURLRequest(URL: NSURL(string: urlString)!) request.HTTPMethod = "GET" request.setValue("Bearer " + token, forHTTPHeaderField: "Authorization") - - let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {(data, response, error) in - let translation: String + + let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in + let translation: String? guard let data = data, let xmlString = NSString(data: data, encoding: NSUTF8StringEncoding) as? String - else { - translation = "" - return + else { + translation = nil + + guard + let error = error + else { + callback(translation: translation, error: nil) + return + } + + callback(translation: translation, error: .SessionError(error)) + return } - + translation = self.translationFromXML(xmlString) - + defer { dispatch_async(dispatch_get_main_queue()) { - callback(translation: translation) + callback(translation: translation, error: nil) } } } task.resume() } } - + private func translationFromXML(XML: String) -> String { let translation = XML.stringByReplacingOccurrencesOfString("", withString: "") return translation.stringByReplacingOccurrencesOfString("", withString: "") } } + +public enum TranslationError : ErrorType { + case SessionError(NSError) +} \ No newline at end of file diff --git a/PolyglotSample.xcodeproj/project.pbxproj b/PolyglotSample.xcodeproj/project.pbxproj index 0516092..e70fb0d 100644 --- a/PolyglotSample.xcodeproj/project.pbxproj +++ b/PolyglotSample.xcodeproj/project.pbxproj @@ -179,7 +179,7 @@ attributes = { LastSwiftMigration = 0710; LastSwiftUpdateCheck = 0710; - LastUpgradeCheck = 0600; + LastUpgradeCheck = 0720; ORGANIZATIONNAME = "Ayaka Nonaka"; TargetAttributes = { B60F87431985F1C200ABB94A = { @@ -345,6 +345,7 @@ "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_DYNAMIC_NO_PIC = NO; GCC_OPTIMIZATION_LEVEL = 0; @@ -411,6 +412,7 @@ CLANG_ENABLE_MODULES = YES; INFOPLIST_FILE = PolyglotSample/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "com.ayakanonaka.${PRODUCT_NAME:rfc1034identifier}"; PRODUCT_NAME = PolyglotSample; SWIFT_OBJC_BRIDGING_HEADER = "PolyglotSample/Polyglot-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; @@ -426,6 +428,7 @@ CLANG_ENABLE_MODULES = YES; INFOPLIST_FILE = PolyglotSample/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "com.ayakanonaka.${PRODUCT_NAME:rfc1034identifier}"; PRODUCT_NAME = PolyglotSample; SWIFT_OBJC_BRIDGING_HEADER = "PolyglotSample/Polyglot-Bridging-Header.h"; }; @@ -447,6 +450,7 @@ ); INFOPLIST_FILE = PolyglotTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "com.ayakanonaka.${PRODUCT_NAME:rfc1034identifier}"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "PolyglotTests/PolyglotTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; @@ -466,6 +470,7 @@ ); INFOPLIST_FILE = PolyglotTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "com.ayakanonaka.${PRODUCT_NAME:rfc1034identifier}"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "PolyglotTests/PolyglotTests-Bridging-Header.h"; TEST_HOST = "$(BUNDLE_LOADER)"; diff --git a/PolyglotSample.xcodeproj/xcshareddata/xcschemes/PolyglotSample.xcscheme b/PolyglotSample.xcodeproj/xcshareddata/xcschemes/PolyglotSample.xcscheme index 2f479dc..b36acce 100644 --- a/PolyglotSample.xcodeproj/xcshareddata/xcschemes/PolyglotSample.xcscheme +++ b/PolyglotSample.xcodeproj/xcshareddata/xcschemes/PolyglotSample.xcscheme @@ -1,6 +1,6 @@ - + - + + - + @@ -16,7 +17,7 @@ - + @@ -27,7 +28,7 @@ - - + diff --git a/PolyglotSample/Info.plist b/PolyglotSample/Info.plist index 300cad6..77f6cb1 100644 --- a/PolyglotSample/Info.plist +++ b/PolyglotSample/Info.plist @@ -7,7 +7,7 @@ CFBundleExecutable ${EXECUTABLE_NAME} CFBundleIdentifier - com.ayakanonaka.${PRODUCT_NAME:rfc1034identifier} + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName @@ -22,6 +22,11 @@ 1 LSRequiresIPhoneOS + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + UIMainStoryboardFile Main UIRequiredDeviceCapabilities diff --git a/PolyglotSample/ViewController.swift b/PolyglotSample/ViewController.swift index 3615f59..004d1f5 100644 --- a/PolyglotSample/ViewController.swift +++ b/PolyglotSample/ViewController.swift @@ -45,7 +45,8 @@ class ViewController: UIViewController { guard let text = inputTextField.text else { return } UIApplication.sharedApplication().networkActivityIndicatorVisible = true - translator.translate(text) { translation in + + translator.translate(text) { translation, error in if let language = self.translator.fromLanguage?.rawValue { self.translationLabel.text = "Translated from \(language.capitalizedString): \(translation)" } diff --git a/PolyglotTests/Info.plist b/PolyglotTests/Info.plist index 5293720..6d32c15 100644 --- a/PolyglotTests/Info.plist +++ b/PolyglotTests/Info.plist @@ -7,7 +7,7 @@ CFBundleExecutable ${EXECUTABLE_NAME} CFBundleIdentifier - com.ayakanonaka.${PRODUCT_NAME:rfc1034identifier} + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName diff --git a/PolyglotTests/PolyglotTests.swift b/PolyglotTests/PolyglotTests.swift index 8e54292..b8dfb69 100644 --- a/PolyglotTests/PolyglotTests.swift +++ b/PolyglotTests/PolyglotTests.swift @@ -61,7 +61,7 @@ class PolyglotTests: XCTestCase { .withBody("I don't know") let polyglot: Polyglot = Polyglot(clientId: "myClientId", clientSecret: "myClientSecret") - polyglot.translate("Ik weet het niet") { translation in + polyglot.translate("Ik weet het niet") { translation, error in XCTAssertEqual(translation, "I don't know") expectation.fulfill() } From 113d39842392c9ef21c7b41e33bbdec6203e8368 Mon Sep 17 00:00:00 2001 From: Reid Chatham Date: Thu, 25 Feb 2016 15:30:38 -0800 Subject: [PATCH 02/12] Made error handling get called on main thread --- Polyglot/Polyglot.swift | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Polyglot/Polyglot.swift b/Polyglot/Polyglot.swift index f43c4b0..1b0d74b 100644 --- a/Polyglot/Polyglot.swift +++ b/Polyglot/Polyglot.swift @@ -123,12 +123,16 @@ public class Polyglot { guard let error = error - else { + else { + dispatch_async(dispatch_get_main_queue()) { callback(translation: translation, error: nil) - return + } + return } - callback(translation: translation, error: .SessionError(error)) + dispatch_async(dispatch_get_main_queue()) { + callback(translation: translation, error: .SessionError(error)) + } return } From 7a90aa5e0f738c26412ced3e1b5073dcc109d1ce Mon Sep 17 00:00:00 2001 From: Reid Chatham Date: Tue, 19 Apr 2016 12:17:27 -0700 Subject: [PATCH 03/12] Updated PolyglotTests --- Podfile.lock | 8 +- Polyglot/Polyglot.swift | 92 +++++++++++++------ Polyglot/Session.swift | 37 +++++--- Polyglot/StringExtension.swift | 2 +- PolyglotSample.xcodeproj/project.pbxproj | 1 + .../xcshareddata/xcschemes/Polyglot.xcscheme | 13 ++- PolyglotSample/Base.lproj/Main.storyboard | 7 +- PolyglotSample/ViewController.swift | 2 +- PolyglotTests/PolyglotTests.swift | 4 +- 9 files changed, 109 insertions(+), 57 deletions(-) diff --git a/Podfile.lock b/Podfile.lock index 01e9589..fdd5215 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -1,5 +1,5 @@ PODS: - - Nocilla (0.9.0) + - Nocilla (0.10.0) - Polyglot (0.4.0) DEPENDENCIES: @@ -11,7 +11,7 @@ EXTERNAL SOURCES: :path: . SPEC CHECKSUMS: - Nocilla: 4d713c56e7357fd0cac0363854d4a58021ad3f41 - Polyglot: dddf99978e893f05bd9f3461922de8958f41480b + Nocilla: ae0a2b05f3087b473624ac2b25903695df51246a + Polyglot: 29a83191af1d56f1af28f195ff2cd748814dab47 -COCOAPODS: 0.36.0 +COCOAPODS: 0.38.2 diff --git a/Polyglot/Polyglot.swift b/Polyglot/Polyglot.swift index a142e22..ccb8fa6 100644 --- a/Polyglot/Polyglot.swift +++ b/Polyglot/Polyglot.swift @@ -23,8 +23,8 @@ import Foundation /** - Supported languages. -*/ + Supported languages. + */ public enum Language: String { case Arabic = "ar" case Bulgarian = "bg" @@ -74,54 +74,86 @@ public enum Language: String { } /** - Responsible for translating text. -*/ + Responsible for translating text. + */ public class Polyglot { - + let session: Session - + /// The language to be translated from. It will automatically detect the language if you do not set this. public var fromLanguage: Language? - + /// The language to translate to. public var toLanguage: Language - - + + /** - :param: clientId Microsoft Translator client ID. - :param: clientSecret Microsoft Translator client secret. - */ + - parameter clientId: Microsoft Translator client ID. + - parameter clientSecret: Microsoft Translator client secret. + */ public init(clientId: String, clientSecret: String) { - self.session = Session(clientId: clientId, clientSecret: clientSecret) - self.toLanguage = Language.English + session = Session(clientId: clientId, clientSecret: clientSecret) + toLanguage = Language.English } - + /** - Translates a given piece of text. - - :param: text The text to translate. - :param: callback The code to be executed once the translation has completed. - */ - public func translate(text: String, callback: ((translation: String) -> (Void))) { - self.session.getAccessToken { token in + Translates a given piece of text. + + - parameter text: The text to translate. + - parameter callback: The code to be executed once the translation has completed. + */ + public func translate(text: String, callback: ((translation: String?, error: TranslationError?) -> (Void))) { + session.getAccessToken { token in self.fromLanguage = text.language let toLanguageComponent = "&to=\(self.toLanguage.rawValue.urlEncoded!)" let fromLanguageComponent = (self.fromLanguage != nil) ? "&from=\(self.fromLanguage!.rawValue.urlEncoded!)" : "" let urlString = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=\(text.urlEncoded!)\(toLanguageComponent)\(fromLanguageComponent)" - + let request = NSMutableURLRequest(URL: NSURL(string: urlString)!) request.HTTPMethod = "GET" request.setValue("Bearer " + token, forHTTPHeaderField: "Authorization") - - let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {(data, response, error) in - var translation = "" - if let xmlString = NSString(data: data, encoding: NSUTF8StringEncoding) { - translation = xmlString.stringByReplacingOccurrencesOfString("", withString: "") - translation = translation.stringByReplacingOccurrencesOfString("", withString: "") + + let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in + let translation: String? + guard + let data = data, + let xmlString = NSString(data: data, encoding: NSUTF8StringEncoding) as? String + else { + translation = nil + + guard + let error = error + else { + dispatch_async(dispatch_get_main_queue()) { + callback(translation: translation, error: nil) + } + return + } + + dispatch_async(dispatch_get_main_queue()) { + callback(translation: translation, error: .SessionError(error)) + } + return + } + + translation = self.translationFromXML(xmlString) + + defer { + dispatch_async(dispatch_get_main_queue()) { + callback(translation: translation, error: nil) + } } - callback(translation: translation) } task.resume() } } + + private func translationFromXML(XML: String) -> String { + let translation = XML.stringByReplacingOccurrencesOfString("", withString: "") + return translation.stringByReplacingOccurrencesOfString("", withString: "") + } } + +public enum TranslationError : ErrorType { + case SessionError(NSError) +} \ No newline at end of file diff --git a/Polyglot/Session.swift b/Polyglot/Session.swift index 159c5de..ba06846 100644 --- a/Polyglot/Session.swift +++ b/Polyglot/Session.swift @@ -44,19 +44,32 @@ class Session { let bodyString = "client_id=\(clientId.urlEncoded!)&client_secret=\(clientSecret.urlEncoded!)&scope=http://api.microsofttranslator.com&grant_type=client_credentials" request.HTTPBody = bodyString.dataUsingEncoding(NSUTF8StringEncoding) - - let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {(data, response, error) in - let resultsDict: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary - - let expiresIn = resultsDict["expires_in"] as! NSString - self.expirationTime = NSDate(timeIntervalSinceNow: expiresIn.doubleValue) - - let token = resultsDict["access_token"] as! String - self.accessToken = token - - callback(token: token) + + let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { + (data, response, error) in + + if let isError = error { + NSLog("Error: %@", isError) + } + guard let data = data else { + NSLog("No data") + return + } + + do { + let resultsDict: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary + + let expiresIn = resultsDict["expires_in"] as! NSString + self.expirationTime = NSDate(timeIntervalSinceNow: expiresIn.doubleValue) + + let token = resultsDict["access_token"] as! String + self.accessToken = token + + callback(token: token) + } catch { + NSLog("Error: \(error)") + } } - task.resume() } else { callback(token: self.accessToken!) diff --git a/Polyglot/StringExtension.swift b/Polyglot/StringExtension.swift index 24a277b..e9db8bd 100644 --- a/Polyglot/StringExtension.swift +++ b/Polyglot/StringExtension.swift @@ -31,7 +31,7 @@ extension String { } public var language: Language? { - if (count(self) > 0) { // Prevent Index Out of Bounds in NSLinguisticTagger + if (self.characters.count > 0) { // Prevent Index Out of Bounds in NSLinguisticTagger let tagger = NSLinguisticTagger(tagSchemes: [NSLinguisticTagSchemeLanguage], options: 0) tagger.string = self if let result = tagger.tagAtIndex(0, scheme: NSLinguisticTagSchemeLanguage, tokenRange: nil, sentenceRange: nil) { diff --git a/PolyglotSample.xcodeproj/project.pbxproj b/PolyglotSample.xcodeproj/project.pbxproj index 90876a7..fbbb142 100644 --- a/PolyglotSample.xcodeproj/project.pbxproj +++ b/PolyglotSample.xcodeproj/project.pbxproj @@ -177,6 +177,7 @@ B60F873C1985F1C200ABB94A /* Project object */ = { isa = PBXProject; attributes = { + LastSwiftUpdateCheck = 0710; LastUpgradeCheck = 0600; ORGANIZATIONNAME = "Ayaka Nonaka"; TargetAttributes = { diff --git a/PolyglotSample.xcodeproj/xcshareddata/xcschemes/Polyglot.xcscheme b/PolyglotSample.xcodeproj/xcshareddata/xcschemes/Polyglot.xcscheme index b03f537..f8261f8 100644 --- a/PolyglotSample.xcodeproj/xcshareddata/xcschemes/Polyglot.xcscheme +++ b/PolyglotSample.xcodeproj/xcshareddata/xcschemes/Polyglot.xcscheme @@ -1,6 +1,6 @@ + shouldUseLaunchSchemeArgsEnv = "YES"> @@ -48,15 +48,18 @@ ReferencedContainer = "container:PolyglotSample.xcodeproj"> + + @@ -72,10 +75,10 @@ diff --git a/PolyglotSample/Base.lproj/Main.storyboard b/PolyglotSample/Base.lproj/Main.storyboard index 61cc83e..5a76f26 100644 --- a/PolyglotSample/Base.lproj/Main.storyboard +++ b/PolyglotSample/Base.lproj/Main.storyboard @@ -1,13 +1,14 @@ - + - + + - + diff --git a/PolyglotSample/ViewController.swift b/PolyglotSample/ViewController.swift index db6ef55..4efc9ac 100644 --- a/PolyglotSample/ViewController.swift +++ b/PolyglotSample/ViewController.swift @@ -48,7 +48,7 @@ class ViewController: UIViewController { @IBAction func didTapTranslateButton(sender: AnyObject) { UIApplication.sharedApplication().networkActivityIndicatorVisible = true - self.translator?.translate(self.inputTextField.text) { translation in + self.translator?.translate(self.inputTextField.text!) { translation in dispatch_async(dispatch_get_main_queue(), { if let language = self.translator?.fromLanguage?.rawValue { self.translationLabel.text = "Translated from \(language.capitalizedString): \(translation)" diff --git a/PolyglotTests/PolyglotTests.swift b/PolyglotTests/PolyglotTests.swift index 8e54292..d02061b 100644 --- a/PolyglotTests/PolyglotTests.swift +++ b/PolyglotTests/PolyglotTests.swift @@ -61,7 +61,9 @@ class PolyglotTests: XCTestCase { .withBody("I don't know") let polyglot: Polyglot = Polyglot(clientId: "myClientId", clientSecret: "myClientSecret") - polyglot.translate("Ik weet het niet") { translation in + polyglot.translate("Ik weet het niet") { (translation, error) in + + guard let translation = translation else { return } XCTAssertEqual(translation, "I don't know") expectation.fulfill() } From 2ba5391f5b9aa7cd45d8662c3a2983afd974616d Mon Sep 17 00:00:00 2001 From: Reid Chatham Date: Tue, 19 Apr 2016 12:36:17 -0700 Subject: [PATCH 04/12] Simplified and cleaned up translate(text:callback:) --- Polyglot/Polyglot.swift | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/Polyglot/Polyglot.swift b/Polyglot/Polyglot.swift index ccb8fa6..f4076ba 100644 --- a/Polyglot/Polyglot.swift +++ b/Polyglot/Polyglot.swift @@ -113,34 +113,21 @@ public class Polyglot { request.HTTPMethod = "GET" request.setValue("Bearer " + token, forHTTPHeaderField: "Authorization") - let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in - let translation: String? + let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { + (data, response, error) in + + var translation : String? guard let data = data, let xmlString = NSString(data: data, encoding: NSUTF8StringEncoding) as? String - else { - translation = nil - - guard - let error = error - else { - dispatch_async(dispatch_get_main_queue()) { - callback(translation: translation, error: nil) - } - return - } - - dispatch_async(dispatch_get_main_queue()) { - callback(translation: translation, error: .SessionError(error)) - } - return - } + else { return } translation = self.translationFromXML(xmlString) defer { dispatch_async(dispatch_get_main_queue()) { - callback(translation: translation, error: nil) + callback(translation: translation, + error: (error != nil) ? .SessionError(error!) : nil) } } } From aee3402f4876cfa0ca7bb15d78cec9cbb2cac793 Mon Sep 17 00:00:00 2001 From: Reid Chatham Date: Thu, 28 Jul 2016 09:05:12 -0700 Subject: [PATCH 05/12] Should pass tests --- Polyglot/Polyglot.swift | 5 ++--- PolyglotTests/PolyglotTests.swift | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Polyglot/Polyglot.swift b/Polyglot/Polyglot.swift index 55d890e..a68f69a 100644 --- a/Polyglot/Polyglot.swift +++ b/Polyglot/Polyglot.swift @@ -125,9 +125,8 @@ public class Polyglot { } } - guard let data = data, - xmlString = NSString(data: data, encoding: NSUTF8StringEncoding) as? String - else { return } + guard let data = data, xmlString = NSString(data: data, encoding: NSUTF8StringEncoding) as? String + else { return } translation = self.translationFromXML(xmlString) } diff --git a/PolyglotTests/PolyglotTests.swift b/PolyglotTests/PolyglotTests.swift index d02061b..5db4100 100644 --- a/PolyglotTests/PolyglotTests.swift +++ b/PolyglotTests/PolyglotTests.swift @@ -63,8 +63,8 @@ class PolyglotTests: XCTestCase { let polyglot: Polyglot = Polyglot(clientId: "myClientId", clientSecret: "myClientSecret") polyglot.translate("Ik weet het niet") { (translation, error) in - guard let translation = translation else { return } - XCTAssertEqual(translation, "I don't know") +// guard let translation = translation else { return } + XCTAssertEqual(translation!, "I don't know") expectation.fulfill() } From bed20a8f3cad5143feaca69f0e0311340d47cfd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Mon, 19 Sep 2016 18:51:15 +0200 Subject: [PATCH 06/12] Convert classes to Swift 3 --- Polyglot/Polyglot.swift | 26 ++++++++++++------------ Polyglot/Session.swift | 26 ++++++++++++------------ Polyglot/StringExtension.swift | 8 ++++---- PolyglotTests/StringExtensionTests.swift | 2 +- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/Polyglot/Polyglot.swift b/Polyglot/Polyglot.swift index 159f1a1..4ebcd5f 100644 --- a/Polyglot/Polyglot.swift +++ b/Polyglot/Polyglot.swift @@ -117,15 +117,15 @@ public enum Language: String { /** Responsible for translating text. */ -public class Polyglot { +open class Polyglot { let session: Session /// The language to be translated from. It will automatically detect the language if you do not set this. - public var fromLanguage: Language? + open var fromLanguage: Language? /// The language to translate to. - public var toLanguage: Language + open var toLanguage: Language /** @@ -143,7 +143,7 @@ public class Polyglot { - parameter text: The text to translate. - parameter callback: The code to be executed once the translation has completed. */ - public func translate(text: String, callback: ((translation: String) -> (Void))) { + open func translate(_ text: String, callback: @escaping ((_ translation: String) -> (Void))) { session.getAccessToken { token in if self.fromLanguage == nil { self.fromLanguage = text.language @@ -152,15 +152,15 @@ public class Polyglot { let fromLanguageComponent = (self.fromLanguage != nil) ? "&from=\(self.fromLanguage!.rawValue.urlEncoded!)" : "" let urlString = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=\(text.urlEncoded!)\(toLanguageComponent)\(fromLanguageComponent)" - let request = NSMutableURLRequest(URL: NSURL(string: urlString)!) - request.HTTPMethod = "GET" + let request = NSMutableURLRequest(url: URL(string: urlString)!) + request.httpMethod = "GET" request.setValue("Bearer " + token, forHTTPHeaderField: "Authorization") - let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {(data, response, error) in + let task = URLSession.shared.dataTask(with: request, completionHandler: {(data, response, error) in let translation: String guard let data = data, - let xmlString = NSString(data: data, encoding: NSUTF8StringEncoding) as? String + let xmlString = NSString(data: data, encoding: String.Encoding.utf8) as? String else { translation = "" return @@ -169,17 +169,17 @@ public class Polyglot { translation = self.translationFromXML(xmlString) defer { - dispatch_async(dispatch_get_main_queue()) { + DispatchQueue.main.async { callback(translation: translation) } } - } + }) task.resume() } } - private func translationFromXML(XML: String) -> String { - let translation = XML.stringByReplacingOccurrencesOfString("", withString: "") - return translation.stringByReplacingOccurrencesOfString("", withString: "") + fileprivate func translationFromXML(_ XML: String) -> String { + let translation = XML.replacingOccurrences(of: "", with: "") + return translation.replacingOccurrences(of: "", with: "") } } diff --git a/Polyglot/Session.swift b/Polyglot/Session.swift index d54b45a..f831c36 100644 --- a/Polyglot/Session.swift +++ b/Polyglot/Session.swift @@ -28,47 +28,47 @@ class Session { let clientSecret: String var accessToken: String? - var expirationTime: NSDate? + var expirationTime: Date? init(clientId: String, clientSecret: String) { self.clientId = clientId self.clientSecret = clientSecret } - func getAccessToken(callback: ((token: String) -> (Void))) { + func getAccessToken(_ callback: @escaping ((_ token: String) -> (Void))) { if (accessToken == nil || isExpired) { - let url = NSURL(string: "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13") + let url = URL(string: "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13") - let request = NSMutableURLRequest(URL: url!) - request.HTTPMethod = "POST" + let request = NSMutableURLRequest(url: url!) + request.httpMethod = "POST" let bodyString = "client_id=\(clientId.urlEncoded!)&client_secret=\(clientSecret.urlEncoded!)&scope=http://api.microsofttranslator.com&grant_type=client_credentials" - request.HTTPBody = bodyString.dataUsingEncoding(NSUTF8StringEncoding) + request.httpBody = bodyString.data(using: String.Encoding.utf8) - let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {(data, response, error) in + let task = URLSession.shared.dataTask(with: request, completionHandler: {(data, response, error) in guard let data = data, - let resultsDict = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers), + let resultsDict = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers), let expiresIn = resultsDict["expires_in"] as? NSString else { callback(token: "") return } - self.expirationTime = NSDate(timeIntervalSinceNow: expiresIn.doubleValue) + self.expirationTime = Date(timeIntervalSinceNow: expiresIn.doubleValue) let token = resultsDict["access_token"] as! String self.accessToken = token callback(token: token) - } + }) task.resume() } else { - callback(token: accessToken!) + callback(accessToken!) } } - private var isExpired: Bool { - return expirationTime?.earlierDate(NSDate()) == self.expirationTime + fileprivate var isExpired: Bool { + return (expirationTime as NSDate?)?.earlierDate(Date()) == self.expirationTime } } diff --git a/Polyglot/StringExtension.swift b/Polyglot/StringExtension.swift index bfe1830..fbd6424 100644 --- a/Polyglot/StringExtension.swift +++ b/Polyglot/StringExtension.swift @@ -25,16 +25,16 @@ import Foundation extension String { public var urlEncoded: String? { - let urlQueryAllowedCharacterSet: NSMutableCharacterSet = NSCharacterSet.URLQueryAllowedCharacterSet().mutableCopy() as! NSMutableCharacterSet - urlQueryAllowedCharacterSet.removeCharactersInString("&=?+") - return self.stringByAddingPercentEncodingWithAllowedCharacters(urlQueryAllowedCharacterSet) + let urlQueryAllowedCharacterSet: NSMutableCharacterSet = (CharacterSet.urlQueryAllowed as NSCharacterSet).mutableCopy() as! NSMutableCharacterSet + urlQueryAllowedCharacterSet.removeCharacters(in: "&=?+") + return self.addingPercentEncoding(withAllowedCharacters: urlQueryAllowedCharacterSet as CharacterSet) } public var language: Language? { if characters.count > 0 { // Prevent Index Out of Bounds in NSLinguisticTagger let tagger = NSLinguisticTagger(tagSchemes: [NSLinguisticTagSchemeLanguage], options: 0) tagger.string = self - if let result = tagger.tagAtIndex(0, scheme: NSLinguisticTagSchemeLanguage, tokenRange: nil, sentenceRange: nil) { + if let result = tagger.tag(at: 0, scheme: NSLinguisticTagSchemeLanguage, tokenRange: nil, sentenceRange: nil) { return Language(rawValue: result) } } diff --git a/PolyglotTests/StringExtensionTests.swift b/PolyglotTests/StringExtensionTests.swift index 3616a46..a77892e 100644 --- a/PolyglotTests/StringExtensionTests.swift +++ b/PolyglotTests/StringExtensionTests.swift @@ -39,7 +39,7 @@ class StringExtensionTests: XCTestCase { XCTAssertNil("".language?.rawValue, "Empty strings should return nil.") } - func AssertEqualOptional(@autoclosure optional: () -> T?, @autoclosure _ expected: () -> T, file: String = __FILE__, line: UInt = __LINE__) { + func AssertEqualOptional(@autoclosure optional: () -> T?, @autoclosure _ expected: () -> T, file: String = #file, line: UInt = #line) { if let nonOptional = optional() { if nonOptional != expected() { self.recordFailureWithDescription("Optional (\(nonOptional)) is not equal to (\(expected()))", inFile: file, atLine: line, expected: true) From c5fef05ffffe14e5d01f24ccf47889becddbb7cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Wed, 21 Sep 2016 23:33:46 +0200 Subject: [PATCH 07/12] Manually update code to Swift 3 and Xcode 8 --- Polyglot/Polyglot.swift | 6 +++--- Polyglot/Session.swift | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Polyglot/Polyglot.swift b/Polyglot/Polyglot.swift index 4ebcd5f..828d3c5 100644 --- a/Polyglot/Polyglot.swift +++ b/Polyglot/Polyglot.swift @@ -152,7 +152,7 @@ open class Polyglot { let fromLanguageComponent = (self.fromLanguage != nil) ? "&from=\(self.fromLanguage!.rawValue.urlEncoded!)" : "" let urlString = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=\(text.urlEncoded!)\(toLanguageComponent)\(fromLanguageComponent)" - let request = NSMutableURLRequest(url: URL(string: urlString)!) + var request = URLRequest(url: URL(string: urlString)!) request.httpMethod = "GET" request.setValue("Bearer " + token, forHTTPHeaderField: "Authorization") @@ -160,7 +160,7 @@ open class Polyglot { let translation: String guard let data = data, - let xmlString = NSString(data: data, encoding: String.Encoding.utf8) as? String + let xmlString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as? String else { translation = "" return @@ -170,7 +170,7 @@ open class Polyglot { defer { DispatchQueue.main.async { - callback(translation: translation) + callback(translation) } } }) diff --git a/Polyglot/Session.swift b/Polyglot/Session.swift index f831c36..01a1fd6 100644 --- a/Polyglot/Session.swift +++ b/Polyglot/Session.swift @@ -39,7 +39,7 @@ class Session { if (accessToken == nil || isExpired) { let url = URL(string: "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13") - let request = NSMutableURLRequest(url: url!) + var request = URLRequest(url: url!) request.httpMethod = "POST" let bodyString = "client_id=\(clientId.urlEncoded!)&client_secret=\(clientSecret.urlEncoded!)&scope=http://api.microsofttranslator.com&grant_type=client_credentials" @@ -48,18 +48,18 @@ class Session { let task = URLSession.shared.dataTask(with: request, completionHandler: {(data, response, error) in guard let data = data, - let resultsDict = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers), - let expiresIn = resultsDict["expires_in"] as? NSString + let resultsDict = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String: Any], + let expiresIn = resultsDict?["expires_in"] as? NSString else { - callback(token: "") + callback("") return } self.expirationTime = Date(timeIntervalSinceNow: expiresIn.doubleValue) - let token = resultsDict["access_token"] as! String + let token = resultsDict?["access_token"] as! String self.accessToken = token - callback(token: token) + callback(token) }) task.resume() From 5f185631c7e3aa0f2bd1fdc4c703d47a1feb54a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Fri, 29 Apr 2016 07:00:19 +0200 Subject: [PATCH 08/12] Rename file StringExtension This prevents issues with class names appearing twice in the project BartyCrouch where HandySwift already has a StringExtension class. --- Polyglot/{StringExtension.swift => StringExtensionPolyglot.swift} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Polyglot/{StringExtension.swift => StringExtensionPolyglot.swift} (100%) diff --git a/Polyglot/StringExtension.swift b/Polyglot/StringExtensionPolyglot.swift similarity index 100% rename from Polyglot/StringExtension.swift rename to Polyglot/StringExtensionPolyglot.swift From 19fed1d643d92dd7900fcaa0a93530cfbde12c8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Sat, 30 Apr 2016 14:07:04 +0200 Subject: [PATCH 09/12] Revert "Revert comment to main thread call" This reverts commit 3ce889dbc776804554f530291a22055cd70ccb5c. --- Polyglot/Polyglot.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Polyglot/Polyglot.swift b/Polyglot/Polyglot.swift index 828d3c5..a07c4a3 100644 --- a/Polyglot/Polyglot.swift +++ b/Polyglot/Polyglot.swift @@ -138,7 +138,8 @@ open class Polyglot { } /** - Translates a given piece of text. + Translates a given piece of text asynchronously. Switch to the main thread within the callback + if you want to update your UI by using `dispatch_async(dispatch_get_main_queue()) { /* code */ }`. - parameter text: The text to translate. - parameter callback: The code to be executed once the translation has completed. From df05686560c1943eb7175921a697cb65852aaeaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Sat, 30 Apr 2016 14:07:13 +0200 Subject: [PATCH 10/12] Revert "Revert to main thread call" This reverts commit 4c7d4e7166bdf7eb785b0e6b3fe53d24f9a8efca. --- Polyglot/Polyglot.swift | 4 +--- PolyglotTests/PolyglotTests.swift | 6 ++++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Polyglot/Polyglot.swift b/Polyglot/Polyglot.swift index a07c4a3..9f203fc 100644 --- a/Polyglot/Polyglot.swift +++ b/Polyglot/Polyglot.swift @@ -170,9 +170,7 @@ open class Polyglot { translation = self.translationFromXML(xmlString) defer { - DispatchQueue.main.async { - callback(translation) - } + callback(translation) } }) task.resume() diff --git a/PolyglotTests/PolyglotTests.swift b/PolyglotTests/PolyglotTests.swift index b36c9ab..3b37ff9 100644 --- a/PolyglotTests/PolyglotTests.swift +++ b/PolyglotTests/PolyglotTests.swift @@ -91,8 +91,10 @@ class PolyglotTests: XCTestCase { let polyglot: Polyglot = Polyglot(clientId: "myClientId", clientSecret: "myClientSecret") polyglot.translate("Ik weet het niet") { translation in - XCTAssertEqual(translation, "I don't know") - expectation.fulfill() + dispatch_async(dispatch_get_main_queue(), { () -> Void in + XCTAssertEqual(translation, "I don't know") + expectation.fulfill() + }) } waitForExpectationsWithTimeout(1, handler: nil) From 8a0ee9879f973e2e9f90d8136989b5a743f7bcb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Tue, 26 Sep 2017 13:12:16 +0200 Subject: [PATCH 11/12] Upgrade to Swift 4 and Xcode 9 --- Polyglot/Polyglot.swift | 2 +- Polyglot/StringExtensionPolyglot.swift | 6 +- PolyglotSample.xcodeproj/project.pbxproj | 142 ++++++++++++------ .../xcschemes/PolyglotSample.xcscheme | 4 +- PolyglotSample/AppDelegate.swift | 2 +- PolyglotSample/Info.plist | 2 +- PolyglotSample/ViewController.swift | 9 +- PolyglotTests/Info.plist | 2 +- PolyglotTests/PolyglotTests.swift | 8 +- PolyglotTests/StringExtensionTests.swift | 6 +- 10 files changed, 115 insertions(+), 68 deletions(-) diff --git a/Polyglot/Polyglot.swift b/Polyglot/Polyglot.swift index 9f203fc..702ce7f 100644 --- a/Polyglot/Polyglot.swift +++ b/Polyglot/Polyglot.swift @@ -161,7 +161,7 @@ open class Polyglot { let translation: String guard let data = data, - let xmlString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as? String + let xmlString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as String? else { translation = "" return diff --git a/Polyglot/StringExtensionPolyglot.swift b/Polyglot/StringExtensionPolyglot.swift index fbd6424..7b43bbe 100644 --- a/Polyglot/StringExtensionPolyglot.swift +++ b/Polyglot/StringExtensionPolyglot.swift @@ -32,10 +32,10 @@ extension String { public var language: Language? { if characters.count > 0 { // Prevent Index Out of Bounds in NSLinguisticTagger - let tagger = NSLinguisticTagger(tagSchemes: [NSLinguisticTagSchemeLanguage], options: 0) + let tagger = NSLinguisticTagger(tagSchemes: [NSLinguisticTagScheme.language], options: 0) tagger.string = self - if let result = tagger.tag(at: 0, scheme: NSLinguisticTagSchemeLanguage, tokenRange: nil, sentenceRange: nil) { - return Language(rawValue: result) + if let result = tagger.tag(at: 0, scheme: NSLinguisticTagScheme.language, tokenRange: nil, sentenceRange: nil) { + return Language(rawValue: result.rawValue) } } return nil diff --git a/PolyglotSample.xcodeproj/project.pbxproj b/PolyglotSample.xcodeproj/project.pbxproj index 0516092..ff29e0d 100644 --- a/PolyglotSample.xcodeproj/project.pbxproj +++ b/PolyglotSample.xcodeproj/project.pbxproj @@ -7,8 +7,10 @@ objects = { /* Begin PBXBuildFile section */ - 6BF86B27E801452077EAF608 /* Pods_PolyglotTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 925AA115444C33786930F106 /* Pods_PolyglotTests.framework */; }; - 76E093B31C869D9803863B0A /* Pods.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DB9A4E55DA20E420F9D1A394 /* Pods.framework */; }; + 00CAD5FE1F7A6CDA0090DD38 /* Polyglot.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00CAD5FB1F7A6CC40090DD38 /* Polyglot.swift */; }; + 00CAD5FF1F7A6CDA0090DD38 /* Session.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00CAD5FC1F7A6CC40090DD38 /* Session.swift */; }; + 00CAD6001F7A6CDA0090DD38 /* StringExtensionPolyglot.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00CAD5FD1F7A6CC40090DD38 /* StringExtensionPolyglot.swift */; }; + 1FE3B86C180B8FB88CB9DA70 /* Pods_PolyglotTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E862700FF2DDDC6E779C805A /* Pods_PolyglotTests.framework */; }; B60F875D1985F1C200ABB94A /* PolyglotTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B60F875C1985F1C200ABB94A /* PolyglotTests.swift */; }; B6156E3F1999D93B002CE52F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B67FC7841985F20B0030ABF8 /* Main.storyboard */; }; B6682231199C68AE00DFA909 /* StringExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6682230199C68AE00DFA909 /* StringExtensionTests.swift */; }; @@ -18,11 +20,13 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ - 169250360E8B162C09FEC696 /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = ""; }; - 18EAC8F54FC94B5025FBB495 /* Pods-PolyglotTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PolyglotTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-PolyglotTests/Pods-PolyglotTests.debug.xcconfig"; sourceTree = ""; }; - 7B2717494CE9EFC17436B68F /* Pods.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.release.xcconfig; path = "Pods/Target Support Files/Pods/Pods.release.xcconfig"; sourceTree = ""; }; - 906A23634E280AEDC1BA250B /* Pods-PolyglotTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PolyglotTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-PolyglotTests/Pods-PolyglotTests.release.xcconfig"; sourceTree = ""; }; - 925AA115444C33786930F106 /* Pods_PolyglotTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_PolyglotTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 00CAD5F31F7A6BE90090DD38 /* Polyglot.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Polyglot.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 00CAD5F71F7A6C470090DD38 /* Nocilla.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Nocilla.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 00CAD5FB1F7A6CC40090DD38 /* Polyglot.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Polyglot.swift; path = Polyglot/Polyglot.swift; sourceTree = SOURCE_ROOT; }; + 00CAD5FC1F7A6CC40090DD38 /* Session.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Session.swift; path = Polyglot/Session.swift; sourceTree = SOURCE_ROOT; }; + 00CAD5FD1F7A6CC40090DD38 /* StringExtensionPolyglot.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = StringExtensionPolyglot.swift; path = Polyglot/StringExtensionPolyglot.swift; sourceTree = SOURCE_ROOT; }; + 12D767DA42F475C66CF8F400 /* Pods-PolyglotTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PolyglotTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-PolyglotTests/Pods-PolyglotTests.release.xcconfig"; sourceTree = ""; }; + B0A0353E6F1D00A71C35119B /* Pods-PolyglotTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PolyglotTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-PolyglotTests/Pods-PolyglotTests.debug.xcconfig"; sourceTree = ""; }; B60F87441985F1C200ABB94A /* PolyglotSample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PolyglotSample.app; sourceTree = BUILT_PRODUCTS_DIR; }; B60F87561985F1C200ABB94A /* PolyglotTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PolyglotTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; B60F875B1985F1C200ABB94A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -36,6 +40,7 @@ B67FC7881985F20B0030ABF8 /* ViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; B67FC7951985F2F20030ABF8 /* Polyglot-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Polyglot-Bridging-Header.h"; sourceTree = ""; }; DB9A4E55DA20E420F9D1A394 /* Pods.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + E862700FF2DDDC6E779C805A /* Pods_PolyglotTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_PolyglotTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -43,7 +48,6 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 76E093B31C869D9803863B0A /* Pods.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -51,20 +55,29 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 6BF86B27E801452077EAF608 /* Pods_PolyglotTests.framework in Frameworks */, + 1FE3B86C180B8FB88CB9DA70 /* Pods_PolyglotTests.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ - 1BC459906112AA6B922B0A50 /* Pods */ = { + 00CAD5FA1F7A6CB40090DD38 /* Polyglot */ = { isa = PBXGroup; children = ( - 169250360E8B162C09FEC696 /* Pods.debug.xcconfig */, - 7B2717494CE9EFC17436B68F /* Pods.release.xcconfig */, - 18EAC8F54FC94B5025FBB495 /* Pods-PolyglotTests.debug.xcconfig */, - 906A23634E280AEDC1BA250B /* Pods-PolyglotTests.release.xcconfig */, + 00CAD5FB1F7A6CC40090DD38 /* Polyglot.swift */, + 00CAD5FC1F7A6CC40090DD38 /* Session.swift */, + 00CAD5FD1F7A6CC40090DD38 /* StringExtensionPolyglot.swift */, + ); + name = Polyglot; + path = "New Group"; + sourceTree = ""; + }; + 02E4E4E277C02F05F31865E5 /* Pods */ = { + isa = PBXGroup; + children = ( + B0A0353E6F1D00A71C35119B /* Pods-PolyglotTests.debug.xcconfig */, + 12D767DA42F475C66CF8F400 /* Pods-PolyglotTests.release.xcconfig */, ); name = Pods; sourceTree = ""; @@ -73,7 +86,7 @@ isa = PBXGroup; children = ( DB9A4E55DA20E420F9D1A394 /* Pods.framework */, - 925AA115444C33786930F106 /* Pods_PolyglotTests.framework */, + E862700FF2DDDC6E779C805A /* Pods_PolyglotTests.framework */, ); name = Frameworks; sourceTree = ""; @@ -81,11 +94,14 @@ B60F873B1985F1C200ABB94A = { isa = PBXGroup; children = ( + 00CAD5FA1F7A6CB40090DD38 /* Polyglot */, + 00CAD5F71F7A6C470090DD38 /* Nocilla.framework */, + 00CAD5F31F7A6BE90090DD38 /* Polyglot.framework */, B67FC7821985F20B0030ABF8 /* PolyglotSample */, B60F87591985F1C200ABB94A /* PolyglotTests */, B60F87451985F1C200ABB94A /* Products */, AD05005C88874F61B93358E3 /* Frameworks */, - 1BC459906112AA6B922B0A50 /* Pods */, + 02E4E4E277C02F05F31865E5 /* Pods */, ); sourceTree = ""; }; @@ -140,7 +156,6 @@ B60F87401985F1C200ABB94A /* Sources */, B60F87411985F1C200ABB94A /* Frameworks */, B60F87421985F1C200ABB94A /* Resources */, - 2C89B4C9E4725608B65AFC36 /* Embed Pods Frameworks */, ); buildRules = ( ); @@ -155,12 +170,12 @@ isa = PBXNativeTarget; buildConfigurationList = B60F87631985F1C200ABB94A /* Build configuration list for PBXNativeTarget "PolyglotTests" */; buildPhases = ( - 408E75F29D9B38C4B9827AB7 /* Check Pods Manifest.lock */, + 7F91ED9EDFC53CF81FF9D7BE /* [CP] Check Pods Manifest.lock */, B60F87521985F1C200ABB94A /* Sources */, B60F87531985F1C200ABB94A /* Frameworks */, B60F87541985F1C200ABB94A /* Resources */, - 9C872251D11A74FF863B9D27 /* Embed Pods Frameworks */, - CEE1C2C59E9D00E35BF90D16 /* Copy Pods Resources */, + EC7E49862C514AB5DB4B90DF /* [CP] Embed Pods Frameworks */, + 1B62D634051E7C5C83362E0B /* [CP] Copy Pods Resources */, ); buildRules = ( ); @@ -179,14 +194,17 @@ attributes = { LastSwiftMigration = 0710; LastSwiftUpdateCheck = 0710; - LastUpgradeCheck = 0600; + LastUpgradeCheck = 0900; ORGANIZATIONNAME = "Ayaka Nonaka"; TargetAttributes = { B60F87431985F1C200ABB94A = { CreatedOnToolsVersion = 6.0; + DevelopmentTeam = DB7EUU9D79; + LastSwiftMigration = 0900; }; B60F87551985F1C200ABB94A = { CreatedOnToolsVersion = 6.0; + LastSwiftMigration = 0900; TestTargetID = B60F87431985F1C200ABB94A; }; }; @@ -230,66 +248,59 @@ /* End PBXResourcesBuildPhase section */ /* Begin PBXShellScriptBuildPhase section */ - 2C89B4C9E4725608B65AFC36 /* Embed Pods Frameworks */ = { + 1B62D634051E7C5C83362E0B /* [CP] Copy Pods Resources */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( ); inputPaths = ( ); - name = "Embed Pods Frameworks"; + name = "[CP] Copy Pods Resources"; outputPaths = ( ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods/Pods-frameworks.sh\"\n"; + shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-PolyglotTests/Pods-PolyglotTests-resources.sh\"\n"; showEnvVarsInLog = 0; }; - 408E75F29D9B38C4B9827AB7 /* Check Pods Manifest.lock */ = { + 7F91ED9EDFC53CF81FF9D7BE /* [CP] Check Pods Manifest.lock */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( ); inputPaths = ( + "${PODS_PODFILE_DIR_PATH}/Podfile.lock", + "${PODS_ROOT}/Manifest.lock", ); - name = "Check Pods Manifest.lock"; + name = "[CP] Check Pods Manifest.lock"; outputPaths = ( + "$(DERIVED_FILE_DIR)/Pods-PolyglotTests-checkManifestLockResult.txt", ); 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_PODFILE_DIR_PATH}/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# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; showEnvVarsInLog = 0; }; - 9C872251D11A74FF863B9D27 /* Embed Pods Frameworks */ = { + EC7E49862C514AB5DB4B90DF /* [CP] Embed Pods Frameworks */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( ); inputPaths = ( + "${SRCROOT}/Pods/Target Support Files/Pods-PolyglotTests/Pods-PolyglotTests-frameworks.sh", + "${BUILT_PRODUCTS_DIR}/Nocilla/Nocilla.framework", + "${BUILT_PRODUCTS_DIR}/Polyglot/Polyglot.framework", ); - name = "Embed Pods Frameworks"; + name = "[CP] Embed Pods Frameworks"; outputPaths = ( + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Nocilla.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Polyglot.framework", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-PolyglotTests/Pods-PolyglotTests-frameworks.sh\"\n"; showEnvVarsInLog = 0; }; - CEE1C2C59E9D00E35BF90D16 /* Copy Pods Resources */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - ); - name = "Copy Pods Resources"; - outputPaths = ( - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-PolyglotTests/Pods-PolyglotTests-resources.sh\"\n"; - showEnvVarsInLog = 0; - }; /* End PBXShellScriptBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ @@ -297,8 +308,11 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 00CAD5FF1F7A6CDA0090DD38 /* Session.swift in Sources */, + 00CAD5FE1F7A6CDA0090DD38 /* Polyglot.swift in Sources */, B67FC7891985F20B0030ABF8 /* AppDelegate.swift in Sources */, B67FC78D1985F20B0030ABF8 /* ViewController.swift in Sources */, + 00CAD6001F7A6CDA0090DD38 /* StringExtensionPolyglot.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -333,20 +347,30 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; 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_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", @@ -375,13 +399,21 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; 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_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -389,6 +421,7 @@ ENABLE_NS_ASSERTIONS = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; @@ -398,42 +431,49 @@ IPHONEOS_DEPLOYMENT_TARGET = 8.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; VALIDATE_PRODUCT = YES; }; name = Release; }; B60F87611985F1C200ABB94A /* Debug */ = { isa = XCBuildConfiguration; - baseConfigurationReference = 169250360E8B162C09FEC696 /* Pods.debug.xcconfig */; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; CLANG_ENABLE_MODULES = YES; + DEVELOPMENT_TEAM = DB7EUU9D79; INFOPLIST_FILE = PolyglotSample/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "com.ayakanonaka.${PRODUCT_NAME:rfc1034identifier}"; PRODUCT_NAME = PolyglotSample; SWIFT_OBJC_BRIDGING_HEADER = "PolyglotSample/Polyglot-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_SWIFT3_OBJC_INFERENCE = Off; + SWIFT_VERSION = 4.0; }; name = Debug; }; B60F87621985F1C200ABB94A /* Release */ = { isa = XCBuildConfiguration; - baseConfigurationReference = 7B2717494CE9EFC17436B68F /* Pods.release.xcconfig */; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; CLANG_ENABLE_MODULES = YES; + DEVELOPMENT_TEAM = DB7EUU9D79; INFOPLIST_FILE = PolyglotSample/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "com.ayakanonaka.${PRODUCT_NAME:rfc1034identifier}"; PRODUCT_NAME = PolyglotSample; SWIFT_OBJC_BRIDGING_HEADER = "PolyglotSample/Polyglot-Bridging-Header.h"; + SWIFT_SWIFT3_OBJC_INFERENCE = Off; + SWIFT_VERSION = 4.0; }; name = Release; }; B60F87641985F1C200ABB94A /* Debug */ = { isa = XCBuildConfiguration; - baseConfigurationReference = 18EAC8F54FC94B5025FBB495 /* Pods-PolyglotTests.debug.xcconfig */; + baseConfigurationReference = B0A0353E6F1D00A71C35119B /* Pods-PolyglotTests.debug.xcconfig */; buildSettings = { BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/PolyglotSample.app/PolyglotSample"; CLANG_ENABLE_MODULES = YES; @@ -447,16 +487,19 @@ ); INFOPLIST_FILE = PolyglotTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "com.ayakanonaka.${PRODUCT_NAME:rfc1034identifier}"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "PolyglotTests/PolyglotTests-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_SWIFT3_OBJC_INFERENCE = On; + SWIFT_VERSION = 4.0; TEST_HOST = "$(BUNDLE_LOADER)"; }; name = Debug; }; B60F87651985F1C200ABB94A /* Release */ = { isa = XCBuildConfiguration; - baseConfigurationReference = 906A23634E280AEDC1BA250B /* Pods-PolyglotTests.release.xcconfig */; + baseConfigurationReference = 12D767DA42F475C66CF8F400 /* Pods-PolyglotTests.release.xcconfig */; buildSettings = { BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/PolyglotSample.app/PolyglotSample"; CLANG_ENABLE_MODULES = YES; @@ -466,8 +509,11 @@ ); INFOPLIST_FILE = PolyglotTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "com.ayakanonaka.${PRODUCT_NAME:rfc1034identifier}"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "PolyglotTests/PolyglotTests-Bridging-Header.h"; + SWIFT_SWIFT3_OBJC_INFERENCE = On; + SWIFT_VERSION = 4.0; TEST_HOST = "$(BUNDLE_LOADER)"; }; name = Release; diff --git a/PolyglotSample.xcodeproj/xcshareddata/xcschemes/PolyglotSample.xcscheme b/PolyglotSample.xcodeproj/xcshareddata/xcschemes/PolyglotSample.xcscheme index 2f479dc..0b7f278 100644 --- a/PolyglotSample.xcodeproj/xcshareddata/xcschemes/PolyglotSample.xcscheme +++ b/PolyglotSample.xcodeproj/xcshareddata/xcschemes/PolyglotSample.xcscheme @@ -1,6 +1,6 @@ Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { return true } } diff --git a/PolyglotSample/Info.plist b/PolyglotSample/Info.plist index 300cad6..2ca21ce 100644 --- a/PolyglotSample/Info.plist +++ b/PolyglotSample/Info.plist @@ -7,7 +7,7 @@ CFBundleExecutable ${EXECUTABLE_NAME} CFBundleIdentifier - com.ayakanonaka.${PRODUCT_NAME:rfc1034identifier} + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName diff --git a/PolyglotSample/ViewController.swift b/PolyglotSample/ViewController.swift index 3615f59..dac5ac9 100644 --- a/PolyglotSample/ViewController.swift +++ b/PolyglotSample/ViewController.swift @@ -7,7 +7,6 @@ // import UIKit -import Polyglot /** @@ -41,18 +40,18 @@ class ViewController: UIViewController { @IBOutlet weak var inputTextField: UITextField! @IBOutlet weak var translationLabel: UILabel! - @IBAction func didTapTranslateButton(sender: AnyObject) { + @IBAction func didTapTranslateButton(_ sender: AnyObject) { guard let text = inputTextField.text else { return } - UIApplication.sharedApplication().networkActivityIndicatorVisible = true + UIApplication.shared.isNetworkActivityIndicatorVisible = true translator.translate(text) { translation in if let language = self.translator.fromLanguage?.rawValue { - self.translationLabel.text = "Translated from \(language.capitalizedString): \(translation)" + self.translationLabel.text = "Translated from \(language.capitalized): \(translation)" } else { self.translationLabel.text = translation } - UIApplication.sharedApplication().networkActivityIndicatorVisible = false + UIApplication.shared.isNetworkActivityIndicatorVisible = false } } } diff --git a/PolyglotTests/Info.plist b/PolyglotTests/Info.plist index 5293720..6d32c15 100644 --- a/PolyglotTests/Info.plist +++ b/PolyglotTests/Info.plist @@ -7,7 +7,7 @@ CFBundleExecutable ${EXECUTABLE_NAME} CFBundleIdentifier - com.ayakanonaka.${PRODUCT_NAME:rfc1034identifier} + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName diff --git a/PolyglotTests/PolyglotTests.swift b/PolyglotTests/PolyglotTests.swift index 3b37ff9..5467bda 100644 --- a/PolyglotTests/PolyglotTests.swift +++ b/PolyglotTests/PolyglotTests.swift @@ -74,11 +74,11 @@ class PolyglotTests: XCTestCase { } func testTranslate() { - let expectation = expectationWithDescription("translation done") + let expectation = self.expectation(description: "translation done") // Stub POST access token stubRequest("POST", "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13") - .withBody("client_id=myClientId&client_secret=myClientSecret&scope=http://api.microsofttranslator.com&grant_type=client_credentials".dataUsingEncoding(NSUTF8StringEncoding)) + .withBody("client_id=myClientId&client_secret=myClientSecret&scope=http://api.microsofttranslator.com&grant_type=client_credentials".dataUsingEncoding(String.Encoding.utf8)) .andReturn(200) .withHeaders(["Content-Type": "application/json"]) .withBody("{\"access_token\":\"octocatsruleeverythingaroundme\", \"expires_in\":\"600\"}") @@ -91,12 +91,12 @@ class PolyglotTests: XCTestCase { let polyglot: Polyglot = Polyglot(clientId: "myClientId", clientSecret: "myClientSecret") polyglot.translate("Ik weet het niet") { translation in - dispatch_async(dispatch_get_main_queue(), { () -> Void in + DispatchQueue.main.async(execute: { () -> Void in XCTAssertEqual(translation, "I don't know") expectation.fulfill() }) } - waitForExpectationsWithTimeout(1, handler: nil) + waitForExpectations(timeout: 1, handler: nil) } } diff --git a/PolyglotTests/StringExtensionTests.swift b/PolyglotTests/StringExtensionTests.swift index a77892e..bbf2e72 100644 --- a/PolyglotTests/StringExtensionTests.swift +++ b/PolyglotTests/StringExtensionTests.swift @@ -39,14 +39,14 @@ class StringExtensionTests: XCTestCase { XCTAssertNil("".language?.rawValue, "Empty strings should return nil.") } - func AssertEqualOptional(@autoclosure optional: () -> T?, @autoclosure _ expected: () -> T, file: String = #file, line: UInt = #line) { + func AssertEqualOptional(_ optional: @autoclosure () -> T?, _ expected: @autoclosure () -> T, file: String = #file, line: UInt = #line) { if let nonOptional = optional() { if nonOptional != expected() { - self.recordFailureWithDescription("Optional (\(nonOptional)) is not equal to (\(expected()))", inFile: file, atLine: line, expected: true) + self.recordFailure(withDescription: "Optional (\(nonOptional)) is not equal to (\(expected()))", inFile: file, atLine: line, expected: true) } } else { - self.recordFailureWithDescription("Optional value is empty", inFile: file, atLine: line, expected: true) + self.recordFailure(withDescription: "Optional value is empty", inFile: file, atLine: line, expected: true) } } From 4059a1c12575ebfe399b2e33c72b6f46633e017c Mon Sep 17 00:00:00 2001 From: Reid Chatham Date: Sun, 1 Oct 2017 20:36:45 -0700 Subject: [PATCH 12/12] more fixes --- Polyglot/Polyglot.swift | 13 ++++--------- PolyglotSample.xcodeproj/project.pbxproj | 2 -- PolyglotSample/ViewController.swift | 4 +--- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/Polyglot/Polyglot.swift b/Polyglot/Polyglot.swift index 0df6e6c..879f50d 100644 --- a/Polyglot/Polyglot.swift +++ b/Polyglot/Polyglot.swift @@ -144,7 +144,7 @@ open class Polyglot { - parameter text: The text to translate. - parameter callback: The code to be executed once the translation has completed. */ - public func translate(text: String, callback: ((translation: String?, error: TranslationError?) -> (Void))) { + public func translate(text: String, callback: @escaping ((_ translation: String?, _ error: Error?) -> (Void))) { session.getAccessToken { token in if self.fromLanguage == nil { self.fromLanguage = text.language @@ -158,8 +158,7 @@ open class Polyglot { request.setValue("Bearer " + token, forHTTPHeaderField: "Authorization") let task = URLSession.shared.dataTask(with: request, completionHandler: {(data, response, error) in - let error: TranslationError? = (error != nil) ? .SessionError(error!) : nil - var translation : String? + let translation : String? guard let data = data, let xmlString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as String? @@ -168,8 +167,8 @@ open class Polyglot { } translation = self.translationFromXML(xmlString) defer { - dispatch_async(dispatch_get_main_queue()) { - callback(translation: translation, error: error) + DispatchQueue.main.async { + callback(translation, error) } } }) @@ -182,7 +181,3 @@ open class Polyglot { return translation.replacingOccurrences(of: "", with: "") } } - -public enum TranslationError : ErrorType { - case SessionError(NSError) -} \ No newline at end of file diff --git a/PolyglotSample.xcodeproj/project.pbxproj b/PolyglotSample.xcodeproj/project.pbxproj index 1ceea16..af25ef5 100644 --- a/PolyglotSample.xcodeproj/project.pbxproj +++ b/PolyglotSample.xcodeproj/project.pbxproj @@ -21,7 +21,6 @@ /* Begin PBXFileReference section */ 00CAD5F31F7A6BE90090DD38 /* Polyglot.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Polyglot.framework; sourceTree = BUILT_PRODUCTS_DIR; }; - 00CAD5F71F7A6C470090DD38 /* Nocilla.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Nocilla.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 00CAD5FB1F7A6CC40090DD38 /* Polyglot.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Polyglot.swift; path = Polyglot/Polyglot.swift; sourceTree = SOURCE_ROOT; }; 00CAD5FC1F7A6CC40090DD38 /* Session.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Session.swift; path = Polyglot/Session.swift; sourceTree = SOURCE_ROOT; }; 00CAD5FD1F7A6CC40090DD38 /* StringExtensionPolyglot.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = StringExtensionPolyglot.swift; path = Polyglot/StringExtensionPolyglot.swift; sourceTree = SOURCE_ROOT; }; @@ -95,7 +94,6 @@ isa = PBXGroup; children = ( 00CAD5FA1F7A6CB40090DD38 /* Polyglot */, - 00CAD5F71F7A6C470090DD38 /* Nocilla.framework */, 00CAD5F31F7A6BE90090DD38 /* Polyglot.framework */, B67FC7821985F20B0030ABF8 /* PolyglotSample */, B60F87591985F1C200ABB94A /* PolyglotTests */, diff --git a/PolyglotSample/ViewController.swift b/PolyglotSample/ViewController.swift index 8d26048..c1c64e3 100644 --- a/PolyglotSample/ViewController.swift +++ b/PolyglotSample/ViewController.swift @@ -44,8 +44,7 @@ class ViewController: UIViewController { guard let text = inputTextField.text else { return } UIApplication.shared.isNetworkActivityIndicatorVisible = true - translator.translate(text) { translation in - dispatch_async(dispatch_get_main_queue(), { + translator.translate(text: text) { translation,error in if let language = self.translator.fromLanguage?.rawValue { self.translationLabel.text = "Translated from \(language.capitalized): \(translation)" } @@ -53,7 +52,6 @@ class ViewController: UIViewController { self.translationLabel.text = translation } UIApplication.shared.isNetworkActivityIndicatorVisible = false - }) } } }