Uploaded image for project: 'Swift'
  1. Swift
  2. SR-583

Add clang attribute to import NSUInteger in C/Objective-C APIs as Int (like Foundation)

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:

      Description

      When C and Objective-C APIs use NSUInteger as parameter types or return types, they get imported to Swift as Int instead of UInt, but only for Apple’s frameworks like Foundation or AppKit.
      This is documented behavior and is fine by me, but it would be nice if there were a way to get the same behavior for my C and Objective-C code.

      This would improve consistency in the code base. For example, if a Foundation class uses NSUInteger – like NSData does – it would be nice if my Objective-C categories or subclasses could use the same types as the framework class does: that is, NSUInteger in Objective-C and Int in Swift.

      This would probably be an attribute on each single parameter/return type of a method/function declaration.
      Maybe something similar to NS_ASSUME_NONNULL_BEGIN/END could be added that can be used to bracket parts of a header to import every NSUInteger occurrence as Int. OTOH, this is by far less common of a problem compared to nullable pointers, so maybe it’s not worth it.

      I asked about this on the Swift Users mailing list and Chris Lattner responded by writing that it would be reasonable to introduce a clang attribute for that. Here’s the link to the start of the thread:
      https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20160111/000848.html

        Attachments

          Activity

            People

            • Assignee:
              dmc Daniel Martín
              Reporter:
              marco.masser Marco Masser
            • Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated: