版本記錄
版本號(hào) | 時(shí)間 |
---|---|
V1.0 | 2017.05.05 |
前言
前面我簡單的寫了寫NSString的初始化,寫了幾篇棘劣,都不難变骡,但是可以對(duì)新手有一定的小幫助离赫,對(duì)于大神級(jí)人物可以略過這幾篇,NSString本來就沒有難的塌碌,都是細(xì)枝末節(jié)渊胸,忘記了查一下就回了,沒有技術(shù)難點(diǎn)台妆,下面我們繼續(xù)~~~
1. NSString簡單細(xì)說(一)—— NSString整體架構(gòu)
2. NSString簡單細(xì)說(二)—— NSString的初始化
3. NSString簡單細(xì)說(三)—— NSString初始化
4. NSString簡單細(xì)說(四)—— 從URL初始化
向文件或者URL寫入
一翎猛、- (BOOL)writeToFile:(NSString ***)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **** _Nullable *)error;
根據(jù)給的路徑,將一個(gè)字符串寫入到這個(gè)路徑的文件中接剩,并返回YES 或者 NO來代表是否寫入成功切厘。先看代碼。我們現(xiàn)在桌面生成一個(gè)空白的測(cè)試文件test.txt懊缺。如下疫稿。
/**
*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);
}
然后我們看輸出
2017-05-05 22:11:49.106 NSString你會(huì)用嗎?[1261:33517] isSuccess---1
結(jié)果1代表成功鹃两,我們?cè)倏匆幌挛募A遗座,看看是否成功寫入。
可見是成功寫入了俊扳,這里重點(diǎn)要說一下這幾個(gè)參數(shù)途蒋。
path:路徑,就是要將字符串寫入的地址馋记。如果路徑包含波形符號(hào)(?)号坡,則在調(diào)用此方法之前懊烤,必須使用stringByExpandingTildeInPath進(jìn)行擴(kuò)展。也就是說系統(tǒng)會(huì)將""替換成主目錄宽堆。另外一個(gè)方法stringByAbbreviatingWithTildeInPath的意思就是將主目錄替換成""腌紧。用的不是很多,大家知道就好畜隶。
useAuxiliaryFile:BOOL 是否使用臨時(shí)文件寄啼,如果傳入YES,則先將文件寫入一個(gè)臨時(shí)文件中代箭,如果寫入成功在重命名到指定的路徑中,它的特點(diǎn)就是安全涕刚,但是效率低嗡综;如果傳入NO,則直接將文件寫入到指定路徑中杜漠,特點(diǎn)就是不安全但是效率高极景。
enc:編碼格式,這個(gè)在ios中是一個(gè)枚舉值驾茴,存儲(chǔ)各種編碼格式盼樟,我們一般用NSUTF8StringEncoding。
error:錯(cuò)誤狀態(tài)碼锈至。如果成功則什么都不返回晨缴,如果寫入失敗則會(huì)返回很多錯(cuò)誤信息,大家可以從信息里面看出來自己哪里出問題了峡捡。如果你對(duì)是否成功不感興趣击碗,可以直接給nil,我這里給的&error们拙。
結(jié)論:簡單不難稍途,大家自己看吧,都能看懂砚婆。
二械拍、- (BOOL)writeToURL:(NSURL *****)url atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError**** _Nullable *)error;
這個(gè)例子不是很好舉,其實(shí)就是向URL地址文件中寫入一個(gè)字符串装盯,這個(gè)例子不好舉不是因?yàn)樗y坷虑,而是因?yàn)槲覜]有可用的URL進(jìn)行寫和測(cè)試。 這里就說一下這幾個(gè)參數(shù)吧验夯。
/**
*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 = @"要寫入的URL";
NSError *error;
BOOL isSuccess = [textStr writeToURL:pathURL atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSLog(@"isSuccess---%d",isSuccess);
if (error) {
NSLog(@"%@---error",error);
}
實(shí)在不好意思猖吴,這個(gè)就沒有測(cè)試結(jié)果了,這個(gè)先就這樣吧挥转,知道怎么用就可以了海蔽。
結(jié)論:雖然簡單但是一般不用共屈。
后記
這個(gè)就先寫這么多吧,待續(xù)党窜,謝謝大家~~~拗引。