NSScanner使用

+ (instancetype)scannerWithString:(NSString *)string

初始化一个NSScanner对象 并且指定该对象的String对象;

- (BOOL)scanUpToString:(NSString *)string intoString:(NSString * __nullable * __nullable)result;

表示从当前的scanLocation 开始扫描到指定的string字符串的尾部,同时把从当前scanLocation位置到string字符串尾部的的字符串放到地址为result的内存中;

例如以下代码 (代码引用的是JSPatch开源库中的代码)

 

 

 1     NSString *string = @"MYChoiceAddressVC : UIViewController<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>";
 2     NSScanner *scanner = [NSScanner scannerWithString:string];
 3     NSString *className;
 4     NSString *superClassName;
 5     NSString *protocolNames;
 6     [scanner scanUpToString:@":" intoString:&className];
 7     if (!scanner.isAtEnd) {
 8         scanner.scanLocation = scanner.scanLocation + 1;
 9         [scanner scanUpToString:@"<" intoString:&superClassName];
10         if (!scanner.isAtEnd) {
11             scanner.scanLocation = scanner.scanLocation + 1;
12             [scanner scanUpToString:@">" intoString:&protocolNames];
13         }
14     }

 

代码输出

1 className:“MYChoiceAddressVC ”(因为原String中:前面带了空格 ,所以ClassName 后面带空格)
2 
3 superClassName:”UIViewController“
4 
5 protocolNames:”UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate“

 



posted @ 2016-03-18 18:10  颜妍  阅读(663)  评论(0编辑  收藏  举报