Swift should provide property-initialization syntax

Originator:kristopherdjohnson
Number:rdar://19511580 Date Originated:18-Jan-2015 12:55 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Version 6.1.1 (6A2008a)
Classification:Enhancement Reproducible:Not applicable
 
Summary:
With the Cocoa frameworks, it is common to create an object and then set several of its properties. For example:

    let action = UIMutableUserNotificationAction()
    action.identifier = "REPLY"
    action.title = NSLocalizedString("Reply", comment: "Reply")
    action.activationMode = .Foreground
    action.destructive = false
    action.authenticationRequired = true

To make such code less noisy, it would be nice if Swift provided a "property initialization" syntax, similar to the "object initializer" syntax in C#. For example, the above could be written as

    let action = UIMutableUserNotificationAction() {
        .identifier = "REPLY",
        .title = NSLocalizedString("Reply", comment: "Reply"),
        .activationMode = .Foreground,
        .destructive = false,
        .authenticationRequired = true
    }

This would lead to more concise, readable code, and reduce the temptation to create lots of convenience initializers with different combinations of parameters.

Steps to Reproduce:
N/A

Expected Results:
N/A

Actual Results:
N/A

Version:
Version 6.1.1 (6A2008a)

Notes:


Configuration:
Swift 1.0

Attachments:

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!