NSString发现符号崩溃了

时间:2013-04-17 13:52:08

标签: objective-c xcode nsstring nsregularexpression

每次我都会从我的服务器获取一个字符串:

  

“& abc; 123:342:431:234&& xyz; 232:2344:433:434&& pqr; 234:453:534:3445&”

但有时我会得到

  

“& pqr; 234:453:534:3445&& abc; 123:342:431& xyz; 232:2344:433:434&”

我想丢弃这些消息(不带“&”结尾)ex:&abc;123:342:431。我试过但我不知道我在哪里犯错误。

这是我的代码:

- (BOOL)ashCheckandsemiclonCheck:(NSString *)string {
    if([string rangeOfString:@"&abc"].location != NSNotFound) {
        NSLog(@"is here");
        NSString *pattern = @"&abc[^&]+&";
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
                                                                               options:0 error:NULL];

        [regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, [string length])
                             usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                                 NSRange range = [result range];
                                 matched = [string substringWithRange:range];

                                 NSLog(@"%@", matched);
                             }];
        NSLog(@"filtered Message :%@", matched);
        int times = [[matched componentsSeparatedByString:@"&"] count] - 1;
        NSLog(@"counted times :%i", times);

        if (times == 2) {
            return TRUE;
        } else {
            return FALSE;
        }       
    } else {   
        return FALSE;
    }
}

如果我有两个&,我想调用此参数但在我的代码中我收到错误NSLog(@"is here");。正如我所说,有时我会得到一半的信息。谁能告诉我哪里出错了?

1 个答案:

答案 0 :(得分:1)

如果我正确地阅读了您的问题,您似乎只需要通过检查消息字符串开始并以“&”结束来测试消息字符串是否有效。我这样做的方式是写这样的东西:

 NSString* testMessage = @"&bfjhfbgfjhb&";
 NSString* firstCharacter = [testMessage substringToIndex:1];
 NSString* lastCharacter  = [testMessage substringFromIndex:testMessage.length - 1];

 if (([firstCharacter isEqualToString:@"&"]) &&
      [lastCharacter isEqualToString:@"&"]){

    //This is a valid message. Proceed.

 }else{

   //This is not a valid message. Discard.

 }

更新

好的,根据您的问题编辑/更新,我编辑了您的代码 - 特别是您的正则表达式模式字符串 - 来做我想要的想想的,这就是检查' & abc ...'消息的子部分以'&'开头和结尾字符:

-(BOOL) ashCheckandsemiclonCheck :(NSString *)string;
{

    if([string rangeOfString:@"&abc"].location !=NSNotFound)
    {
        NSLog(@"is here");

        NSString *pattern = @"&abc[^&\\s]+[^\\s]";

        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
                                                                           options:0 error:NULL];
        NSString* __block matched;
        [regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, [string length])
                         usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                             NSRange range = [result range];
                             matched = [string substringWithRange:range];

                             NSLog(@"Matched: %@", matched);

                         }];

        NSLog(@"filtered Message :%@", matched);
        int times =[[matched componentsSeparatedByString:@"&"]count]-1;
        NSLog(@"counted times :%i",times);

        if(times ==2){
            return YES;
        }else  
        {
            return NO;
        }       
    }
    else
    {   
        return NO;
    }
}

还改变了你的BOOL返回到Objective-C标准YES和NO而不是TRUE和FALSE。

希望能满足您的需求。