我正在尝试从Parse查询一个类,但是一个名为(coverImage)的列可以为空(未定义)或者有一个图像,具体取决于用户是否上传了一个!
我知道如何获取带有图像的行,但我还需要下载其中包含空的coverImage的其他行,因为它包含数据...但到目前为止,我似乎无法获得没有它的空行崩溃...
所以我只是想知道即使该类的一列是空的,我怎样才能得到数据?
这是我的结构&查询:
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)
}}
如果有人可以帮助我,请提前感谢你!
答案 0 :(得分:0)
如果通过添加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
)
请注意as
的{{1}}后面有一个问号。
对于你的init,你需要使参数可选:
coverImage
请注意init (dataTypeInit: Int, dateInit : NSDate, usernameInit: String, textInit: String, titleInit: String, uuidInit: String,profileImageInit: PFFile, coverImageInit: PFFile?) {
// your init code (omitted for brevity)
}
参数的PFFile
类型后面的问号。