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?

    Swift is an advanced language for iOS development, known for its concise and expressive syntax. It enables fast-running software and is used for developing iOS and OS X applications, making it ideal f..

    Swift Interview Questions

    What is Dictionary in Swift?

    In Swift, key-value pairs allow for efficient data storage and retrieval, functioning similarly to hash tables in other programming languages. Accessing values is straightforward by using the correspo..

    Swift Interview Questions

    Explain Enum in Swift?

    In Swift, enums define a common type for a group of related values, enabling type-safe handling within your code. Essentially, an enum groups various related values under the same umbrella, facilitati..