[swift-evolution] [Pitch] Bridging nil to Objective-C Primitives

Jeff Kelley slaunchaman at gmail.com
Fri Feb 17 14:36:16 CST 2017


Hi Charles,

If Swift code were to send NSNotFound to a method that took a nullable
NSArrayIndex, under this example, one of two things could happen:

1.) The Swift compiler fails with a fix-it to use nil instead.
2.) The value is passed as-is and Objective-C handles it as usual.

My preference would be for 1, as then the fix-its can help update code to
use the new syntax, but my opinion can definitely be swayed.


Jeff Kelley

SlaunchaMan at gmail.com | @SlaunchaMan <https://twitter.com/SlaunchaMan> |
jeffkelley.org

On Fri, Feb 17, 2017 at 3:24 PM, Charles Srstka <cocoadev at charlessoft.com>
wrote:

> On Feb 13, 2017, at 9:33 PM, Jeff Kelley via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>
> Hi all,
>
> I don’t have a formal proposal written up yet, but in my continued quest
> to make better-annotated Objective-C code, I had an idea for bridging nil with
> primitives. Since in Objective-C we often use constant values to represent
> invalid values or nil, the most obvious being NSNotFound, could we use
> that as a shorthand for nil? Something like this for NSArray:
>
> - (NSUInteger NS_SWIFT_NIL(NSNotFound))indexOfObject:(ObjectType)anObject;
>
> This is a little verbose, so it could also work with a typedef:
>
> typedef NSUInteger NS_SWIFT_NIL(NSNotFound) NSArrayIndex;
> - (NSArrayIndex)indexOfObject:(ObjectType)anObject;
>
> This would change the existing Swift interface to return an Int? instead
> of an Int. I see this as working both ways—converting these values to nil when
> returning from Objective-C to Swift, and sending these values instead of
> nil when Swift calls into Objective-C code.
>
> Is this worth writing up a proposal for? Is another, better method already
> in someone’s mind?
>
>
> What happens if code on the Swift side sets the property’s value to the
> sentinel?
>
> Charles
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170217/5b894165/attachment.html>


More information about the swift-evolution mailing list