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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:20:11+00:00 2026-06-16T09:20:11+00:00

Most (all I’ve seen) Core Data tutorials use the following code snippet with @MyEntityClass

  • 0

Most (all I’ve seen) Core Data tutorials use the following code snippet with @"MyEntityClass" hard-coded in:

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"MyEntityClass"];

Is it safe to use NSStringFromClass() as an Entity Name?

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([MyEntityClass class])];

This seams to be much easer to deal with regarding refactoring and the like. Especially since I am having Xcode create my NSManagedObject subclasses. I ask because I have never seen this before, so perhaps I am missing something.

  • 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-16T09:20:12+00:00Added an answer on June 16, 2026 at 9:20 am

    Yes, that code is fine, if your entity’s class is set to MyEntityClass in your model.

    I prefer to give the entity class a class method that returns the entity name:

    + (NSString *)entityName {
        return NSStringFromClass(self);
    }
    

    and call it like this:

    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:[MyEntityClass entityName]];
    

    This way, if I want to change the class name without changing the entity name in the model, I can just make the change in the class method:

    + (NSString *)entityName {
        return @"NewEntityName";
    }
    

    Why would I do that? Well, I might decide on a better name for the entity. Changing the class name doesn’t break compatibility with an existing Core Data persistent store, but changing the entity name in the model file does. I can change the class name, and the entityName method, but leave the entity name unchanged in the model, and then I don’t have to worry about migration. (Lightweight migration supports renamed entities so it’s not that big of a deal either way.)

    You could go further and actually have the entityName method look up the entity name from the managed object model at runtime. Suppose your application delegate has a message that returns the managed object model:

    + (NSString *)entityName {
        static NSString *name;
        static dispatch_once_t once;
        dispatch_once(&once, ^{
            NSString *myName = NSStringFromClass(self);
            NSManagedObjectModel *model = [(AppDelegate *)[UIApplication delegate] managedObjectModel];
            for (NSEntityDescription *description in model.entities) {
                if ([description.managedObjectClassName isEqualToString:myName]) {
                    name = description.name;
                    break;
                }
            }
            [NSException raise:NSInvalidArgumentException
                format:@"no entity found that uses %@ as its class", myName];
        });
        return name;
    }
    

    Obviously, if you really want to do this, you should factor out the contents of the dispatch_once block into a helper method, probably on your app delegate (or wherever you get the model).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use my own database in an Android application. Most all the
Most (all?) OAuth resources - both information about the protocol and code libraries for
I have seen al most all the other posts about this, but I can't
I was looking through the GHUnit source code, and noticed that they wrap most/all
For most (all?) Facebook pages, I can access its graph data by getting the
i'd like to add all or most of my mouse events to stage, but
I am trying to find a universal way to expand most if not all
We've got a multi-agent Java environment where different agent would most likely produce all
I've written a fair amount of software over recent years, most of which all
We all know that most applications out there assume class names to follow the

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.