Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

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.

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
    }
like image 375
user2411290 Avatar asked Jun 27 '17 18:06

user2411290


1 Answers

guard let function needs to exit the current scope of your gai variable. So you need to modify your code to

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

like image 136
Fangming Avatar answered Sep 29 '22 23:09

Fangming