Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mutate structs in Swift using map?

Tags:

ios

swift

I have the following struct defined.

struct Person {

    var firstName :String
    var lastName :String
    var active :Bool
}

I have created a collection of Person as shown below:

var persons :[Person] = []

for var i = 1; i<=10; i++ {

    var person = Person(firstName: "John \(i)", lastName: "Doe \(i)", active: true)
    persons.append(person)
}

and Now I am trying to change the active property to false using the code below:

let inActionPersons = persons.map { (var p) in

    p.active = false
    return p
}

But I get the following error:

Cannot invoke map with an argument list of type @noescape (Person) throws

Any ideas?

SOLUTION:

Looks like Swift can't infer types sometimes which is kinda lame! Here is the solution:

let a = persons.map { (var p) -> Person in

        p.active = false
        return p
}

THIS DOES NOT WORK:

let a = persons.map { p in

        var p1 = p
        p1.active = false
        return p1
}
like image 815
john doe Avatar asked Dec 06 '15 02:12

john doe


People also ask

Can you subclass a struct Swift?

It is not possible to subclass a struct in Swift, only classes can be subclassed. An extension is not a subclass, it's just adding additional functionality on to the existing struct , this is comparable to a category in Objective-C.

Can we override struct in Swift?

Swift struct methods aren't dynamically dispatched; they're just function calls. But if you could subclass a struct and override methods, then the methods would need to be dynamically dispatched … but that implies that structs would need to contain vtables (or isa pointers), which makes them a lot more heavyweight.

Why struct is faster than class Swift?

Rather than a copy, a reference to the same existing instance is used. Structures and classes in Swift have many things in common. The major difference between structs and classes is that they live in different places in memory. Structs live on the Stack(that's why structs are fast) and Classes live on Heap in RAM.

Are structs passed by value Swift?

All structures and enumerations are value types in Swift. This means that any structure and enumeration instances you create—and any value types they have as properties—are always copied when they're passed around in your code.


2 Answers

When using the brackets for arguments so that var works, you have to put the return type as well:

let inActionPersons = persons.map { (var p) -> Person in

    p.active = false
    return p
}
like image 32
Kametrixom Avatar answered Oct 22 '22 08:10

Kametrixom


There are exactly two cases where the Swift compiler infers the return type of a closure automatically:

  • In a "single-expression closure," i.e. the closure body consists of a single expression only (with or without explicit closure parameters).
  • If the type can be inferred from the calling context.

None of this applies in

let inActionPersons = persons.map { (var p) in
    p.active = false
    return p
}

or

let a = persons.map { p in
        var p1 = p
        p1.active = false
        return p1
}

and that's why you have to specify the return type explicitly as in Kametrixom's answer.

Example of a single-expression closure:

let inActionPersons = persons.map { p in
    Person(firstName: p.firstName, lastName: p.lastName, active: false)
}

and it would compile with (var p) in or (p : Person) in as well, so this has nothing to do with whether the closure arguments are given explicitly in parentheses or not.

And here is an example where the type is inferred from the calling context:

let a : [Person] = persons.map { p in
    var p1 = p
    p1.active = false
    return p1
}

The result of map() must be a [Person] array, so map needs a closure of type Person -> Person, and the compiler infers the return type Person automatically.

For more information, see "Inferring Type From Context" and "Implicit Returns from Single-Expression Closures" in the "Closures" chapter in the Swift book.

like image 56
Martin R Avatar answered Oct 22 '22 08:10

Martin R