gpt4 book ai didi

ios - block 无法导入嵌套 block

转载 作者:行者123 更新时间:2023-11-29 13:03:10 25 4
gpt4 key购买 nike

我在一个方法中有这段代码:

- (NetworkOperation *)runOperationWithPath:(NSString *)path

params:(NSDictionary *)params

httpMethod:(NSString *)httpMethod

completionHandler:(DictionaryCompletionHandler)completionHandler;

{
NetworkOperation *op = (NetworkOperation *)[self operationWithPath:path

params:[params mutableCopy]

httpMethod:httpMethod

ssl:YES];


[op addCompletionHandler:^(NetworkOperation *completedOperation) {
NSData *responseData = [completedOperation responseData];

NSError *error;

NSDictionary *returnDict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];

if(error == nil && !self.timedOut) {
completionHandler(returnDict, nil);
} else {
completionHandler(nil, error);
}
} errorHandler:^(NetworkOperation *completedOperation, NSError *error) {
//Manage error
}];



[self enqueueOperation:op];



return op;

}

我正在为它创建单元测试,我发现了这种奇怪的行为。在单元测试中,我使用 OHHTTPStubs 来 stub 网络响应。单元测试的代码在这里

- (void)testThatTheRequestRunAndCallsTheCompletationBlock; {
//Here i have the setup of OHHTTPStubs to stub the responses.

__block BOOL called = NO;

[self.restClient runOperationWithPath:@"Rest/clientTest" params:nil httpMethod:@"POST" authenticationEngine:nil completionHandler:^(NSDictionary *result, NSError *error) {
called = YES;

}];

while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true) && !blockFinished)

STAssertTrue(called, @"The completation block should be called");

}

如果我运行单个单元测试一切正常,如果我运行整套单元测试应用程序崩溃调用 completationHandler 为 nil。我不明白,因为在第一种情况下(单次运行)嵌套 block 被正确复制,当我运行整个测试套件时嵌套 block 没有被容器 block 正确捕获。

我尝试删除 OHHTTPStubs 并阻止等待代码,但没有任何变化。关于如何调试它的一些想法?

最佳答案

错别字:

- (void)testThatTheRequestRunAndCallsTheCompletationBlock; {
// ^

关于ios - block 无法导入嵌套 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19299812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com