从 Swit2.3 到 Swift3 的转换导致“以 NSException 类型的未捕获异常终止”

Posted

技术标签:

【中文标题】从 Swit2.3 到 Swift3 的转换导致“以 NSException 类型的未捕获异常终止”【英文标题】:Conversion from Swit2.3 to Swift3 cause "Terminating with uncaught exception of type NSException" 【发布时间】:2017-01-16 15:35:21 【问题描述】:

这是我想从 Swift2.3 转换为 Swift3 的代码:

AWSCloudLogic.defaultCloudLogic().invokeFunction(functionName,
                                                         withParameters: parameters, completionBlock: (result: AnyObject?, error: NSError?) -> Void in
                                                            if let result = result 
                                                                dispatch_async(dispatch_get_main_queue(), 
                                                                    print("CloudLogicViewController: Result: \(result)")
                                                                    //self.activityIndicator.stopAnimating()
                                                                    //self.resultTextView.text = prettyPrintJson(result)
                                                                )
                                                            
                                                            var errorMessage: String
                                                            if let error = error 
                                                                if let cloudUserInfo = error.userInfo as? [String: AnyObject],
                                                                    cloudMessage = cloudUserInfo["errorMessage"] as? String 
                                                                    errorMessage = "Error: \(cloudMessage)"
                                                                    print(errorMessage)
                                                                 else 
                                                                    errorMessage = "Error occurred in invoking the Lambda Function. No error message found."
                                                                    print(errorMessage)
                                                                
                                                                dispatch_async(dispatch_get_main_queue(), 
                                                                    print("Error occurred in invoking Lambda Function: \(error)")
                                                                    //self.activityIndicator.stopAnimating()
                                                                    //self.resultTextView.text = errorMessage
                                                                    let alertView = UIAlertController(title: NSLocalizedString("Error", comment: "Title bar for error alert."), message: error.localizedDescription, preferredStyle: .Alert)
                                                                    alertView.addAction(UIAlertAction(title: NSLocalizedString("Dismiss", comment: "Button on alert dialog."), style: .Default, handler: nil))
                                                                    self.presentViewController(alertView, animated: true, completion: nil)
                                                                )
                                                            
        )

Swift2.3的定义:

/**
     Invokes the specified AWS Lambda function and passes the results and possible error back to the application asynchronously.

     @param name AWS Lambda function name, e.g., hello-world
     @param parameters The object from which to generate JSON request data. Can be `nil`.
     @param completionBlock handler for results from the function
     */
    public func invokeFunction(name: String, withParameters parameters: AnyObject?, completionBlock: (AnyObject, NSError) -> Void)

这里是 Swift3 版本:

    AWSCloudLogic.defaultCloudLogic().invokeFunction(functionName, withParameters: parameters)  (result : Any?, error: Error?) in


        if let result = result
            print(result)

        
        if let error = error
            print(error)
            print("error")

        else

            print("No error but issue")
        

    

Swift3的定义:

/**
 Invokes the specified AWS Lambda function and passes the results and possible error back to the application asynchronously.

 @param name AWS Lambda function name, e.g., hello-world
 @param parameters The object from which to generate JSON request data. Can be `nil`.
 @param completionBlock handler for results from the function
 */
open func invokeFunction(_ name: String, withParameters parameters: Any?, completionBlock: @escaping (Any, Error) -> Swift.Void)

更多详情:

当我在 Swift 2.3 上运行此代码时,它运行良好,但是当我在 Swift 3 中运行它时,它到达这一行

AWSCloudLogic.defaultCloudLogic().invokeFunction(functionName, withParameters: parameters)  (result : Any?, error: Error?) in

得到当前错误:

libc++abi.dylib: terminating with uncaught exception of type NSException

没有其他描述了!

【问题讨论】:

【参考方案1】:

Swift 3 支持刚刚发布,请重新下载 MobileHub 示例应用。检查它是否使用顶部任何文件的 cmets 中显示的 v0.10 模板生成。 https://console.aws.amazon.com/mobilehub/home

【讨论】:

以上是关于从 Swit2.3 到 Swift3 的转换导致“以 NSException 类型的未捕获异常终止”的主要内容,如果未能解决你的问题,请参考以下文章

将 XLPagerTabStrip 转换为 swift3 会导致错误

导致“无法将不可变作为 inout 传递”的 Swift 3 转换 [重复]

代码转换 swift 2 --> 3 导致索引路径处的二进制运算符错误

将响应 NSArray 转换为对象数组 swift3

数据 > JSON - Swift3 - 转换和解析

[转]iOS地球坐标、火星坐标和百度坐标之间转换(Swift3.0)