Reputation: 13
I'm trying to write a predicate to check a specific value in a dictionary I have set up.
This dictionary has string versions of "0", "1", and "2" as keys which I would like to access.
The predicate I would like to write is:
$player.currencyDictionaries.0.earned > 1000
The problem is that .0 is not allowed. Assuming I cannot easily change how the dictionary stores values (this is in older versions and I'd like to use the same predicate on all versions as it is hosted on a server) is there any way to access the data?
Upvotes: 1
Views: 436
Reputation: 243156
IIRC, you can do this:
$player.currencyDictionaries[0].earned > 1000
(You might need to do ['0']
to guarantee that the passed value is a string and not a number)
Note that this syntax ([0]
) only works in predicate format strings. It does not work with key paths.
This syntax is defined under the "Index Expression" section of the Predicate BNF.
EDIT
Actually, this won't work, and here's why:
The string "$player.currencyDictionaries[0].earned"
will be read and turned into an NSExpression
of type NSKeyPathExpression
. This means, when it's evaluated, it's going to basically take that string and run it through the receiver's -valueForKeyPath:
method. As I mentioned above, the bracket syntax doesn't work with key paths, and thus this will produce the incorrect answer.
However, since you know the currencyDictionaries
returns an NSDictionary
, and since NSDictionary
overrides the -valueForKey:
method, you can turn the [0]
bit into a key path, by turning it into a literal string:
$player.currencyDictionaries.'0'.earned
Upvotes: 2