From d0df01d23b4910b7af06746fbe10869860449c4c Mon Sep 17 00:00:00 2001 From: David Keegan Date: Sun, 17 Jul 2016 12:52:39 -0700 Subject: [PATCH] update to swift3 --- NSDate+Extension.swift | 58 +++++++++---------- NSDateTimeAgo.xcodeproj/project.pbxproj | 11 +++- .../xcschemes/NSDateTimeAgo-OSX.xcscheme | 2 +- .../xcschemes/NSDateTimeAgo.xcscheme | 2 +- Tests/NSDateTimeAgoTests.swift | 10 ++-- 5 files changed, 46 insertions(+), 37 deletions(-) diff --git a/NSDate+Extension.swift b/NSDate+Extension.swift index 7db5c01..3f7349d 100644 --- a/NSDate+Extension.swift +++ b/NSDate+Extension.swift @@ -9,30 +9,30 @@ import Foundation -func NSDateTimeAgoLocalizedStrings(key: String) -> String { +func NSDateTimeAgoLocalizedStrings(_ key: String) -> String { let resourcePath: String? - if let frameworkBundle = NSBundle(identifier: "com.kevinlawler.NSDateTimeAgo") { + if let frameworkBundle = Bundle(identifier: "com.kevinlawler.NSDateTimeAgo") { // Load from Framework resourcePath = frameworkBundle.resourcePath } else { // Load from Main Bundle - resourcePath = NSBundle.mainBundle().resourcePath + resourcePath = Bundle.main().resourcePath } if resourcePath == nil { return "" } - let path = NSURL(fileURLWithPath: resourcePath!).URLByAppendingPathComponent("NSDateTimeAgo.bundle") - guard let bundle = NSBundle(URL: path) else { + let path = try! URL(fileURLWithPath: resourcePath!).appendingPathComponent("NSDateTimeAgo.bundle") + guard let bundle = Bundle(url: path) else { return "" } return NSLocalizedString(key, tableName: "NSDateTimeAgo", bundle: bundle, comment: "") } -extension NSDate { +extension Date { // shows 1 or two letter abbreviation for units. // does not include 'ago' text ... just {value}{unit-abbreviation} @@ -41,33 +41,33 @@ extension NSDate { let components = self.dateComponents() if components.year > 0 { - return stringFromFormat("%%d%@yr", withValue: components.year) + return self.string(fromFormat: "%%d%@yr", withValue: components.year!) } if components.month > 0 { - return stringFromFormat("%%d%@mo", withValue: components.month) + return self.string(fromFormat: "%%d%@mo", withValue: components.month!) } // TODO: localize for other calanders if components.day >= 7 { - let value = components.day/7 - return stringFromFormat("%%d%@w", withValue: value) + let value = components.day!/7 + return self.string(fromFormat: "%%d%@w", withValue: value) } if components.day > 0 { - return stringFromFormat("%%d%@d", withValue: components.day) + return self.string(fromFormat: "%%d%@d", withValue: components.day!) } if components.hour > 0 { - return stringFromFormat("%%d%@h", withValue: components.hour) + return self.string(fromFormat: "%%d%@h", withValue: components.hour!) } if components.minute > 0 { - return stringFromFormat("%%d%@m", withValue: components.minute) + return self.string(fromFormat: "%%d%@m", withValue: components.minute!) } if components.second > 0 { - return stringFromFormat("%%d%@s", withValue: components.second ) + return self.string(fromFormat: "%%d%@s", withValue: components.second! ) } return "" @@ -80,7 +80,7 @@ extension NSDate { if components.year < 2 { return NSDateTimeAgoLocalizedStrings("Last year") } else { - return stringFromFormat("%%d %@years ago", withValue: components.year) + return self.string(fromFormat: "%%d %@years ago", withValue: components.year!) } } @@ -88,17 +88,17 @@ extension NSDate { if components.month < 2 { return NSDateTimeAgoLocalizedStrings("Last month") } else { - return stringFromFormat("%%d %@months ago", withValue: components.month) + return self.string(fromFormat: "%%d %@months ago", withValue: components.month!) } } // TODO: localize for other calanders if components.day >= 7 { - let week = components.day/7 + let week = components.day!/7 if week < 2 { return NSDateTimeAgoLocalizedStrings("Last week") } else { - return stringFromFormat("%%d %@weeks ago", withValue: week) + return self.string(fromFormat: "%%d %@weeks ago", withValue: week) } } @@ -106,7 +106,7 @@ extension NSDate { if components.day < 2 { return NSDateTimeAgoLocalizedStrings("Yesterday") } else { - return stringFromFormat("%%d %@days ago", withValue: components.day) + return self.string(fromFormat: "%%d %@days ago", withValue: components.day!) } } @@ -114,7 +114,7 @@ extension NSDate { if components.hour < 2 { return NSDateTimeAgoLocalizedStrings("An hour ago") } else { - return stringFromFormat("%%d %@hours ago", withValue: components.hour) + return self.string(fromFormat: "%%d %@hours ago", withValue: components.hour!) } } @@ -122,7 +122,7 @@ extension NSDate { if components.minute < 2 { return NSDateTimeAgoLocalizedStrings("A minute ago") } else { - return stringFromFormat("%%d %@minutes ago", withValue: components.minute) + return self.string(fromFormat: "%%d %@minutes ago", withValue: components.minute!) } } @@ -130,25 +130,25 @@ extension NSDate { if components.second < 5 { return NSDateTimeAgoLocalizedStrings("Just now") } else { - return stringFromFormat("%%d %@seconds ago", withValue: components.second) + return self.string(fromFormat: "%%d %@seconds ago", withValue: components.second!) } } return "" } - private func dateComponents() -> NSDateComponents { - let calander = NSCalendar.currentCalendar() - return calander.components([.Second, .Minute, .Hour, .Day, .Month, .Year], fromDate: self, toDate: NSDate(), options: []) + private func dateComponents() -> DateComponents { + let calander = Calendar.current() + return calander.components([.second, .minute, .hour, .day, .month, .year], from: self, to: Date(), options: []) } - private func stringFromFormat(format: String, withValue value: Int) -> String { - let localeFormat = String(format: format, getLocaleFormatUnderscoresWithValue(Double(value))) + private func string(fromFormat format: String, withValue value: Int) -> String { + let localeFormat = String(format: format, getLocaleFormatUnderscores(withValue: Double(value))) return String(format: NSDateTimeAgoLocalizedStrings(localeFormat), value) } - private func getLocaleFormatUnderscoresWithValue(value: Double) -> String { - guard let localeCode = NSLocale.preferredLanguages().first else { + private func getLocaleFormatUnderscores(withValue value: Double) -> String { + guard let localeCode = Locale.preferredLanguages().first else { return "" } diff --git a/NSDateTimeAgo.xcodeproj/project.pbxproj b/NSDateTimeAgo.xcodeproj/project.pbxproj index 98250cd..1f346c4 100644 --- a/NSDateTimeAgo.xcodeproj/project.pbxproj +++ b/NSDateTimeAgo.xcodeproj/project.pbxproj @@ -187,7 +187,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0700; - LastUpgradeCheck = 0700; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = "Kevin Lawler"; TargetAttributes = { 535788311CD03AAB00F72193 = { @@ -195,9 +195,11 @@ }; 833CD3771BCD9BD90085A751 = { CreatedOnToolsVersion = 7.0.1; + LastSwiftMigration = 0800; }; 83AC39081BD066C500A9C7F7 = { CreatedOnToolsVersion = 7.0.1; + LastSwiftMigration = 0800; }; }; }; @@ -323,6 +325,7 @@ PRODUCT_NAME = NSDateTimeAgo; SDKROOT = macosx; SKIP_INSTALL = YES; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; }; name = Release; }; @@ -431,6 +434,7 @@ PRODUCT_NAME = NSDateTimeAgo; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -449,6 +453,8 @@ PRODUCT_BUNDLE_IDENTIFIER = com.kevinlawler.NSDateTimeAgo; PRODUCT_NAME = NSDateTimeAgo; SKIP_INSTALL = YES; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -459,6 +465,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.kevinlawler.NSDateTimeAgoTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -469,6 +476,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.kevinlawler.NSDateTimeAgoTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/NSDateTimeAgo.xcodeproj/xcshareddata/xcschemes/NSDateTimeAgo-OSX.xcscheme b/NSDateTimeAgo.xcodeproj/xcshareddata/xcschemes/NSDateTimeAgo-OSX.xcscheme index aa99ebe..98920d1 100644 --- a/NSDateTimeAgo.xcodeproj/xcshareddata/xcschemes/NSDateTimeAgo-OSX.xcscheme +++ b/NSDateTimeAgo.xcodeproj/xcshareddata/xcschemes/NSDateTimeAgo-OSX.xcscheme @@ -1,6 +1,6 @@ Void) -> NSDate? { - let calander = NSCalendar.currentCalendar() - let components = NSDateComponents() - block(components: components) - return calander.dateByAddingComponents(components, toDate: NSDate(), options: []) + func dateForComponents(block: (inout components: DateComponents) -> Void) -> Date? { + let calander = Calendar.current() + var components = DateComponents() + block(components: &components) + return calander.date(byAdding: components, to: Date(), options: []) } // MARK: - timeAgoSimple