这是我的代码:迅速沮丧的问题继承
class Student {
var name: String?
init (name: String) {
self.name = name
}
}
class MasterStudent: Student {
var degree: String?
init(name: String, degree: String) {
self.degree = degree
super.init(name: name, degree: degree)
}
}
fun updateStudent(stu: Student) {
var count = 0
for st in studentArray {
if (st.id == stu.id) {
studentArray.removeAtIndex(count)
st as! MasterStudent //thread 1 signal :SIGABRT
studentArray.append(stu)
}
count += 1
}
}
如果我通过功能updateStudent
一个Student
对象,中投以MasterStudent
导致崩溃。我想将Student
对象变成MasterStudent
对象。
感谢
只要切断那条线。这条线是没有意义的(一个单独的演员什么也不做),并且你无论如何都没有理由演员。 – matt
我认为你滥用基本的OOP编程。 MasterStudent从Student继承,所以你的**被强制downcast **可能导致崩溃。简单地说,你不能总是从学生向MasterStudent低调。 – dfd