我正在尝试从Parse查询一个类,但是一列名为(coverImage)的列可以为空(未定义),也可以包含一个图像,具体取决于用户是否上传了一个!

我知道如何获取带有图像的行,但是我还需要下载其他包含空CoverImage的行,因为它包含了数据...但是到目前为止,我似乎无法在没有崩溃的情况下获取空行。 。

所以我只是想知道即使该类的1列为空,也如何获取数据?

这是我的结构和查询:

 struct PostStruct {

        var dataType : Int = 0 // 0 for text, 1 for picture
        var date : NSDate
        var username : NSString
        var text : NSString?
        var title : NSString?
        var image : PFFile?
        var uuid : NSString
        var profileImage : PFFile
        var caption : NSString?
        var coverImage : PFFile?

        init (dataTypeInit: Int, dateInit : NSDate, usernameInit: String, textInit: String, titleInit: String, uuidInit: String,profileImageInit: PFFile, coverImageInit: PFFile) {
            dataType        = dataTypeInit
            date            = dateInit
            username        = usernameInit
            text            = textInit
            title           = titleInit
            uuid            = uuidInit
            profileImage    = profileImageInit
            coverImage      = coverImageInit
        }

        init (dataTypeInit: Int, dateInit : NSDate, usernameInit: String, imageInit: PFFile, uuidInit: String, profileImageInit: PFFile, captionInit: String) {
            dataType        = dataTypeInit
            date            = dateInit
            username        = usernameInit
            image           = imageInit
            uuid            = uuidInit
            profileImage    = profileImageInit
            caption         = captionInit

        }


查询:

 let query = PFQuery(className: "Posts")
                query.whereKey("username", containedIn: self.followArray)
                query.addDescendingOrder("createdAt")
                query.findObjectsInBackgroundWithBlock({ (objects:[PFObject]?, error:NSError?) -> Void in
                    if error == nil {

                        self.posts.removeAll(keepCapacity: false)


                                for object in objects! {


                            if let _ = object.valueForKey("textPost") as? String {
                                if object.valueForKey("coverImage") != nil {


                                // this must be a text post
                                var post = PostStruct(dataTypeInit: 0,
                                    dateInit: object.createdAt!,
                                    usernameInit: object.valueForKey("username") as! String,
                                    textInit: object.valueForKey("textPost") as! String,
                                    titleInit: object.valueForKey("titlePost") as! String,
                                    uuidInit: object.valueForKey("uuid") as! String,
                                    profileImageInit: object.valueForKey("profilePicture") as! PFFile,
                                    coverImageInit: object.valueForKey("coverImage") as! PFFile
                                    )


                                post.comments = comments

                                self.posts.append(post)
                                } else {
                                 print("jkh")
                                }

                                 } else {
                                // this must be an image
                                var post = PostStruct(dataTypeInit: 1,
                                    dateInit: object.createdAt!,
                                    usernameInit: object.valueForKey("username") as! String,
                                    imageInit: object.valueForKey("image") as! PFFile,
                                    uuidInit: object.valueForKey("uuid") as! String, profileImageInit: object.valueForKey("profilePicture") as! PFFile, captionInit: object.valueForKey("caption") as! String)

                                post.comments = comments

                                self.posts.append(post)
                            }}


如果有人可以帮助我,请提前谢谢!

最佳答案

如果是通过添加as?(可选展开)而不是as!(显式展开)设置的,则可以选择分配值。这样,如果有值,将在object.valueForKey()中分配值,否则将是nil

var post = PostStruct(
     dataTypeInit: 0,
     dateInit: object.createdAt!,
     usernameInit: object.valueForKey("username") as! String,
     textInit: object.valueForKey("textPost") as! String,
     titleInit: object.valueForKey("titlePost") as! String,
     uuidInit: object.valueForKey("uuid") as! String,
     profileImageInit: object.valueForKey("profilePicture") as! PFFile,
     coverImageInit: object.valueForKey("coverImage") as? PFFile
)


请注意,ascoverImage后面有一个问号。

同样对于您的init,您需要将参数设为可选:

init (dataTypeInit: Int, dateInit : NSDate, usernameInit: String, textInit: String, titleInit: String, uuidInit: String,profileImageInit: PFFile, coverImageInit: PFFile?) {
   // your init code (omitted for brevity)
}


注意PFFile参数的coverImageInit类型后的问号。

09-07 12:09