objective-c - 在 swift 端形式 objective-c 端出现错误

标签 objective-c swift cocoa

我尝试从 ZipArchive 的这个函数中获取错误对象

guard  ZipArchive.unzipFileAtPath(filePath as String,toDestination: folderPath as String,overwrite: true, password: nil,error: whatShouldGoHere) else 
{
              throw whatShouldGoHere
}

我的问题是如何从这个 unzipFileAtPath Objective-C 函数中取回 whatShouldGoHere?

我注意到在 unzipFileAtPath 函数的定义中可以访问 NSError 对象到 whatShouldGoHere 参数

NSError *err = [NSError errorWithDomain:@"ZipArchiveErrorDomain" code:-1 userInfo:userInformation];

if (error) {
    error = err;
}

最佳答案

我将使用这个库 ZipArchive向您展示如何通过调用 objective-c 函数获取错误对象。

在这个库中,它也具有上述相同的功能。

+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error;

在 swift 2.1 中,你可以使用 Handling Errors Using Do-Catch获取错误对象。

do {
    try SSZipArchive.unzipFileAtPath(filePath, toDestination: folderPath, overwrite: true, password: nil)
} catch {
    print(error)
}

关于objective-c - 在 swift 端形式 objective-c 端出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34562702/

相关文章:

objective-c - 有没有办法知道 NSUserNotificationCenter 中有多少个 NSUserNotifications?

iphone - 如何在访问其他文本字段时使其他文本字段的键盘消失

iphone - 在 objective-c 中解析文本文件

ios - Swift - 从按钮点击打开特定的标签

ios - Swift:从实用程序类调用 UIAlertController

objective-c - NSOutlineview 在单元格中包含图像

iphone - 向我自己的类(class)添加类别

ios - Core Data 不会对更改的谓词使用react

swift - 了解 Swift 2 中的惰性函数

cocoa - 捕获一个 View 并输出到另一个 View