无法在Objective-C中找到Swift AppDelegate

时间:2015-11-18 13:35:57

标签: ios objective-c swift

我试图在Objective-C AppDelegate中调用我的Swift AppDelegate。 我已经创建了Bridging标头和头文件" myProjectName-Swift.h"

错误是: 未知类型名称" NatDelegate"你的意思是" NRAppDelegate"

我的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)

我的NRAppDelegate.h:

#import "UnityAppController.h"

@interface NRAppDelegate : UnityAppController<UIApplicationDelegate>

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

@end

我的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和Swift头文件:

enter image description here

我还查了一下:

  • 产品模块名称:渠道
  • 定义模块:是
  • 嵌入式内容包含Swift:是
  • 安装Objective-C兼容性标题:是

我已经阅读了各种理论和建议,但没有努力,即: Can't use Swift classes inside Objective-C

我们将不胜感激任何帮助:)

PS:当我点击第一个NatAppDelegate进入&#34; NRAppDelegate.h&#34;时,我注意到一种奇怪的行为。当我点击第二个&#34; NatAppDelegate&#34;它进入了快速的课程。

NatAppDelegate &lt ;-(点击这里我转到NRAppDelegate.h) * delegate = [NatAppDelegate &lt ;-(点击这里,我去swift) sharedInstance];

1 个答案:

答案 0 :(得分:1)

@objc属性添加到class NatAppDelegate

根据https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

  

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