ios 用正则获取需要的内容出现闪退(异常捕捉 try...catch...finally使用。)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Z1591090/article/details/85047689

1.用正则表达式取出需要的字符串( @".\$([A-Za-z0-9]{13})\$.||.\#([A-Za-z0-9]{13})\#.||.\([A-Za-z0-9]{13})\.||.\%([A-Za-z0-9]{13})\%.||.\€([A-Za-z0-9]{13})\€."

有些机型发现闪退用 异常捕捉 try…catch…finally使用

for (NSString *regex in self.commandRegex)
{
if (![CMBCBLStringUtil strNilOrEmpty:regex])
{
NSRegularExpression *regexExp = [NSRegularExpression regularExpressionWithPattern:regex
options:NSRegularExpressionCaseInsensitive
error:nil];
NSArray *matches = [regexExp matchesInString:aString
options:0
range:NSMakeRange(0, aString.length)];
NSTextCheckingResult *result = [matches objectAtIndex_cmbc:0];
if (result)
{
NSRange range = [result rangeAtIndex:1];

            // 生产环境部分设备发生闪退
            @try {
                self.commandString = [aString substringWithRange:range];
            } @catch (NSException *exception) {
                self.commandString = @"";
            } @finally {
                
            }
            break;
        }
    }
}

对异常的解释


@try {
    // 可能会出现崩溃的代码
} @catch (NSException *exception) {
    // 捕获到的异常exception
 
    // 抛出异常
    @throw exception
} @finally {
    // 结果处理
}

猜你喜欢

转载自blog.csdn.net/Z1591090/article/details/85047689