我从UITextField对象中获取值,并且难以将UITextField.text属性用作字符串。我试图将值传递给清理函数并将返回的值附加到变量但我无法通过错误' String'不能转换为'()' ,反之亦然。即使我注释掉字符串清理并简单地返回传递给函数的值,我也会遇到同样的问题。我在这里做错了什么?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var cityName: UITextField!
var city: String? = clean(cityName.text) // '()' is not convertible to 'String'
if city != nil { /* Do Stuff... */ }
func clean(str: String)
{
var trimmedStr = str.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
var cleanedStr = str.stringByReplacingOccurrencesOfString(" ", withString: "-")
return cleanedStr // 'String' is not convertible to '()'
// return str produces same error
}
答案 0 :(得分:2)
您没有为清洁功能设置返回类型。它应该看起来像;
func clean(str: String) -> String