Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Chris Ediger
1,965 PointsCan't figure out how to append to an array (within a struct)
I'm completely stuck. I'm working on building an iOS app in Swift based on some Treehouse courses I've completed. I need to define an array of tools within a struct, but if I include any more than 4 items Xcode gets stuck indexing.
Found the solution here: http://stackoverflow.com/a/27531394. I can't figure out how to append to the array, however. When I attempt to do the code below, I get the following error: "Expected declaration". Any ideas?
import Foundation
struct Toolkit {
var tools = [ [
"name": "Know Yourself to Lead Yourself",
"shape": "icon_knowyourself.pdf",
"image": "know_yourself_to_lead_yourself.pdf",
"backgroundColor": ["red": 215, "green": 34, "blue": 14, "alpha": 1.0]
],
[
"name": "The Core",
"shape": "icon_thecore.pdf",
"image": "the_core.pdf",
"backgroundColor": ["red": 185, "green": 34, "blue": 14, "alpha": 1.0]
],
[
"name": "5 Circles of Influence",
"shape": "icon_5circles.pdf",
"image": "5_circles_of_influence.pdf",
"backgroundColor": ["red": 185, "green": 34, "blue": 14, "alpha": 1.0]
],
[
"name": "Support Challenge Matrix",
"shape": "icon_supportchallenge.pdf",
"image": "support_challenge_matrix.pdf",
"backgroundColor": ["red": 205, "green": 34, "blue": 14, "alpha": 1.0]
]
]
tools.append([
"name": "Creating Healthy Culture",
"shape": "icon_healthyculture.pdf",
"image": "creating_healthy_culture.pdf",
"backgroundColor": ["red": 185, "green": 34, "blue": 14, "alpha": 1.0]
])
}
3 Answers

Andrew Rodko
27,881 PointsI think the problem is that you're trying to append within the struct, and you can only place declarations in a struct. Try appending somewhere else outside the struct, like in the init.

Chris Ediger
1,965 PointsThanks, Andrew. I tried moving the append statement outside of the struct, but that didn't work. How would I add an init with it in it? (Sorry, pretty new to Swift.)

Chris Ediger
1,965 PointsI think I've figured it out...
import Foundation
struct Toolkit {
var tools = [ [
"name": "Know Yourself to Lead Yourself",
"shape": "icon_knowyourself.pdf",
"image": "know_yourself_to_lead_yourself.pdf",
"backgroundColor": ["red": 215, "green": 34, "blue": 14, "alpha": 1.0]
],
[
"name": "The Core",
"shape": "icon_thecore.pdf",
"image": "the_core.pdf",
"backgroundColor": ["red": 185, "green": 34, "blue": 14, "alpha": 1.0]
],
[
"name": "5 Circles of Influence",
"shape": "icon_5circles.pdf",
"image": "5_circles_of_influence.pdf",
"backgroundColor": ["red": 185, "green": 34, "blue": 14, "alpha": 1.0]
],
[
"name": "Support Challenge Matrix",
"shape": "icon_supportchallenge.pdf",
"image": "support_challenge_matrix.pdf",
"backgroundColor": ["red": 205, "green": 34, "blue": 14, "alpha": 1.0]
]
]
init() {
tools.append([
"name": "Creating Healthy Culture",
"shape": "icon_healthyculture.pdf",
"image": "creating_healthy_culture.pdf",
"backgroundColor": ["red": 185, "green": 34, "blue": 14, "alpha": 1.0]
])
}
}