gpt4 book ai didi

ios - 如何在 Swift 中访问结构体类型数组中的变量?

转载 作者:行者123 更新时间:2023-11-30 14:18:01 25 4
gpt4 key购买 nike

如何访问数组中结构体的变量?我尝试通过打印数组本身和使用第一个索引打印数组来隔离变量,但没有输出。

taskList: [["vacuum", "20 mins", "medium"], ["take out trash", "5 mins", "low"]] // list to choose from

struct Task { //to reassign from multidimensional array
var name: String = "None"
var time: String = "None"
var materials: String = "None"
}

var newTask = Task() //creation of a new task
var taskPool = [Task]() //pool of selected tasks

newTask.name = taskList[0][0]//assign name found in another list
newTask.time = taskList[0][1]//assign time found in another list
newTask.materials = taskList[0][2]//assign priority found in another list
taskPool += [newTask]
println(taskPool)
println(taskPool[0])

最佳答案

通过这种方式您可以访问它:

var taskList = [["vacuum", "20 mins", "medium"],["take out trash", "5 mins", "low"]] // list to choose from

struct Task { //to reassign from multidimensional array
var name: String = "None"
var time: String = "None"
var materials: String = "None"
}

var newTask = Task() //creation of a new task
var taskPool = [Task]() //pool of selected tasks
//
newTask.name = taskList[0][0]//assign name found in another list
newTask.time = taskList[0][1]//assign time found in another list
newTask.materials = taskList[0][2]//assign priority found in another list
taskPool += [newTask]

taskPool // [{name "vacuum", time "20 mins", materials "medium"}]
taskPool[0].name // "vacuum"
taskPool[0].time // "20 mins"
taskPool[0].materials // "medium"

关于ios - 如何在 Swift 中访问结构体类型数组中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30888500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com