Search code examples
iosswiftuitapgesturerecognizer

(Swift) How to get rid of "Left side of mutating operator isn't mutable"


complete error:

Left side of mutating operator isn't mutable: 'abc' is a 'let' constant

Happens because I am trying to change value of a variable sent by parameter to function.
Can I get rid of this, or find some other solution?
Code(My code is much complex, but in effect doing the same as this):

func generateABC() {
var abc = "this"
abc += "is"

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(abc)) )
tapGesture.delegate = self
webView.addGestureRecognizer(tapGesture)

abc += "function"
}

handleTap function :

@objc
func handleTap(_ someString: String) {   
     someString += "my"          
}

Solution

  • Short story: It's impossible to add custom parameters to (any) target/action

    Either there is no parameter

    @objc
    func handleTap() { ...
    

    or the affected recognizer is the parameter

    @objc
    func handleTap(_ recognizer : UITapGestureRecognizer) { ...
    

    That's it. In both cases the corresponding declaration is

    UITapGestureRecognizer(target: self, action: #selector(handleTap))
    

    You have to use a temporary variable to handle the string.