简体   繁体   中英

Can't find Swift AppDelegate inside Objective-C

I'm trying to call my Swift AppDelegate inside my Objective-C AppDelegate. I've created the Bridging header and the header file "myProjectName-Swift.h"

The error is: Unknown type name "NatDelegate" did you mean "NRAppDelegate"

My Objective-C AppDelegate "NRAppDelegate.mm:

#import "NRAppDelegate.h"
#import <UIKit/UIKit.h>
#import "UnityAppController.h"
#import "UI/UnityView.h"
#import "UI/UnityViewControllerBase.h"
#import "VuforiaRenderDelegate.h"
#import "Constants.h"
#import <sys/utsname.h>
#import <channels-Swift.h>

// Unity native rendering callback plugin mechanism is only supported
// from version 4.5 onwards
#if UNITY_VERSION>434
// Exported methods for native rendering callback
//extern "C" void UnitySetGraphicsDevice(void* device, int deviceType, int eventType);
//extern "C" void UnityRenderEvent(int marker);
extern "C" void VuforiaRenderEvent(int marker);
#endif

@implementation NRAppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{


    NatAppDelegate *delegate = [NatAppDelegate sharedInstance]; //Error it's here!!
    bool launchBool;
    launchBool = [delegate application:application didFinishLaunchingWithOptions:launchOptions];
    launchBool = [super application:application didFinishLaunchingWithOptions:launchOptions];
    //return launchBool;*/
    return launchBool;

     }

@end

IMPL_APP_CONTROLLER_SUBCLASS(NRAppDelegate)

My NRAppDelegate.h :

#import "UnityAppController.h"

@interface NRAppDelegate : UnityAppController<UIApplicationDelegate>

@property (nonatomic, strong) UINavigationController *navigationController;
@property (nonatomic) BOOL orientationIsLocked;

@end

My NatAppDelegate.swift:

import Foundation
import UIKit

//@UIApplicationMain
class NatAppDelegate : UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    //MARK: Singleton & Constructors
    class var sharedInstance :NatAppDelegate {
        struct Singleton {
            static let instance = NatAppDelegate()
        }

        return Singleton.instance
    }


    override init() {
        super.init()
    }


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


        NRChannelsManager.sharedInstance().loadChannelsList();

               return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

    func applicationDidReceiveMemoryWarning(application: UIApplication) {

        NSLog("Application received a memory warning. Sending to NR")

    }

}

My Bridging Header and Swift header file:

在此处输入图片说明

I also checked:

  • Product Module Name : channels
  • Defines Module : YES
  • Embedded Content Contains Swift : YES
  • Install Objective-C Compatibility Header : YES

I've read all kinds of theories and advices but no effort ie: Can't use Swift classes inside Objective-C

Any help will be appreciated :)

PS: I notice a strange behavior when i click in the first NatAppDelegate it goes to the "NRAppDelegate.h" when i click i the second "NatAppDelegate" it goes to the swift class.

NatAppDelegate <-(Click here i go to the NRAppDelegate.h) *delegate = [NatAppDelegate <-(Click here and i go to the swift) sharedInstance];

Add the @objc attribute to class NatAppDelegate .

According to https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

A Swift class or protocol must be marked with the @objc attribute to be accessible and usable in Objective-C. This attribute tells the compiler that this piece of Swift code can be accessed from Objective-C.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM