我在嘗試將fajerTime
轉換爲NSDate
。當我編譯項目dateValue
是nil
。任何想法如何解決這個問題?如何將字符串轉換爲NSdate?
if prayerCommingFromAdan.id == 0 && prayerCommingFromAdan.ringToneId != 0{
// NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(_:)), name:"NotificationIdentifier", object: nil)
let fajerTime = "\(prayer0.time[0...1]):\(prayer0.time[3...4])" as String
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
dateFormatter.timeZone = NSTimeZone.localTimeZone()
// convert string into date
let dateValue = dateFormatter.dateFromString(fajerTime) as NSDate!
print(dateValue)
var dateComparisionResult:NSComparisonResult = NSDate().compare(dateValue)
if dateComparisionResult == NSComparisonResult.OrderedDescending {
addNotificationAlarm(year, month: month, day: day, hour: prayer0.time[0...1], minutes: prayer0.time[3...4], soundId: prayerCommingFromAdan.ringToneId, notificationBody: "It is al fajr adan")
}
的可能的複製[如何字符串轉換爲NSDate的在iphone?(http://stackoverflow.com/questions/6288371/how-to-convert-string-to-nsdate-in-iphone ) –
顯示'fajerTime'的內容 – Arsen
問題可能是'fajerTime'的格式。看起來'fajerTime'是一個時間字符串,例如'12:34',而日期格式化器被配置爲接受包含月份,日期和年份的字符串,例如, '24-07-2016'。您需要根據「MM-dd-yyyy」格式格式化「fajerTime」,或者配置日期格式化程序以接受一段時間,例如, 'dateFormatter.dateFormat =「hh:mm」'。 –