[swift-users] Clarification about the Swift API Design Guide - Initializer and factory methods calls

Gaétan Zanella gaetan.zanella at fabernovel.com
Mon Jan 8 04:25:18 CST 2018


Swift API Design Guide <https://swift.org/documentation/api-design-guidelines> says :

>    The first argument to initializer and factory methods calls should not form a phrase starting with the base name

Specially, it advises us against writing things like :

>    let foreground = Color(havingRGBValuesRed: 32, green: 64, andBlue: 128)  
>    let newPart = factory.makeWidget(havingGearCount: 42, andSpindleCount: 14)  
>    let ref = Link(to: destination)  

But in UIKit, the Swift Standard Library or in Foundation, we find code like :

    struct Array<T> {
        // ...
        init(repeating repeatedValue: Array.Element, count: Int)
    }

    struct Data {
        // ...
        init(referencing reference: NSData)
    }

    class UIImage {
        // …
        init?(named name: String, in bundle: Bundle?, compatibleWith traitCollection: UITraitCollection?)
    }

I understand that some UIKit methods inherit their ObjC declarations like imageNamed:inBundle:compatibleWithTraitCollection: <https://developer.apple.com/documentation/uikit/uiimage/1624154-imagenamed?language=objc>.
But what about Foundation and the Swift Standard Library ?

I agree that the initializers respect the Swift fundamentals : clean, short, readable, and consistent.
But the guide seems to be clear about it : we should not write such things.

Could someone shed some light on this point for me ? The subtlety about it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20180108/5baeef13/attachment.html>


More information about the swift-users mailing list