Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6363745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:04:32+00:00 2026-05-25T00:04:32+00:00

I have two objects of class WidgetClass in my stored model. They are saved

  • 0

I have two objects of class WidgetClass in my stored model. They are saved each time the app exits and reloaded each time it starts. I want to update my model to make one of them a WidgetSubclass object. WidgetSubclass will be a subclass of WidgetClass.

WidgetClass has quite a lot of ivars. WidgetSubclass will add few or none.

What is the most efficient way to accomplish the update? I am not using core data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:04:33+00:00Added an answer on May 25, 2026 at 12:04 am

    Couple of things.

    If the subclass does not add any ivars to the superclass, you can actually get away with the following:

    WidgetSubclass* widget = (WidgetSubclass*)[[WidgetClass alloc]initWithCoder: someCoder];
    Class object_setClass(widget, [WidgetSubclass class]);
    

    There is some risk that changes in the runtime could break the above code. So here is a safer way:

    Foo.m:

    -(void) copyIvarsTo: (Foo*) foo {
        [super copyIvarsTo: foo];
        foo.ivar1 = [self.objectIvar1 copy];
        foo.ivar2 = [self.objectIvar2 copy];
        foo.floatIvar = self.floatIvar;
        // etc.  Method works fine if foo is actually a member of a subclass.
     }
    
     -(Foo*) copy {
        Foo* clone = [[self class]alloc];
        [self copyIvarsTo: clone];
        return clone;
     }
    

    Now I can have the following NSObject category method:

     -(NSObject*) wj_copyWithSubclass: (Class) subclass {
        if (![self respondsToSelector: @selector(copyIvarsTo:)])
            return nil;
        NSAssert([subclass isSubclassOfClass: [self class]], @"call copyWithSubclass only on subclasses");
        NSObject* clone = [subclass alloc];
        [self copyIvarsTo: clone];
        return clone; // at this point, clone has copied all the ivars that are members of the receiver's class.  Other ivars have their default values.  Calling code needs to handle that.
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two objects of a class. I need to compare each field with
I have two objects. @Entity class Person extends Model { ... @OneToOne Category category;
Let's say I have created two objects from class foo and now want to
I have two objects and I want to merge them: public class Foo {
Let's say I have two objects: User and Race. class User attr_accessor :first_name attr_accessor
I have two student objects. class Student{ int physics; int english; int chemistry; }
Using Dozer to map two objects, I have: /** /* This first class uses
I have a class A containing two pointers to objects of another class B.
I have a NIB with two NSWindow objects. The controller class has two IBOutlets,
I need to return two values at a time, so I have: class IterableObject(object):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.