在Objective-C中找不到Swift AppDelegate吗

时间:2023-01-16 12:10:22

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"

我在Objective-C AppDelegate中调用Swift AppDelegate。我创建了桥接头和头文件myprojectname -斯威夫特。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头文件:

在Objective-C中找不到Swift AppDelegate吗

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 i.e: 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.

我注意到一个奇怪的行为,当我点击第一个NatAppDelegate它进入“NRAppDelegate”。当我点击第二个“NatAppDelegate”时,它会转到swift类。

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 <-(点击这里,转到swift) sharedInstance];

1 个解决方案

#1


1  

Add the @objc attribute to class NatAppDelegate .

向类NatAppDelegate添加@objc属性。

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. This attribute tells the compiler that this piece of Swift code can be accessed from Objective-C.

Swift类或协议必须使用@objc属性进行标记,以便在Objective-C中可以访问和使用。这个属性告诉编译器可以从Objective-C访问这段Swift代码。

#1


1  

Add the @objc attribute to class NatAppDelegate .

向类NatAppDelegate添加@objc属性。

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. This attribute tells the compiler that this piece of Swift code can be accessed from Objective-C.

Swift类或协议必须使用@objc属性进行标记,以便在Objective-C中可以访问和使用。这个属性告诉编译器可以从Objective-C访问这段Swift代码。