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)

Source: Internet
Author: User

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)

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.