Missing argument in SwiftUI ForEach

I’ve been experimenting a bit with SwiftUI and ran into an error that it took me an embarrassingly long time to diagnose. The error message is “Missing argument for parameter #1 in call” and it appeared on a ForEach statement in a View. Here’s the cause as well as some troubleshooting steps I wish I’d followed.

The SwiftUI Code

@State var selectedItem: StuffItem? 

var stuffList: some View {
  List() {
    ForEach(stuff.all) { item in
      .... // Code here that uses stuff and stuff.all
      Text( item.title )
        .onTapGesture() {
          selectedItem = item

The change I was trying to make was to the return type of the property stuff.all. I wanted to return a more specialized type to handle some localization conditions. For example, a change like this:

Struct stuff {
  // old code
  //var all: [StuffItem] {
  //  return [StuffItem]()

  // new code
  var all: [StuffItem] {
    return [StuffItemLocalized]()

As soon as I made the above change I started getting the error.

Missing argument for parameter #1 in call

And the error was on this line:

ForEach(stuff.all) { item in

The Solution

The actual issue had nothing to do with that line, though it was caused by the change. To resolve the error I had to fix the rest of the code inside the ForEach. In the first code example the assignment to selectedItem is wrong because the type of selectedItem has to change to StuffItemLocalized. In the full version of the code there were other uses of the stuff.all that were also wrong. The problem was with those errors and had nothing to do with the return type of stuff.all. Unfortunately because the compiler didn’t show any of those errors and just showed an issue on the ForEach statement it took me a while to find the problem. I finally figured out the error message wasn’t helpful and checked each statement in the loop. For example the type of selectedItem had to be changed to StuffItemLocalized. Once all the issues inside the loop were fixed the error message went away.


What I should have done on seeing that error message was to remove the content from the body of the ForEach statement and see if the error went away. If it did then I could have put statements back one by one until the problem came back, then see what was wrong with that statement. The issue seems to be that the SwiftUI compiler can get a bit lost in trying to locate and report the error. You can’t always trust its judgement on the location and nature of an issue.

Leave a Reply