當前位置:成語大全網 - 新華字典 - iOS開發,如何替換plist這個數組下數據

iOS開發,如何替換plist這個數組下數據

//從plist文件中讀取數據

- (void)readDataFromPlist

{

//1.先獲取文件路徑

NSString * filePath = [[NSBundle mainBundle] pathForResource:@"Book" ofType:@"plist"];

//2.根據路徑初始化字典對象

self.dic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];

//將不可邊字典變成可變字典

self.addressBook = [NSMutableDictionary dictionaryWithDictionary:_dic];

//將字典封裝成對象

//(1)從字典中遍歷出key

for (NSString * key in self.dic) {

//根據key獲取對應的數組

NSArray * groupArr =self.dic[key];

//(5) 創建可變的小數組,存儲封裝的對象

NSMutableArray * newGroupArr = [NSMutableArray array];

//(3)獲取小數組中元素(遍歷小數組)

for (NSDictionary * groupDic in groupArr) {

//(4)通過字典初始化AddressPerson

AddressPerson * Person = [[AddressPerson alloc]initWithDic:groupDic];

[newGroupArr addObject:Person];//將對象存儲到新的可變數組中

RELEASE_SAFE(Person);

}

//將要原來key對應的存儲字典的小數組替換成裝有對象的心得可變的數組

[self.addressBook setObject:newGroupArr forKey:key];

}

//取出字典中所有的key

self.orderedKeys= [[self.addressBook allKeys]sortedArrayUsingSelector:@selector(compare:)];

// NSLog(@"%@",addressBook);

}