我试图实施谷歌登录按钮,当我将以下行添加到我的AppDelegate.swift文件时,我收到此错误:
无法下标类型' [String:AnyObject]'索引类型为' String'
任何想法此代码有什么问题?顺便说一句,此代码只是从谷歌页面复制并粘贴: https://developers.google.com/identity/sign-in/ios/sign-in?configured&ver=swift
func application(application: UIApplication,
openURL url: NSURL, options options: [String: AnyObject]) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey],
annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}
由于
答案 0 :(得分:12)
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)
}
Swift 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)
}
这也让我永远。希望有所帮助!
答案 1 :(得分:8)
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])
}