【问题标题】:How to show Timestamp from Firebase Realtime Database on Xcode in UiTableView如何在 UiTableView 中的 Xcode 上显示 Firebase 实时数据库的时间戳
【发布时间】:2018-08-13 00:16:39
【问题描述】:

好吧,这很难。我正在构建一个社交媒体应用程序,它的提要有一个 Uitableview,它存储在帖子包含的 firebase 实时数据库中

作者 文字:“嗨” 时间戳:“1532194221795”

我的目标是以更吸引人的方式在我的单元格中显示此时间戳,例如(15 分钟前、2 小时前、5 天前、2 个月前)根据单元格的发布时间。

我的 NewPostViewController 中有这个 "timestamp": [".sv":"timestamp"]

像这样let timestamp = dict["timestamp"] as? Double {在我的提要中保存为双精度

它由我的 PostTableViewCell subtitleLabel.text = "\(post.timestamp)" 显示在桌子上

我也收集过这样的信息

`func timeSinceNowString() -> 字符串 { 让 calendar = Calendar.current

    let components = calendar.dateComponents([.day, .hour, .minute, .second], from: self, to: Date())

    if components.day! >= 365 {
        return "\(components.day! / 365) years ago"
    }

    if components.day! >= 7 {
        return "\(components.day! / 7) weeks ago"
    }

    if components.day! > 0 {
        return "\(components.day!) days ago"
    }
    else if components.hour! > 0 {
        return "\(components.hour!) hours ago"
    }
    else if components.minute! > 0 {
        return "\(components.minute!) minutes ago"
    }
    return "Now"
}

and this tooDate(timeIntervalSince1970: createdAt / 1000)`

它给我的错误,比如表达式类型在没有更多上下文的情况下是模棱两可的”对于第一部分和“使用未解析的标识符'createdAt'”对于日期第二部分。有谁知道如何在没有这些错误的情况下进行设置?我永远爱你,感谢阅读。

【问题讨论】:

    标签: ios xcode uitableview firebase timestamp


    【解决方案1】:

    只需将那些 createdAt 更改为您已经转换为 Doubletimestamp

    这是我的游乐场:

    func timeSinceNowString(_ timeInterval:TimeInterval) -> String {
    
        let calendar = Calendar.current
    
        let components = calendar.dateComponents([.day, .hour, .minute, .second], from: Date(timeIntervalSince1970: timeInterval/1000), to: Date())
    
        guard let days = components.day, let hours = components.hour, let minutes = components.minute else {return ""}
    
        switch days {
        case 366...:
            return "\(days/365) years ago"
        case 7...:
            return "\(days/7) \(days/7 == 1 ? "week" : "weeks") ago"
        case 1...:
            return "\(days) \(days == 1 ? "day" : "days") ago"
        default:
            if hours > 0 {
                return "\(hours) \(hours == 1 ? "hour" : "hours") ago"
            }else if minutes > 0 {
                return "\(minutes) \(minutes == 1 ? "minute" : "minutes") ago"
            }else{
                return "now"
            }
        }
    
    }
    
    timeSinceNowString(1532194221795) // "3 weeks ago"
    

    【讨论】:

    • 谢谢!这适用于该错误。在这行代码中,我仍然收到“表达式类型不明确,没有更多上下文”错误 let components = calendar.dateComponents([.day, .hour, .minute, .second], from: self, to: Date( ))
    • “self”指的是什么实例?
    猜你喜欢
    • 1970-01-01
    • 2021-06-11
    • 2020-07-23
    • 1970-01-01
    • 2019-08-09
    • 2020-04-12
    • 2021-12-15
    • 2020-07-24
    • 2022-01-06
    相关资源
    最近更新 更多