【发布时间】:2016-07-04 13:47:13
【问题描述】:
一个用户在不同的时区发表评论,而另一个用户在不同的时区查看评论。转换回设备上的用户时区时,时间应该相同。
这是我正在尝试的,但收到“致命错误:在展开可选值时意外发现 nil”。当它试图减去“时间”时,错误发生在我的距离时间变量中。我在它执行之前打印了时间,上面写着“nil”。请问有什么帮助吗?
import UIKit
类 EventCommentCell: UITableViewCell {
var obj : EventCommentObj!
@IBOutlet var profileImage: UIImageView!
@IBOutlet var commentMessage: UITextView!
@IBOutlet var time: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func setData(){
profileImage.layer.borderWidth = 1
profileImage.layer.masksToBounds = false
profileImage.layer.borderColor = UIColor(red: 0.125, green: 0.757, blue: 0.569, alpha: 1.0).CGColor
profileImage.layer.cornerRadius = profileImage.frame.height/2
profileImage.clipsToBounds = true
print("The Image URL : \(obj.profileImage)")
ImageLoader.sharedLoader.imageForUrl(obj.profileImage, completionHandler:{(image: UIImage?, url: String) in
self.profileImage.image = image
})
let message = "\(obj.username) \n\(obj.comment)"
let attributedString = NSMutableAttributedString(string: message as String)
let range: NSRange = (message as NSString).rangeOfString(obj.username as String)
let range1: NSRange = (message as NSString).rangeOfString(obj.comment as String)
let font = UIFont.boldSystemFontOfSize(commentMessage.font!.pointSize)
attributedString.addAttribute(NSFontAttributeName, value: font, range: range)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor(red: 0.125, green: 0.757, blue: 0.569, alpha: 1.0), range: range)
commentMessage.attributedText = attributedString
time.text = getCreationTimeInt(obj.created_on)
}
func getCreationTimeInt(dateString : String) -> String{
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale.currentLocale()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
dateFormatter.timeZone = NSTimeZone.systemTimeZone()
let time = dateFormatter.dateFromString(dateString)?.timeIntervalSince1970
let distanceTime = NSDate().timeIntervalSince1970 - time!
let stringTime = returnDistanceTime(distanceTime)
print("**** The time \(stringTime)", terminator: "")
return stringTime
}
func returnDistanceTime(distanceTime : Double) -> String{
var stringTime = ""
if (distanceTime < 0) {
stringTime = "0s"
}else{
if(distanceTime < 60){
stringTime = "\(Int(distanceTime))s"
}else{
if(distanceTime < 3600){
stringTime = "\(Int(distanceTime/60))m"
}else{
if(distanceTime < 3600*24){
stringTime = "\(Int(distanceTime/3600))h"
}else{
if(distanceTime < 3600*24*7) {
stringTime = "\(Int(distanceTime/3600/24))d"
}else{
stringTime = "\(Int(distanceTime/3600/24/7))w"
}
}
}
}
}
return stringTime
}
}
【问题讨论】:
-
您将日期存储在哪里?通常,您只需将日期存储为 NSDate(如果支持)或包含时区的字符串,然后您可以轻松地将其转换回 NSDate。你不应该在格林威治标准时间的几秒钟内搞砸
-
只有在使用“!”时才会出现该错误强制展开。因此
time为零。我的猜测是dateString不符合你的格式,所以dateFromString失败。 -
如果我在计算机上将时区更改为 UTC,然后在模拟器中再次运行我的代码,它将无法正常工作并打印时间。它只是不会将其转换为 GMT。
-
dateString长什么样子? -
如果您解释了您正在尝试做什么,将会有所帮助。您是否尝试获取本地时间并将其转换为 UTC 字符串进行存储,然后稍后在本地时区重新显示?无论如何,你设置了两次
timeZone,这可能是错误的,而且你不太可能真正想要systemTimeZone(localTimeZone更有可能)。
标签: ios xcode swift timezone nsdate