Note that there are some explanatory texts on larger screens.

plurals
  1. PONSNull handling for NSManagedObject properties values
    primarykey
    data
    text
    <p>I'm setting values for properties of my <code>NSManagedObject</code>, these values are coming from a <code>NSDictionary</code> properly serialized from a JSON file. My problem is, that, when some value is <code>[NSNull null]</code>, I can't assign directly to the property:</p> <pre><code> fight.winnerID = [dict objectForKey:@"winner"]; </code></pre> <p>this throws a <code>NSInvalidArgumentException</code></p> <pre><code>"winnerID"; desired type = NSString; given type = NSNull; value = &lt;null&gt;; </code></pre> <p>I could easily check the value for <code>[NSNull null]</code> and assign <code>nil</code> instead:</p> <pre><code>fight.winnerID = [dict objectForKey:@"winner"] == [NSNull null] ? nil : [dict objectForKey:@"winner"]; </code></pre> <p>But I think this is not elegant and gets messy with lots of properties to set.</p> <p>Also, this gets harder when dealing with <code>NSNumber</code> properties:</p> <pre><code>fight.round = [NSNumber numberWithUnsignedInteger:[[dict valueForKey:@"round"] unsignedIntegerValue]] </code></pre> <p>The <code>NSInvalidArgumentException</code> is now:</p> <pre><code>[NSNull unsignedIntegerValue]: unrecognized selector sent to instance </code></pre> <p>In this case I have to treat <code>[dict valueForKey:@"round"]</code> before making an <code>NSUInteger</code> value of it. And the one line solution is gone.</p> <p>I tried making a @try @catch block, but as soon as the first value is caught, it jumps the whole @try block and the next properties are ignored.</p> <p>Is there a better way to handle <code>[NSNull null]</code> or perhaps make this entirely different but easier?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload