追加包含其他类数组的Class数组

时间:2017-10-11 15:57:20

标签: arrays swift firebase uicollectionview

我有一个Notification Class数组,它包含String值和3个其他类(用户,照片和视频类)。我将通知类附加到集合视图时遇到麻烦。问题是如果Photo Class为空它失败或者如果Video Class为空它也无法追加。我已经在Notification Class数组中选择了所有Classes,但它似乎没有什么区别,所以我不确定下一步该做什么。请帮助我们。提前谢谢!

通知数组

private Bitmap cropBitmap() {
    int minX = mDrawnAreaRect.left <= 0 ? 0 : (int) mDrawnAreaRect.left;
    int maxY = mDrawnAreaRect.top > mBitmap.getHeight() ? mBitmap.getHeight() : (int) mDrawnAreaRect.top;
    int maxX = mDrawnAreaRect.right > mBitmap.getWidth() ? mBitmap.getWidth() : (int) mDrawnAreaRect.right;
    int minY = mDrawnAreaRect.bottom <= 0 ? 0 : (int) mDrawnAreaRect.bottom;

    int width = maxX - minX;
    int height = maxY - minY;

    width = width > mBitmap.getWidth() ? mBitmap.getWidth() : width;
    height = height > mBitmap.getHeight() ? mBitmap.getHeight() : height;

    return Bitmap.createBitmap(mBitmap, minX, minY, width, height);
}

CollectionViewController

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       
        }

2 个答案:

答案 0 :(得分:1)

您的代码存在多个问题。我将逐一介绍:

您正在以级联方式从Firebase获取数据。首先,您抓住user,而不是photo,最后抓住video

在您的方法fetch(Post|Video)withUUID中,您调用完成块,如果,则返回结果,否则您将无法执行任何操作。这意味着,永远不会调用下一个方法/块。

解决问题的一种方法:

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现在在完成块中是可选的(?): @escaping (Post?)代替@escaping (Post)@escaping (videoPost?)代替@escaping (videoPost)

  2. 如果您没有结果,它仍会使用nil值调用完成功能块:completion(nil)

答案 1 :(得分:0)

您可以使用

static  func fetchPostWithUID(uid: String, postId: String, completion: @escaping (Post?) -> ()) {

然后在其他部分你可以像这样调用完成处理程序

completion(nil)

和其他功能相同

然后

您需要更新通知的初始化,如下所示:

 init (user: userAccount, post: Post?, videoPost: videoPost?, dictionary: [String: Any])