0

I have just started learning swift ios development. I am encountering this problem which I am not able to solve. Can someone please tell what I am doing wrong? The image below contains screenshot of the error.

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var dayPicker: UIPickerView!
    @IBOutlet weak var addButton: UIButton!
    @IBOutlet weak var textField: UITextField!
    let days = ["Sunday","Monday","Tuesday","Thursday","Friday","Saturday"]
    // creates an array of 7 empty arrays
    var tableData:Array = [[String]] (count: 7,repeatedValue:[])
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func addObjectToTable(sender: AnyObject) {
        if(textField.text != "")
        {
            print (days[dayPicker.selectedRowInComponent(0)])
            print (textField.text!)
            tableData[dayPicker.selectedRowInComponent(0)].append(textField.text!); // Error: Cannot subscript a value of type 'Array'
            textField.text = ""
            tableView.reloadData()
        }
    }
}

Screenshot

Thanks!

4
  • Please post your code within the answer, not a screenshot. It looks like you are trying to append a string to an array of arrays... you can append an array to the array of arrays, but you cannot append a string to an array that holds arrays. Commented Feb 8, 2016 at 4:32
  • Added the code. Can you please what do I need to change in my code? Commented Feb 8, 2016 at 4:36
  • Please specify what you are trying to do Commented Feb 8, 2016 at 4:42
  • Looks like you want to store strings in the tableData. Currently you have declared it to be an array of array of strings!. Can you double check what are you trying here? Commented Feb 8, 2016 at 4:51

2 Answers 2

2

I just remembered I ran into this problem a few times when I went back to some old projects that were a bit outdated. For me the fix was simply to change, in your example,

var tableData:Array = [[String]]

to

var tableData = [[String]]
Sign up to request clarification or add additional context in comments.

Comments

0

If you are sure that the tableview will contain only days try to typecast the objects Array of AnyObject to Array of String. then use it.

var day= self.objects![indexPath.row] as! String

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.