我正在尝试实现谷歌登录按钮,当我将以下行添加到我的AppDelegate.swift文件时,我收到此错误:
无法使用索引类型为"String"的类型'[String:AnyObject]'下标值
任何想法这个代码有什么问题?顺便说一句,这段代码只是从谷歌页面复制并粘贴:https: //developers.google.com/identity/sign-in/ios/sign-in?configurationdéver= swtift
func application(application: UIApplication, openURL url: NSURL, options options: [String: AnyObject]) -> Bool { return GIDSignIn.sharedInstance().handleURL(url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey], annotation: options[UIApplicationOpenURLOptionsAnnotationKey]) }
谢谢
handleURL正在为sourceApplication和annotation查找类型为"String"的参数,但options提供了"AnyObject".将这些字典值转换为"String"应该这样做.
试试这个:
更新:Swift 3
func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any]) -> Bool return GIDSignIn.sharedInstance().handle(url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation] as? String) }
斯威夫特2
func application(application: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool { return GIDSignIn.sharedInstance().handleURL(url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey] as? String) }
这也让我永远.希望有所帮助!
Xcode 8 Swift 3
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { return GIDSignIn.sharedInstance().handle( url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: options[UIApplicationOpenURLOptionsKey.annotation]) }