Variables Swift:无法将函数返回值分配给UILabel
我遇到这样一种情况:函数调用返回的字符串被分配给Variables Swift:无法将函数返回值分配给UILabel,variables,uilabel,Variables,Uilabel,我遇到这样一种情况:函数调用返回的字符串被分配给UILabel文本。但是,ui标签带有空值。如果我要分配一个静态字符串viz。直接对UILabel进行“这是测试”。在函数中,我可以打印返回值 import Foundation import Firebase import UIKit class checkLocation{ var items = [addedItems]() func getLocationOfItem(textFieldText: (String)) -&
UILabel
文本。但是,ui标签
带有空值。如果我要分配一个静态字符串viz。直接对UILabel
进行“这是测试”。在函数中,我可以打印返回值
import Foundation
import Firebase
import UIKit
class checkLocation{
var items = [addedItems]()
func getLocationOfItem(textFieldText: (String)) -> String{
let itemLet = addedItems()
var location = ""
Database.database().reference().child("item").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject]{
//itemLet.setValuesForKeys(dictionary)
itemLet.itemName = dictionary["itemName"] as? String
itemLet.itemLocation = dictionary["itemLocation"] as? String
itemLet.itemImageUrl = dictionary["itemImageUrl"] as? String
itemLet.itemTS = dictionary["itemTS"] as? String
itemLet.id = dictionary["id"] as? String
if(textFieldText == itemLet.itemName){
location = itemLet.itemLocation!
print(location)
}
}
}, withCancel: nil)
return location
}
}
调用程序
import UIKit
import Firebase
class checkLocationViewController: ViewController {
@IBOutlet weak var itemNameInLctn: UITextField!
@IBOutlet weak var itemLocationLbl: UILabel!
var items = [addedItems]()
var strLocation = "";
override func viewDidLoad() {
super.viewDidLoad()
refItems = Database.database().reference().child("item")
}
@IBAction func getLocation(_ sender: UIButton) {
self.strLocation = checkLocation().getLocationOfItem(textFieldText: itemNameInLctn.text!)
itemLocationLbl.text = strLocation
//itemLocationLbl.text = strLocation
//print(itemNameInLctn.text)
}
}
有帮助吗?函数getLocationOfItem(textFieldText:(String))->String正确返回初始值为“”的
位置
您面临的问题是,您正在同步函数调用中从firebase调用异步任务
在进行以下更改时,可以重构方法调用以异步方式返回所需字符串:
func getLocationOfItem(textFieldText: String, completion: @escaping (_ itemLocation: String?) -> Void){
let itemLet = addedItems()
Database.database().reference().child("item").observe(.childAdded, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: AnyObject] else {
return completion(nil)
}
itemLet.itemName = dictionary["itemName"] as? String
itemLet.itemLocation = dictionary["itemLocation"] as? String
itemLet.itemImageUrl = dictionary["itemImageUrl"] as? String
itemLet.itemTS = dictionary["itemTS"] as? String
itemLet.id = dictionary["id"] as? String
if(textFieldText == itemLet.itemName){
completion(itemLet.itemLocation)
} else {
completion(nil)
}
}, withCancel: nil)
}
/// Usage
@IBAction func getLocation(_ sender: UIButton) {
guard let itemNameInLocation = itemNameInLctn.text else { return }
getLocationOfItem(textFieldText: itemNameInLocation) { [weak self] (itemLocation) in
self?.strLocation = itemLocation
self?.itemLocationLbl.text = itemLocation
}
}
注:
请注意,每次在firebase数据库的项
路径下添加值时,都将调用完成处理程序。
在这种情况下,我强烈建议进一步重构,以使用firebase提供的observeSingleEvent
有关上述方法的文档,请参阅下面的链接:
如果这是你的意图,那么这是最好的选择