Why locations.last! have optional type

@rwenderlich
On the page 43 of the third tutorial there is following code provides.

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let newLocation = locations.last!
    print("didUpdateLocations \(newLocation)")
}

The newLocation constant which have the CLLocation type is an optional. Why so? There’s no word about such type in both method declaration nor Apple API Reference documentation.

Hi @yasroslav

That’s because method ‘last()’ of Array class (which is used to retrieve newLocation constant) returns an optional. Here is a link to documentation - Apple Developer Documentation.

Nikita

1 Like