首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么xctool会在NSRegularExpression匹配时崩溃?

为什么xctool会在NSRegularExpression匹配时崩溃?
EN

Stack Overflow用户
提问于 2014-06-19 10:13:05
回答 1查看 777关注 0票数 3

由于一些未知的原因,这个测试在xctool (0.1.16)中运行时会失败,但是当我通过XCode运行它时,它会通过。

代码语言:javascript
复制
- (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在控制台中发布的内容:

代码语言:javascript
复制
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:options:range:usingBlock:时,测试就会崩溃。同样,它使用不同的UUID生成器传递,该生成器提供完全相同的格式。

附带说明:这不应该与类型4的UUID匹配,这就是为什么正则表达式是原样的。

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

EN

回答 1

Stack Overflow用户

发布于 2014-06-19 10:34:02

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

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

代码语言:javascript
复制
xctool -workspace MyApp.xcworkspace -scheme MyApp -sdk iphonesimulator7.1 build test

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

你应该添加这样的东西

代码语言:javascript
复制
-destination 'name=iPhone Retina (4-inch 64-bit),OS=7.1'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24304106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档