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

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.