(WP / S)Swift中的ZipArchive进度/完成处理程序类型

时间:2016-09-29 14:20:34

标签: swift ios9 handler

我试图使用ZIP库WPZipArchive,它是ZipArchive aka SZipArchive的分支,但我对unzipFileAtPath:toDestination:overwrite:password:progressHandler:completionHandler(phew)函数有疑问。 WPZipArchive.h#L35-L40

我在Swift编程,我在为进度和完成处理程序编写处理程序时遇到问题

e.g。如何创建((String!, unz_file_info, Int, Int)->Void)!处理程序?

一些尝试:

    WPZipArchive.unzipFileAtPath(help, toDestination: temp, progressHandler: (entry:String!, info:unz_file_info, current:Int, total:Int) {
    }){ (path:String!, succeeded:Bool, error:NSError!) in
    }

有错误

  

... / ViewController.swift:45:142:无法转换类型'() - >的值   ()'到期望的参数类型'(entry:String!,info:unz_file_info,   current:Int,total:Int)' (又名'(条目:   ImplicitlyUnwrappedOptional,info:unz_file_info_s,current:   Int,总数:Int)')

这种修改似乎有效:)

    WPZipArchive.unzipFileAtPath(help, toDestination: temp, progressHandler: {(entry:String!, info:unz_file_info, current:Int, total:Int) in


    }){ (path:String!, succeeded:Bool, error:NSError!) in

    }

1 个答案:

答案 0 :(得分:0)

我在这里看不到函数名,但是有两个处理程序(闭​​包)进度处理程序和完成处理程序。我不确定哪一个叫     ((String!, unz_file_info, Int, Int)->Void)!

func foo(completionHandler: (String!, unz_file_info, Int, Int)->Void){
    //do whatever you need to
    completionHandler("this is a string", <object of type unz_file_info>, 1, 2)
}

或者,如果您尝试使用处理程序中的数据,您将实现它

foo(completionHandler: { 
    (w:String!, x:unz_file_info, y:Int, z:Int) in

    print(w)
    print(x)
    print(y)
    print(z)
})

您可以将其他参数放在第一位,有关闭包的更多信息,请参阅: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

https://thatthinginswift.com/completion-handlers/