IosCoreData uses the parent class's instance object "person" to receive arrays containing child classes and print "person. class". The result is the subclass class name? Or parent class name (resolved)
Create Person class
Student, a subclass of person
Teacher
What I think of today's database encapsulation is that the query results are inherited from NSManagerObject, but their subclass attributes are not necessarily the same, so I want to know if I can determine after the query that the sub-class is and assign values according to the sub-class attributes. So first, I want to judge the class of the object stored in the array.
The test is as follows: (contribute the main code as follows. We recommend that you use other files. This is very simple)
NSMutableArray * array = [NSMutableArray array]; NSMutableArray * array2 = [NSMutableArray array]; for (int I = 0; I <5; I ++) {Student * stu1 = [[Student alloc] init]; stu1.name = [NSString stringWithFormat: @ "jack % I", I]; [array addObject: stu1]; teacher * teacher = [[Teacher alloc] init]; teacher. name = [NSString stringWithFormat: @ "limei % I", I]; [array2 addObject: teacher] ;}for (Person * p in array) {NSLog (@ "% @ ------", p. class);} for (Person * p in array2) {NSLog (@ "% @ ------", p. class);} // you can see from the printed results that although I receive it with its parent class ,. the subclass name can still be displayed after the class, so that I can use the object traversed from the array. class gets which subclass he is and determines how to assign a value to him/for example (Person * p in array2) {if (p. class = [Student class]) {NSLog (@ "Student");} else {NSLog (@ "not Student ");}} if ([array [0] class] = [Student class]) {NSLog (@ "Student");} else {NSLog (@ "not a Student, yes % @ ", [array [0] class]);}
Ios CoreData receives arrays containing child classes using the parent class's instance object "person" and prints the person. class. What is the result of the subclass class name? Or is the parent class name (solved) ios CoreData receiving arrays containing subclasses with the parent class instance object "person", printing the person. class, and the result being the subclass class name? Or is the parent class name (solved) ios CoreData receiving arrays containing subclasses with the parent class instance object "person", printing the person. class, and the result being the subclass class name? Or is the parent class name (solved) ios CoreData receiving arrays containing subclasses with the parent class instance object "person", printing the person. class, and the result being the subclass class name? Or parent class name (resolved)