How can I add key (an int) and value (an int) pair to a NSDictionary object? and how can they be retrieved as int?
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The dictionary key and value has to be a object reference (
id) so you can’t use an integer as the key. You should instead wrap your integers in aNSNumber:You can then initialize your
NSDictionaryusing one of theinitWithObjectsinitializers. I chose theinitWithObjectsAsKeys:initializer which accepts key/values in a value, key, value, key.. nil format.To access the value, you need to do the same:
EDIT: According to your comment, it seems you should be using a NSMutableDictionary, not a NSDictionary. The same thing applies to wrapping your integers, but you’ll want to use the setObject:forKey method:
Does that help?