目标C阻止快速阻止

Blu*_*eni 0 objective-c ios swift

我在使用swift调用方法的目标C中编写方法时遇到了麻烦.我已经正确设置了所有标头,并且简单的方法正在运行,但我不知道如何编写此块的目标C端.

我的快速课程

我试图打电话的功能:

func getOrders(completionHandler: (responseObject: NSString?) -> ()) {
    makeCall(completionHandler)
}
Run Code Online (Sandbox Code Playgroud)

我是如何在swift中做的(以及我想在Objective-C中做什么):

getOrders() { responseObject in
            // use responseObject and error here
            println("responseObject = \(responseObject); error = ")
            return
        }
Run Code Online (Sandbox Code Playgroud)

这是我对Objective-C块的尝试:

[billingService getOrders:completionHandler:^(NSString * responseObject) {
    NSLog(@"objective c callback: %@",responseObject);

}];
Run Code Online (Sandbox Code Playgroud)

以上我使用未声明的标识符'completionHandler'

我只是不确定如何使这项工作或在目标c中构建一个正确的块.

小智 6

如果您不确定如何在Objective C中使用Block,请查看以下链接:http: //fuckingblocksyntax.com/(http://goshdarnblocksyntax.com/ - SFW替代方案)

要恢复:作为局部变量:

returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
Run Code Online (Sandbox Code Playgroud)

作为财产:

@property (nonatomic, copy) returnType (^blockName)(parameterTypes);
Run Code Online (Sandbox Code Playgroud)

作为方法参数:

- (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;
Run Code Online (Sandbox Code Playgroud)

作为方法调用的参数:

[someObject someMethodThatTakesABlock:^returnType (parameters) {...}];
Run Code Online (Sandbox Code Playgroud)

作为typedef:

typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};
Run Code Online (Sandbox Code Playgroud)

如果您需要帮助在swift中使用块,使用以下链接:https: //thatthinginswift.com/completion-handlers/

Objective-C的

- (void)hardProcessingWithString:(NSString *)input withCompletion:(void (^)(NSString *result))block;

    [object hardProcessingWithString:@“commands” withCompletion:^(NSString *result){
        NSLog(result);
    }];
}
Run Code Online (Sandbox Code Playgroud)

迅速

func hardProcessingWithString(input: String, completion: (result: String) -> Void) {
    …
    completion(“we finished!”)
}

hardProcessingWithString(“commands”) {
    (result: String) in
    println(“got back: (result)“)
}
Run Code Online (Sandbox Code Playgroud)

所以对于你的问题,它可能是:

[billingService getOrders: ^(NSString * responseObject) {
    NSLog(@"objective c callback: %@",responseObject);

}];
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!