国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

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

2019-10-21 18:47:58
字體:
供稿:網(wǎng)友

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

我們先來看一下后臺返回的部分json數(shù)據(jù),稍后再來分析問題,仔細看一下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,是因為簡單方便,而且可以滿足開發(fā),所以就沒有采用其他的緩存方式.

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

遇到問題

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

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

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

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

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

附上當時代碼

- (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];}

分析問題

實際開發(fā)當中,總是有細節(jié)的東西,雖然有時候覺得,這些東西太基礎(chǔ),但是就在這些基礎(chǔ)的知識上,我們卻忽略了一些本應(yīng)該注意的點.好比說我們明明知道向數(shù)組中添加元素的時候,元素不能為空,記得考慮為nil,null的情況.這誰都知道,但是卻最容易被忽略,因為你無法確定后臺的數(shù)據(jù)返回什么,包括那些規(guī)范文檔明確要求不能為nil的字段,都有可能返回一個nil or Null .這個時候開始想靜靜了.明白這個世界其實沒有必然的東西.另外,數(shù)組越界問題也一直都在,當然為了防止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這些類型,當然自定義類型不可以.

解決問題

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

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

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

 

注:相關(guān)教程知識閱讀請移步到IOS開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 湛江市| 灵武市| 越西县| 师宗县| 房山区| 永康市| 特克斯县| 青浦区| 汝南县| 东乌珠穆沁旗| 城市| 库伦旗| 西畴县| 如东县| 凭祥市| 青铜峡市| 青冈县| 定远县| 桦南县| 平乡县| 五原县| 呼图壁县| 曲麻莱县| 夹江县| 淮阳县| 河间市| 湖南省| 清徐县| 资兴市| 镇远县| 肇源县| 垣曲县| 安庆市| 栾川县| 自治县| 南木林县| 新田县| 定日县| 富阳市| 庆阳市| 类乌齐县|