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

查看:211
本文介绍了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).

问题是,当来自另一个APP(在本例中是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的错误吗?
你们中有谁遇到过这个问题和/或知道如何解决这个问题?

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

推荐答案

你犯了两个错误:


  1. 来自app Delegate的函数声明是 func应用程序(应用程序: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 value(在您的情况下返回 nil )。键入 nil 字符串是不安全的,因此它会崩溃。

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. 在您的情况下不需要类型转换因为返回的值是字符串类型

  2. 使用可选的表单类型强制转换操作符作为?来安全地键入强制转换即

  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

如果让appString = sourceApplication {
println(appString as?String)
}

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

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