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

  • Home
  • SEARCH
  • 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 8824165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:32:30+00:00 2026-06-14T06:32:30+00:00

Say I have a subclass of NSManagedObject called MBManagedSquare and MBManagedCircle . An MBManagedSquare

  • 0

Say I have a subclass of NSManagedObject called MBManagedSquare and MBManagedCircle. An MBManagedSquare and MBManagedCircle define a method prepareFromDictionary:(NSDictionary*)dic, and both of their implementations are different.

Say I have this code:

NSString *type = // could be @"MBManagedSquare" or @"MBManagedCircle"
NSEntityDescription *desc = [NSEntityDescription entityForName:type inManagedObjectContext:_context];
NSManagedObject *object = [[NSManagedObject alloc] initWithEntity:desc insertIntoManagedObjectContext:_context];

So the type of entity it will be with Core Data is determined dynamically via a type string. So all I know is that it is an NSManagedObject.

What I want to do is call the prepareFromDictionary: method for the right class.

So if the type is “MBManagedSquare”, I want to cast the object to an MBManagedSquare, and then call

[castedObject prepareFromDictionary:dic];

What I tried doing is:

Class class = NSClassFromString(type);
class *castedObject = (class*)object;

but I get an expected expression error. I’m not sure if this is even possible. How would I do this?

  • 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-06-14T06:32:31+00:00Added an answer on June 14, 2026 at 6:32 am

    You don’t need to worry about calling the right class if the selectors and their parameters match — ObjC has plenty of dynamic dispatch powers.

    As far as an implementation, it’s pretty common to either:

    • create a common base with the interface you want
    • or create a protocol which both classes adopt:

    MONProtocol.h

    @protocol MONManagedShapeProtocol < NSObject >
    - (void)prepareFromDictionary:(NSDictionary *)pDictionary;
    @end
    

    then (since you know it is one of the two types, MBManagedSquare or MBManagedCircle) either derive from the base or adopt the protocol and declare your variable like:

    // if subclass
    MBManagedShape * castedObject = (MBManagedShape*)object;
    

    or

    // if protocol
    NSManagedObject<MONManagedShapeProtocol>* castedObject =
                            (NSManagedObject <MONManagedShapeProtocol>*)object;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an NSManagedObject subclass called Item . Whenever an item instance is
Say I have a class called A, and in A there's a method called
I have a custom NSManagedObject subclass, say, Person . I also have a UIView
Say I have an abstract parent class called Parent that implements a method called
Say I have NavigationController->Root[Table View Controller subclass] So from didSelectRowAtIndexPath of my Table View
Let's say I have the following headers: @interface SuperClass : NSObject @interface SubClass :
Say I have this class Myclass that contains this method: public class MyClass {
Let's say I have a model called MySuper : class MySuper(models.Model): some_attr = models.CharField(max_length=128)
Say you have a method that returns a newly generated NSArray instance that is
So say I have a subclass that extends a superclass. In what scenarios do

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.