问题
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 ifnil
is specified as the thrid value thenUIApplication
is assumed, but why then does my error message include my custom class name? I have to assumenil
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