多亏了社区,我昨天才解决了在转为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?'"
最佳答案
对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)
...但是parse希望将
PFQueryArrayResultBlock
类型的块传递给findObjectsInBackground
。两者之间的区别似乎是数组元素的预期类型略有不同(查询的[PFObject]?
与[AnyObject]?
)。
关于jquery - 快速捕获中 Unresolved 已识别错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33438526/