我尝试使用ZipZap方法
ZZArchiveEntry(fileName: String!, compress: Bool, dataBlock: ((NSErrorPointer) -> NSData!)!)
在Swift中,但无法找出dataBlock闭包的正确语法。 我尝试了以下代码:
let fileEntry = ZZArchiveEntry(fileName: "test.txt", compress: true, dataBlock: {
(error: NSErrorPointer) in
return "test".dataUsingEncoding(NSUTF8StringEncoding)!
})
导致以下错误:
Cannot find an initializer for type 'ZZArchiveEntry' that accepts an argument list of type '(fileName: String, compress: Bool, dataBlock: (NSErrorPointer) -> _)'
这样的闭包是否可行,如果是,那么在Xcode 7.0 beta 3中如何?
答案 0 :(得分:0)
您可以使用Swift的尾随闭包语法,如下所示:
let fileEntry = ZZArchiveEntry(fileName: "test.txt", compress: true) { (error) -> NSData! in
return ("test" as NSString).dataUsingEncoding(NSUTF8StringEncoding)!
}
或更简单:
let fileEntry = ZZArchiveEntry(fileName: "test.txt", compress: true) { error in
("test" as NSString).dataUsingEncoding(NSUTF8StringEncoding)!
}
您还需要明确地将"test"
投射到NSString
以使用dataUsingEncoding(_:)
方法。
答案 1 :(得分:0)
答案 2 :(得分:0)
你正在返回一个未包装的可选Sub test()
Dim i As Integer
Dim j As Integer
i = ActiveSheet.Range("A1").End(xlDown).Row - 1
For j = 1 To 50
On Error GoTo Err_Execute
Sheets("A").Rows(j).Copy
Sheets("B").Rows(i).Insert Shift:=xlDown
i = i + 1
Next j
Err_Execute:
If Err.Number = 0 Then MsgBox "All have been copied!" Else _
MsgBox Err.Description
End Sub
(即你正在返回一个非可选的)并且编译器只是感到困惑。
您可以通过在变量中暂存值来解决它:
NSData
或者,因为该闭包的预期返回类型是可选的,如果删除let fileEntry = ZZArchiveEntry(fileName: "test.txt", compress: true, dataBlock: {
(error: NSErrorPointer) in
let data = "test".dataUsingEncoding(NSUTF8StringEncoding)!
return data
})
,则错误消失:
!
或者,正如其他人所指出的那样,您可以进一步简化:
let fileEntry = ZZArchiveEntry(fileName: "test.txt", compress: true, dataBlock: {
(error: NSErrorPointer) in
return "test".dataUsingEncoding(NSUTF8StringEncoding)
})