NSScanner

+ (instancetype)scannerWithString:(NSString *)string

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

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

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

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

  NSString *string = @"MYChoiceAddressVC : UIViewController<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>";
    NSScanner *scanner = [NSScanner scannerWithString:string];
    NSString *className;
    NSString *superClassName;
    NSString *protocolNames;
    [scanner scanUpToString:@":" intoString:&className];
    if (!scanner.isAtEnd) {
        scanner.scanLocation = scanner.scanLocation + 1;
        [scanner scanUpToString:@"<" intoString:&superClassName];
        if (!scanner.isAtEnd) {
            scanner.scanLocation = scanner.scanLocation + 1;
            [scanner scanUpToString:@">" intoString:&protocolNames];
        }
    }

 代码输出 

className:“MYChoiceAddressVC ”(因为原String中:前面带了空格 ,所以ClassName 后面带空格)

superClassName:”UIViewController“

protocolNames:”UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate“
posted @ 2016-03-18 17:40  颜妍  阅读(117)  评论(0编辑  收藏  举报