无法弄清楚如何附加到数组(在结构内)(Swift) [英] Can't figure out how to append to an array (within a struct) (Swift)

查看:47
本文介绍了无法弄清楚如何附加到数组(在结构内)(Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全被困住了.我正在使用Swift构建iOS应用.我需要在一个结构中定义一系列工具,但是如果我包含的项目超过4个,则Xcode会卡住索引.

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.

在此处找到针对该特定问题的解决方案: https://stackoverflow.com/a/27531394 .但是,我不知道如何追加到数组.当我尝试执行以下代码时,出现以下错误:预期的声明".有什么想法吗?

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

}

推荐答案

在定义结构或类的接口时,可以执行哪些类型的操作受到限制.您可以对值进行一些基本的初始化,但是不允许调用诸如append()之类的方法.这是对您的代码的快速调整,在该代码中,我将附加内容移到了init()方法中:

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)

这篇关于无法弄清楚如何附加到数组(在结构内)(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆