Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Weather App with Swift (Retired) Displaying Our Weather Data Handling Failures

Nicolo Figiani
Nicolo Figiani
4,012 Points

where do things like ".Default" come from?

in this video, parameters are used the following initialisers:

UIAlertAction(title: "Ok", style: .Default, handler: nil)

UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)

I know they are defined as "UIAlertActionStyle" but in the documentation nothing more is explained.

Can someone explain more in details where the dot notation of parameters ".Default" and ".Cancel" come from? What are actually these parameters and how they work?

Thanks a lot, bye!

2 Answers

Stone Preston
Stone Preston
42,016 Points

the AlertActionStyle enum is defined here

as you can see, it has 3 values. Default, Cancel, and Destructive.

enumeration values can be accessed using

EnumerationName.Value

so you could access the Cancel value of the UIAlertActionStyle enumeration like so:

UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)

however, in the UIAlertAction() initializer, Swift infers the type of the style argument to be that of UIAlertActionStyle, so you can drop the name of the enum, and just use .Cancel

//swift knows that style is going to be of type UIAlertActionStyle, so you dont need to provide that before the .
UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)

it infers the type from the definition of the initializer here:

convenience init(title title: String,
           style style: UIAlertActionStyle,
         handler handler: ((UIAlertAction!) -> Void)!)

you can see that the type of the style parameter is indeed UIAlertActionStyle, which is why you can drop the enum name before the dot

Nicolo Figiani
Nicolo Figiani
4,012 Points

Thanks a lot for the answer, so accurate and straight to the point!