ios开发出现fatal error: unexpectedly found nil while unwrapping an Optional value
来源:互联网 发布:sql select from 嵌套 编辑:程序博客网 时间:2024/05/22 08:25
fatal error: unexpectedly found nil while unwrapping an Optional value 错误
由于在模拟器上进行测试时候并没有问题,但在真机上出现崩溃错误,调试后发现是由于未设置时间的时区导致,希望能引起注意
代码如下(此方法是用来将参数中的时间与当前时间比较并返回相应的字符串(在微博中的发布时间中可以用到)):
func getCreateAt(date: String) ->String{
let formatter = NSDateFormatter()
//!!!!!必须设置时区
formatter.locale =NSLocale(localeIdentifier: "cn")
//Z 时区
formatter.dateFormat ="EEE MMM dd HH:mm:ss Z yyyy"
//进行转换
let time = formatter.dateFromString(date)
let canlader = NSCalendar.currentCalendar()
//设置获取的年 月 日
let unit = NSCalendarUnit(arrayLiteral: .Year,.Month,.Day,.Hour,.Minute)
//比较时间
let comps = canlader.components(unit, fromDate: time!, toDate:NSDate(), options: [])
if comps.year ==0{//是本年
if comps.month ==0{//是本月
if comps.day ==0{//是今天
if comps.hour ==0{//1小时之内
if comps.minute ==0{//刚刚
return "刚刚"
}else{//xx分钟之前
return "\(comps.minute)分钟前"
}
}else{
return "\(comps.hour)小时前"
}
}else if comps.day == 1{// 是昨天
formatter.dateFormat = "HH:mm"
let str = formatter.stringFromDate(time!)
return"昨天\(str)"
}else{
formatter.dateFormat = "MM-DD HH:mm"
let str = formatter.stringFromDate(time!)
return "\(str)"
}
}else {//不是本月
formatter.dateFormat = "MM-DD HH:mm"
let str = formatter.stringFromDate(time!)
return "\(str)"
}
}else {//不是今年
formatter.dateFormat = "YY-MM-DD HH:mm"
let str = formatter.stringFromDate(time!)
return "\(str)"
}
return ""
}
- ios开发出现fatal error: unexpectedly found nil while unwrapping an Optional value
- fatal error: unexpectedly found nil while unwrapping an Optional value
- IOS fatal error: unexpectedly found nil while unwrapping an Optional value的一个原因
- Swift Error fatal error: unexpectedly found nil while unwrapping an Optional value
- Swift fatal error: unexpectedly found nil while unwrapping an Optional value?
- unexpectedly found nil while unwrapping an Optional value的解释 && Swift中Optional
- swift3.0 unexpectedly found nil while unwrapping an Optional value的解释 && Swift中Optional
- github中在本地进行上传的时候出现ERROR: Repository not found. fatal: The remote end hung up unexpectedly
- 出现NSScanner: nil string argument libc++abi.dylib: terminate_handler unexpectedly threw an exception
- 【Mood-17】 github中在本地进行上传的时候出现ERROR: Repository not found. fatal: The remote end hung up unexpectedly
- 【iOS开发-27】解决方案:An error was encountered while running(Domain=FBSOpenApplicationErrorDomain, Code=4)
- iOS开发解决方案:An error was encountered while running(Domain=FBSOpenApplicationErrorDomain, Code=4)
- PHP使用出现 Fatal error: Class 'SQLite3' not found
- “An error message cannot be displayed because an optional resource assembly containing it cannot be found“ 怎么解决
- eclipse 出现 !MESSAGE An error occurred while automatically activating bundle
- ubuntu+windows8出现 An error occurred while mounting
- android开发,stopped unexpectedly error
- Apache出现Error:Apache shutdown unexpectedly怎么办?
- ZooKeeper3.4.6学习笔记(一)集群配置
- hdu 1874 畅通工程续
- Windows下使用Hadoop2.6.0-eclipse-plugin插件
- 53.Unique Paths(动态规划)
- 文章标题
- ios开发出现fatal error: unexpectedly found nil while unwrapping an Optional value
- hash+dp打印路径 Codeforces615C Running Track
- NodeJS in Visual Studio
- Android自定义View和ViewGroup——总结于Dave Smith的视频
- H264之ffmpeg解码
- 【HTML/XML 4】实例分析HTML和XML的不同
- vector<KeyPoint> vecKeyPoints 的数据结构
- 进程调度相关命令解析
- 算法 - 矩阵乘法