From 80d54322581d28bced3536043d9cec9af86c59cb Mon Sep 17 00:00:00 2001 From: Tomasz Szulc Date: Sat, 1 Aug 2015 17:52:50 +0200 Subject: [PATCH] Convert project to Swift 2 --- .../InequalityExpressionParser.swift | 4 ++-- Swifternalization/JSONFileLoader.swift | 16 ++++++++++------ Swifternalization/Regex.swift | 18 +++++++++--------- Swifternalization/RegexExpressionParser.swift | 2 +- .../SharedExpressionsProcessor.swift | 2 +- Swifternalization/Swifternalization.swift | 4 ++-- Swifternalization/Translation.swift | 2 +- Swifternalization/TranslationsLoader.swift | 4 ++-- .../LoadedTranslationsProcessorTests.swift | 10 +++++----- SwifternalizationTests/RandomNumbers.swift | 8 ++++---- .../SwifternalizationTests.swift | 2 +- 11 files changed, 38 insertions(+), 34 deletions(-) diff --git a/Swifternalization/InequalityExpressionParser.swift b/Swifternalization/InequalityExpressionParser.swift index 5d692e1..2af5d8d 100644 --- a/Swifternalization/InequalityExpressionParser.swift +++ b/Swifternalization/InequalityExpressionParser.swift @@ -71,7 +71,7 @@ class InequalityExpressionParser: ExpressionParser { if let value = Regex.matchInString(pattern, pattern: regex, capturingGroupIdx: capturingGroupIdx) { return NSString(string: value).doubleValue } else { - println("\(failureMessage), pattern: \(pattern), regex: \(regex)") + print("\(failureMessage), pattern: \(pattern), regex: \(regex)") return nil } } @@ -89,7 +89,7 @@ class InequalityExpressionParser: ExpressionParser { let sign = InequalitySign(rawValue: rawValue) { return sign } else { - println("\(failureMessage), pattern: \(pattern), regex: \(regex)") + print("\(failureMessage), pattern: \(pattern), regex: \(regex)") return nil } } diff --git a/Swifternalization/JSONFileLoader.swift b/Swifternalization/JSONFileLoader.swift index cd24d65..4a916a2 100644 --- a/Swifternalization/JSONFileLoader.swift +++ b/Swifternalization/JSONFileLoader.swift @@ -44,7 +44,7 @@ final class JSONFileLoader { if let fileURL = bundle.URLForResource(fileName, withExtension: "json") { return load(fileURL) } - println("Cannot find file \(fileName).json.") + print("Cannot find file \(fileName).json.") return nil } @@ -56,12 +56,16 @@ final class JSONFileLoader { */ private class func load(fileURL: NSURL) -> JSONDictionary? { if let data = NSData(contentsOfURL: fileURL) { - var error: NSError? - if let dictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: &error) as? JSONDictionary { - return dictionary + do { + if let dictionary = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? JSONDictionary { + return dictionary + } + print("Cannot parse JSON. It might be broken.") + return nil + } catch { + print("Cannot parse JSON. It might be broken.") + return nil } - print("Cannot parse JSON. It might be broken.") - return nil } print("Cannot load content of file.") return nil diff --git a/Swifternalization/Regex.swift b/Swifternalization/Regex.swift index 9c12be7..b0857bb 100644 --- a/Swifternalization/Regex.swift +++ b/Swifternalization/Regex.swift @@ -23,8 +23,8 @@ final class Regex { class func matchInString(str: String, pattern: String, capturingGroupIdx: Int?) -> String? { var resultString: String? - let range = NSMakeRange(0, count(str)) - regexp(pattern)?.enumerateMatchesInString(str, options: nil, range: range, usingBlock: { result, flags, stop in + let range = NSMakeRange(0, distance(str.startIndex, str.endIndex)) + regexp(pattern)?.enumerateMatchesInString(str, options: NSMatchingOptions.ReportCompletion, range: range, usingBlock: { result, flags, stop in if let result = result { if let capturingGroupIdx = capturingGroupIdx where result.numberOfRanges > capturingGroupIdx { resultString = self.substring(str, range: result.rangeAtIndex(capturingGroupIdx)) @@ -46,7 +46,7 @@ final class Regex { :returns: `String` that matches pattern or nil. */ class func firstMatchInString(str: String, pattern: String) -> String? { - if let result = regexp(pattern)?.firstMatchInString(str, options: .ReportCompletion, range: NSMakeRange(0, count(str))) { + if let result = regexp(pattern)?.firstMatchInString(str, options: .ReportCompletion, range: NSMakeRange(0, distance(str.startIndex, str.endIndex))) { return substring(str, range: result.range) } return nil @@ -61,7 +61,7 @@ final class Regex { */ class func matchesInString(str: String, pattern: String) -> [String] { var matches = [String]() - if let results = regexp(pattern)?.matchesInString(str, options: .ReportCompletion, range: NSMakeRange(0, count(str))) as? [NSTextCheckingResult] { + if let results = regexp(pattern)?.matchesInString(str, options: .ReportCompletion, range: NSMakeRange(0, distance(str.startIndex, str.endIndex))) { for result in results { matches.append(substring(str, range: result.range)) } @@ -77,12 +77,12 @@ final class Regex { :returns: `NSRegularExpression` object or nil if it cannot be created. */ private class func regexp(pattern: String) -> NSRegularExpression? { - var error: NSError? = nil - var regexp = NSRegularExpression(pattern: pattern, options: .CaseInsensitive, error: &error) - if error != nil { - println(error!) + do { + return try NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive) + } catch let error as NSError { + print(error) } - return regexp + return nil } /** diff --git a/Swifternalization/RegexExpressionParser.swift b/Swifternalization/RegexExpressionParser.swift index 2a004d7..4a5b050 100644 --- a/Swifternalization/RegexExpressionParser.swift +++ b/Swifternalization/RegexExpressionParser.swift @@ -50,7 +50,7 @@ class RegexExpressionParser: ExpressionParser { if let regex = Regex.firstMatchInString(pattern, pattern: "(?<=^\(ExpressionPatternType.Regex.rawValue):).*") { return regex } else { - println("Cannot find any regular expression, pattern: \(pattern)") + print("Cannot find any regular expression, pattern: \(pattern)") return nil } } diff --git a/Swifternalization/SharedExpressionsProcessor.swift b/Swifternalization/SharedExpressionsProcessor.swift index c132939..534969b 100644 --- a/Swifternalization/SharedExpressionsProcessor.swift +++ b/Swifternalization/SharedExpressionsProcessor.swift @@ -55,7 +55,7 @@ class SharedExpressionsProcessor { let uniqueBaseExpressions = baseLanguageExpressions CountryCode { // Get preferred language, the one which is set on user's device - return bundle.preferredLocalizations.first as! CountryCode + return bundle.preferredLocalizations.first! as CountryCode } } diff --git a/Swifternalization/Translation.swift b/Swifternalization/Translation.swift index 89840f0..8adc1f0 100644 --- a/Swifternalization/Translation.swift +++ b/Swifternalization/Translation.swift @@ -44,7 +44,7 @@ struct Translation { If variation width is shorter or equal `fittingWidth` take associated value. */ - for variation in expression.lengthVariations.sorted({$0.width < $1.width}) { + for variation in expression.lengthVariations.sort({$0.width < $1.width}) { if variation.width <= fittingWidth! { localizedValue = variation.value } diff --git a/Swifternalization/TranslationsLoader.swift b/Swifternalization/TranslationsLoader.swift index 8af250c..20b3ec3 100644 --- a/Swifternalization/TranslationsLoader.swift +++ b/Swifternalization/TranslationsLoader.swift @@ -22,7 +22,7 @@ final class TranslationsLoader { if let type = detectElementType(dictionary) { loadedTranslations.append(LoadedTranslation(type: type, key: key, content: dictionary)) } else { - println("Translation type is not supported for: \(dictionary)") + print("Translation type is not supported for: \(dictionary)") } } } @@ -42,7 +42,7 @@ final class TranslationsLoader { var strings = 0 // Count every string or dict occurence. - for (key, value) in element { + for (_, value) in element { if value is String { strings++ } else if value is Dictionary { diff --git a/SwifternalizationTests/LoadedTranslationsProcessorTests.swift b/SwifternalizationTests/LoadedTranslationsProcessorTests.swift index 4059782..0370468 100644 --- a/SwifternalizationTests/LoadedTranslationsProcessorTests.swift +++ b/SwifternalizationTests/LoadedTranslationsProcessorTests.swift @@ -72,7 +72,7 @@ class LoadedTranslationsProcessorTests: XCTestCase { let enTranslations = TranslationsLoader.loadTranslations(_enTranslations()) var translations = LoadedTranslationsProcessor.processTranslations(baseTranslations, preferedLanguageTranslations: enTranslations, sharedExpressions: expressions) - translations.sort({$0.key < $1.key}) + translations.sortInPlace({$0.key < $1.key}) XCTAssertEqual(translations.count, 4, "") @@ -99,10 +99,10 @@ class LoadedTranslationsProcessorTests: XCTestCase { // Get patterns of expressions var k3ExpressionPatterns: [String] = k3Translation.expressions.map({ $0.pattern }) - k3ExpressionPatterns.sort({$0 < $1}) + k3ExpressionPatterns.sortInPlace({$0 < $1}) var k3ExpressionsToMatch: [String] = [_enExpressions()["e1"]!, _enExpressions()["e2"]!] - k3ExpressionsToMatch.sort({$0 < $1}) + k3ExpressionsToMatch.sortInPlace({$0 < $1}) XCTAssertEqual(k3ExpressionPatterns, k3ExpressionsToMatch, "") @@ -115,10 +115,10 @@ class LoadedTranslationsProcessorTests: XCTestCase { // Get patterns of expressions var k4ExpressionPatterns: [String] = k4Translation.expressions.map({ $0.pattern }) - k4ExpressionPatterns.sort({$0 < $1}) + k4ExpressionPatterns.sortInPlace({$0 < $1}) var k4ExpressionsToMatch: [String] = [_enExpressions()["e2"]!, _enExpressions()["e3"]!, _enExpressions()["e4"]!] - k4ExpressionsToMatch.sort({$0 < $1}) + k4ExpressionsToMatch.sortInPlace({$0 < $1}) XCTAssertEqual(k4ExpressionPatterns, k4ExpressionsToMatch, "") } diff --git a/SwifternalizationTests/RandomNumbers.swift b/SwifternalizationTests/RandomNumbers.swift index c9d5996..25d613f 100644 --- a/SwifternalizationTests/RandomNumbers.swift +++ b/SwifternalizationTests/RandomNumbers.swift @@ -16,20 +16,20 @@ func arc4random (type: T.Type) -> T { } extension Float { - static func random(#lower: Float, upper: Float) -> Float { + static func random(lower lower: Float, upper: Float) -> Float { let r = Float(arc4random(UInt32)) / Float(UInt32.max) return (r * (upper - lower)) + lower } - static func randomNumbers(#lower: Float, upper: Float, count: Int) -> [Float] { + static func randomNumbers(lower lower: Float, upper: Float, count: Int) -> [Float] { var nums = [Float]() - for i in 0.. [String] { + static func randomNumbersStrings(lower lower: Float, upper: Float, count: Int) -> [String] { var numStr = [String]() for i in randomNumbers(lower: lower, upper: upper, count: count) { numStr.append(String(format: "%f", i)) diff --git a/SwifternalizationTests/SwifternalizationTests.swift b/SwifternalizationTests/SwifternalizationTests.swift index e466c9b..cda6639 100644 --- a/SwifternalizationTests/SwifternalizationTests.swift +++ b/SwifternalizationTests/SwifternalizationTests.swift @@ -14,7 +14,7 @@ class SwifternalizationTests: XCTestCase { override func setUp() { super.setUp() - Swifternalization.configure(bundle: NSBundle.testBundle()) + Swifternalization.configure(NSBundle.testBundle()) } func testShouldReturnKeyWhenNotTranslated() {