Ios 按时间戳组织firebase数据
我在firebase中有一些数据,我正试图按时间戳组织这些数据。我已将此代码合并到cellForRowAt中Ios 按时间戳组织firebase数据,ios,swift,firebase,Ios,Swift,Firebase,我在firebase中有一些数据,我正试图按时间戳组织这些数据。我已将此代码合并到cellForRowAt中 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if let seconds = post["pub_time"] as? Double { let timeStampDate = NSDate(
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let seconds = post["pub_time"] as? Double {
let timeStampDate = NSDate(timeIntervalSince1970: seconds/1000)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
let formating = timeStampDate as Date
let timeAgo = timeAgoSinceDate(formating)
cell.Time.text = timeAgo
posts.sort(by: { $0.timeStampDate.compare($1.timeStampDate) == .orderedAscending })
}
但是在posts.sort(按:{$0.timeStampDate.compare($1.timeStampDate)==.orderedascensing})
上,我不断得到错误表达式类型bool在没有更多上下文的情况下是不明确的,我不确定我做错了什么
这或多或少就是我的视图控制器的样子
var posts = NSMutableArray()
func loadData(){
Database.database().reference().child("main").child("posts").queryOrdered(byChild: "pub_time").observeSingleEvent(of: .value, with: { snapshot in
if let postsDictionary = snapshot .value as? [String: AnyObject] {
for post in postsDictionary {
posts.add(post.value)
}
self.TableView.reloadData()
}
})
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Configure the cell...
let cell = tableView.dequeueReusableCell(withIdentifier: "Cells", for: indexPath) as! FeedTableViewCell
//Configure the cell
let post = posts[indexPath.row] as! [String: AnyObject]
cell.Title.text = post["title"] as? String
if let seconds = post["pub_time"] as? Double {
let timeStampDate = NSDate(timeIntervalSince1970: seconds/1000)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
let formating = timeStampDate as Date
let timeAgo = timeAgoSinceDate(formating)
cell.Time.text = timeAgo
}
return cell
}
以下是如何在适当的位置对对象数组进行排序;acending是默认顺序 假设你有这样一个邮政类
class PostClass {
var timeStampDate = "" //assume the timestamps are yyyyMMddhhmmss strings
var post = ""
}
它们存储在posts数组中
var posts = [PostClass]()
对数组中的posts对象进行排序的代码为:
posts.sort(by: {$0.timeStampDate < $1.timeStampDate })
我们想加载我们的帖子,按时间戳排序
let postsRef = self.ref.child("posts")
let postQuery = postsRef.queryOrdered(byChild: "timestamp")
postQuery.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let snap = child as! DataSnapshot
let dict = snap.value as! [String: Any]
let post = dict["post"] as! String
let ts = dict["timestamp"] as! Int
print(ts, post)
}
})
以及输出
1 The post for you
2 Posting about how to post
3 A post about some stuff
在本例中,我使用1、2、3作为时间戳,但yyyyMMddhhmmss(20180618101000)格式也可以工作,并提供人类可读的、正确的排序。您的代码和问题中有很多奇怪之处;首先,这个问题与Firebase无关,所以标签应该被移除。第二,您正在为tableView中的每个单元格重新使用posts数据源数组,这可能意味着当显示第4行的单元格时,数据源顺序可能会发生更改,现在可以是第2行。第三,如果这是时间戳,则将其存储为时间戳:例如20180617095100。这使得排序很容易。您还无缘无故地将较旧的NS对象与较新的Swift对象混合在一起。您在这里尝试做什么也不清楚-您是否试图计算帖子发布的时间,然后在您的手机中打印时间差(即1小时前或3天前)?@jay是的,但这不是我的问题。我已经知道如何显示这篇文章是3天前还是一周前发表的。我试图做的是根据时间戳组织表视图,以显示最近发布到较旧帖子的帖子。然后a)按时间戳顺序从Firebase加载,或者b)从Firebase加载,排序,然后更新您的表视图。不要在显示期间排序,而要在显示之前排序。这里的一个问题是将快照转换为字典。那样你就不会下订单了。最佳实践是迭代snapshot.children并填充posts数据源数组。这将维持他们的秩序。这里有很多关于如何做的例子。当我这样做时,我得到一个错误参数标签(by:)与任何可用的不匹配overloads@juelizabeth该消息表示您正在运行旧版本的Xcode或Swift。这是目前在Xcode 9.4.1中使用的Swift 4。Swift 3的版本是什么?我已经更新了我的问题,以显示如何加载我的表格我的帖子是一个NSMUTABLEARRY(),以及当我使用上述代码组织我的
时间帖子时。排序(按:{$0.timeStampDate<$1.timeStampDate})
它给我一个错误,标记为(按:)不匹配任何可用的重载
1 The post for you
2 Posting about how to post
3 A post about some stuff