Can';t访问函数外部的数组值-Swift
我试图查询我的解析数据库,以便搜索用户名并显示与用户名相关的所有帖子。帖子分为两个字段,我将它们“粘贴”到函数中Can';t访问函数外部的数组值-Swift,swift,Swift,我试图查询我的解析数据库,以便搜索用户名并显示与用户名相关的所有帖子。帖子分为两个字段,我将它们“粘贴”到函数中 func searchDataBase() -> ([String]) { if let username = PFUser.currentUser()?.username { userHeading.text = username + "'s Profile" var query = PFQuery(className:"
func searchDataBase() -> ([String]) {
if let username = PFUser.currentUser()?.username {
userHeading.text = username + "'s Profile"
var query = PFQuery(className:"Wish")
query.whereKey("user", equalTo:username)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) wishes.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
//println(object.objectId)
var parseID = object.objectId!
var query = PFQuery(className:"Wish")
query.getObjectInBackgroundWithId(parseID) {
(detailedWish: PFObject?, error: NSError?) -> Void in
if error == nil {
let partOne = detailedWish!["wishFieldOne"] as! String
let partTwo = detailedWish!["wishFieldTwo"] as! String
let fullWish = "I wish I " + partOne + " when I " + partTwo
self.wishes.append(fullWish)
} else {
println(error)
}
}
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
} else {
println("No user logged in")
}
return wishes
}
运行上述函数后,我想将用户的帖子放在UITableView
中:
//TABLE VIEW FUNCTIONS
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
searchDataBase()
return wishes.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = recentPosts.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! UITableViewCell
searchDataBase()
let row = indexPath.row
cell.textLabel?.text = wishes[row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
recentPosts.deselectRowAtIndexPath(indexPath, animated: true)
searchDataBase()
let row = indexPath.row
println(wishes[row])
}
当我构建应用程序时,UITableView
不会显示我在searchDataBase
函数中查询的帖子。如果在附加数组后立即执行Iprintln(wishes)
,则该数组有效,但在函数外部打印数组时,该数组为空查询成功后,如何使愿望
数组的内容在函数外部可访问?
谢谢你的帮助 searchDataBase()
执行返回数组的操作。您正在访问的变量wiships
在哪里
相反:
let wishes = searchDataBase()
然后,您将拥有用于表视图委托函数的数组。您无法通过这种方式完成您想要完成的任务
您需要做的是重写
searchDataBase
方法,将完成闭包作为参数。然后在调用query.getObjectInBackgroundHiId的完成块中,构建结果数组并传递给searchDataBase
完成闭包。这是一种奇怪的方法。对每个tableView方法调用searchDataBase()。你肯定不想每次都打电话给parse?另外,searchDataBase()方法不需要返回数组。您只需将结果附加到此数组,并在完成后重新加载表即可。我想这对你来说是一个更好的方法
因此,我的解决方案是从所有tableView方法中删除searchDatabase()函数调用,并将其放在viewDidLoad()中。您还应该将searchDataBase()更改为
func searchDataBase(){
if let username = PFUser.currentUser()?.username {
userHeading.text = username + "'s Profile"
var query = PFQuery(className:"Wish")
query.whereKey("user", equalTo:username)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) wishes.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
//println(object.objectId)
var parseID = object.objectId!
var query = PFQuery(className:"Wish")
query.getObjectInBackgroundWithId(parseID) {
(detailedWish: PFObject?, error: NSError?) -> Void in
if error == nil {
let partOne = detailedWish!["wishFieldOne"] as! String
let partTwo = detailedWish!["wishFieldTwo"] as! String
let fullWish = "I wish I " + partOne + " when I " + partTwo
self.wishes.append(fullWish)
} else {
println(error)
}
}
}
}
self.tableView.reloadData() // <- This will populate your tableView with your wishes
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
} else {
println("No user logged in")
}
}
func searchDataBase(){
如果让username=PFUser.currentUser()?.username{
userHeading.text=username+““的配置文件”
var query=PFQuery(类名:“Wish”)
query.whereKey(“用户”,equalTo:username)
query.findObjectsInBackgroundWithBlock{
(objects:[AnyObject]?,错误:NSError?->Void in
如果错误==nil{
//发现成功了。
println(“已成功检索\(objects!.count)个对象。”)
//对找到的对象执行一些操作
如果让对象=对象为?[PFObject]{
对于对象中的对象{
//println(object.objectId)
var parseID=object.objectId!
var query=PFQuery(类名:“Wish”)
query.getObjectInBackgroundHiId(parseID){
(detailedWish:PFObject?,错误:NSError?->Void in
如果错误==nil{
让partOne=detailedWish![“WishFieldDone”]作为!字符串
让partTwo=detailedWish![“wishFieldTwo”]作为!字符串
让fullWish=“我希望我”+partOne+”当我“+partTwo
self.wish.append(fullWish)
}否则{
println(错误)
}
}
}
}
self.tableView.reloadData()//这不起作用,因为searchDataBase函数正在使用解析函数query.GetObjectInBackgroundHithid
,它是异步的。当searchDataBase返回时,结果将不可用。我不熟悉完成块-是否有适用于我的情况的文章?尽管我必须移动self.tableView.reloadData()
就在self.wishes.append(fullWish)
行的正下方,您的代码运行正常!感谢您的帮助!啊,是的,很好。在后台查询完成之前,我被调用了刷新。很高兴提供帮助。