patnz
patnz

Reputation: 1141

Swift - UITextField.text - error: string is not convertible to ()

I'm taking a value from a UITextField object and am having difficulty using the UITextField.text property as a string. I'm trying to pass the value to a sanitizing function and attach the returned value to a variable but I can't get past the error 'String' is not convertible to '()' and vice-versa. Even if I comment out the string sanitisation and simply return the value passed to the function I get the same problem. What am I doing wrong here?

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
    }

Upvotes: 1

Views: 666

Answers (1)

Richard Roylance
Richard Roylance

Reputation: 58

You didn't set a return type for your clean function. It should look like;

func clean(str: String) -> String

Upvotes: 2

Related Questions