在iOS项目上设置Google Analytics时(在Swift中)获取"警卫体可能不会失败"错误

use*_*290 15 xcode google-analytics ios swift

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

/Users/AppDelegate.swift:18:9:'守卫'身体可能无法通过,考虑使用'return'或'break'退出范围

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

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

编辑:我也尝试在断言后放置一个休息或继续,但我得到一个错误...它不是一个循环.

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
    }
Run Code Online (Sandbox Code Playgroud)

Fan*_*ing 32

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
}
Run Code Online (Sandbox Code Playgroud)

这是文件:

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

返回休息继续投掷

  • @AbhishekMitra您只需要退出当前范围.返回类型基于函数返回类型.如果你的函数没有返回任何东西,那么只需要`return` (2认同)