2

I'm trying to get my SwiftUI Demo running but I get weird compiler issues:

'Color' is not convertible to 'Color?'

Unable to infer complex closure return type; add explicit type to disambiguate

struct LandmarkRow: View {
  var landmark: Landmark
    
  var body: some View {
    HStack {
      landmark.image(forSize: 50)
      Text(landmark.name)
      Spacer()
    
      if landmark.isFavorite {
        Image(systemName: "star.fill")
           .imageScale(.medium)
           .foregroundColor(.yellow) // Here 'Color' is not convertible to 'Color?'
      }
    }
  }
}

struct LandmarkList: View {
  @State var showFavoritesOnly = true
    
  var body: some View {
      NavigationView {
          List {
              Toggle(isOn: $showFavoritesOnly) {
                  Text("Favorites only")
              }
    
              ForEach(landmarkData) { landmark in // Here Unable to infer complex closure return type; add explicit type to disambiguate
                  if !self.showFavoritesOnly || landmark.isFavorite {
                      NavigationButton(destination: LandmarkDetail(landmark: landmark)) {
                          LandmarkRow(landmark: landmark)
                      }
                  }
              }
              .navigationBarTitle(Text("Landmarks"))
          }
      }
   }
}
2
  • I was also following the tutorial and ran into the same issue on that line. This appears to be a bug in the Xcode beta. Commented Jun 5, 2019 at 5:54
  • 1
    You cannot continue with your results from tutorial 2 without making some changes before you start with tutorial 3 github.com/teameh/Apple-SwiftUI-Tutorials-SampleCode/commit/… It's better to download their sample code for tutorial 3 and continue with that. Commented Jun 5, 2019 at 11:15

2 Answers 2

6

Well... This is related to the isFavorite property it's missing in the Landmark file. (It's missing 🤦‍♂️)

So I'm going to fill a bug about this bad compiler message. This is definitely a bug. The radar number FB6118410

If you want to fix it you need to do 2 things: 1

  1. Add this to the Landmark struct var isFavorite: Bool
  2. You need to edit the landmarkData.json and add this key and value "isFavorite": true otherwise it will crash.
Sign up to request clarification or add additional context in comments.

3 Comments

Spurious errors (like the Color? one really being caused by malformed JSON) in SwiftUI are driving me nuts.
I know this is kind of dumb, but make sure when you add to your .json file you putt a comma before the previous line.
I also just ran into a bug where "isFavorite" was in the JSON and the Landmark code, however, I needed to restart xcode before it realised I'd added the isFavorite attribute.
0

Currently the Swift compiler is generating error messages which can be some distance from where the actual problem is located. We are being encouraged to file radars on such issues.

In this instance, I can see you are missing the verbatim: parameter name on creating the first Text():

    var body: some View {
        HStack {
            landmark.image(forSize: 50)
            Text(verbatim: landmark.name) // don't forget the "verbatim" parameter name
            Spacer()

            if landmark.isFavorite {
                Image(systemName: "star.fill")
                    .imageScale(.medium)
                    .foregroundColor(.yellow)
            }
        }
    }

...however I was not able to reproduce the compiler error you are seeing by removing this. I would therefore recommend carefully diffing against the completed project files which you can download from the tutorial page.

Edit: looks like someone else had the same issue - you can find an answer here: https://stackoverflow.com/a/56451329/233602 (check the isFavourite property on Landmark).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.