0

I'm completely stuck. I'm working on building an iOS app in Swift. 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 to that particular issue here: https://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]
    ])

}
Community
  • 1
  • 1
  • 1
    You cannot call `tools.append()` outside of any function or method ... – Martin R Apr 23 '15 at 19:34
  • An explicit type annotation can also sometimes help to solve the "indexing indefinitely" problem, in your case `var tools : [NSDictionary] = ...` – Martin R Apr 23 '15 at 19:38

1 Answers1

0

There are limitations on what kinds of operations you can perform while you're defining the interface for a struct or class. You can do some basic initialization of values, but calling a method like append() isn't allowed. Here's a quick tweak to your code where I've moved the append into the init() method instead:

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]
        ])
    }
}

let kit = Toolkit()
println(kit.tools)
mattdaw
  • 121
  • 4