# Initialize Struct with Array

**URL:** <https://forums.kodeco.com/t/initialize-struct-with-array/43583>\
**Category:** Swift\
**Created:** [June 13, 2018, 9:49pm UTC](https://forums.kodeco.com/t/initialize-struct-with-array/43583 "2018-06-13T21:49:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sprouse5](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@sprouse5](https://forums.kodeco.com/u/sprouse5)\
**Post date:** [June 13, 2018, 9:49pm UTC](https://forums.kodeco.com/t/initialize-struct-with-array/43583/1 "2018-06-13T21:49:37Z")

</div>

So I have a struct:

```
struct MyStruct {
    let title: String
    let selected: Bool
}

```

And an array of strings:

```
let myStrings = ["Apple", "Banana", "Carrot"]

```

I want to initialize an array of `MyStruct`s with this array of strings, and then false for the selected property. For instance:

```
var structArray: [MyStruct]?

for string in myStrings {
    let myObject = MyStruct(title: string, selected: false)
    structArray.append(myObject)
}

```

Is there a better way to do this? I was thinking of trying to use `map` or `filter` but I’m not sure if this is the right direction to go. Thanks!

---

<div class="post-metadata">

**Author:** ![jessycatterwaul](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jessycatterwaul/32/110937_2.png) [@jessycatterwaul](https://forums.kodeco.com/u/jessycatterwaul)\
**Post date:** [June 14, 2018, 1:08am UTC](https://forums.kodeco.com/t/initialize-struct-with-array/43583/2 "2018-06-14T01:08:42Z")

</div>

`map` is the way to go! Have you tried it yet?

---

<div class="post-metadata">

**Author:** ![sprouse5](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@sprouse5](https://forums.kodeco.com/u/sprouse5)\
**Post date:** [June 14, 2018, 12:16pm UTC](https://forums.kodeco.com/t/initialize-struct-with-array/43583/3 "2018-06-14T12:16:06Z")

</div>

Thanks for that insight! Here is what I ended up with:

```
struct MyStruct {
    let title: String
    let selected: Bool
}

var myStrings: [String]?

...

// simple version of initializing optional string array
myStrings = [String]()
myStrings?.append("Apple")
myStrings?.append("Banana")
myStrings?.append("Carrot")

guard let mStrings = myStrings else { return }
let structArray = mStrings.map { MyStruct(title: $0, selected: false) }

```

The thing I think was I missing was the fact that my original string array is an optional, so I added the guard statement before mapping and now it works! Thanks for the help!

---

<div class="post-metadata">

**Author:** ![personalcompass](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/personalcompass/32/75865_2.png) [@personalcompass](https://forums.kodeco.com/u/personalcompass)\
**Post date:** [June 18, 2018, 3:53pm UTC](https://forums.kodeco.com/t/initialize-struct-with-array/43583/4 "2018-06-18T15:53:49Z")

</div>

```auto
var myStrings: [String]?
myStrings = [String]()

```

If you’re just going to initialize it as a non-optional array, why declare it as optional in the first place?

You could just do

```auto
var myStrings = [String]()

```

That would also remove the `guard let` later on 🙂

ALSO

If they’re all going to start off as `false`, i would suggest creating an initializer with a default value:

```auto
init(title: String) {
    self.title = title
    selected = false
}

...

{ MyStruct(title: $0) }

```

OR just set the value in the declaration:

```auto
let selected = false

```

That way you don’t need to initialize it separately every time.

---

<div class="post-metadata">

**Author:** ![personalcompass](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/personalcompass/32/75865_2.png) [@personalcompass](https://forums.kodeco.com/u/personalcompass)\
**Post date:** [June 18, 2018, 3:59pm UTC](https://forums.kodeco.com/t/initialize-struct-with-array/43583/5 "2018-06-18T15:59:22Z")

</div>

I’ve also created a list of other common swift mistakes (and how to fix them) here:  
[www.mypersonalcompass.com/swift-mistakes](http://www.mypersonalcompass.com/swift-mistakes)

---

<div class="post-metadata">

**Author:** ![sprouse5](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@sprouse5](https://forums.kodeco.com/u/sprouse5)\
**Post date:** [June 18, 2018, 5:06pm UTC](https://forums.kodeco.com/t/initialize-struct-with-array/43583/6 "2018-06-18T17:06:06Z")

</div>

The array `myStrings` is optional because in my actual code it is initialized via a network call, so there’s the chance it could be `nil`. I kept that out for this simple example.

I added the `init` function to `MyStruct`, thanks for that! It now looks like this:

```
struct MyStruct: Codable {
    let title: String
    var selected = true

    init(title: String) {
        self.title = title
    }

    mutating func setSelected(_ value: Bool) {
        selected = value
    }
}

```

---

<div class="post-metadata">

**Author:** ![personalcompass](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/personalcompass/32/75865_2.png) [@personalcompass](https://forums.kodeco.com/u/personalcompass)\
**Post date:** [June 18, 2018, 5:29pm UTC](https://forums.kodeco.com/t/initialize-struct-with-array/43583/7 "2018-06-18T17:29:50Z")

</div>

At that point wouldn’t it be easier to make it a class? That way you don’t need a fancy setter with the mutating keyword.

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [November 27, 2018, 1:57pm UTC](https://forums.kodeco.com/t/initialize-struct-with-array/43583/8 "2018-11-27T13:57:30Z")

</div>

Closed on November 27, 2018, 1:57pm UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
