每次我都会从我的服务器上得到一个字符串:
"&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&"
我想丢弃这些消息(不以“&”结尾)例如:&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");
。正如我所说,有时我会收到一半的消息。谁能告诉我哪里出错了?