Ryan
Ryan

Reputation: 1465

How can I explain this shortened version of code in Swift?

I know that the preferredStyle property on UIAlertController takes either UIAlertControllerStyleAlert or UIAlertControllerStyleActionSheet. When I see the example below, it seems it can be shortened by just using .Alert or .ActionSheet. I'm trying to figure out the technical term for this shortcut so I can better explain its use to others, I would prefer not to say "Use the shortened version with a dot" if there is a better way. Thank you very much.

Shortened

let actionSheetController: UIAlertController = UIAlertController(title: "title", message: "message", preferredStyle: .ActionSheet)

Regular

let actionSheetController: UIAlertController = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyleActionSheet)

Upvotes: 1

Views: 80

Answers (1)

rob mayoff
rob mayoff

Reputation: 385600

Here's a quote from The Swift Programming Language > Language Guide > Enumerations > Enumeration Syntax:

Once directionToHead is declared as a CompassPoint, you can set it to a different CompassPoint value using a shorter dot syntax:

directionToHead = .East

Honestly, calling it “shorter dot syntax” is probably going to be the clearest way to explain it to others.

But if you want to sound confusing, here's a quote from The Swift Programming Language > Language Reference > Expressions > Implicit Member Expression:

An implicit member expression is an abbreviated way to access a member of a type, such as an enumeration case or a type method, in a context where type inference can determine the implied type. It has the following form:

.member name

Upvotes: 2

Related Questions