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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:32:45+00:00 2026-06-10T12:32:45+00:00

How can I code a custom init for NSManagedObject subclass? I would like something

  • 0

How can I code a custom init for NSManagedObject subclass? I would like something like initItemWithName:Volume: for instance. Where Item is a NSManagedObject subclass with two attributes, name and volume.

  • 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-10T12:32:47+00:00Added an answer on June 10, 2026 at 12:32 pm

    Carlos,

    As Nenad Mihajlovic suggested you could create a category for this.

    So, for example, if you have an Item class, you could create a category called Item+Management and put the creation code there. Here you can find a simple example.

    // .h
    
    @interface Item (Management)
    
    + (Item*)itemWithName:(NSString *)theName volume:(NSNumber*)theVolume inManagedObjectContext:(NSManagedObjectContext *)context;
    
    @end
    
    // .m
    
    + (Item*)itemWithName:(NSString *)theName volume:(NSNumber*)theVolume inManagedObjectContext:(NSManagedObjectContext *)context
    {
        Item* item = (Item*)[NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:context];
        theItem.name = theName;
        theItem.volume = theVolume;
    
        return item;
    }
    

    When you want to create a new Item, do an import like

    #import "Item+Management.h"
    

    and use like this

    Item* item = [Item itemWithName:@"test" volume:[NSNumber numberWithInt:10] inManagedObjectContext:yourContext];
    // do what you want with item...
    

    This approach is very flexible and very easy to maintain during the app developing.

    You can find further info at Stanford Course Lecture 14 code sample. In addition, see also free videos on iTunes by Stanford (if you have an Apple ID).

    Hope that helps.

    P.S. For the sake of simplicity, I suppose name is a NSString and volume is a NSNumber. For volume it could be better to use NSDecimalNumber type.

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

Sidebar

Related Questions

Can anyone tell me how I can execute my custom jQuery code when Drupal's
How can I protect my web server, if I run custom users code on
How can I set a custom filter in my code to ignore accents and
In the aspx code view , we can code like this: <asp:ListBox runat=server> <asp:ListItem
I have a Core Data entity that uses a custom subclass of NSManagedObject. I
How can I specify a custom error code/response to a DATA command using Twisted's
Suppose we have this example: http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html with source code available here: http://code.google.com/p/myandroidwidgets/source/browse/trunk/Phonebook/src/com/abeanie/ How can
I can code in Java, and I'm trying to understand the wiki article on
One thing I lack understanding on is how can code compiled for an Intel
I am using XAMPP and was wondering if I can code my site to

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.