如何在多个视图中使用单例中的一个回调

时间:2018-09-23 04:25:07

标签: ios swift singleton

如何在多个类视图中使用function GetPaths(moves,possiblePaths) { var paths = []; var allmoves=[]; for (var x = 0 ; x < moves.length ; x++) { var path = []; var move = []; var data2 = []; data2.push([moves[x].from, moves[x].to]); for (var y = x + 1 ; y < moves.length ; y++) { if (moves[x].to == moves[y].from) { if (!(path.includes(data2[0]))) { path.push(data2[0]); move.push(moves[x]); } var data = [moves[y].from, moves[y].to]; path.push(data); move.push(moves[y]); } } if (path.length > 0) paths.push(path); if (move.length > 0) { allmoves.push(move); } } if (paths.length>1) { var newpaths = []; var newmoves =[]; var nextRow = paths[0]; var nextmove = allmoves[0]; var len = paths.length; for (var h = 1 ; h < nextRow.length; h++) { for (var j = 1 ; j < len ; j++) { var newpath = []; var newmove =[]; if (isInArray(nextRow[h], paths[j][0])) { newpath.push(nextRow[0]); newmove.push(nextmove[0]); var nextfound = false; for (var k = j + 1 ; k < paths.length ; k++) { if (isInArray(paths[j][paths[j].length - 1], paths[k][0])) { newpath.push(paths[j][0]); if (paths[k][0][0] - paths[k][0][1] != -(paths[k][1][0] - paths[k][1][1])) { newpath.push(paths[k]); newmove.push(allmoves[k]); } else { newpath.push(paths[k][0]); newmove.push(allmoves[k][0]); } nextfound = true; } } if (!nextfound) { newpath.push(paths[j]); newmove.push(allmoves[j]); } } if (newpath.length > 0) { newpaths.push(newpath); newmoves.push(newmove); } } } return newmoves; } return allmoves; }

已更新完整代码!!!

onCompleted

3 个答案:

答案 0 :(得分:2)

要点1

您应该在下一行中出现错误

     let static shared = Service()

因为应该先使用static关键字,然后再声明。

static let shared = Service()

要点2

用完成处理程序实现onDownload功能

    func doDownload(onCompleted: @escaping ()->()) {
    onCompleted()
    }

调用以下函数

    let service = Service.shared
    service.doDownload { () in
        print("Called in completion Handler")
    }

有关闭合的更多详细信息,请通过以下链接。 Closures

答案 1 :(得分:1)

删除属性onCompleted并使其成为doDownload方法的参数的示例:

class Service {
     let static shared = Service()

     func doDownload(onCompleted: (()->())?) {
          //...
          onCompleted?()
     }
}

答案 2 :(得分:1)

您在viewDidLoad中的代码看起来不错,但是您是否可以重新访问您的singleton类,并且可以在添加调用super.init的私有init方法之后尝试运行代码,看看您的代码是否有效。