Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <pre><code>return [[parentObject allKeysForObject:item] objectAtIndex:0]; </code></pre> <p>However, when there are multiple items with the same value within a given dictionary in the tree, this statement always returns the first item that has that value …</p> </blockquote> <p>Well, yeah. That's what you told it to do: “Get me all the keys for this value; get me the first item in the array; return that”.</p> <blockquote> <p>… this statement always returns the first item that has that value (it appears to compare the strings using isEqualToString: or hash values).</p> </blockquote> <p>It's not that statement that's doing it; it's how dictionaries work: Each key can only be in the dictionary once and can only have exactly one object as its value, and this is enforced using the hash of the key and by sending the keys <code>isEqual:</code> messages (not the NSString-specific <code>isEqualToString:</code>—keys are not required to be strings*).</p> <p>The values, on the other hand, are not uniquified. Any number of keys can have the same value. That's why going from values to keys—and especially to <em>a</em> key—is so problematic.</p> <p>*Not in NSDictionary, anyway. When you attempt to generate the plist output, it will barf if the dictionary contains any non-string keys.</p> <blockquote> <p>I next tried;</p> <pre><code>-(NSString*)keyFromDictionary:(NSDictionary*)dict forItem:(id)item { for( uint i = 0; i &lt; [[dict allKeys] count]; i++ ) { id object = [dict objectForKey:[[dict allKeys] objectAtIndex:i]]; if ( &amp;object == &amp;item ) { return [[dict allKeys] objectAtIndex:i]; } } return nil; } </code></pre> <p>But this always returns nil.</p> </blockquote> <p>That's the least of that code's problems.</p> <p>First, when iterating on an NSArray, you generally should not use indexes unless you absolutely need to. It's much cleaner to use <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocFastEnumeration.html" rel="nofollow">fast enumeration</a>.</p> <p>Second, when you do need indexes into an NSArray, the correct type is <code>NSUInteger</code>. Don't mix and match types when you can help it.</p> <p>Third, I don't know what you meant to do with the address-of operator there, but what you actually did was take the address of those two variables. Thus, you compared whether the local variable <code>object</code> is the same variable as the argument variable <code>item</code>. Since they're not the same variable, that test always returns false, which is why you never return an object—the only other exit point returns <code>nil</code>, so that's what always happens.</p> <p>The problem with this code and the earlier one-liner is that you're attempting to go from a value to a single key, which is contrary to how dictionaries work: Only the keys are unique; any number of keys can have the same value.</p> <p>You need to use something else as the items. Using the keys as the items would be one way; making a <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ModelObjects/" rel="nofollow">model object</a> to represent each row would be another.</p> <p>If you go the model-object route, don't forget to prevent multiple rows in the same virtual dictionary from having the same key. An NSMutableSet plus implementing <code>hash</code> and <code>isEqual:</code> would help with that.</p> <p>You probably should also make the same change to your handling of arrays.</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.
    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.
 

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