:雨燕3.0無法得到所需的對象指針(代替NULL):無法加載的「自我」,因爲這段代碼執行時,其價值無法評估
public enum Month : String {
case January = "JAN"
case February = "FEB"
case March = "MAR"
case April = "APR"
case May = "MAY"
case June = "JUN"
case July = "JUL"
case August = "AUG"
case September = "SEP"
case October = "OCT"
case November = "NOV"
case December = "DEC"
func toDate() -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd.MMM.yyyy hh:mm a"
let dateString = "01.\(self.rawValue).2016 12:00 PM"
return dateFormatter.date(from: dateString)!
}
}
我越來越:
fatal error: unexpectedly found nil while unwrapping an Optional value
當我嘗試打印「po dateFormatter.date(from:dateString)!」當碰撞發生的事情:
fatal error: unexpectedly found nil while unwrapping an Optional value error: warning: couldn't get required object pointer (substituting NULL): Couldn't load 'self' because its value couldn't be evaluated
error: Execution was interrupted, reason: EXC_BREAKPOINT (code=1, subcode=0x100c551ec). The process has been returned to the state before expression evaluation.
這僅發生在運行iOS 9.1.1我的iPhone 6S,而不是發生在任何模擬器和運行iOS 9.xx
任何人有任何的設備上想法是怎麼回事以及什麼「無法加載」自我「,因爲它的價值無法評估」的意思。
我在一個新的項目中用這個代碼試過了,我仍然可以重現這個錯誤。
即使硬編碼的一個月,而自體部分給出了同樣的錯誤:
let dateString = "01.NOV.2016 12:00 PM"
UPDATE
對於任何人來此建立一個Date對象的正確方法是這樣的:
let calendar = Calendar.current
var components = DateComponents()
components.day = 1
components.month = month
components.year = year
components.hour = 12
components.minute = 00
let date = calendar.date(from: components)!
驗證您的代碼在10.0和它的工作對我很好。 –
@Sharpkits我沒有10.0設備,只嘗試10.1.1 – Simon
>'let calendar = Calendar.current'。如果你有特定的日期,我不建議在這裏使用'current'日曆,我想你知道你使用的日曆。但是'當前日曆'可能與它不同,並且伊斯蘭曆日曆2016年第11個月的第1天與公曆不同,差異很大。 – user28434