Bread Tastes Good.
Bread Tastes Good.

Reputation: 39

Type 'Int' has no subscript members?

I am coding my first application in Swift. Right off the bat I am sorry if this is a duplicate as I have looked at others errors and wasn't really sure how to insert the help they got into my code.

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return textView.text.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        let myText = Int(entertextTextView.text!)
        cell.textLabel?.text = "\(myText?[myText.index(myText.startIndex, offsetBy: indexPath.row)])"
        return cell
    }

I get a "Type 'Int' has no subscript members" error. I am trying to add a new cell into a textview for each character in a textview.

Upvotes: 1

Views: 863

Answers (1)

Rakesha Shastri
Rakesha Shastri

Reputation: 11242

If you are adding a character to every cell, you shouldn't need to convert it to a Int. Only when it is a string will you be able to access a character using indices.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    let myText = entertextTextView.text!
    cell.textLabel?.text = "\(myText[myText.index(myText.startIndex, offsetBy: indexPath.row)])"
    return cell
}

Also, it doesn't matter if your content is a number or not, which is why i'm assuming you changed it to an Int. Unless you are performing arithmetic operations or similar operations on the number, you can just let the content be a String.

Upvotes: 2

Related Questions