![](/img/trans.png)
[英]Cannot convert value of type 'String?.Type' to expected argument type 'String?'
[英]Cannot convert value of type '()' to expected argument type 'String'
我正在使用用戶輸入的郵政編碼,並將其轉換為城市名稱。 將其轉換為城市名稱后,我會將信息保存到Firebase數據庫中。 嘗試執行此操作時,出現錯誤“無法將類型'()'的值轉換為預期的參數類型'字符串'”。 我從一個文本字段中獲取郵政編碼,並從以前的ViewController中傳遞它。 錯誤
//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)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.