Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

iphone - Using valueForKeyPath on NSDictionary if a key starts the @ symbol?

I want to use valueForKeyPath on my NSDictionary, but the problem is that one of the keys is a string that starts with the @ symbol. I have no control over the naming of the key.

I'm having problems trying to create the key path as I'm getting a format exception, even when trying to escape the @ symbol:

This works fine:

[[[dict objectForKey:@"key1"] objectForKey:@"@specialKey"] objectForKey:@"key3"]

However none of these work:

[dict valueForKeyPath:@"[email protected]"]
[dict valueForKeyPath:@"key1.@@specialKey.key3"]

Any ideas?

Thanks,

Mike

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you shouldn't be using @ signs with your key names if you want to use key value coding.

apple's guidelines for key names are as follows:

Keys must use ASCII encoding, begin with a lowercase letter, and may not contain whitespace.

You'll have to find a workaround to reformat the key string whereever you're getting your keys from to be KVC compliant.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...