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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:16:08+00:00 2026-05-27T22:16:08+00:00

I’m using custom classes for my CoreData stack. Class properties are set properly in

  • 0

I’m using custom classes for my CoreData stack. Class properties are set properly in the model. There’s some points in the app that are abstracted to use setValue… on an NSManagedObject, but I have a few cases where that fails with an NSInvalidArgumentException, specifically when setting a related object; error indicates it wants the specific type, and it’s’ getting an NSManagedObject, hence the error. So, I thought I would just take the short route and cast my instance prior to the offending call, if it’s of a certain entity; like this:

NSManagedObject *addressObject = [NSEntityDescription insertNewObjectForEntityForName:@"Address" inManagedObjectContext:[object managedObjectContext]];
    if ([[[object entity]name] isEqualToString:@"Hospital"]) {
        Contact *contact = (Contact*)object;
        DLog(@"The class of contact is: %@", [contact class]);
        contact.Address = addressObject;

    }
    else{
        [object setValue:addressObject forKey:@"Address"];
    }

I know, Address shouldn’t be capitalized; I inherited this mess… anyway, I would totally expect that the contact object is a Contact, but it’s not, it’s an NSManagedObject! What am I doing wrong with the cast? Everything I’ve uncovered says this is the right way to cast, but for some reason, it’s not working for me here. Of course, this wouldn’t be necessary if the addressObject didn’t complain about getting an NSManagedObject instead of a Contact (sorry, Hospital inherits from Contact here), and that’s another baffling thing, but first things first. How can I coerce object to type Contact, which it really is?

Here’s the relevant trace:

* Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘Unacceptable type of value for to-one relationship: property = “Contact”; desired type = Contact; given type = NSManagedObject; value = …

For completeness sake, the Address class has declaration for Contact as:

@property (nonatomic, retain) NSManagedObject * Contact;

with implementation for Contact as the normal dynamic, like:

@dynamic Contact;

Maybe I need some sleep? 😉 Thanks

  • 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-27T22:16:09+00:00Added an answer on May 27, 2026 at 10:16 pm

    The cast isn’t your problem. Casting doesn’t change what class an object is — just what the compiler thinks it is.

    You object is an NSManagedObject but not a Contact instance. In you code the object you have is a Hospital entity. Double-check that the Hospital entity is set to use the Contact class (or a subclass)).

    It is important to note that Entity inheritance and Objective-C class inheritance don’t have to match. I.e. you can have Hospital be a subentry of Contact and still have the Contact entity map to a Contact class without Hospital map to a subclass of the Contact class. It is valid for the Hospital entity to map to the (same) Contact class or even to NSManagedObject (which is what I suspect you’ve done).

    This may seem confusing but can be very powerful if used correctly.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.