如何避免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中的错误无效重新声明? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

如何根据swift 3中的数组选择复选标记?

如何在swift中保存核心数据中的值

迁移到 Swift 2 的错误:从类型的抛出函数到非抛出函数类型的无效转换以及使用未声明的类型

如何避免在 PHP 7 中的每个文件上重新声明刻度

Swift:选择器新视图

如何在 Swift 3 中保存 UITableViewCell 附件复选标记的状态