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