APPDelegate 中的 openURL 转换错误 NSString ->字符串 (Swift & iOS8) [英] openURL in APPDelegate conversion error NSString -> String (Swift & iOS8)

查看:33
本文介绍了APPDelegate 中的 openURL 转换错误 NSString ->字符串 (Swift & iOS8)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个集成 Facebook 的 iOS 应用程序,在使用 Swift 进行调查时遇到了一些问题(使用 ObjC 我没有问题).

I'm currently developing an iOS application that integrates Facebook and I'm having a bit of a problem while investigating this with Swift (with ObjC I have no problems).

问题是,当来自另一个应用程序(在本例中为 WebBrowser 中的 FB)时,这是在 appDelegate 中执行的方法:

The thing is, this is the method that gets executed in the appDelegate when coming from another APP (in this case FB in a WebBrowser):

func application(
    application: UIApplication,
    openURL url: NSURL,
    sourceApplication: NSString,
    annotation: AnyObject)
    -> Bool { 
       let appString : String = sourceApplication as String // Try to convert format => EXCEPTION
       let appString : String = String(sourceApplication)   // 'SSS' Suggestion: EXCEPTION
       println(sourceApplication) // Try to print the value => EXCEPTION
       return FBAppCall.handleOpenURL(url, sourceApplication:sourceApplication,
            withSession:session) // With Parse => EXCEPTION
}

在该方法中,我在使用sourceApplication"参数时遇到了真正的麻烦.我尝试使用它,但出现异常.我尝试转换它,另一个异常......甚至无法记录它的值,因为它在访问其值时崩溃.将函数签名中的参数类型更改为 String 均无效.

And inside that method I'm having real trouble with the 'sourceApplication' parameter. I try to use it, I get an exception. I try to convert it, another exception...can't even log its value because it crashes when accessing its value. Changing the parameter type in the functions signature to String neither worked.

这是我得到的错误:

EXEC_BAD_ACCESS

而且我一直在追踪直到我读到这绝对是一个有价值的提示:

And I've been able to track down until I could read this that it's definitely a valuable hint:

ObjectiveC.NSString.__conversion (ObjectiveC.NSString)() ->Swift.String

会不会是 iOS8 的 bug?你们中有人遇到过这个问题和/或知道如何解决吗?

Could it be an iOS8 bug? Any of you has had this problem and/or knows how to solve it?

推荐答案

您犯了两个错误:

  1. 来自app Delegate的函数声明是func application(application: UIApplication!, openURL url: NSURL!, sourceApplication: String!, annotation: AnyObject!) ->Bool : sourceApplication 是一个可选的 String 值而不是 NSString.

  1. The function declaration from the app Delegate is func application(application: UIApplication!, openURL url: NSURL!, sourceApplication: String!, annotation: AnyObject!) -> Bool : sourceApplication is an optional String value not NSString.

由于 sourceApplication 是可选的,它可能返回 nil 值(在您的情况下返回 nil).将 nil 类型转换为 String 是不安全的,因此它会崩溃.

Since sourceApplication is an optional it may return nil value (In your case returning nil) . Type casting nil to String is not safe , therefore it is crashing.

解决方案:

  1. 在您的情况下不需要类型转换,因为返回值是 String 类型
  2. 使用可选的表单类型转换运算符 as? 来安全地进行类型转换,即
  1. No type casting is required in your case Since returned value is String type
  2. Use optional form type cast operator as? to safely type cast i.e

if let appString = sourceApplication {println(appString as?String)}

这篇关于APPDelegate 中的 openURL 转换错误 NSString ->字符串 (Swift & iOS8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆