1

Two Classes:

import UIKit struct ListSection { var rows : [ListRow]? var sectionTitle : String? } import UIKit struct ListRow { var someString: String? } 

Now when I try to append:

var row = ListRow() row.someString = "Hello" var sections = [ListSection]() sections[0].rows.append(row) // I get the following error: //Cannot invoke 'append' with an argument list of type (ListRow)' 

If I try this:

sections[0].rows?.append(row) // I get the following error: //Will never be executed 

How do I append to rows in section[0]?

3 Answers 3

1

You need to add a ListSection to the sections array first

 var sections = [ListSection]() var firstSection = ListSection(rows:[ListRow](), sectionTitle:"title") sections.append(firstSection) var row = ListRow() row.someString = "Hello" sections[0].rows!.append(row) 
Sign up to request clarification or add additional context in comments.

Comments

1

Start with fixing the sections[0] problem: There is no sections[0] at the time you attempt to access it. You need to append at least one section before accessing sections[0].

2 Comments

Thanks. What would be the syntax to create a section[0] if it doesn't exists?
For example sections.append(listSection) where listSection is an existing ListSection instance...
1

You need to at least have one ListSection in your sections array, but you also need the rows array in each ListSection to be initialized or empty instead of a nil Optional.

struct ListRow { var someString: String? } struct ListSection { var rows = [ListRow]() var sectionTitle : String? } var row = ListRow() row.someString = "Hello" var sections = [ListSection]() sections.append(ListSection()) sections[0].rows.append(row) 

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.