I'm trying to archive my App so I can publish it but the archive fails with this error:
'guard' body must not fall through, consider using a 'return' or 'throw' to exit the scope
I've changed my code from this:
guard
let nibObjects = Bundle.main.loadNibNamed("NativeAdView", owner: nil, options: nil),
let adView = nibObjects.first as? GADNativeAdView
else {
assert(false, "Could not load nib file for adView")
}
setAdView(adView)
refreshAd()
To this:
guard false else {
guard
let nibObjects = Bundle.main.loadNibNamed("NativeAdView", owner: nil, options: nil),
let adView = nibObjects.first as? GADNativeAdView
else {
assert(false, "Could not load nib file for adView")
}
setAdView(adView)
refreshAd()
return
}
but I still get the same error!
The strange thing is that when I test my App on my device or simulator, I don't get any errors at all!
This error only happens during the archive.
CodePudding user response:
During the archive process the configuration is no more debug, that means all asserts are omitted and you have no exit command in the else case.
Use return beside the assert:
guard condition else { assertionFailure(); return }
or (not recommended)
guard condition else { fatalError() }
CodePudding user response:
If you believe the path of execution should never pass through this point, the way to express this is to change
assert(false, "Could not load nib file for adView")
To
fatalError("Could not load nib file for adView")
Or simply return if you want to behave normally and safely.
Saying assert(false) was always the wrong way to go about this; there is never a reason to assert false, as it can never be true. Assertions are for verifying that what you believe true is true, not for playing mind games with the compiler. When programming, say the thing you mean, and not some tricky other thing.
CodePudding user response:
assert is not run in production builds or doesn't have to fail, so you still need
assert(false)
return
in the body of else
PS.
For always failing asserts use assertionFailure and if this is supposed to also fail on production, use preconditionFailure
