Kyle Gillen Kyle Gillen - 1 year ago 111
Swift Question

How to denote mutable parameters in closures with Swift > 2.2?

Perhaps this is an Xcode 8 beta issue, however, prior to 2.2 the

keyword is allowed to prepend parameters in function signatures:

func (var stringName: String) { ... }

This is has since been deprecated in lieu of there being little benefit over

func (stringName: inout String) { ... }

I've attempted the following in a
closure, and though I don't receive a deprecation warning as mildly expected I should, the error was rather a
segmentation fault: 11

let demoString = ["hi", "there", "world"].map { (var word) -> String in
let firstChar = word.remove(at: word.startIndex)

The error kicks in as soon as I attempt to mutate the (assumedly mutable)

I've attempted other variation e.g. using

let demoString = ["hi", "there", "world"].map { (word: inout String) -> String in
let firstChar = word.remove(at: word.startIndex)

But the compiler complains that this erroneously changes the signature of the closure altogether and won't compile.

Obviously, the workaround is simply to copy the variable to a local one within the closure:

let demoString = ["hi", "there", "world"].map { (word) -> String in
let tempWord = word
let firstChar = tempWord.remove(at: tempWord.startIndex)

However, I am interested in knowing if this is expected functionality & whether or not there is a way of mutating a parameter passed into a closure directly?

Answer Source

Closures can mutate inout arguments just fine:

var str1 = "Pine"
var str2 = "Juniper"

let closure = { (s1: inout String, s2: inout String) -> Void in
    s1 = "Oak"
    s2 = "Chestnut"

closure(&str1, &str2)

print(str1, str2)

The problem you are facing is doesn't have a method signature that includes an inout parameter.

The only way around this that I can think of is to extend Array and add the map method yourself:

extension Array {
    func map<T>(_ closure: (inout T) -> Void) -> Array<T> {
        var arr = [T]()
        for i in 0..<self.count {
            var temp : T = self[i] as! T
        return arr

var hi = "hi", there = "there", world = "world"
var demoStrings = [hi, there, world]
var result = { (word: inout String) in 
    word.remove(at: word.startIndex)

print(result) // ["i", "here", "orld"]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download