Expected expression after operator error

Hello, I am going through the swift tutorial and am experiencing an 'expected expression after operator' error in when I am trying to add the favorite button. I'm not quite sure how to fix it.

Below is the code and where the error is.

import SwiftUI

struct LandmarkDetail: View { @Environment(ModelData.self) var modelData var landmark: Landmark

var landmarkIndex: Int{
    modelData.landmarks.firstIndex(where: { $0.id == landmark.id}) !
}  **_expected expression after operator_**


var body: some View {
  @Bindable var modelData = modelData
   
    ScrollView {
     
        MapView(coordinate: landmark.locationCoordinates)
              .frame(height: 300)
      
        CircleImage(image: landmark.image)
            .offset(y:-130)
Answered by Claude31 in 815972022

Welcome to the forum.

You have an extra space before !

    modelData.landmarks.firstIndex(where: { $0.id == landmark.id}) !

Correct code is

    modelData.landmarks.firstIndex(where: { $0.id == landmark.id})!

As you know, ! is the unwrapping operator, it is postfix and must be immediately after the optional. The same for ? optional operator

For instance:

var myVar : Int? 
let unwrappedVar = myVar!
Accepted Answer

Welcome to the forum.

You have an extra space before !

    modelData.landmarks.firstIndex(where: { $0.id == landmark.id}) !

Correct code is

    modelData.landmarks.firstIndex(where: { $0.id == landmark.id})!

As you know, ! is the unwrapping operator, it is postfix and must be immediately after the optional. The same for ? optional operator

For instance:

var myVar : Int? 
let unwrappedVar = myVar!
Expected expression after operator error
 
 
Q