gpt4 book ai didi

ios - 无法将类型 '()' 的值转换为预期的参数类型 'String'

转载 作者:行者123 更新时间:2023-11-28 07:55:45 24 4
gpt4 key购买 nike

我正在获取用户输入的邮政编码并将其转换为城市名称。将其转换为城市名称后,我会将信息保存到我的 Firebase 数据库中。当我尝试执行此操作时,出现错误“无法将类型 '()' 的值转换为预期的参数类型 'String'”。我从文本字段中获取邮政编码并将其从以前的 ViewController 传递。 error

 //first ViewController


override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{


if let destination = segue.destination as? SignUpSecondViewController{

destination.zipCode = zipCodeInput.text!
destination.name = nameText.text!
destination.email = emailText.text!
destination.password = passwordText.text!
destination.pictureData = userImageView.image!
}}

}

//second ViewController

var zipCode = String()

func getLocationFromPostalCode(postalCode : String){
let geocoder = CLGeocoder()

geocoder.geocodeAddressString(postalCode) {
(placemarks, error) -> Void in
// Placemarks is an optional array of type CLPlacemarks, first item in array is best guess of Address

if let placemark = placemarks?[0] {

if placemark.postalCode == postalCode{
// you can get all the details of place here
print("\(placemark.locality)")
print("\(placemark.country)")
}
else{
print("Please enter valid zipcode")
}
}
}
}




@IBAction func completeButtonAction(_ sender: Any) {

let nameText = name
let emailField = email.lowercased()
let finalEmail = emailField.trimmingCharacters(in: .whitespacesAndNewlines)
let location = getLocationFromPostalCode(postalCode: zipCode)
let biography = bioTextView.text!
let passwordText = password
let interests = options.joined(separator: " , ")

var pictureD: NSData?


if let imageView = self.sentPic.image {
pictureD = UIImageJPEGRepresentation(self.sentPic.image!, 0.70) as! NSData
}


if finalEmail.isEmpty || biography.isEmpty || password.isEmpty || pictureD == nil {
self.view.endEditing(true)
let alertController = UIAlertController(title: "OOPS", message: " You must fill all the fields", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)

}else {
SVProgressHUD.show()

self.view.endEditing(true)
authService.signUP(firstLastName: nameText, email: finalEmail, location: location, biography: biography, password: password, interests: interests, pictureData: pictureD as NSData!)

}
SVProgressHUD.dismiss()


let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NewTabBarViewController") as! UIViewController
// Alternative way to present the new view controller
self.navigationController?.present(vc, animated: true, completion: nil)


}

最佳答案

您的问题从这一行开始:

let location = getLocationFromPostalCode(postalCode: zipCode)

问题是你的 getLocationFromPostalCode 没有返回值所以编译器认为 location 的隐式类型是 () 这意味着没有返回 (void) 类型的函数。

因此,理论上,您需要更改:

func getLocationFromPostalCode(postalCode : String) {

到:

func getLocationFromPostalCode(postalCode : String) -> String {

并让函数返回一个 String 值。

但是,您也不能这样做,因为您的 getLocationFromPostalCode 的实现包括从异步网络调用中获取位置。

正确的解决方案是重写该函数以采用返回所获得位置的完成处理程序。

func getLocationFromPostalCode(postalCode: String, completion: (String?) -> Void) {
let geocoder = CLGeocoder()

geocoder.geocodeAddressString(postalCode) { (placemarks, error) -> Void in
// Placemarks is an optional array of type CLPlacemarks, first item in array is best guess of Address

if let placemark = placemarks?.first {
if placemark.postalCode == postalCode {
// you can get all the details of place here
print("\(placemark.locality)")
print("\(placemark.country)")
completion(placemark.locality) // or whatever value you want
return
}
else{
print("Please enter valid zipcode")
}
}

completion(nil) // no location found
}
}

现在,所有这些都已解决,您需要重做获取位置的方式。

@IBAction func completeButtonAction(_ sender: Any) {
var pictureD: Data? = nil
if let imageView = self.sentPic.image {
pictureD = UIImageJPEGRepresentation(imageView, 0.70)
}

let emailField = email.lowercased()
let finalEmail = emailField.trimmingCharacters(in: .whitespacesAndNewlines)
let biography = bioTextView.text!
let passwordText = password

if finalEmail.isEmpty || biography.isEmpty || password.isEmpty || pictureD == nil {
self.view.endEditing(true)
let alertController = UIAlertController(title: "OOPS", message: " You must fill all the fields", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)
}else {
getLocationFromPostalCode(postalCode: zipCode) { (location) in
guard let location = location else {
print("no location")
return
}

let nameText = name
let interests = options.joined(separator: " , ")

SVProgressHUD.show()

self.view.endEditing(true)
authService.signUP(firstLastName: nameText, email: finalEmail, location: location, biography: biography, password: password, interests: interests, pictureData: pictureD!)
SVProgressHUD.dismiss()
}
}

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NewTabBarViewController") as! UIViewController
// Alternative way to present the new view controller
self.navigationController?.present(vc, animated: true, completion: nil)
}

关于ios - 无法将类型 '()' 的值转换为预期的参数类型 'String',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48106979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com