Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 附加到数组的条件_Swift_Xcode_Dictionary - Fatal编程技术网

Swift 附加到数组的条件

Swift 附加到数组的条件,swift,xcode,dictionary,Swift,Xcode,Dictionary,我将数据从Firebase加载到我的自定义“CommentModel”(新成员): // Load the comment with id func observeComment(commentId: String, completion: @escaping (CommentModel) -> Void) { let db = Firestore.firestore() db.collection("comments").document(commentId).getDo

我将数据从Firebase加载到我的自定义“CommentModel”(新成员):

// Load the comment with id
func observeComment(commentId: String, completion: @escaping (CommentModel) -> Void) {
    let db = Firestore.firestore()
    db.collection("comments").document(commentId).getDocument { (snapshot, error) in
        guard let dic = snapshot?.data() else { return }
        let newComment = CommentModel(dictionary: dic)

        completion(newComment)
    }
}
import UIKit

class CommentModel {

var postId: String?
var userUid: String?
var postText: String?
var postDate: Double?

init(dictionary: [String: Any]) {
    postId = dictionary["postId"] as? String
    userUid = dictionary["userUid"] as? String
    postText = dictionary["postText"] as? String
    postDate = dictionary["postDate"] as? Double
    }
}
我的型号:

// Load the comment with id
func observeComment(commentId: String, completion: @escaping (CommentModel) -> Void) {
    let db = Firestore.firestore()
    db.collection("comments").document(commentId).getDocument { (snapshot, error) in
        guard let dic = snapshot?.data() else { return }
        let newComment = CommentModel(dictionary: dic)

        completion(newComment)
    }
}
import UIKit

class CommentModel {

var postId: String?
var userUid: String?
var postText: String?
var postDate: Double?

init(dictionary: [String: Any]) {
    postId = dictionary["postId"] as? String
    userUid = dictionary["userUid"] as? String
    postText = dictionary["postText"] as? String
    postDate = dictionary["postDate"] as? Double
    }
}
这给了我以下结果:

["user2" : 5], ["user1" : 4], ["user2" : 3], ["user1" : 2], ["user1" : 1]
我试图实现的是:调用“observeComments”中的函数,如果用户ID不存在,并且如果该数字低于现有数字,则追加数据,并将其加载到新的“CommentModel”,无论我有多少评论或用户

结果应该如下所示:

["user1" : 2], ["user1" : 1]

因为我有两个用户名,并且返回的是最小的数字。

字典不能有重复的键,而且字典不是基于排序的,而是基于键值的。这意味着你不能订购字典

但是,另一方面,在swift中从口述词中删除值很容易,您只需要这样做

dic.removeValue(forKey: "user2")
有条件

var hues = ["Heliotrope": 296, "Coral": 16, "Aquamarine": 156]
if let value = hues.removeValue(forKey: "Coral") {
    print("The value \(value) was removed.")
}
// Prints "The value 16 was removed."
就这么简单,

if v < comments["john"] { comments["john'] = v }
如果v
再次阅读问题我不清楚
postMapping
是否包含所有元素,您是否希望对其进行过滤以获得结果,或者如果您只想在postDate小于给定用户的现有postDate时对其进行追加?嗨,Joakim,我认为第二种方法应该比第一种方法更正确,但是是的,如果。。。