Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-C: Can't put primitives in NSDictionary and retrieve them successfully
    text
    copied!<p>I know I can't put them in directly, but I have to convert them to an <code>NSNumber</code>, for example. I am converting them to <code>NSNumber</code>s, and then putting them in the dictionary. I immediately log the keys I'm populating, and they are blank.</p> <p>Here's an example:</p> <pre><code>- (NSDictionary*) dictionaryFromWrestler { /* * Used ot create a dictionary from the instance of the wrestler * Can be used to send and receive wrestlers through notification center */ NSDictionary* dictionary; NSNumber* _score = [NSNumber numberWithInt:score]; dictionary = [NSDictionary dictionaryWithObjectsAndKeys:firstName, @"firstName", lastName, @"lastName", fullName, @"fullName", shortName, @"shortName", team, @"team", seed, @"seed", actualWeight, @"actualWeight", dob, @"dob", club, @"club", hometown, @"hometown", state, @"state", grade, @"grade", extraField, @"extraField", phone, @"phone", address, @"address", zip, @"zip", record, @"record", gradeAbbr, @"gradeAbbr", twid, @"twid", teamId, @"teamId", position, @"position", _score, @"score", [NSNumber numberWithInt:teamScore], @"teamScore", [NSNumber numberWithInt:period1Score], @"period1Score", [NSNumber numberWithInt:period2Score], "@period2Score", [NSNumber numberWithInt:periods], @"periods", [NSNumber numberWithBool:hasRidingTime], @"hasRidingTime", nil]; NSLog(@"dictionaryFromWrestler Score: %@", lastName); NSLog(@"dictionaryFromWrestler Score: %i", score); NSLog(@"dictionaryFromWrestler Score: %@", _score); NSLog(@"dictionaryFromWrestler Score: %d", [[dictionary objectForKey:@"score"] intValue]); NSLog(@"dictionaryFromWrestler Score: %@", [dictionary valueForKey:@"score"]); NSLog(@"dictionaryFromWrestler Score: %@", [dictionary valueForKey:@"lastName"]); return dictionary; } </code></pre> <p>My log looks like this:</p> <pre><code>2012-05-18 17:46:53.117 Wrestling Tools[31509:403] dictionaryFromWrestler Score: Brown 2012-05-18 17:46:53.117 Wrestling Tools[31509:403] dictionaryFromWrestler Score: 2 2012-05-18 17:46:53.117 Wrestling Tools[31509:403] dictionaryFromWrestler Score: 2 2012-05-18 17:46:53.117 Wrestling Tools[31509:403] dictionaryFromWrestler Score: (null) 2012-05-18 17:46:53.117 Wrestling Tools[31509:403] dictionaryFromWrestler Score: (null) 2012-05-18 17:46:53.117 Wrestling Tools[31509:403] dictionaryFromWrestler Score: Brown </code></pre> <p>As you can see, the primitives fail, but the objects work. What the heck?</p>
 

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