a Swift, ho una classe School
, ha un students
proprietà di tipo [AnyObject]!
Non è possibile assegnare un valore di tipo 'NSArray' per un valore di tipo '[ANYOBJECT]'
class School : NSObject {
var students: [AnyObject]!
...
}
ho ricevuto un'istanza di School
e un di stringa che rappresenta i nomi degli studenti. Voglio assegnare questa variabile NSArray
-students
:
var school = School()
var studentArray : NSArray = getAllStudents()
//ERROR:Cannot assign a value of type 'NSArray' to a value of type '[AnyObject]'
school.students = studentArray
Perché questo errore? L'array non è compatibile in modo rapido con NSArray nell'obiettivo c ??? Come sbarazzarsi di errore del compilatore sopra?
L'utilizzo del tipo effettivo nella dichiarazione consente al compilatore di applicare compiti che generalmente sono una "cosa buona". – zaph