4

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.

Community
  • 1
  • 1
Andreas
  • 2,665
  • 2
  • 29
  • 38

1 Answers1

5

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".

MANIAK_dobrii
  • 6,014
  • 3
  • 28
  • 55
  • Bullseye, thank you :) After googling "@objc" I found this which will hopefully also help someone else: https://developer.apple.com/LIBRARY/PRERELEASE/IOS/documentation/Swift/Conceptual/BuildingCocoaApps/Migration.html – Andreas Oct 06 '14 at 10:01