如何在Swift中访问类型为struct的数组中的变量?

时间:2015-06-17 10:09:14

标签: ios arrays swift

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

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

1 个答案:

答案 0 :(得分: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"