无法弄清楚如何追加到数组(在结构内)(Swift)
Can't figure out how to append to an array (within a struct) (Swift)
我完全卡住了。我正在 Swift 中构建一个 iOS 应用程序。我需要在一个结构中定义一组工具,但如果我包含的项目超过 4 个,Xcode 就会卡在索引中。
在此处找到该特定问题的解决方案:。但是,我不知道如何附加到数组。当我尝试执行下面的代码时,出现以下错误:"Expected declaration"。有什么想法吗?
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]
])
}
在定义结构或 class 的接口时,您可以执行的操作类型是有限制的。您可以对值进行一些基本的初始化,但不允许调用类似 append() 的方法。这是对您的代码的快速调整,我已将 append 移动到 init() 方法中:
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 中构建一个 iOS 应用程序。我需要在一个结构中定义一组工具,但如果我包含的项目超过 4 个,Xcode 就会卡在索引中。
在此处找到该特定问题的解决方案:。但是,我不知道如何附加到数组。当我尝试执行下面的代码时,出现以下错误:"Expected declaration"。有什么想法吗?
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]
])
}
在定义结构或 class 的接口时,您可以执行的操作类型是有限制的。您可以对值进行一些基本的初始化,但不允许调用类似 append() 的方法。这是对您的代码的快速调整,我已将 append 移动到 init() 方法中:
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)