This may sound like a completely stupid question, but how can I get the size in bytes of an NSDictionary? Can I convert it to NSData, and then get the length of that?
Help!
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.
You should say more about why you care about the size of the dictionary in bytes, because the answer might be different depending.
In general, the “size” of an NSDictionary’s footprint in memory is not something you can see or care about. It abstracts its storage mechanism from the programmer, and uses some form of overhead beyond the actual data it’s storing.
You can, however, serialize the dictionary to NSData. If the contents of the dictionary are only “primitive” types like NSNumber, NSString, NSArray, NSData, NSDictionary, you can use the NSPropertyListSerialization class to turn it into a binary property list, which will be about the most compact byte representation of its contents that you can get:
If it contains other custom objects, you could use NSKeyedArchiver to archive it to an NSData, but this will be significantly larger and requires the cooperation of your custom classes.