How to create a constant in Swift Programming?

devquora
devquora

Posted On: Feb 22, 2018

 

You have to use the let keyword to declare a constant in Swift Programming. Here is the example in which we show you how to use declare street as a constant in lieu of a variable. But for this, you need to use let keyword.

  1. let street: String = "5th Avenue"
  2. var number: Int
  3. street = "Main Street"
  4. number = 10

Swift does not allow you to change the value of constant. If you want to update the first line then XCode will show an error for a naive reason. Comment out or remove the line in which you want to change the value to the street so that it won't create any type of error.

  1. let street: String = "5th Avenue"
  2. var number: Int
  3. // street = "Main Street"
  4. number = 10

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Swift Interview Questions

    What is IOS Swift?

    iOS swift is an intuitive and stalwart language for the iOS. It is joyful to deal with the writing stuff in Swift. It is very advanced and its syntax is also concise and expressive too...

    Swift Interview Questions

    What is Dictionary in Swift?

    It enables you to store the key-value pairs and access the value by providing the key.It is similar to that of the hash..

    Swift Interview Questions

    Explain Enum in Swift?

    Basically, it is a type that contains a group of various related values under the same umbrella...