Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A couple of thoughts.</p> <p>First, you have:</p> <pre><code>Person { NSString *name; NSNumber *age; } @property(nonatomic, retain) NSNumber* TheAge; @property(nonatomic, retain) NSString *TheName; </code></pre> <p>And in your implementation, you have:</p> <pre><code>@synthesize TheName = name; @synthesize TheAge = age; </code></pre> <p>Apple no longer recommends you define your ivars explicitly, but let your synthesize statement take care of it (probably because if you misspell one of those, you can end up with an extra, unintended ivar). So you might just want:</p> <pre><code>@interface Person : NSObject @property(nonatomic, retain) NSNumber* theAge; @property(nonatomic, retain) NSString* theName; @end </code></pre> <p>Also, when naming your ivars, the emerging standard is (but, of course, you can do what you want) to use underscores for ivars, and starting your variable names with lowercase ... uppercase for classes, lowercase for variables, e.g.:</p> <pre><code>@synthesize theName = _theName; @synthesize theAge = _theAge; </code></pre> <p>Second, in your <code>dealloc</code> you're setting those ivars to nil. While that's the correct way to release in ARC code, in non-ARC code, you should just use your ivars, and use the release command:</p> <pre><code>- (void)dealloc { [_theAge release]; [_theName release]; [super dealloc]; } </code></pre> <p>Third, are you writing that setter <code>setTheAge</code> or are you guessing what the compiler is doing itself? Your code can probably be improved (you're using a local variable which is the same as your property, which is just confusing, you'd want to do key value notifications, etc.), but I won't address that because you'd be better off just letting the compiler do its own setter unless there is something else that you're trying to accomplish. Let me know.</p> <p>Fourth, your <code>-(Person*)personFromDictionary:(NSDictionary*)dic</code> is a curious method. I'd suggest writing your own init, like:</p> <pre><code>- (Person*)initFromDictionary:(NSDictionary*) dic { self = [super init]; // I always inherit from NSObject, in which case you'd have this line if (self) { self.theAge = [dic objectForKey:kAge]; self.theName = [dic objectForKey:kName]; } return self; } </code></pre> <p>This way, you could create your Person object like:</p> <pre><code>Person *aPerson = [[Person alloc] initWithDictionary:entryDictionary]; </code></pre> <p>Your current implementation assumes the <code>Person</code> object already exists (because you preface the method name with "-" rather than "+"), but then creates a new one. It's a little strange. You might be able to do something like that, but the above code pattern is more commonplace and achieves what I think you want.</p> <p>Finally, I'm not sure what you're trying to do with <code>updateEntries</code>, so I'm not sure what to suggest there, but I don't quite get the <code>updateEntries</code> outside of your pool, whether you meant it to be a dictionary full of Person objects, etc. If you describe what you're trying to accomplish there, I'd be happy to give you my two cents on that, too.</p> <p><strong>Update:</strong></p> <p>By the way, if you're debugging your code, sometimes adding <code>NSLog</code> statements is helpful. You might want to create a <code>description</code> method for your <code>Person</code> class to facilitate that, though, so you can see the contents of your <code>Person</code> objects, e.g.:</p> <pre><code>- (NSString *)description { return [NSString stringWithFormat:@"Person (%p) {\n theName = '%@'\n theAge = %@\n}", self, self.theName, self.theAge]; } </code></pre> <p>This way, if you have a <code>Person</code> object named, say, <code>aRandomPerson</code>, you can then have a statement like:</p> <pre><code>NSLog(@"%@", aRandomPerson); </code></pre> <p>Also, if you have a dictionary entry with <code>Person</code> objects, if you <code>NSLog</code> that dictionary, you'll now have a meaningful log statement. This may help you diagnose what's in your <code>NSDictionary</code> items (if it really has <code>Person</code> objects rather than just <code>NSString</code> and <code>NSNumber</code> objects).</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COGreat detailed answer, I actually made a typo in my personFromDictionary, it is actually a class method. But now I wonder what happens if that method gets called multiple times? Would that affect the behaviour?
      singulars
    2. CO@MrShoot No, if that's a class method, then that probably works. But your snippet that's invoking `updateEntries` is, at best, hard to follow, and possibly a little suspect. Perhaps in an effort to isolate us from irrelevant details you omitted some code that would place this in context. How is the `personFromDictionary` variable getting set (btw, using the same name for a method and a variable is very confusing)? Also, in entryDictionary you are setting the key kAge to a Person (but in `personFromDictionary` method, you suggest that this key would be a NSNumber). ...
      singulars
    3. CO@MrShoot In the `updateEntries` method, you also pass a `updatedDict`, and then you say that each key has a Person in it. Is it _really_ a dictionary for which every key contains a Person object? These are just a few random questions, but that `updateEntries` code just looks pretty darn weird. Also, it looks like you create a pool, but you have code before and after that pool. Is there any reason why this isn't all in the pool? Perhaps if you provide a slightly more complete snippet of that code, maybe we can make sense of this, but I can't make heads or tails of what you're trying to do.
      singulars
 

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