ios - 使用上传日期不断修改子项

标签 ios swift firebase swift3 firebase-realtime-database

我目前正在构建 Instagram 克隆。 我刚辞职,所以如果这个问题回答起来很容易,请原谅我。

我只想为每个帖子附加一个特定的分数。 我已经设法给每个新帖子打分 0f 0 开始,每次它喜欢它增加 100,不喜欢它减少 100。 每条评论增加 50 分。 因为我想巧妙地对它们进行排序,并希望它随着时间的推移在顶部显示不同的帖子,所以我想包含影响分数的第三个变量。

我希望它在上传后每小时将分数降低 -10。

增加和减少是在我的函数 incrementLikes()/incrementComments() 中完成的。 我知道自从它上传到那里以来我无法修改 score 的值,但我不知道还有什么地方。

我的日期延期(我可以在哪里做?)

延期日期{

func timeAgoDisplay() -> String {

    let secondsAgo = Int(Date().timeIntervalSince(self))

    let minute = 60
    let hour = 60 * minute
    let day = hour * 24
    let week = day * 7
    let month = week * 4

    let quotient : Int
    let unit: String

    if secondsAgo < minute {
        quotient = secondsAgo
        unit = "Sekunde"
    } else if secondsAgo < hour {
        quotient = secondsAgo / minute
        unit = "Minute"
    } else if secondsAgo < day {
        quotient = secondsAgo / hour
        unit = "Stunde"
    } else if secondsAgo < week {
        quotient = secondsAgo / day
        unit = "Tage"
    } else if secondsAgo < month {
        quotient = secondsAgo / week
        unit = "Woche"
    } else  {
        quotient = secondsAgo / month
        unit = "Monat"
    }

    return "Vor \(quotient) \(unit)\(quotient == 1 ? "" : "n")"
}

}

我在 homeTableViewCell 中设置日期的函数

函数更新 View (){

    captionLabel.userHandleLinkTapHandler = { label,string, range in
        let mention = String(string.characters.dropFirst())

        API.User.observeUserByUsername(username: mention.lowercased(), completion: { (user) in
            self.delegate?.goToProfileUserViewController(userId: user.id!)

        })
    }

    guard let count = post?.commentCount else {return}
    if count == 0 {
        commentButton.setTitle("Schreibe den ersten Kommentar", for: UIControlState.normal)
    }else if count == 1 {
        commentButton.setTitle("Sieh dir den ersten Kommentar an", for: UIControlState.normal)

    } else {
        commentButton.setTitle("Alle \(count) Kommentare ansehen", for: UIControlState.normal)
    }

    let timeAgoDisplay = post?.creationDate?.timeAgoDisplay()

    timeLabel.text = timeAgoDisplay
    }

感谢您的帮助:)

最佳答案

使用时间每小时更新分数的想法是基于应用程序不断运行的想法,这似乎有缺陷。 你可以改用类似的东西

var score: Int {
    return likes*100 - dislikes*100 + comments*50 - hoursSinceUpload()*10
}

其中 hoursSinceUpload 由类似的东西计算(参见 Getting the difference between two NSDates in (months/days/hours/minutes/seconds) 以供引用)

func hoursSinceUpload() -> Int {
    return Calendar.current.dateComponents([.hour], from: uploadDate, to: Date()).hour
}

关于ios - 使用上传日期不断修改子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43782492/

相关文章:

ios - Firebase 查询快照为零?

ios - 使用自动布局扩展方形 View 以填充矩形 super View

ios - 如何为UINavigation栏添加阴影效果

ios - "Type ' 任务。在 Swift 中键入 ' has no subscript members"错误

ios - 在多个类中添加 touchesBegan

ios - 在 Swift 中创建 AudioQueueBufferRef 时遇到问题

swift - 基于复杂图像的按钮布局

swift - 根据内容检测文本方向

android - Firebase 数据库离线写入

ios - iOS 的 Firebase 卡在 `Run your app to verify installation`