Nerver use NSNumber as keys in Dictionary when Serialize
作者:ani di
版權所有,轉載請註明出處blog.csdn.net/ani_di
如果你的NSDictionary中有包含NSNumber的Key,那麼在序列化此字典時你會莫名其妙的失敗。像這樣
- NSMutableDictionary* dic = [NSMutableDictionary dictionaryWithCapacity:10];
- [dic setObject:@"test" forKey:[NSNumber numberWithLongLong:1]];
- if ([dic writeToFile:@"/tmp/1.xml" atomically:NO] == NO)
- {
- puts("Oops.");
- }
網絡上已經有很多人在這裏栽過跟頭了。這個也不能怪蘋果,因爲在文檔中已經說得很清楚了Property List Programming Guide
...although NSDictionary and CFDictionary objects allow their keys to be objects of any type, if the keys are not string objects, the collections are not property-list objects.
plist格式就是這麼死,只有key是string纔是合法的。大家不能把NSDictionay和Property list混爲一談,儘管它們之間的轉換非常簡單。