NSString simple details - write to file or URL

Posted by jalbey on Tue, 28 Sep 2021 10:03:46 +0200

Version record

Version numbertime
V1.02017.05.05

preface

I simply wrote the initialization of NSString and wrote several articles. They are not difficult, but they can be of some small help to novices. For great gods, you can skip these articles. NSString is not difficult, but they are all details. I forgot to check and went back. There are no technical difficulties. Let's continue~~~
1. Brief details of NSString (I) -- overall architecture of NSString
2. NSString (II) -- initialization of NSString
3. NSString (III) - NSString initialization
4. NSString (4) -- initialization from URL

Write to file or URL

1, - (bool) writetofile: (nsstring * * *) path atomically: (bool) useauxiliary file encoding: (nsstringencoding) enc error: (nserror * * * * _nullable *) error;

According to the given path, write a string to the file of this path, and return YES or NO to represent whether the writing is successful. Look at the code first. We now generate a blank test file test.txt on the desktop. As follows.

Generate file

    /**
     *1. - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
     *
     *  @param path :If YES, the receiver is written to an auxiliary file, and then the auxiliary file is renamed to path. If NO, the receiver is written directly to path. The YES option guarantees that path, if it exists at all, won't be corrupted even if the system should crash during writing.
     *  @param enc :The encoding to use for the output. For possible values, see NSStringEncoding.
     *  @param error :If there is an error, upon return contains an NSError object that describes the problem. If you are not interested in details of errors, you may pass in NULL.
     *
     *  @return :YES if the file is written successfully, otherwise NO (if there was a problem writing to the file or with the encoding).
     */

    NSString *textStr = @"balabalabala~~~~";
    NSString *path = @"/Users/lucy/Desktop/test.txt";
    NSError *error;
    BOOL isSuccess = [textStr writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"isSuccess---%d",isSuccess);
    if (error) {
        NSLog(@"%@---error",error);
    }


Then we look at the output

2017-05-05 22:11:49.106 NSString Can you use it?[1261:33517] isSuccess---1

Result 1 means success. Let's look at the folder again to see if it is written successfully.

View results

It can be seen that it has been written successfully. Here we will focus on these parameters.

  • Path: path, which is the address to write the string to. If the path contains a waveform symbol (~), it must be extended with stringByExpandingTildeInPath before calling this method. That is, the system will replace "" with the home directory. Another method, stringbyabbrevicatingwithtildeinpath, means to replace the home directory with "". I don't use a lot. Just know.

  • Useauxiliary file: BOOL whether to use a temporary file. If YES is passed in, the file will be written to a temporary file first. If the writing is successful, it will be renamed to the specified path, which is characterized by security but low efficiency; If NO is passed in, the file will be written directly to the specified path, which is characterized by insecurity but high efficiency.

  • enc: encoding format. This is an enumeration value in ios to store various encoding formats. We generally use NSUTF8StringEncoding.

  • Error: error status code. If it succeeds, nothing will be returned. If it fails, many error messages will be returned. You can see what's wrong with you from the information. If you are not interested in success, you can directly give it to nil, which I give here & error.

Conclusion: it's not difficult to be simple. You can read it by yourself.

2, - (bool) writetourl: (nsurl *****) URL atomically: (bool) useauxiliary file encoding: (nsstringencoding) enc error: (nserror ***** _nullable *) error;

This example is not very good. In fact, it is to write a string to the URL address file. This example is not good because it is difficult, but because I have no available URL to write and test. Let's talk about these parameters here.

    /**
     *2.- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
     *
     *  @param url : The URL to which to write the receiver. Only file URLs are supported.
     *  @param useAuxiliaryFile : If YES, the receiver is written to an auxiliary file, and then the auxiliary file is renamed to url. If NO, the receiver is written directly to url. The YES option guarantees that url, if it exists at all, won't be corrupted even if the system should crash during writing.The useAuxiliaryFile parameter is ignored if url is not of a type that can be accessed atomically.
     *  @param enc :The encoding to use for the output.
     *  @param errot: error
     *
     *  @return :YES if the URL is written successfully, otherwise NO (if there was a problem writing to the URL or with the encoding).
     */

        NSString *textStr = @"balabalabala~~~~";
        NSURL *pathURL = @"To write URL";
        NSError *error;
        BOOL isSuccess = [textStr writeToURL:pathURL atomically:YES encoding:NSUTF8StringEncoding error:&error];
        NSLog(@"isSuccess---%d",isSuccess);
        if (error) {
            NSLog(@"%@---error",error);
        }

I'm really sorry. There are no test results for this. Let's do this first. Just know how to use it.

Conclusion: although simple, it is generally not used.

Postscript

Let's write so much first. To be continued, thank you ~ ~ ~.

beautiful scenery

 



Author: Legend of Swordsman
Link: https://www.jianshu.com/p/61e0524175d6
Source: Jianshu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

Topics: iOS