Objective-C project with Swift UIApplication subclass. Error: class not loaded

情到浓时终转凉″ 提交于 2019-12-23 15:28:43

问题


I have a project written in Objective-C and I just added a UIApplication subclass using Swift: RMFApplication.swift (implementation at end of post). However, I am getting this error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to instantiate the UIApplication subclass instance. No class named RMFApplication is loaded.'

  • In my info.plist I correctly specified the name under NSPrincipalClass, and double checked it.
  • I read this answer and then looked in my own main.m which says: return UIApplicationMain(argc, argv, nil, NSStringFromClass([RMFAppDelegate class]));.
  • The docs for UIApplicationMain say that if nil is specified as the thrid value then UIApplication is assumed, but why then does my error message include my custom class name? I have to assume nil means it will read the info.plist value.

Note: Subclassing UIApplication worked well with RMFApplication.h and RMFApplication.m. The only thing that changed was the renaming of those files and the addition of a RMFApplication.swift file, which compiles with no warnings:

import UIKit

class RMFApplication: UIApplication {

    override func openURL(url: NSURL) -> Bool
    {
        println(url.description)
        return false;
    }
}

I am aware that I can just continue using Objective-C, but I would like to try and migrate to Swift unless the two really don't play well together.


回答1:


Maybe that's because it uses NSStringFromClass or something, so that in Swift RMFApplication is "mangled" and becomes something like "somePrefix"RMFApplication (or whatever different from just "RMFApplication").

You could try this:

@objc(RMFApplication) class RMFApplication: UIApplication

And then NSStringFromClass will return just "RMFApplication".



来源:https://stackoverflow.com/questions/26213279/objective-c-project-with-swift-uiapplication-subclass-error-class-not-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!