0

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.

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?

Bobby S
  • 27
  • 5

2 Answers2

1

It is difficult to know what's wrong without seeing the declaration of cellData, however as far as I know the most obvious reason would be, that the constructor of cellData only takes 3 argument instead of 7.

In this answer they also mention, that this error could be thrown if you don't pass along the correct type for each argument.

We could help more if given the definition of cellData

J.Paravicini
  • 882
  • 12
  • 39
  • I have managed to resolve my issue by using @jrturton's support, I was adding each of my items as separate array items, which caused the compiler to interpret these as extra function arguments. – Bobby S Apr 23 '21 at 11:30
1

when I only use [productName] in my sectionData then this compiles

[productName] is an array with one item in it.

I also add , [listingPrice], [briefDescription], [productURL], [activeUntil] to my sectionData

You're adding extra arrays, separated by commas, which the compiler is interpreting as extra function arguments.

self.tableViewData = [cellData(opened: false, title: "Item 1", sectionData: [productName, listingPrice, briefDescription, productURL, activeUntil])]

Put all of the items inside a single set of square brackets. That's how you declare an array with multiple items.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Perfect - thank you. I'm still learning Swift so this information has been very helpful to know! – Bobby S Apr 23 '21 at 11:29