If you want to create an extension you just need to add
extension YourViewController {
func stringEncode(encodedString: String) {
let escapedString =
encodedString.addingPercentEncoding(with(withAllowedCharacters: .urlHostAllowed)
print (escapedString!)
}
}
Then call that function wherever you want to use it. Then just pass the string through the encodedString portion of the function. Like if you were going to use it on a button press to encode something from a textField:
@IBOutlet weak var textField: UITextField!
@IBAction func buttonPressed (_ sender: Any) {
stringEncode(encodedString: textField.text)
}
Then on the button press it should perform the function you want. I'm still learning Swift myself, but I thought I would try to answer- let me know if that works!