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 9296191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:46:48+00:00 2026-06-18T21:46:48+00:00

I have a category for NSManagedObjectContext , which inserts an NSManagedObject in another NSManagedObjectContext

  • 0

I have a category for NSManagedObjectContext , which inserts an NSManagedObject in another NSManagedObjectContext and returns item:

@implementation NSManagedObjectContext (GTEntity)

- (id)addEntity:(id)entity {
     NSManagedObject *entityObject = [self objectWithID:[entity objectID]];
     return entityObject;
}

@end

My question is how I return object the same type as I’ve got. I mean, if I call [ctx addEntity:city] with City *city, where the City is a NSManagedObject subclass, I would like to get the object in other context but the same type, not in NSManagedObject type. This type is generic and should be determinated at the runtime from entity, like objc_getClass(entity). I found some macros, but it has some strange errors Expected "]" after (cls *):

#define objc_dynamic_cast(obj, cls) \
([obj isKindOfClass:(Class)objc_getClass(#cls)] ? (cls *)obj : NULL)
  • 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-18T21:46:49+00:00Added an answer on June 18, 2026 at 9:46 pm

    Just do City *city = [someManagedObjectContext addEntity:someCityObject]; assuming City derives from NSManagedObject. Your return variable will have been dynamically casted. Though I don’t believe there is anything to be gained from making a category out of it, you could just do it directly like so:

    City *city = [someManagedObjectContext objectWithID:[someCityObject objectID]];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Category entity which has a Nullable ParentId field. When the method
I have category entity and subcategory entity. I need to get subcategory data to
I have an entity named Commercial. I have an Category entity where the list
I have accounts which have category associated with it & orders table where order
great community :) I have category list which have nested child category list, which
I have 2 models which have a has_and_belongs_to_many relation: class Category < ActiveRecord::Base has_and_belongs_to_many
I have a simple function inside a NSManagedObjectContext category to help me get results
I have category model, in which I want to load some default data. How
I have a category of NSMutableArray which has a deletion method: @interface NSMutableArray(MyCategory) -
I have a category entity that has a category as parent and a list

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.