Create Constant

Hi,Is it possible to create an empty state? Swift 4

Could you elaborate on what you want?

Constant, Sorry

I'm going to guess that this belongs in the Using Swift category.

What do you want to do with this empty constant?
Is it an idea from another language?
What scenario do you want to use them?
How do you want it to interact with other part of the source code?

It would also work if you write some pseudo-swift code with the feature you’re asking and explain how the program are expected to behave.

Do note though that all types in Swift can be made optional which allows the variable to have no value at all

var a: Int? = 0 // `a` is of type Optional Int
a = nil // now `a` doesn’t have any Int value

No purpose, just on assignment I had to create an empty constant

So, what is an empty constant?

Constants are if we add something inside we can't change

Sounds like you're asking for help with a school assignment you don't understand.

2 Likes

You can't have an empty (i.e. uninitialised) constant.

let constant: Any
print(constant) // error

struct Foo {
  static let constant: Any // error
}

print(Foo.constant) // error

You can declare a constant inside a local scope, for example:

func doSomething() {
  let constant: Any // ok
  print(constant) // error
}

but, you have to initialise a constant before using it.