Swift传递块作为参数

时间:2015-11-16 11:04:00

标签: ios swift parse-platform closures

我有以下函数调用Parse写的我swift:

func listCatalogItemsWithBlock(completion: (result: ([ParseCatalog], NSError)) -> Void)
    {
        let parseClassName = ParseCatalog.parseClassName()
        let query = PFQuery(className: parseClassName)
        query.findObjectsInBackgroundWithBlock { (ParseCatalog catalog, NSError error) -> Void in

        }
    }

我想要的是将findObjectsInBackgroundWithBlock的结果块转发到listCatalogItemsWithBlock函数的块,以便我可以在其他地方处理结果。

我该怎么做?

更新1

我设法很容易做到这一点:

 func listCatalogItemsWithBlock(completion: PFQueryArrayResultBlock)
    {
        let parseClassName = ParseCatalog.parseClassName()
        let query = PFQuery(className: parseClassName)

        query.findObjectsInBackgroundWithBlock(completion)
    }

但在这种情况下,我传递了PFQueryArrayResultBlock类型。这是:

public typealias PFQueryArrayResultBlock = ([PFObject]?, NSError?) -> Void

如果我以原始方式保留我的功能,如何返回相同的结果:

func listCatalogItemsWithCompletion(completion:([ParseCatalog]?, NSError?) -> Void)
 /* What goes here? */
}

更新2 像这样:

func listCatalogItemsWithCompletion(completion:(fvResponse: [ParseCatalog], fvError: NSError?) -> Void)
{
    let parseClassName = ParseCatalog.parseClassName()
    let query = PFQuery(className: parseClassName)

    query.findObjectsInBackgroundWithBlock { (response:[PFObject]?, error: NSError?) -> Void in

        completion(fvResponse: response as! [ParseCatalog], fvError: error)
    }        
}

0 个答案:

没有答案