如何避免swift 3中的错误无效重新声明? [复制]
Posted
技术标签:
【中文标题】如何避免swift 3中的错误无效重新声明? [复制]【英文标题】:How to avoid the error invalid redeclaration in swift 3? [duplicate] 【发布时间】:2018-05-07 04:45:05 【问题描述】:在这里我已经集成了 Braintree PayPal 和 google 登录集成,然后我在应用程序委托类中实现了代码,但是两个代码具有相同的功能并显示错误 Invalid redeclaration of 'application(_:open:options:)'
任何人都可以帮助我如何避免这种情况?
这是我的代码
import UIKit
import Braintree
import GoogleSignIn
//import GoogleMaps
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,GIDSignInDelegate
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
BTAppSwitch.setReturnURLScheme("com.ewallsolutions.basic.Gometoo.payments")
IQKeyboardManager.shared.enable = true
GIDSignIn.sharedInstance().delegate = self
return true
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool
if url.scheme?.localizedCaseInsensitiveCompare("com.ewallsolutions.basic.Gometoo.payments") == .orderedSame
return BTAppSwitch.handleOpen(url, options: options)
return false
@available(ios 9.0, *)
func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
annotation: [:])
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!)
if (error == nil)
// Perform any operations on signed in user here.
let userId = user.userID // For client-side use only!
let idToken = user.authentication.idToken // Safe to send to the server
let name = user.profile.name
let email = user.profile.email
let userImageURL = user.profile.imageURL(withDimension: 200)
else
print("\(error.localizedDescription)")
func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!,
withError error: NSError!)
print("user disconnected")
// Perform any operations when the user disconnects from app here.
// ...
【问题讨论】:
见这个例如:***.com/questions/35510410/…\ 【参考方案1】:你不能把同一个方法写两次。所以,你可以这样写。
func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool
let braintreeHandle = false
if url.scheme?.localizedCaseInsensitiveCompare("com.ewallsolutions.basic.Gometoo.payments") == .orderedSame
braintreeHandle = BTAppSwitch.handleOpen(url, options: options)
let googleHandle = GIDSignIn.sharedInstance().handleURL(url,
sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
annotation: [:])
return braintreeHandle || googleHandle
【讨论】:
【参考方案2】:您收到此错误是因为您使用了两次打开 url 方法。删除一种方法并使用以下代码来避免此错误:
func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool
if url.scheme?.localizedCaseInsensitiveCompare("com.ewallsolutions.basic.Gometoo.payments") == .orderedSame
return BTAppSwitch.handleOpen(url, options: options)
else
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
annotation: [:])
【讨论】:
以上是关于如何避免swift 3中的错误无效重新声明? [复制]的主要内容,如果未能解决你的问题,请参考以下文章