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: or
    initWithContentsOfFile: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 as     initWithURL: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:&longestCompletion
                       caseSensitive:NO
                       matchesIntoArray:&outputArray
                       filterTypes:NULL];

// allMatches = 3
// longestCompletion = @"~/Demo/re"
// outputArray = (@"~/Demo/readme.html", "~/Demo/readme.rtf", "~/Demo/recondite.txt"

posted @ 2013-07-25 02:20  星期V  阅读(346)  评论(0编辑  收藏  举报