我正在尝试传递从GMSAutocompleteViewController中选择地点后收集的数据,并遇到一些问题。我收到此错误:无法将'NSTaggedPointerString'(0x1afeb5c50)类型的值转换为'UILabel'(0x1afedd508)。不确定我做错了什么...感谢任何和所有的帮助!
// MARK: GOOGLE AUTO COMPLETE DELEGATE
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
// Do something with the selected place.
// A hotel in Saigon with an attribution.
// let placeID = "ChIJV4k8_9UodTERU5KXbkYpSYs"
let placeID = place.placeID
placesClient.lookUpPlaceID(placeID, callback: { (place, error) -> Void in
if let error = error {
print("lookup place id query error: \(error.localizedDescription)")
return
}
guard let place = place else {
print("No place details for \(placeID)")
return
}
print("Place name \(place.name)")
print("Place address \(place.formattedAddress)")
print("Place placeID \(place.placeID)")
print("Place attributions \(place.attributions)")
})
//
let selectedPlace = place.name
self.placeNameLabel = (selectedPlace as AnyObject) as! UILabel
self.dismiss(animated: true, completion: nil)
setupConfirmationPopUp()
}
// label I am trying to pass the place.name to
lazy var placeNameLabel: UILabel = {
let placeNameLabel = UILabel()
placeNameLabel.textColor = .black
placeNameLabel.text = "Are you sure you want to add < Placename Placename > to places you have visited?"
placeNameLabel.frame = CGRect(x: 50, y: 120, width: 200, height: CGFloat.greatestFiniteMagnitude) // use constraints later
placeNameLabel.numberOfLines = 0
placeNameLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
placeNameLabel.sizeToFit()
placeNameLabel.layer.zPosition = 1
placeNameLabel.isUserInteractionEnabled = true
return placeNameLabel
}()
答案 0 :(得分:1)
不需要任何演员(当然不是两个)。只需:
self.placeNameLabel.text = place.name
您试图将字符串分配给标签。您需要将字符串分配给标签的文本。
另一个严重的问题。您需要更新lookUpPlaceID
的完成处理程序内的标签。正如您现在所做的那样,将标签设置为传递给视图控制器方法的place
参数,但您需要使用传递给place
方法的完成处理程序的lookupPlaceID
参数。由于更新标签是UI操作,因此您必须使用DispatchQueue.main.async
在主队列上设置标签。
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
let placeID = place.placeID
placesClient.lookUpPlaceID(placeID, callback: { (place, error) -> Void in
if let error = error {
print("lookup place id query error: \(error.localizedDescription)")
return
}
guard let place = place else {
print("No place details for \(placeID)")
return
}
print("Place name \(place.name)")
print("Place address \(place.formattedAddress)")
print("Place placeID \(place.placeID)")
print("Place attributions \(place.attributions)")
DispatchQueue.main.async {
self.placeNameLabel = place.name
}
})
self.dismiss(animated: true, completion: nil)
setupConfirmationPopUp()
}
答案 1 :(得分:0)
self.placeNameLabel=UILabel()
if let name = selectedPlace as? String
{
self.placeNameLabel.text = name
}
您必须将文本分配给UILabel
的{{1}}媒体资源。 UILabel会显示文本,但它的功能远不止于此,因此它具有更多属性,例如:backgroundColor,size,frame,bounds等。
因此,文本部分只是UILabel所具有的众多属性之一,您必须明确指定它。
顺便说一下,如果您在故事板中创建了标签并将其与插座连接,则可以删除第一行。
顺便说说:
此:
text
总是返回true,所以你可以完全忘记它。