Help with Apinotes

Hi,

I'm using a 3rd party C/Objective-C library that does not have any header annotations.
I was hoping to improve the API by adding an apinotes file.

For example, I was hoping that the importer could import this enum as a Swift enum:
C:

    typedef enum
    {
        AHActionA = 0,
        AHActionB,
        AHActionC
    } AHAction;

Swift:

    enum AHAction {
        case a
        case b
        case c
    }

I tried these versions which both failed

    Name: FrameworkName
    Enumerators:
    - Name: AHActionA
      SwiftName: a
    - Name: AHActionB
      SwiftName: b
    - Name: AHActionC
      SwiftName: c

--> Result: Using AHActionA in the code gives me a fixit saying "AHActionA' has been renamed to 'a'".
a is declared as: "var a: AHAction { get }".

    Name: FrameworkName
    Enumerators:
    - Name: AHActionA
      SwiftName: AHAction.a
    - Name: AHActionB
      SwiftName: AHAction.b
    - Name: AHActionC
      SwiftName: AHAction.c

--> Result: Using AHActionA in the code gives me a fixit saying "AHActionA' has been renamed to 'AHAction.a'".
Trying to use 'AHAction.a' gives me "Type 'AHAction' has no member 'a'".

Anyone know if the above is possible using apinotes?

Hi, Anders. I would have expected your last attempt to work (and you can file a bug that it doesn't), but it's still not applying all of the effects that NS_ENUM does. You should be able to get that with

Typedefs:
- Name: AHAction
  EnumKind: NSEnum

(but note that I didn't test that before typing it out here; it's possible the typedef-for-anonymous-enum might mess things up)

And before someone brings it up, yes, API notes need a proper reference doc. It's on my list as rdar://problem/38123494.

1 Like

Hi Jordan,

Thank you for your reply.

After checking clang/lib/APINotes it seems EnumKind only works with tags

Tags:
- Name: AHAction
  EnumKind: NSEnum

Still my case does not work.
If I change the enum to not be anonymous it is possible to make it work using:

Tags:
- Name: AHAction
  EnumKind: NSEnum

Enumerators:
- Name: AHActionA
  SwiftName: AHAction.a
...

I filed a bug: https://bugs.swift.org/browse/SR-7784

Thanks!

2 Likes