0

let's say i have override this function and within it's body i defined property without default value

let identifier: String 

why swift allows me to skip providing default value for this property?

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let identifier: String
    if indexPath.item == 1 {
        identifier = trendingCellId

    } else if indexPath.item == 2 {
        identifier = subscriptionCellId
    } else {
        identifier = cellId
    }
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)

            return cell
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Ninja
  • 309
  • 7
  • 26

3 Answers3

2

All good because you are initialising it before use. if, else if and else are initialising the property. You you remove else part then compiler will show error.

Bilal
  • 18,478
  • 8
  • 57
  • 72
1

The important thing is that you have given the variable a type, and you do initialize the value, in the next line. Hence the declaration is legal.

This construction is called "conditional initialization" and allows you to initialize the variable differently depending on the circumstances (condition). This is a very useful feature and is legal even for a constant.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Covered in my discussion here: http://www.apeth.com/swiftBook/ch03.html#_variable_declaration – matt Oct 13 '17 at 12:44
  • thank you very much! so if i won't use this property (but whatever reason u putted that string (let identifier: String)) it turns out to be legal? – Ninja Oct 13 '17 at 12:51
0

You don't need to provide a value when you declare variables in Swift You can always assign the value later on in your code. You should do this before trying to access it, as otherwise:

  • Non-Optional variable: You get a runtime error.
  • Optional variable: You get nil.
Akaino
  • 1,025
  • 6
  • 23
  • "*as otherwise it's NULL*" – no, it's uninitialised; the value is is undefined. But the compiler prevents you from accessing a variable before it has been initialised, so this isn't a real concern. – Hamish Oct 13 '17 at 14:15
  • The wording of this answer is confusing and misleading. The first sentence should state *"You don't need to provide a value when you declare variables in Swift"*. Initializing is the act of assigning the first value to the variable. So it makes no sense to state you don't need to provide a value when you initialize a variable. And you do not get `NULL` if you try to access an uninitialized variable. Instead, you get a compiler error. – rmaddy Oct 13 '17 at 15:52
  • You are absolutely right, @rmaddy. I've changed my sentence. – Akaino Oct 15 '17 at 08:49
  • Your last sentence is still wrong. You do not get `NULL` at runtime for an uninitialized variable. You actually get a compiler error if you try to reference an uninitialized variable. – rmaddy Oct 15 '17 at 15:38
  • Well yeah. In his case it's non-optional so you are right. Optionals are nil though afaik. Edited my post again. – Akaino Oct 16 '17 at 06:49
  • No, it's still wrong. Optional or not you get a compiler error if you try to access an uninitialized variable. – rmaddy Oct 19 '17 at 17:42
  • No. As for my testing goes with xCode 11 I do not get a compiler error whilst accessing an optional value which has not been initialized yet. And even Apple states that in their docs. https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309 [If you define an optional variable without providing a default value, the variable is automatically set to nil for you...] – Akaino Oct 23 '17 at 13:00