Array的静态通用扩展,用于迭代异步回调的帮助函数

时间:2015-11-27 08:41:07

标签: swift

我有两种帮助方法,我想转移到扩展名以增加其可重用性。这两种方法在调用parallell http请求时管理多个回调。但是,当使方法静态并将它们移动到扩展名时,我会收到此错误:

Cannot convert value of type '[Action]' to expected argument type '[_]'

代码是

extension Array
{
    private static func iterateObjectList<Type>(objectList:[Type], multiplier:Int=1, foreach:(object:Type, (newObject:Type?, error:NSError?) -> Void) -> (), finally: (objectList:[Type], errorList:[NSError]) -> Void)
    {
        var iterationsLeft = objectList.count * multiplier
        var errorList:[NSError] = []
        var returnObjectList:[Type] = []
        if (iterationsLeft == 0) {
            finally (objectList:objectList, errorList:[])
        }
        for object:Type in objectList {
            foreach (object:object, { (requestObject, requestError) -> Void in

                iterationsLeft -= 1
                if (requestError != nil) {
                    errorList.append(requestError!);
                }
                if (requestObject != nil) {
                    returnObjectList.append(requestObject!)
                }
                if (iterationsLeft <= 0) {
                    finally (objectList:returnObjectList, errorList:errorList)
                }
            })
        }
    }

    private static func simpleIterate<Type>(objectList:[Type], multiplier:Int=1, foreach:(object:Type, Void -> Void) -> (), finally: Void -> Void)
    {
        var iterationsLeft = objectList.count * multiplier

        if (iterationsLeft == 0) {
            finally ()
        }
        for object:Type in objectList {
            foreach (object:object, { Void -> Void in

                iterationsLeft -= 1
                if (iterationsLeft <= 0) {
                    finally ()
                }
            })
        }
    }
}

使用方法时出错:

Array.iterateObjectList(actions, foreach: { (action, iterationComplete) -> () in
    self.fetchStatusAndUpdateAction(action, callback: { (error) -> Void in
        iterationComplete(newObject: action, error:error)
    })
}, finally: { (objectList, errorList) -> Void in
    callback(error: errorList.first)
})

其中actions的类型为[Action],其中Action是自定义对象。

1 个答案:

答案 0 :(得分:1)

问题在于

public struct Array<Element> 

是泛型类型,在

Array.iterateObjectList(actions, foreach: { (action, iterationComplete) -> () in
    // ...
}, finally: { (objectList, errorList) -> Void in
    // ...
})

编译器无法推断Element应该是什么。你可以 将其编译为

Array<Action>.iterateObjectList(actions, foreach: { (action, iterationComplete) -> () in
    // ...
    }, finally: { (objectList, errorList) -> Void in
    // ...
})

甚至

Array<Int>.iterateObjectList(...)

数组Element与您的通用占位符Type无关, 所以任何类型都可以。

更好的解决方案将是静态方法 实例方法:

func iterateObjectList(multiplier:Int=1, foreach:(object:Element, (newObject:Element?, error:NSError?) -> Void) -> (), finally: (objectList:[Element], errorList:[NSError]) -> Void)
{
    // Your code with `Type` replaced by `Element`,
    // and `objectList` replaced by `self`.
    // ...
}

并在actions数组中调用它:

actions.iterateObjectList(foreach: { (action, iterationComplete) -> () in
    // ...
    }, finally: { (objectList, errorList) -> Void in
    // ...
})