-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
  • 45,449
  • 17
  • 80
  • 115
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,211
  • 3
  • 30
  • 39