swift 函数不返回字符串?
swift function not returning string?
我创建了一个函数来 return 用户名,在 userID 参数上使用 Firebase 查询。我想使用此用户名来填充 tableView 中的文本标签。尽管函数内的查询 returns 是正确的值,但该值似乎不是 returned:
func getUser(userID: String) -> String {
var full_name: String = ""
rootRef.child("users").child(userID).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
// Get user value
let first_name = snapshot.value!["first_name"] as! String
let last_name = snapshot.value!["last_name"] as! String
full_name = first_name + " " + last_name
print(full_name) // returns correct value
})
return full_name //printing outside here just prints a blank space in console
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let inviteDict = invites[indexPath.row].value as! [String : AnyObject]
if let userID = inviteDict["invitedBy"] as? String {
let name = getUser(userID)
cell.textLabel!.text = name
}
return cell
}
}
单元格没有文本。将函数 return 打印到控制台只会打印空白 space。有什么问题吗?
谢谢!
你的问题是你的 getUser
函数执行一个块来获取 full_name
值,但是你在另一个线程上返回,所以当这一行 return full_name
执行时,几乎不可能你的块已经结束所以你的函数 returns ""
而不是你想要的值
试试这个
func getUser(userID: String,closure:((String) -> Void)?) -> Void {
var full_name: String = ""
rootRef.child("users").child(userID).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
// Get user value
let first_name = snapshot.value!["first_name"] as! String
let last_name = snapshot.value!["last_name"] as! String
full_name = first_name + " " + last_name
print(full_name) // returns correct value
closure(full_name)
})
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let inviteDict = invites[indexPath.row].value as! [String : AnyObject]
if let userID = inviteDict["invitedBy"] as? String {
let name = getUser(userID, closure: { (name) in
cell.textLabel!.text = name
})
}
return cell
}
我希望这对你有帮助,PS我不确定这是否有效,因为我没有这个库
我创建了一个函数来 return 用户名,在 userID 参数上使用 Firebase 查询。我想使用此用户名来填充 tableView 中的文本标签。尽管函数内的查询 returns 是正确的值,但该值似乎不是 returned:
func getUser(userID: String) -> String {
var full_name: String = ""
rootRef.child("users").child(userID).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
// Get user value
let first_name = snapshot.value!["first_name"] as! String
let last_name = snapshot.value!["last_name"] as! String
full_name = first_name + " " + last_name
print(full_name) // returns correct value
})
return full_name //printing outside here just prints a blank space in console
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let inviteDict = invites[indexPath.row].value as! [String : AnyObject]
if let userID = inviteDict["invitedBy"] as? String {
let name = getUser(userID)
cell.textLabel!.text = name
}
return cell
}
}
单元格没有文本。将函数 return 打印到控制台只会打印空白 space。有什么问题吗?
谢谢!
你的问题是你的 getUser
函数执行一个块来获取 full_name
值,但是你在另一个线程上返回,所以当这一行 return full_name
执行时,几乎不可能你的块已经结束所以你的函数 returns ""
而不是你想要的值
试试这个
func getUser(userID: String,closure:((String) -> Void)?) -> Void {
var full_name: String = ""
rootRef.child("users").child(userID).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
// Get user value
let first_name = snapshot.value!["first_name"] as! String
let last_name = snapshot.value!["last_name"] as! String
full_name = first_name + " " + last_name
print(full_name) // returns correct value
closure(full_name)
})
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let inviteDict = invites[indexPath.row].value as! [String : AnyObject]
if let userID = inviteDict["invitedBy"] as? String {
let name = getUser(userID, closure: { (name) in
cell.textLabel!.text = name
})
}
return cell
}
我希望这对你有帮助,PS我不确定这是否有效,因为我没有这个库