Use of typealias in Swift

Updated 9 February 2018

Save

Hello Guys , I hope you all are enjoying mobikul blogs.

In today’s blog we will write a small program about the use of typealias in swift.

Now the first thing is “What is typealias and how to use it in our programming?”. Your answer is written below.

Typealias – A type alias declaration introduces a named alias of an existing type into your program. Type alias declarations are declared using the keyword typealias and have the following form:

typealias name = existing type

eg. typealias myDataType = String

ok , lets start by understanding the following code.

Alright guys lets dive into the code and learn whats happening here.

typealias test = (Bool?) -> Void

Created a test named typealias of type (Bool?)->Void

typealias stringType = String

Created a stringType named typealias of type String

The above function will take test type parameter and will call it.

The function callvalueFalse will take two parameters of type test and stringType and will call the test after 3 seconds.

the @escaping keyword represents that our closure will escape the function means first the function will complete and on its completion the closure will get executed.

now inside our button action we called the functions.

self.valueLabel.text = “false”

the false text is assigned to label after 3 seconds because the callvalueFalse functions closure will get executed after 3 seconds.

i.e why we declared escaping there.

Thats enough guys If you have any confusion please comment below.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home