Author Topic: Subclassing entities  (Read 6816 times)

rtamesis

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Subclassing entities
« on: November 01, 2013, 01:11:07 PM »
When I try to create a subclass of another entity in the CoreData modeler, I find that the SQLite file that Xcode creates when I run the app contains only the table of the parent entity class. For instance, I first created an entity called QuizEntity and filled its attributes, then I created two entities called QuizPhysicsEntity and QuizGeometricalEntity and made QuizEntity its parent class (I didn't add any new attributes to the two child entities since I want them to inherit the attributes of the parent QuizEntity). After running the app, I then use FireFox's SQLite Manager to look at the SQL file created so that I can fill it with data, but I find only the QuizEntity table. Is this normal behavior or is there some additional step that I missed in order to make Xcode create an SQL file with tables for QuizPhysicsEntity and QuizGeometricalEntity?

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
Re: Subclassing entities
« Reply #1 on: November 01, 2013, 01:58:09 PM »
Yes, that's the default and correct behavior--subclasses share the same table as superclasses. If you have properties in the subclass that are not in the parent, then corresponding columns are added to the table and will simply be void for the superclass entities.

All the best!
Kevin

rtamesis

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: Subclassing entities
« Reply #2 on: November 01, 2013, 05:45:31 PM »
But what if the subclass entities do not have unique attributes that are absent in the parent class entity? If the subclassed entities share the same table as that of the parent, then how can I fill the common SQL database with data and have them be associated with the correct subclass entity? Does that mean that I have to create a unique attribute for each of the subclassed entities in order to accomplish this?

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
Re: Subclassing entities
« Reply #3 on: November 03, 2013, 07:13:18 PM »
Not at all...remember from Book 3: Navigating Xcode, Chapter 12: Working with Core Data, under the section Firefox and the SQLite Manager, there is a Z_ENT column in the SQLite table that contains the entity ID. Every entity of a particular type has the same entity ID, so these will be different for your class and your subclass.

All the best!
Kevin

rtamesis

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: Subclassing entities
« Reply #4 on: November 07, 2013, 10:43:13 PM »
Thank you! That was just driving me nuts!

Off topic, I was looking at a tutorial at http://www.raywenderlich.com/50151/text-kit-tutorial on Text Kit, and they mentioned that there is no way to modify a UITextview created on the storyboard so that one can have dynamic text formatting without having to create it programmatically instead. Wouldn't creating a UITextView subclass or perhaps a category to UITextview and then having the textview in the storyboard use that subclass go about solving this problem?

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
Re: Subclassing entities
« Reply #5 on: November 08, 2013, 02:59:02 PM »
Yes, you could create a subclass, style it the way that you want and then use it in the storyboard. If you have a number of text views that you want styled the same way, this could save you some code. However, if you only have one text view, it's easier to just write code to change the style dynamically at run time.

Kevin