struct GroupStruct {
var groupName = " "
var groupAge= " "
var tests: [TestStruct] = []
}
struct TestStruct {
var desc= " "
var title= " "
}
当GroupStruct.tests [0] .desc =“abc”时,索引超出范围。 为什么会这样?谢谢你的帮助。
答案 0 :(得分:1)
您必须初始化GroupStruct。 然后在其上添加一个新元素。
import Foundation
struct GroupStruct {
var groupName = " "
var groupAge = " "
var tests: [TestStruct] = []
}
struct TestStruct {
var desc = " "
var title = " "
}
// Creates a new instance of GroupStruct
var groupStruct = GroupStruct()
// Appends a new instance of TestStruct (will be at index 0)
groupStruct.tests.append(TestStruct(desc: "abd", title: ""))
答案 1 :(得分:0)
这样做:
var groupStruct = GroupStruct() // Declare groupStruct instance
var TestStructObj = TestStruct(desc: "abc", title: "abc") // Create TestStruct instance
groupStruct.tests.append(TestStructObj) // Append it to tests array
提示 - 以这种方式声明数组:
var tests = [TestStruct]()