OC 將NSString寫入本地檔案,ocnsstring寫入

來源:互聯網
上載者:User

OC 將NSString寫入本地檔案,ocnsstring寫入

最近在公司偶爾遇到一些不經常複現的bug,為了調試,只好把關索引值記錄到本地檔案中,在遇到問題時,調出本地檔案查看一下就可以很方便的知道是不是代碼邏輯的錯誤或者問題考慮不夠周全了。

 

廢話不多說,流程在代碼裡

 1 //設定檔案路徑 2 NSString * doc = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:@"/test"]; 3  4 //建立路徑 5 NSFileManager * manager = [NSFileManager defaultManager]; 6 if (![manager fileExistsAtPath:logPath]) { 7     [manager createDirectoryAtPath:logPath withIntermediateDirectories:YES attributes:nil error:NULL]; 8 } 9 10 //擷取檔案中已存在的內容(因為直接寫入本地會覆蓋掉之前檔案的內容,所以在寫入前先擷取,做好字串拼接之後再寫入檔案)11 NSString * pure_contant = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@/test_log.txt",logPath] encoding:NSUTF8StringEncoding error:NULL];12 13 //--要添加的內容--14 NSString * current_contant = @"xxx \n";15 16 NSString * log;17 if (pure_contant){18     log = [pure_contant stringByAppendingString:current_contant];19 }else{20     log = current_contant;21 }22 23 //將內容寫入/Document/test/test_log.txt檔案中24 [log writeToFile:[NSString stringWithFormat:@"%@/test_log.txt",logPath] atomically:YES encoding:NSUTF8StringEncoding error:NULL];

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.