最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IOS開發(fā)中使用writeToFile時的注意事項(xiàng)

 更新時間:2017年03月05日 09:56:04   作者:SimpleLife˚∆  
本篇文章主要介紹了開發(fā)中使用writeToFile時的注意事項(xiàng),具有很好的參考價值。下面跟著小編一起來看下吧

總會有一些坑在前面等著你

我們先來看一下后臺返回的部分json數(shù)據(jù),稍后再來分析問題,仔細(xì)看一下userId和userCode兩個字段,其他不用看

"list": [{
   "classId": 5000285,
   "className": "考勤(A)班",
   "schoolId": 50011,
   "schoolName": "星星局測中學(xué)25",
   "classLeaderUserId": 2000163,
   "parentList": [{
    "userId": 2000790,
    "userName": "zhaomin",
    "gender": "0",
    "mobile": "15071362222",
    "email": "",
    "areaCode": "440105",
    "avatarUrl": "",
    "userCode": "2000790",
    "id": 1542,
    "roleType": 2,
    "nickName": "zhaomin"
   }, {
    "userId": 2000846,
    "userName": "劉玄德",
    "gender": "1",
    "mobile": "18825113388",
    "email": "",
    "areaCode": "440105",
    "avatarUrl": "",
    "userCode": "2000846",
    "id": 1631,
    "roleType": 2,
    "nickName": "劉玄德"
   }],

問題背景

這個問題是在我集成環(huán)信IM的時候,由于需要處理用戶頭像和昵稱問題,所以會將聯(lián)系人的頭像url和用戶昵稱做一個本地緩存,緩存的方式就是采用簡單的寫入plist文件來處理.之所以使用plist,是因?yàn)楹唵畏奖?而且可以滿足開發(fā),所以就沒有采用其他的緩存方式.

問題就是出現(xiàn)在寫入plist文件上面.

遇到問題

在獲取到后臺返回的聯(lián)系人數(shù)據(jù)以后,我就將返回的list進(jìn)行篩選,只是篩選出所需的用戶姓名和頭像地址.返回字段中,userId和userCode看似一樣,其實(shí)解析出來,前者是NSNuber類型,后者是NSString類型,當(dāng)時只記得后臺直接使用Sqlite語句,將userCode=userId,根本沒有考慮到類型問題.心想,既然這樣,不如直接使用userId得了,于是將' [userNameDict setObject:dict[@"userName"] forKey:dict[@"userCode"]];'換成了'[userNameDict setObject:dict[@"userName"] forKey:dict[@"userId"]];'.問題就是出現(xiàn)在換了一個字段上.

剛開始沒有發(fā)現(xiàn)問題,因?yàn)橹耙恢笔褂胾serCode字段取值作為字典的key,所以在本地已經(jīng)有了緩存.直到有一天,重新安裝App測試時才發(fā)現(xiàn),聊天界面的頭像和昵稱都不在顯示,才最終想到當(dāng)初換了了一個字段取值.

但是,更換為userId后,打印出來的字典一模一樣,就是writeToFile寫入plist時總是失敗.后來使用isEqualToDictionary方法比較兩個字典又是不一樣的.問題實(shí)在難找,當(dāng)然解決辦法就是切換為原來的userCode,但是遇到問題一向不想通過回避的方式去解決,所以就排查原因,甚至去比較過所有的key和value值,發(fā)現(xiàn)還是一樣.最后,感覺實(shí)在找不出問題所在,于是去查看返回數(shù)據(jù),于是便發(fā)現(xiàn)了,字段userId和userCode所對應(yīng)的Value值的類型是不一樣的.這才得出一下結(jié)論

如果是可變字典,那么在使用'setObject: forKey:'方法時,如果key使用的是NSNumber類型的key,會導(dǎo)致writeToFile失敗.

至于為什么是這樣,有待進(jìn)一步研究,當(dāng)然,如果有人遇到過并找出原因,也可以回復(fù)一下,相互學(xué)習(xí),共同進(jìn)步.

附上當(dāng)時代碼

- (void)saveContactListDict:(id)list {
 NSMutableArray *contactListArray = [NSMutableArray array];
 for (NSDictionary *dict in list) {
  for (NSString *key in dict) {
   if ([dict[key] isKindOfClass:[NSArray class]]) {
    [contactListArray addObjectsFromArray:dict[key]];
   }
  }
 }
 NSMutableDictionary *userNameDict = [NSMutableDictionary dictionary];
 NSMutableDictionary *avatarurlDict = [NSMutableDictionary dictionary];
 NSMutableDictionary *avatarurlAndNameDict = [NSMutableDictionary dictionary];
 for (NSDictionary *dict in contactListArray) {
  if (dict[@"userId"] == nil) {
   return;
  }
  [userNameDict setObject:dict[@"userName"] forKey:dict[@"userId"]];
  NSString *url =dict[@"avatarUrl"];
  NSString *avatarUrl = [CPUtil getThumUrl:url size:CGSizeMake(200, 200)];
  [avatarurlDict setObject:avatarUrl forKey:dict[@"userId"]];
  if (dict[@"userName"] == nil) {
   return;
  }
  [avatarurlAndNameDict setObject:avatarUrl forKey:dict[@"userName"]];
 }
 NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
 NSString *userNameDictPath = [path stringByAppendingPathComponent:@"userNameDict.plist"];
 NSString *avatarurlDictPath = [path stringByAppendingPathComponent:@"avatarurlDict.plist"];
 NSString *avatarurlAndNameDictPath = [path stringByAppendingPathComponent:@"avatarurlAndNameDict.plist"];
 [userNameDict writeToFile:userNameDictPath atomically:YES];
 [avatarurlDict writeToFile:avatarurlDictPath atomically:YES];
 [avatarurlAndNameDict writeToFile:avatarurlAndNameDictPath atomically:YES];
}

分析問題

實(shí)際開發(fā)當(dāng)中,總是有細(xì)節(jié)的東西,雖然有時候覺得,這些東西太基礎(chǔ),但是就在這些基礎(chǔ)的知識上,我們卻忽略了一些本應(yīng)該注意的點(diǎn).好比說我們明明知道向數(shù)組中添加元素的時候,元素不能為空,記得考慮為nil,null的情況.這誰都知道,但是卻最容易被忽略,因?yàn)槟銦o法確定后臺的數(shù)據(jù)返回什么,包括那些規(guī)范文檔明確要求不能為nil的字段,都有可能返回一個nil or Null .這個時候開始想靜靜了.明白這個世界其實(shí)沒有必然的東西.另外,數(shù)組越界問題也一直都在,當(dāng)然為了防止App直接閃退,你可以選擇去覆蓋系統(tǒng)的方法......好了,言歸正傳.我們看一下蘋果官方文檔,回顧一下基礎(chǔ)的東西,文檔中關(guān)于NSDictionary和writeToFile有下面兩段內(nèi)容

NSDictionary

*A key-value pair within a dictionary is called an entry. Each entry consists of one object that represents the key and a second object that is that key's value. Within a dictionary, the keys are unique. That is, no two keys in a single dictionary are equal (as determined by isEqual(_:)). In general, a key can be any object (provided that it conforms to the NSCopying protocol—see below), but note that when using key-value coding the key must be a string (see Accessing Object Properties). Neither a key nor a value can be nil; if you need to represent a null value in a dictionary, you should use NSNull.*

這里說,字典中的key可以是遵守NSCopying協(xié)議的任何對象類型,但是 key-value coding中的key必須是一個string.

'- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;'

This method recursively validates that all the contained objects are property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.

這里描述了寫入文件的對象要求,也就是平時常用的 NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary這些類型,當(dāng)然自定義類型不可以.

解決問題

當(dāng)然最后的處理就是將NSNumber格式化為NSString,看下代碼

 NSString *dictKey = [NSString stringWithFormat:@"%@",dict[@"userId"]];
 [userNameDict setObject:dict[@"userName"] forKey:dictKey];

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

最新評論

松阳县| 常熟市| 昌都县| 民勤县| 张北县| 韶关市| 乌苏市| 龙江县| 双牌县| 文山县| 永仁县| 秀山| 舟山市| 沁水县| 沭阳县| 芜湖市| 鄱阳县| 小金县| 万宁市| 肥城市| 金山区| 奈曼旗| 泾川县| 饶平县| 怀宁县| 乐清市| 长垣县| 诸暨市| 安国市| 武穴市| 锡林郭勒盟| 英超| 建水县| 绍兴县| 河间市| 营山县| 德惠市| 永平县| 威宁| 嘉兴市| 雅安市|