简体   繁体   English

在Objective-C中找不到Swift AppDelegate

[英]Can't find Swift AppDelegate inside Objective-C

I'm trying to call my Swift AppDelegate inside my Objective-C AppDelegate. 我试图在我的Objective-C AppDelegate中调用Swift AppDelegate。 I've created the Bridging header and the header file "myProjectName-Swift.h" 我创建了Bridging头文件和头文件“ myProjectName-Swift.h”

The error is: Unknown type name "NatDelegate" did you mean "NRAppDelegate" 错误是: 未知类型名称“ NatDelegate”是否表示“ NRAppDelegate”

My Objective-C AppDelegate "NRAppDelegate.mm: 我的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 : 我的NRAppDelegate.h:

#import "UnityAppController.h"

@interface NRAppDelegate : UnityAppController<UIApplicationDelegate>

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

@end

My NatAppDelegate.swift: 我的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: 我的桥接头和Swift头文件:

在此处输入图片说明

I also checked: 我还检查了:

  • Product Module Name : channels 产品模块名称:渠道
  • Defines Module : YES 定义模块:是
  • Embedded Content Contains Swift : YES 嵌入式内容包含Swift:是
  • Install Objective-C Compatibility Header : YES 安装Objective-C兼容性标头:是

I've read all kinds of theories and advices but no effort ie: Can't use Swift classes inside Objective-C 我已经阅读了各种理论和建议,但没有付出任何努力,即: 无法在Objective-C中使用Swift类

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. PS:我在第一个NatAppDelegate中单击时会发现一个奇怪的行为,在第二个“ NatAppDelegate”中单击时它会进入“ NRAppDelegate.h”。

NatAppDelegate <-(Click here i go to the NRAppDelegate.h) *delegate = [NatAppDelegate <-(Click here and i go to the swift) sharedInstance]; NatAppDelegate <-(单击此处,我​​进入NRAppDelegate.h) * delegate = [NatAppDelegate <-(单击此处,我​​进入快速状态) sharedInstance];

Add the @objc attribute to class NatAppDelegate . @objc属性添加到class NatAppDelegate

According to https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html 根据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. Swift类或协议必须使用@objc属性标记,才能在Objective-C中访问和使用。 This attribute tells the compiler that this piece of Swift code can be accessed from Objective-C. 该属性告诉编译器可以从Objective-C访问这段Swift代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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