[swift-evolution] Proposal: Change Obj-C name for nested types to include enclosing types

Kevin Ballard kevin at sb.org
Wed Dec 9 17:24:18 CST 2015


When exposing some type to Obj-C, if the type is nested within another type, the Obj-C name should include the enclosing types.

For example:

class Foo: NSObject {
    @objc enum Bar: Int {
        case One, Two
    }
    @objc class Baz: NSObject {}
}

is currently exported to Swift as

SWIFT_CLASS("_TtC7unnamed3Foo")
@interface Foo : NSObject
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

typedef SWIFT_ENUM(NSInteger, Bar) {
  BarOne = 0,
  BarTwo = 1,
};

SWIFT_CLASS("_TtCC7unnamed3Foo3Baz")
@interface Baz : NSObject
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

I think it should be exported instead as

SWIFT_CLASS("_TtC7unnamed3Foo")
@interface Foo : NSObject
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

typedef SWIFT_ENUM(NSInteger, FooBar) {
  FooBarOne = 0,
  FooBarTwo = 1,
};

SWIFT_CLASS("_TtCC7unnamed3Foo3Baz")
@interface FooBaz : NSObject
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

This is because the Obj-C declarations are all at the top level (as Obj-C does not have nested types), so a type that is clearly unambiguous in Swift may become ambiguous in Obj-C. As a trivial example, the following Swift code:

class Foo: NSObject {
    @objc enum Bar: Int {
        case One, Two
    }
}

class Baz: NSObject {
    @objc enum Bar: Int {
        case Apple, Orange
    }
}

is currently exported to Obj-C as:

SWIFT_CLASS("_TtC7unnamed3Baz")
@interface Baz : NSObject
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

typedef SWIFT_ENUM(NSInteger, Bar) {
  BarApple = 0,
  BarOrange = 1,
};

SWIFT_CLASS("_TtC7unnamed3Foo")
@interface Foo : NSObject
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

typedef SWIFT_ENUM(NSInteger, Bar) {
  BarOne = 0,
  BarTwo = 1,
};

This is attempting to redefine the type Bar, which is of course problematic.

-Kevin Ballard


More information about the swift-evolution mailing list