我剛剛完成我的第一個應用程序,並已本地化不同的功能。我在我的應用程序中有一個功能,我不確定我是否可以本地化。本地化問候消息的問題
基本上,當用戶打開我的應用程序時,他們會收到一條消息,指出'下午好','早上好'或'晚上好'。我創建了一些代碼來檢查時間的前綴以決定顯示什麼消息,但由於不同的國家對時間進行了不同的格式化,我不確定如何對其進行本地化。
我必須弄清楚它的工作所在的國家並添加一個if語句來決定應用程序是否可以顯示此問候語嗎?否則,根據他們的名字顯示一個問候呢?
這裏是我的代碼:
var date = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = .ShortStyle
let time = dateFormatter.stringFromDate(date)
var currentTimeOfDay = ""
if time.hasPrefix("0") {
currentTimeOfDay = "morning"
} else if time.hasPrefix("10") {
currentTimeOfDay = "morning"
} else if time.hasPrefix("11") {
currentTimeOfDay = "morning"
} else if time.hasPrefix("12") {
currentTimeOfDay = "morning"
} else if time.hasPrefix("13") {
currentTimeOfDay = "afternoon"
} else if time.hasPrefix("14") {
currentTimeOfDay = "afternoon"
} else if time.hasPrefix("15") {
currentTimeOfDay = "afternoon"
} else if time.hasPrefix("16") {
currentTimeOfDay = "afternoon"
} else if time.hasPrefix("17") {
currentTimeOfDay = "afternoon"
} else if time.hasPrefix("18") {
currentTimeOfDay = "evening"
} else if time.hasPrefix("19") {
currentTimeOfDay = "evening"
} else if time.hasPrefix("2") {
currentTimeOfDay = "evening"
}
啊!非常感謝!我應該在我的原始代碼中使用範圍運算符,但我完全忘記了它。 – user3746428 2014-08-29 15:04:15
@ user3746428:不客氣。也可以看一下'NSLocalizedString()'方法來獲取本地化到設備設置的輸出。 – 2014-08-29 15:05:58
我剛剛在iOS 7設備上第一次嘗試了我的應用程序,並且收到錯誤消息。當我註釋掉這段代碼時,應用程序運行正常,但是當我離開時,出現此錯誤' - [_ NSCopyOnWriteCalendarWrapper component:fromDate:]:無法識別的選擇器發送到實例0x175b5580' – user3746428 2014-08-29 21:07:15