感谢社区,我在昨天解决了错误处理问题,同时转移到swift 2.这些更正实际上产生了一个新错误,我不知道如何纠正它。以下是显示Feed中某些图片帖子的当前代码:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
ParseHelper.timelineRequestForCurrentUser {
(result: [AnyObject]?, error: NSError?) -> Void in
self.posts = result as? [Post] ?? []
for post in self.posts {
do
{
let data = try post.imageFile?.getData()
}
catch
{
print("Error: \(error)")
//Handle the error instead of print probably
}
post.image = UIImage(data: data!, scale:1.0) --> where I get the error message "Use of unresolved identified 'data'"
}
self.tableView.reloadData()
}
此外,我在上一个方法中引用的ParseHelper文件中出现了一个新错误。
static func timelineRequestForCurrentUser(completionBlock: PFArrayResultBlock) {
let followingQuery = PFQuery(className: "Follow")
followingQuery.whereKey("fromUser", equalTo:PFUser.currentUser()!)
let postsFromFollowedUsers = Post.query()
postsFromFollowedUsers!.whereKey("user", matchesKey: "toUser", inQuery: followingQuery)
let postsFromThisUser = Post.query()
postsFromThisUser!.whereKey("user", equalTo: PFUser.currentUser()!)
let query = PFQuery.orQueryWithSubqueries([postsFromFollowedUsers!, postsFromThisUser!])
query.includeKey("user")
query.orderByDescending("createdAt")
// 3
query.findObjectsInBackgroundWithBlock(completionBlock) --> I get an error message too here "Cannot convert value of type 'PFArrayResultBlock' to expected argument type 'PFQueryArrayResultBlock?'"
答案 0 :(得分:0)
对Swift不太熟悉,但编译器输出有意义。首先,let data
声明的范围仅限于do / catch的do
块。为了使catch
块使用数据,必须在do / catch周围的范围内重新声明数据...
let data : NSData? // assuming type is NSData (my point here is about scope)
do {
data = try post.imageFile?.getData()
} catch {
print("Error: \(error)")
//Handle the error instead of print probably
}
// now data is in scope here...
post.image = UIImage(data: data!, scale:1.0)
第二个问题似乎是在这里将完成块声明为PFArrayResultBlock
类型的函数参数:
static func timelineRequestForCurrentUser(completionBlock: PFArrayResultBlock)
...但是解析期望将PFQueryArrayResultBlock
类型的块传递给findObjectsInBackground
。看起来它们之间的差异是数组元素的预期类型略有不同([PFObject]?
用于查询,而[AnyObject]?
)。
答案 1 :(得分:0)
这是我纠正这个问题的方法:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
ParseHelper.timelineRequestForCurrentUser {
(result: [PFObject]?, error: NSError?) -> Void in
self.posts = result as? [Post] ?? []
self.tableView.reloadData() } }