Nerver use NSNumber as keys in Dictionary when Serialize

Nerver use NSNumber as keys in Dictionary when Serialize


作者:ani di
版權所有,轉載請註明出處blog.csdn.net/ani_di

如果你的NSDictionary中有包含NSNumber的Key,那麼在序列化此字典時你會莫名其妙的失敗。像這樣

  1.     NSMutableDictionary* dic = [NSMutableDictionary dictionaryWithCapacity:10]; 
  2.     [dic setObject:@"test" forKey:[NSNumber numberWithLongLong:1]]; 
  3.     if ([dic writeToFile:@"/tmp/1.xml" atomically:NO] == NO) 
  4.     { 
  5.         puts("Oops."); 
  6.     }

網絡上已經有很多人在這裏栽過跟頭了。這個也不能怪蘋果,因爲在文檔中已經說得很清楚了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混爲一談,儘管它們之間的轉換非常簡單。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章