iOS之NSString
做了不少時間的iOS開發了,現在在閱讀官方文檔,特意整理出來,沒任何技術含量,純粹是筆記形式,希望對自己對大家有些幫助。
?
首先,要理解NSString需要學習下字符編碼(ASCii,Unicode,UTF8)的一些知識;
可上維基百科搜索:http://zh.wikipedia.org/zh-cn/UTF-8
?
現在開始分享iOS NSString的一些特性:
NSString編碼:NSString的本質是基于Unicode編碼的字符組成的數組;
NSString創建:如果你是用這種方式創建的:那么temp對象是在編譯時被創建,并且在程序運行時一直存在,永遠不會被釋放;
NSString *temp = @"Contrafibularity";數據存儲:NSString不是設計用來存儲任意序列的字節的,如果想要這功能,就用NSData;
生成C String:如果要用NSString產生一個C String,建議使用UTF8String方法;
const char *cString = [@"Hello, world" UTF8String];在iOS中,CString是基于UTF-8編碼的,注意:此時你得到的cString只是一個臨時對象,當自動釋放發生后,該對象會失效;如果要繼續使用,需要重新申請內存并做拷貝;
從File和URL中讀寫NSString:
當讀或寫一個NSString時,需要明確指定字符編碼,如果無法獲取到編碼信息,官方文檔建議如下:
1.Try stringWithContentsOfFile:usedEncoding:error: orinitWithContentsOfFile:usedEncoding:error: (or the URL-based equivalents).These methods try to determine the encoding of the resource, and if successful return by reference the encoding used. 2.If (1) fails, try to read the resource by specifying UTF-8 as the encoding. 3.If (2) fails, try an appropriate legacy encoding. "Appropriate" here depends a bit on circumstances;it might be the default C string encoding, it might be ISO or Windows Latin 1, or something else,depending on where your data are coming from. 4.Finally, you can try NSAttributedString's loading methods from the Application Kit (such asinitWithURL:options:documentAttributes:error:).These methods attempt to load plain text files, and return the encoding used.They can be used on more-or-less arbitrary text documents, and are worth considering if your application has no special expertise in text.
They might not be as appropriate for Foundation-level tools or documents that are not natural-language text.
?NSScaner:是一個字符串掃描工具,非常好用,運營一個官網的Demo應該就能做一些基本使用了,
NSString *string = @"Product: Acme Potato Peeler; Cost: 0.98 73\n\Product: Chef Pierre Pasta Fork; Cost: 0.75 19\n\Product:Chef Pierre Colander; Cost: 1.27 2\n"; NSCharacterSet *semicolonSet; NSScanner *theScanner;NSString *PRODUCT = @"Product:"; NSString *COST = @"Cost:";NSString *productName; float productCost;NSInteger productSold; semicolonSet = [NSCharacterSet characterSetWithCharactersInString:@";"]; theScanner = [NSScanner scannerWithString:string];while ([theScanner isAtEnd] == NO) {if ([theScanner scanString:PRODUCT intoString:NULL] &&[theScanner scanUpToCharactersFromSet:semicolonSet intoString:&productName] &&[theScanner scanString:@";" intoString:NULL] &&[theScanner scanString:COST intoString:NULL] &&[theScanner scanFloat:&productCost] &&[theScanner scanInteger:&productSold]){NSLog(@"Sales of %@: $%1.2f", productName, productCost * productSold);} }?創建標準文件路徑:
NSString *path = @"/usr/bin/./grep"; NSString *standardizedPath = [path stringByStandardizingPath]; // standardizedPath: /usr/bin/grep 補全相對路徑:NSString *meHome = [@"~me" stringByExpandingTildeInPath]; // meHome = @"/Users/me" 獲取Document目錄:
NSString *documentsDirectory; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);if ([paths count] > 0) {documentsDirectory = [paths objectAtIndex:0]; } 路徑Component操作:
文件名匹配搜索:假設路徑“~/Demo/”下有如下文件ReadMe.txt readme.html readme.rtf recondite.txt test.txt
NSString *partialPath = @"~/Demo/r"; NSString *longestCompletion; NSArray *outputArray;unsigned allMatches = [partialPath completePathIntoString:&longestCompletioncaseSensitive:NOmatchesIntoArray:&outputArrayfilterTypes:NULL];// allMatches = 3 // longestCompletion = @"~/Demo/re" // outputArray = (@"~/Demo/readme.html", "~/Demo/readme.rtf", "~/Demo/recondite.txt"
轉載于:https://www.cnblogs.com/mrfriday/p/3213518.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的iOS之NSString的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [旧博客]Python 第一次
- 下一篇: 放弃winform的窗体吧,改用html