【发布时间】:2018-01-05 03:52:34
【问题描述】:
我正在获取用户输入的邮政编码并将其转换为城市名称。将其转换为城市名称后,我会将信息保存到我的 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)的问题。这绝对是错误的。 -
nameText是你的文本字段吗? -
如何解决问题以从该函数获取输出?
-
nameText 是一个不同的输入,zipCode 文本字段用于邮政编码,我正在尝试将其转换为位置属性的城市名称后保存
-
你必须在
getLocationFromPostalCode中返回一个字符串。
标签: ios swift xcode core-location