Skip to content

Commit

Permalink
Convert project to Swift 2
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkowz committed Aug 1, 2015
1 parent fa3f881 commit 80d5432
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Swifternalization/InequalityExpressionParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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
}
}
Expand Down
16 changes: 10 additions & 6 deletions Swifternalization/JSONFileLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down
18 changes: 9 additions & 9 deletions Swifternalization/Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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))
}
Expand All @@ -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
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Swifternalization/RegexExpressionParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion Swifternalization/SharedExpressionsProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class SharedExpressionsProcessor {
let uniqueBaseExpressions = baseLanguageExpressions <! preferedLanguageExpressions

// Expressions from json files.
var loadedExpressions = uniqueBaseExpressions + preferedLanguageExpressions
let loadedExpressions = uniqueBaseExpressions + preferedLanguageExpressions

// Load prefered language nad base built-in expressions. Get unique.
let prefBuiltInExpressions = loadBuiltInExpressions(preferedLanguage)
Expand Down
4 changes: 2 additions & 2 deletions Swifternalization/Swifternalization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final public class Swifternalization {
:param: bundle A bundle when expressions.json and other files are located.
*/
public class func configure(bundle: NSBundle = NSBundle.mainBundle()) {
sharedInstance.load(bundle: bundle)
sharedInstance.load(bundle)
}

/**
Expand Down Expand Up @@ -173,6 +173,6 @@ final public class Swifternalization {
*/
private func getPreferredLanguage(bundle: NSBundle) -> 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
}
}
2 changes: 1 addition & 1 deletion Swifternalization/Translation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions Swifternalization/TranslationsLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
}
}
Expand All @@ -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<String, AnyObject> {
Expand Down
10 changes: 5 additions & 5 deletions SwifternalizationTests/LoadedTranslationsProcessorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")

Expand All @@ -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, "")

Expand All @@ -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, "")
}
Expand Down
8 changes: 4 additions & 4 deletions SwifternalizationTests/RandomNumbers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ func arc4random <T: IntegerLiteralConvertible> (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..<count {
for _ in 0..<count {
nums.append(random(lower: lower, upper: upper))
}
return nums
}

static func randomNumbersStrings(#lower: Float, upper: Float, count: Int) -> [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))
Expand Down
2 changes: 1 addition & 1 deletion SwifternalizationTests/SwifternalizationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SwifternalizationTests: XCTestCase {

override func setUp() {
super.setUp()
Swifternalization.configure(bundle: NSBundle.testBundle())
Swifternalization.configure(NSBundle.testBundle())
}

func testShouldReturnKeyWhenNotTranslated() {
Expand Down

0 comments on commit 80d5432

Please sign in to comment.