用户在不同时区发布评论,而其他用户在不同时区查看评论。转换回设备上的用户时区时,时间应该相同。
这是我正在尝试的但却得到了一个致命的错误:在打开一个可选值"时意外地发现了nil。当它试图减去" time"时,我的距离时间变量发生错误。我在执行之前打印了一段时间,并且说了一遍" nil"。有什么帮助吗?
import UIKit
class 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
}
}
答案 0 :(得分:0)
欢迎使用堆栈溢出。我将你所拥有的东西插入游乐场,它完美无缺。这就是我所拥有的:
//: Playground - noun: a place where people can play
import Cocoa
func returnDistanceTime(distanceTime: NSTimeInterval) -> String{
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return dateFormatter.stringFromDate(NSDate())
}
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()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
let time = dateFormatter.dateFromString(dateString)?.timeIntervalSince1970
let distanceTime = NSDate().timeIntervalSince1970 - time!
let stringTime = returnDistanceTime(distanceTime)
print("**** The time \(stringTime)", terminator: "")
return stringTime
}
getCreationTimeInt("2016-03-01 12:12:12")
答案 1 :(得分:0)
这是来自Aaron的答案,但来自Swift 5
def interpolate(yval, df, xcol, ycol):
return np.exp(np.interp([yval], df[ycol], np.log(df[xcol])))