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 parent class name (resolved), coredata storage array
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 = [NSMutableArrayarray];
NSMutableArray * array2 = [NSMutableArrayarray];
For (int I = 0; I <5; I ++ ){
Student * stu1 = [[Studentalloc] init];
Stu1.name = [NSStringstringWithFormat: @ "jack % I", I];
[ArrayaddObject: stu1];
Teacher * teacher = [[Teacheralloc] init];
Teacher. name = [NSStringstringWithFormat: @ "limei % I", I];
[Array2addObject: teacher];
}
For (Person * pin array ){
NSLog (@ "% @ ------", p. class );
}
For (Person * pin array2 ){
NSLog (@ "% @ ------", p. class );
}
// You can see from the printed result 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 the Child class he is and determines how to assign values to him.
// Example
For (Person * pin array2 ){
If (p. class = [Studentclass]) {
NSLog (@ "student ");
} Else {
NSLog (@ "not a student ");
}
}
If ([array [0] class] = [Studentclass]) {
NSLog (@ "student ");
} Else {
NSLog (@ "not a student, % @", [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)