0

I am attempting to use count on a String.

var items = String()

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return items.count // Error: 'count' is unavailable
}

This gives the error:

Error: 'count' is unavailable: there is no universally good answer, see the documentation comment for discussion

Why is count unavailable? Why is there no "universally good answer"? How can I get the number of items in the String?

pkamb
  • 33,281
  • 23
  • 160
  • 191
Sonny Sluiter
  • 139
  • 2
  • 9
  • 1
    This is an older error message. In modern Swift, you *can* use `.count` to [Get the length of a String](https://stackoverflow.com/questions/24037711/get-the-length-of-a-string) – pkamb Apr 21 '20 at 18:18

1 Answers1

0

Your class SecondViewController has an instance variable items. items is a String.

Strings do not have a property count, so the expression items.count is not valid.

From the fact that the name is a plural, and that you seem to be trying to use it to fetch content for cells, perhaps items should be an array of strings?

var items = ["string1", "string2", "string3"] //type [String]

Then in your cellForRowAtIndexPath the code should index into the array:

cell!.textLabel?.text = self.items[indexPath.row]
TylerH
  • 20,799
  • 66
  • 75
  • 101
Duncan C
  • 128,072
  • 22
  • 173
  • 272