我想将一个对象数组分配给名为的类中的数组变量: SchoolClass
假设我的项目名称是 MyProjectName ,并且我有以下类:
import Foundation
class Student {
var name : String
var age : Int
init(name : String, age : Int) {
self.name = name
self.age = age
}
}
class SchoolClass {
var students : [Students]
var speciality : String
init (students : [Students], speciality : String) {
self.students = students
self.speciality = speciality
}
}
我给他们填写了一些数据:
var studentsClassA = [Student]()
studentsClassA[0] = Student(name: "Toto", age: 1)
studentsClassA[1] = Student(name: "Titi", age: 2)
var studentsClassB = [Student]()
studentsClassB[0] = Student(name: "Lolo", age: 3)
studentsClassB[1] = Student(name: "Lili", age: 4)
var allClasses = [SchoolClass]()
allClasses.append(students : Student(students), speciality : "Sport")
allClasses.append(students : Student(students), speciality : "Art")
为什么我要打印allClasses [0]的内容。这样的学生:
print("Student list info in 1st class : \(allClasses[0].students)")
它只显示类型而不是像这样的内容值:
MyProjectName.Student
而不是像:
SchoolClass(student: [{"Toto", 1}, {"Titi", 2}], speciality: "Sport")