iOS CoreData receives an array of subclasses with the instance object of the parent class, prints the Person.class, and the result is the subclass class name? or the parent class name (resolved)

Source: Internet
Author: User

New Person class

Subclass of Person Student

Teacher

Today I think of the database encapsulation, because the query results are inherited from Nsmanagerobject, but his sub-class properties are not necessarily the same, so I think I can not judge after the query that he is the subclass and according to the properties of the subclass to assign values, So first I want the class of the object to be stored in the array to judge

The test is as follows: (the main code to contribute as follows, other documents to suggest it, 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 * p in array) {

NSLog (@ "%@------", p.class);

}

for ( Person * p in Array2) {

NSLog (@ "%@------", p.class);

}

// from the printed result it can be seen that although I receive it with his parent class, the. class can still display the subclass name so that the object traversed from the array I can use . class to get what kind of child he is, and decide how to assign him a value.

For example

for ( Person * p in Array2) {

if (p.class = = [Studentclass]) {

NSLog (@ " is a student ");

}Else {

NSLog (@ " not a student ");

}

}

if ([array[0]class] = = [Studentclass]) {

NSLog (@ " is a student ");

}Else{

NSLog (@ " not a student, is %@", [array[0]class]);

}


iOS CoreData receives an array of subclasses with the instance object of the parent class, prints the Person.class, and the result is the subclass class name? Or is the parent class name (resolved) iOS CoreData with the instance object of the parent class the person receives an array that has subclasses, prints the Person.class, and the result is the subclass class name? Or is the parent class name (resolved) iOS CoreData with the instance object of the parent class the person receives an array that has subclasses, prints the Person.class, and the result is the subclass class name? Or is the parent class name (resolved) iOS CoreData with the instance object of the parent class the person receives an array that has subclasses, prints the Person.class, and the result is the subclass class name? or the parent class name (resolved)

iOS CoreData receives an array of subclasses with the instance object of the parent class, prints the Person.class, and the result is the subclass class name? or the 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.