23

Given a declaration of a Swift class like

@objc(NSFoo) public class Foo {
   public func bar() -> () {}
}

I would expect, from my reading of the documentation, that on the Objective-C side of things we would be able to refer to this class using the identifier NSFoo. This is not what seems to be happening for me. The generated definition in ProjectName-Swift.h is:

SWIFT_CLASS("NSFoo")
@interface Foo
- (void)bar;
- (instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

whereas what I would expect is

SWIFT_CLASS("Foo")
@interface NSFoo
...

I am using Xcode 6.0.1.

I missing something, or is this just a Xcode bug?

5
  • Did you actually tried and failed to instantiate it in Objective C as NSFoo?
    – Milos
    Oct 1, 2014 at 1:56
  • 1
    @milos Yes. The compiler balks with Use of undeclared identifier 'NSFoo'
    – Manav
    Oct 1, 2014 at 3:56
  • Just a note. I'd avoid using any of the already used class prefixes like NS, CA, UI, etc...
    – Fogmeister
    Oct 1, 2014 at 11:30
  • @Fogmeister Yup. This was just an example though.
    – Manav
    Oct 7, 2014 at 7:25
  • This bug was fixed in XC7b4, though it’s still broken for extensions to @objc(NSFoo) classes. Jul 24, 2015 at 16:31

2 Answers 2

23

Note: Things have change since this answer was first written - see updates at the end!

Yeah, this does seam to be a bug... though, controlling Obj-C runtime names of methods does work:

Say we define a pair of minimal Obj-C and Swift classes that interact with each other:

Foo.swift

import Foundation

@objc(SwiftFoo)
class Foo { // inheriting from NSObject makes no difference in this case

    @objc(postcardFromSwift)
    class func postcard() -> String {
        return "Postcard from Swift!"
    }

    @objc(getMailInSwift)
    class func getMail() {
        if let hello = NSBar.postcard() { // NSBar is an Obj-C class (see below)
            println("Printed in Swift: \(hello)")
        }
    }
}

NSBar.h

#import <Foundation/Foundation.h>

@interface NSBar : NSObject
+ (NSString *)postcard;
+ (void)getMail;
@end

NSBar.m

#import "NSBar.h"
#import "ObjS-Swift.h"

@implementation NSBar
+ (void)getMail {
    // notice that I am not referring to SwiftFoo in spite of @objc(SwiftFoo)
    printf("Printed in Objective C: %s", [[Foo postcardFromSwift] UTF8String]);
}
+ (NSString *)postcard {
    return @"Postcard from Objective C!";
}
@end

If we now call their class methods, say, from main.m:

#import <Cocoa/Cocoa.h>
#import "NSBar.h"
#import "ObjS-Swift.h"

int main(int argc, const char * argv[]) {

    // notice that I am not referring to SwiftFoo in spite of @objc(SwiftFoo)
    [Foo getMailInSwift]; 
    [NSBar getMail];

    return NSApplicationMain(argc, argv);
}

This prints the following:

// --> Printed in Swift: Postcard from Objective C!
// --> Printed in Objective C: Postcard from Swift!

But it shouldn't have! Foo should only be visible to Obj-C as SwiftFoo since that is what @objc(SwiftFoo) is promising to do. Indeed, using SwiftFoo triggers the Use of undeclared identifier compiler error instead. The fact that this did work for method names, leaves little doubt that this is a bug. I am just amazed that you seem to be the first to ask about it! Plus one for that!

And yes:

// <#ModuleName#>-Swift.h
SWIFT_CLASS("SwiftFoo")
@interface Foo
+ (NSString *)postcardFromSwift;
+ (void)getMailInSwift;

... does seam to be inverted for the class name, yet this is how that macro works – see WWDC video Swift Interoperability In Depth (c. 45 min and c. 48 min into the video). The relevant documentation is Exposing Swift Interfaces in Objective-C.

Xcode 7 beta 4

The issue is now fixed (thanks to @ScottBerrevoets for the comment).

Xcode 7.1

(thanks to @Pang for the comment)

@objc class C { } // error: only classes that inherit from NSObject can be declared @objc
2
  • In Xcode 7.1, compiler says: "Only classes that inherit from NSObject can be declared @objc".
    – Pang
    Oct 31, 2015 at 7:40
  • Is there a way to also define how a obj-c method signature should look in swift?
    – Georg
    May 9, 2017 at 9:33
5

Currently (in XCode8) this seems to have been addressed.

Defined in XYZLogger.h and XYZLogger.m

NS_ASSUME_NONNULL_BEGIN

NS_SWIFT_NAME(Logger)
@interface XYZLogger : NSObject

+ (void)verbose:(NSString *)logString;
+ (void)debug:(NSString *)logString;
+ (void)info:(NSString *)logString;
+ (void)warn:(NSString *)logString;
+ (void)error:(NSString *)logString;

@end

NS_ASSUME_NONNULL_END

Used in objc like this:

[XYZLogger debug:@"Hi from objective C"];

Used in Swift like this:

Logger.debug("Hi from swift");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.