What does 'Extra arguments at positions in call' mean?

I have a string - "productRaw" which I am splitting up to show in separate sectionData cells, whilst under the same "Item 1" cellData table view. This is the code I am using to split up the String and add each separate part to a separate sectionData cell.

Code Block
let group_array = document["product"] as? [String] ?? [""]
let productName1 = group_array.first ?? "No data to display :("
let productRaw = "\(productName1)"
let componentsRaw = productRaw.components(separatedBy: ", ")
var product = [String: String]()
for component in componentsRaw {
let keyVal = component.components(separatedBy: ": ")
product[keyVal[0]] = keyVal[1]
}
if let productName = product["Product Name"], let listingPrice = product["Listing Price"], let briefDescription = product["A brief description"], let productURL = product["Product URL"], let activeUntil = product["Listing active until"] {
self.tableViewData = [cellData(opened: false, title: "Item 1", sectionData: [productName], [listingPrice], [briefDescription], [productURL], [activeUntil])]
}

Where I am declaring the individual cells I want to be shown for the first section - "Item 1", when I only use [productName] in my sectionData then this compiles without any issues.

However when I also add , [listingPrice], [briefDescription], [productURL], [activeUntil] to my sectionData I receive the following error for the [cellData(opened: syntax.

Extra arguments at positions #4, #5, #6, #7 in call

So my question is, why am I receiving the above error and what can I do to resolve it?
Answered by OOPer in 671773022

So my question is, why am I receiving the above error and what can I do to resolve it?

It depends on many things,
  • How tableViewData is declared and used

  • How cellData(opened:title:sectionData) is defined

My rough guess is something like this:
Code Block
self.tableViewData = [cellData(opened: false, title: "Item 1", sectionData: [productName, listingPrice, briefDescription, productURL, activeUntil])]


If this is not what you want, you may need to show more info.
Accepted Answer

So my question is, why am I receiving the above error and what can I do to resolve it?

It depends on many things,
  • How tableViewData is declared and used

  • How cellData(opened:title:sectionData) is defined

My rough guess is something like this:
Code Block
self.tableViewData = [cellData(opened: false, title: "Item 1", sectionData: [productName, listingPrice, briefDescription, productURL, activeUntil])]


If this is not what you want, you may need to show more info.
It means you pass 7 arguments where your func signature hase only 3.
OOPer correction should likely make it work.

However, passing an array for sectionData
sectionData: [productName, listingPrice, briefDescription, productURL, activeUntil]

Where the item nature (name, price, …) depend on the position in the array is not robust.
If you mix order, for whatever reason, you'll get messy display.

You could use a dictionary instead:
Code Block
let sectionDict = ["name": productName, "price": listingPrice, "brief": briefDescription, "url": productURL, "until": activeUntil]

Then when you use in the cell, you call
Code Block
sectionDict["brief"] ?? ""

if brief exists, you get it
otherwise you have an empty string.

But no risk to display price in place for brief.
Perfect - thank you both, I'm still learning the basics of Swift so this information has been very helpful to know! 

Perfect - thank you both, I'm still learning the basics of Swift so this information has been very helpful to know! 

Thanks for reporting. You may find other issues with your code, please include as much info as you can when you have any chance to write another question.
What does 'Extra arguments at positions in call' mean?
 
 
Q