Home > Enterprise >  Strange behavior of Obj-C Bridging Header and main function
Strange behavior of Obj-C Bridging Header and main function

Time:10-10

I converted my app long ago from Obj-C to Swift. During conversion, I needed a Bridging-Header file.
Now I realized that this file is still defined for one target, although the project uses now only Swift files.
Thus I thought I can simply delete Target/Build Settings/Objective-C Bridging Header.
However, the project then no longer builds.

The bridging header file contains (for historical reasons, since I used earlier the Obj-C version of the StoreKit, but no longer) only a single entry:

#import <StoreKit/StoreKit.h>  

If this entry is out commented, I get the error

Cannot find 'UIApplicationMain' in scope  

in my main file that contains essentially only

let appDelegateClassName: String?
if !ProcessInfo.processInfo.isTesting {
    // No unit test. Use the normal app delegate.
    appDelegateClassName = NSStringFromClass(AppDelegate.self)
} else {
    // Unit test. No app delegate is used.
    appDelegateClassName = nil
}
let args = UnsafeMutableRawPointer(CommandLine.unsafeArgv).bindMemory(to: UnsafeMutablePointer<Int8>.self, capacity: Int(CommandLine.argc))
UIApplicationMain(CommandLine.argc, CommandLine.unsafeArgv, nil, appDelegateClassName)  

I assume, I don't need a Bridging-Header, if I don't have any Obj-C files. So why do I get this error when I simply out comment #import <StoreKit/StoreKit.h>? And how do I get rid of all old Obj-C traces?

CodePudding user response:

UIApplicationMain is defined in the UIKit framework, so you need to add

import UIKit

to the main.swift file. With

#import <StoreKit/StoreKit.h>

in the bridging header file it happens to compile because StoreKit.h includes SKOverlay.h, which in turn includes UIKit.h (when compiled for iOS).

  • Related