ios - 为什么 xctool 在 NSRegularExpression 匹配时崩溃?

标签 ios objective-c regex xctool

由于某些奇怪的未知原因,此测试在 xctool (0.1.16) 中运行时失败,但在我通过 XCode 运行时通过。

- (void)testGetAppleIdfa
{
    NSString *sample_uuid = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

    // For regex pattern matching to verify if it's of UUID
    NSString *pattern = @"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}";
    NSRange searchRange = NSMakeRange(0, [sample_uuid length]);
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
    NSArray *matches = [regex matchesInString:sample_uuid options:0 range:searchRange];

    NSLog(@"UUID generated: %@", sample_uuid);

    XCTAssertEqual([matches count], 1, @"UUID generated doesn't match the UUID RFC");
}

奇怪的原因是因为我使用了完全相同的代码,但是将 sample_uuid 替换为 [[NSUUID UUID] UUIDString] .

这是 xctool 在控制台中输出的内容:

2014-06-19 05:50:25.677 xctest[11190:303] LaunchServices: failed to get advertiserID
Unknown File:0: *** -[NSRegularExpression enumerateMatchesInString:options:range:usingBlock:]: nil argument
(
    0   CoreFoundation                      0x00b331e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x007a68e5 objc_exception_throw + 44
    2   CoreFoundation                      0x00b32fbb +[NSException raise:format:] + 139
    3   Foundation                          0x00079086 -[NSRegularExpression(NSMatching) enumerateMatchesInString:options:range:usingBlock:] + 215
    4   Foundation                          0x00042735 -[NSRegularExpression(NSMatching) matchesInString:options:range:] + 143
    5   SnowplowTests                       0x02f09462 -[TestUtils testGetAppleIdfa] + 338
    6   CoreFoundation                      0x00b2791d __invoking___ + 29
    7   CoreFoundation                      0x00b2782a -[NSInvocation invoke] + 362
    8   XCTest                              0x20103c6c -[XCTestCase invokeTest] + 221
    9   XCTest                              0x20103d7b -[XCTestCase performTest:] + 111
    10  otest-shim-ios.dylib                0x00008cc7 XCPerformTestWithSuppressedExpectedAssertionFailures + 172
    11  otest-shim-ios.dylib                0x00008c15 XCTestCase_performTest + 31
    12  XCTest                              0x20104c48 -[XCTest run] + 82
    13  XCTest                              0x201033e8 -[XCTestSuite performTest:] + 139
    14  XCTest                              0x20104c48 -[XCTest run] + 82
    15  XCTest                              0x201033e8 -[XCTestSuite performTest:] + 139
    16  XCTest                              0x20104c48 -[XCTest run] + 82
    17  XCTest                              0x201033e8 -[XCTestSuite performTest:] + 139
    18  XCTest                              0x20104c48 -[XCTest run] + 82
    19  XCTest                              0x201066ba +[XCTestProbe runTests:] + 183
    20  libobjc.A.dylib                     0x007b8743 +[NSObject performSelector:withObject:] + 70
    21  xctest                              0x0000233e xctest + 4926
    22  xctest                              0x00002590 xctest + 5520
    23  xctest                              0x00002671 xctest + 5745
    24  xctest                              0x00002007 xctest + 4103
    25  libdyld.dylib                       0x01687701 start + 1

):

我通过反复试验发现测试在 NSArray *matches = [regex matchesInString:sample_uuid options:0 range:searchRange]; 处崩溃,当它调用 enumerateMatchesInString 时:选项:范围:使用 block :。同样,它通过一个不同的 UUID 生成器传递,该生成器提供完全相同的格式。

旁注:这不应该匹配类型 4 UUID,这就是正则表达式保持原样的原因。

编辑:当我使用 xctool -workspace MyApp.xcworkspace -scheme MyApp -sdk iphonesimulator7.1 build test 从终端运行 xctools 时,出现上述错误。但是,当我通过 XCode 运行测试时,我在 NSLog 中获得了我的 UUID 并且它通过了。

最佳答案

您应该确保 xctool 正在启动与 Xcode 相同的模拟器。

请注意,您没有在命令中指定任何模拟器

xctool -workspace MyApp.xcworkspace -scheme MyApp -sdk iphonesimulator7.1 build test

因为-sdk命令只在构建时使用,启动模拟器时忽略。

你应该添加类似的东西

-destination 'name=iPhone Retina (4-inch 64-bit),OS=7.1'

关于ios - 为什么 xctool 在 NSRegularExpression 匹配时崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24304106/

相关文章:

ios - 使用查尔斯代理看不到 iOS 模拟器流量

objective-c - iOS:将对象添加到NSMutableArray的特定位置

iphone - UIGestureRecognizers 的自定义操作(带有自定义参数)

ruby - string#gsub 链的性能影响?

python - 在 Notepad++ 中的匹配模式之前添加具有特定模式的新行

ios - 使 UISlider 高度变大?

ios - Objective-C MPMoviePlayerController iOS7 问题

python - Pandas 在与正则表达式匹配的列中用零替换负值

ios - 如何清除封装数据的概念?

android - 我可以将 iOS 应用程序 View 流式传输到 Chromecast 吗