xcode 在 iOS 项目上设置 Google Analytics 时出现“guard body may not fall through”错误(在 Swift 中)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44786972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:05:34  来源:igfitidea点击:

Getting "guard body may not fall through" error when setting up Google Analytics on iOS Project (in Swift)

iosswiftxcodegoogle-analytics

提问by user2411290

I am getting the following error when trying to archive my build on XCode:

尝试在 XCode 上存档我的构建时出现以下错误:

/Users/AppDelegate.swift:18:9: 'guard' body may not fall through, consider using 'return' or 'break' to exit the scope

/Users/AppDelegate.swift:18:9: 'guard' body 可能不会落下,考虑使用 'return' 或 'break' 退出作用域

It is a little frustrating, because it is the exact code that Google Analytics (I just copied/pasted) suggests you to put in appdelegate to set-up their analytics. Also, it only occurs when archiving my build. It does not occur when just running my code in the simulator.

这有点令人沮丧,因为它是 Google Analytics(我刚刚复制/粘贴)建议您放入 appdelegate 以设置其分析的确切代码。此外,它仅在归档我的构建时发生。仅在模拟器中运行我的代码时不会发生这种情况。

Would appreciate if anyone had some ideas.

如果有人有一些想法,将不胜感激。

EDIT: I also tried placing a break or continue after the assert, but I got an error...Something about it not being a loop.

编辑:我也尝试在断言后放置一个中断或继续,但我得到了一个错误......关于它不是一个循环的东西。

import UIKit
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        FIRApp.configure()

        //Google Analytics
        guard let gai = GAI.sharedInstance() else {
            assert(false, "Google Analytics not configured correctly")
        }
        gai.tracker(withTrackingId: "xxxxxxxxxxx")
        // Optional: automatically report uncaught exceptions.
        gai.trackUncaughtExceptions = true

        // Optional: set Logger to VERBOSE for debug information.
        // Remove before app release.
        gai.logger.logLevel = .verbose;


        return true
    }

回答by Fangming

guard letfunction needs to exit the current scope of your gaivariable. So you need to modify your code to

guard let函数需要退出gai变量的当前范围。所以你需要修改你的代码

guard let gai = GAI.sharedInstance() else {
    assert(false, "Google Analytics not configured correctly")
    return true//Base on your function return type, it may be returning something else
}

Here is the document:

这是文件

The else clause of a guard statement is required, and must either call a function marked with the noreturn attribute or transfer program control outside the guard statement's enclosing scope using one of the following statements:

return break continue throw

保护语句的 else 子句是必需的,并且必须调用标有 noreturn 属性的函数或使用以下语句之一将程序控制转移到保护语句的封闭范围之外:

返回 中断 继续 抛出