OC writes NSString to a local file, and ocnsstring writes

Source: Internet
Author: User

OC writes NSString to a local file, and ocnsstring writes

Recently, the company occasionally encountered some bugs that were not frequently reproduced. In order to debug the bug, we had to record the key values to the local file, check the local file and check whether the code logic is incorrect or the problem is not fully considered.

 

The process is in the code.

1 // set the file path 2 NSString * doc = [[NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString: @ "/test"]; 3 4 // create Path 5 NSFileManager * manager = [NSFileManager defaultManager]; 6 if (! [Manager fileExistsAtPath: logPath]) {7 [manager createDirectoryAtPath: logPath withIntermediateDirectories: YES attributes: nil error: NULL]; 8} 9 10 // get the existing content in the file (because the content of the previous file will be overwritten when writing locally, it is obtained before writing, 11 NSString * pure_contant = [NSString stringWithContentsOfFile: [NSString stringWithFormat: @ "% @/test_log.txt", logPath] encoding: NSUTF8StringEncoding error: NULL]; 12 13 // -- content to be added -- 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 // write the content into the/Document/test/test_log.txt file 24 [log writeToFile: [NSString stringWithFormat: @ "% @/test_log.txt", logPath] atomically: YES encoding: NSUTF8StringEncoding error: NULL];

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.