[swift-evolution] Extending init checks for property initialization

Wallacy wallacyf at gmail.com
Fri Apr 29 08:27:56 CDT 2016


Why not use the Designated Initializers and Convenience Initializers
<https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html>
 ?

    private var videoPlayer: AVPlayer
    private var videoPlayerLayer: AVPlayerLayer

    required override init(){ // <-- commonInitialization()
        videoPlayer = AVPlayer()
        videoPlayerLayer = AVPlayerLayer()
        super.init()
    }

    convenience init?(coder decoder: NSCoder) {
        self.init();
    }

Em qui, 28 de abr de 2016 às 12:36, Shannon Potter via swift-evolution <
swift-evolution at swift.org> escreveu:

> Consider a relatively-common init pattern:
>
> class SomeViewController: UIViewController {
>
>     // MARK: Properties
>
>     private var videoPlayer: AVPlayer
>     private var videoPlayerLayer: AVPlayerLayer
>
>     // MARK: - Object Lifecycle
>
>     override init(nibName: String?, bundle nibBundle: NSBundle?) {
>         super.init(nibName: nibName, bundle: nibBundle)
>
>         commonInitialization()
>     }
>
>     required init?(coder decoder: NSCoder) {
>         super.init(coder: decoder)
>
>         commonInitialization()
>     }
>
>     private func commonInitialization() {
>         videoPlayer = AVPlayer(...)
>         videoPlayerLayer = AVPlayerLayer(player: videoPlayer)
>     }
>
> }
>
> This does not work. Both properties are non-optional, and the compiler
> complains that they are not initialized in either init method. It seems
> rather common to want a single point of contact regarding object
> initialization, regardless of the path taken to initialize that object.
> Ideally, objects could all be funneled to one designated initializer, but
> this isn’t always the case.
>
> What are people’s thoughts about either a specialized function that is
> always called at the very end of each object’s lifecycle OR some sort of
> attribute for a function that hints that the compiler should follow it if
> called in an init function to check for property initialization?
>
> func commonInit() {
>
> }
>
> or
>
> @extend_init private func commonInitialization() {
>
> }
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160429/053a2ac2/attachment.html>


More information about the swift-evolution mailing list