-2

Why do we have to put exclamation point when I create, in this case, a range?

Example:

var str = "joe satriani"
var range = str.startIndex..<str.characters.index(of: " ")!

If I don't put it I get an error.

Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
Vasilii
  • 11
  • 2

1 Answers1

0

There might not be an index of " " if there is no space in the string, therefore returning nil.

var str = "stringwithnospaces"
var range = str.startIndex..<str.characters.index(of: " ") // nil
tktsubota
  • 9,371
  • 3
  • 32
  • 40