Swift的目标C-DoWithBlock [英] Objective C to Swift - DoWithBlock

查看:64
本文介绍了Swift的目标C-DoWithBlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图仅在目标C类函数完成时在Swift中创建一个数组.我在目标C函数中使用了DoWithBlock,但是我对IOS并不陌生,并且正在尝试确定它是否以正确的顺序运行.

I am trying to only create an array in Swift when an objective C class function is completed. I am using a DoWithBlock in the objective C function but I am am new to IOS and am trying to work out if it is actually running in the correct order.

我得到了帮助,提出了此处的解决方案,但是我不确定我对该答案所做的扩展是否正确,我认为这应该是一个新问题.

I got help to come up the solution on here but I am not sure if what I did to extend on that answer is correct and I believe it should be a new question.

在目标C类中,我使用如下代码块调用函数:

In the objective C class I am calling the function with the block like this:

- (void)scanTableDoWithBlock:(void(^)(NSArray *scanResult, NSError *error))handler {

    AWSDynamoDBObjectMapper *dynamoDBObjectMapper = [AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper];
    AWSDynamoDBScanExpression *scanExpression = [AWSDynamoDBScanExpression new];
    scanExpression.limit = @10;


    [[dynamoDBObjectMapper scan:[Mapper class]
                     expression:scanExpression]
     continueWithBlock:^id(AWSTask *task) {
         if (task.error) {
             NSLog(@"The request failed. Error: [%@]", task.error);
             if (handler != nil) {
                 handler(nil, task.error);
             }
         }
         if (task.exception) {
             NSLog(@"The request failed. Exception: [%@]", task.exception);
         }
         if (task.result) {
             AWSDynamoDBPaginatedOutput *paginatedOutput = task.result;
             NSMutableArray *scanResult = [[NSMutableArray alloc] initWithArray:paginatedOutput.items];  //// ADDED /////

             if (handler != nil) {
                 handler([scanResult copy], nil);
             }
         }

         return nil;
     }];    
}
@end

然后在swift类中,我调用目标C函数,希望创建这样的数组:

Then in the swift class I am calling the objective C function and I am hoping creating the array like this:

override func viewDidLoad() {
    super.viewDidLoad()

    let scanTable = ScanTable();

    scanTable.scanTableDoWithBlock { (scanResult, error) in

         let swiftArray = scanTable.scanResult
    }

仅当目标C函数完成时才运行代码"let swiftArray = scanTable.scanResult",或者如果它先于目标C函数运行就纯粹是碰运气.我还没有找到有关快速使用块的良好文档.

Does the code "let swiftArray = scanTable.scanResult" only run when the objective C function has completed or would it be just pure luck if it ran before the other. I haven't been able to find good documentation on using blocks in swift.

感谢您的帮助

///Edit我最近一次尝试在快速视图中调用目标C扫描函数的意愿(在扫描函数中,正在创建该数组,我可以对其进行循环.在快速方面,我将两个数组都返回直到代码运行并且////

/// Edit My latest attempt at calling the objective C scan function in swift viewwilappear (Within the scan function the array is being created and I can for loop through it fine. On on the swift side I am getting both arrays return as nill the code is running and ////

class RateSongsViewController: UIViewController {

    override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated)


        let scanTable = ScanTable();

        scanTable.scanTableDoWithBlock { (scanResult, error) in

            let swiftArray = scanTable.scanResult


            if (scanTable.scanResult == nil){

                print(" scanResult ARRAY IS NILL")

            } else {
                print(" scanResult ARRAY IS NOT NILL")
            }

            if (swiftArray == nil){

                 print(" SWIFT ARRAY IS NILL")

            } else {
                 print(" SWIFT ARRAY IS NOT NILL")
            }

        }

    }

推荐答案

  1. 不要在viewDidLoad中调用scanTable.scanTableDoWithBlock;如果在MainThread上进行大量计算,则会延迟视图转换.在viewDidAppear中调用它.
  2. let swiftArray = scanTable.scanResult将在scanTableDoWithBlock完成后调用.

  1. Don't call scanTable.scanTableDoWithBlock in viewDidLoad; if its heavy computation on MainThread it will delay the view transition. Call it in viewDidAppear.
  2. let swiftArray = scanTable.scanResult will call after scanTableDoWithBlock completion.

如果task.exception您应该致电

if (handler != nil) {
    handler(nil, nil);
}

删除return nil,因为它不会调用完成块.

Remove return nil because it will not call completion block.

这篇关于Swift的目标C-DoWithBlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆