[swift-users] Convenience initializers in structs?

Slava Pestov spestov at apple.com
Tue Jul 18 14:46:19 CDT 2017


Hi Jens,

While I’m not familiar with the integer API in the standard library, structs and enums certainly can have convenience initializers. They must delegate to another initializer (either convenience or designated) rather than initializing the fields of the type one by one.

Slava

> On Jul 18, 2017, at 6:46 AM, Jens Persson via swift-users <swift-users at swift.org> wrote:
> 
> Start a command line project in Xcode 9 beta 3 and copy paste this single line of code into main.swift
> 
> let _ = UInt8.init(extendingOrTruncating: UInt64(123456))
> 
> Now look at Quick Help while placing the cursor on `init` and then on `extendingOrTruncating`.
> 
> Note that (and how) the documentation for the initializer differs depending on where you place the cursor.
> 
> If the cursor is on `init`, the initializer is shown to be a convenience(!) initializer even though structs (such as UInt8) cannot have convenience initializers, right?
> 
> Even the official documentation for this and several other initializer like eg:
> https://developer.apple.com/documentation/swift/int/2885075-init <https://developer.apple.com/documentation/swift/int/2885075-init>
> clearly shows convenience initializers in structs.
> 
> By the way, .init(extendingOrTruncating:) doesn't show in the list of completions for "UInt8.init" but it does for "UInt8(".
> 
> 
> Can anyone explain what's going on?
> 
> Are these known issues that will go away in time for Xcode 9 GM?
> 
> /Jens
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170718/9738e0f3/attachment.html>


More information about the swift-users mailing list