gpt4 book ai didi

arrays - 追加包含其他类数组的类数组

转载 作者:行者123 更新时间:2023-11-28 15:16:53 26 4
gpt4 key购买 nike

我有一个通知类数组,它由字符串值和其他 3 个类(用户、照片和视频类)组成。我在将通知类附加到 Collection View 时遇到问题。问题是如果 Photo Class 是空的,它会失败,或者如果 Video Class 是空的,它也无法追加。我已经在 Notification Class 数组中将所有 Classes 设为可选,但它似乎没有什么区别,所以我不确定下一步该做什么。请帮助将不胜感激。提前致谢!

通知数组

class notification { 
var profilePic: String?
var fullName: String?
var descriptionLabel: String?
var descriptionImage: String?
var postID: String?
var user: userAccount?
var post: Post?
var videoPost: videoPost?
var postDescription: String?
var date: NSNumber?

init (user: userAccount, post: Post, videoPost: videoPost, dictionary: [String: Any])
{
self.videoPost = videoPost
self.post = post
self.user = user
self.postDescription = dictionary["postDescription"] as? String ?? ""
self.fullName = dictionary["fullName"] as? String ?? ""
self.profilePic = dictionary["profilePic"] as? String ?? ""
self.descriptionLabel = dictionary["textDesciption"] as? String ?? ""
self.descriptionImage = dictionary["desciptionImage"] as? String ?? ""
self.postID = dictionary["postID"] as? String ?? ""
self.date = dictionary["notificationDate"] as? NSNumber
}

Collection View Controller

func loadData(){

let userID = Auth.auth().currentUser!.uid
Database.database().reference().child("notification").child(userID).observe(.childAdded, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: Any] else { return }
let uid = dictionary["uid"] as? String
let postId = dictionary["postID"] as? String

Database.fetchUserWithUID(uid: uid!, completion: { (snapshot) in
Database.fetchPostWithUID(uid: userID, postId: postId!, completion: { (postShot) in
Database.fetchVideoPostWithUID(uid: userID, postId: postId!, completion: { (videoShot) in

let comment = notification(user: snapshot, post: postShot, videoPost: videoShot, dictionary: dictionary)
self.notifications.append(comment)
self.notificationTableView?.reloadData()
})
})
})
})
}

extension Database {

static func fetchUserWithUID(uid: String, completion: @escaping (userAccount) -> ()) {
Database.database().reference().child("user").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in

let user = userAccount(snapshot:snapshot)
completion(user)
})
}

static func fetchPostWithUID(uid: String, postId: String, completion: @escaping (Post) -> ()) {
Database.database().reference().child("post").child(uid).child(postId).observeSingleEvent(of: .value, with: { (snapshot) in

if snapshot.exists() {
let post = Post(snapshot:snapshot)
completion(post)
}else{
// Not sure what to write here when snapshot is equal to nil

})
}

static func fetchVideoPostWithUID(uid: String, postId: String, completion: @escaping (videoPost) -> ()) {
Database.database().reference().child("videoPost").child(uid).child(postId).observeSingleEvent(of: .value, with: { (snapshot) in

if snapshot.exists(){
let post = videoPost(snapshot:snapshot)
completion(post)
}else{
// Again not sure what to write here when snapshot is equal to nil

}
})
}

最佳答案

您的代码存在多个问题。我会一一道来:

您正在级联中从 Firebase 获取数据。因此,首先获取 user,然后是 photo,最后是 video

在您的方法 fetch(Post|Video)withUUID 中,您调用完成 block if 返回结果,否则您什么都不做。这意味着,下一个方法/ block 将永远不会被调用。

一种修复方法:

init (user: userAccount, post: Post?, videoPost?: videoPost, dictionary: [String: Any]) {
// your code after
}

static func fetchPostWithUID(uid: String, postId: String, completion: @escaping (Post?) -> ()) {
Database.database().reference().child("post").child(uid).child(postId).observeSingleEvent(of: .value, with: { (snapshot) in

if snapshot.exists() {
let post = Post(snapshot:snapshot)
completion(post)
}else{
completion(nil)
})
}

static func fetchVideoPostWithUID(uid: String, postId: String, completion: @escaping (videoPost?) -> ()) {
Database.database().reference().child("videoPost").child(uid).child(postId).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists(){
let post = videoPost(snapshot:snapshot)
completion(post)
}else{
completion(nil)
}
})
}

对您的代码进行几处更改:

  1. postvideoPost 现在在完成 block 中是可选的 (?):@escaping (Post?) 代替 @escaping (Post)@escaping (videoPost?) 代替 @escaping (videoPost )

  2. 如果您没有结果,它仍会调用带有 nil 值的完成 block :completion(nil)

关于arrays - 追加包含其他类数组的类数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46692725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com