struct GroupStruct {
var groupName = " "
var groupAge= " "
var tests: [TestStruct] = []
}
struct TestStruct {
var desc= " "
var title= " "
}
Index out of range when GroupStruct.tests[0].desc = "abc" . why did that happen? thanks for helping.
You must initialize the GroupStruct. Then add the a new element on it.
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: ""))
Do it this way:
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
Tip - declare the array this way:
var tests = [TestStruct]()
GroupStruct.tests? have you add anyTestStruct?GroupStruct.tests[0].groupName = "abc"- I see three mistakes in this line. Try to rethink what are you doing and what are you need to achieve