0

maybe someone can help me. I made a copy of the project “add rows to TableView” from IOSCcreator. Now I change the file where an item can be added with a pickerview. At line 44 (pickOption = carName.text) it fails:

Cannot assign a value of type String to a value of type [String].

Try a lot of things but nothing works.

import UIKit

class CarDetailViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

  @IBOutlet weak var carName: UITextField!
  var pickOption = ["Opel", "Mercedes", "Hyundai", "Kia"]

    override func viewDidLoad() {
        super.viewDidLoad()

        var pickerView = UIPickerView()
        pickerView.delegate = self
        carName.inputView = pickerView
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickOption.count
    }
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return pickOption[row]
    }
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        carName.text = pickOption[row]
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "doneSegue" {
           pickOption = carName.text
        }
    }
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        self.view.endEditing(true)
    }
}

6 Answers 6

2

Since pickOption is an array and carName text is a single string, you have two options:

  • Add carName to the array, in which case you do pickOption.append(carName.text)
  • Replace old list with a single item, in which case you do pickOption = [carName.text]
Sign up to request clarification or add additional context in comments.

Comments

1

Here:

pickOption = carName.text

You are assigning a string to pickOption which is an array.

(This answer is basically what the compiler is telling you, but don't know what else to say).

Comments

1

What are you trying to do? If you are attempting to make the list have just the item that was selected, then you will want to make pickOption an array with just that string in it:

pickOption = [carName.text]

If you are trying to just save the data that was picked, then you should use a different instance variable altogether. The reason for this is that if your picker gets reloaded after the user chooses something, then the picker will only show the single value (that the user just picked) instead of all the values because you set the array that provided the picker with data to only have the selected value. Since you are hardcoding the different car options at load time, you have no way of getting the different options again and have just lost data.

1 Comment

Thanx. See my other file above.
0

It's a simple fix. If you're trying to start the array over and only have that one value (carName.text) stored inside of it, do this: pickOption = [carName.text] If you're trying to add carName.text into the array, do this: pickOption.append("\(carName.text)". Good luck with your project!

Comments

0

Try this:

pickOption = String(carName.text)

Comments

0

Thanks everyone. pickOption.append(carName.text) helped. To complete here is the file where the text goes.

import UIKit

class CarListViewController: UITableViewController {

var cars = String var newCar: String = ""

@IBAction func cancel(segue:UIStoryboardSegue) {

}

@IBAction func done(segue:UIStoryboardSegue) {

var carDetailVC = segue.sourceViewController as! CarDetailViewController
newCar = carDetailVC.carName.text
cars.append(newCar)
self.tableView.reloadData()

}

override func viewDidLoad() {
    super.viewDidLoad()

    cars = ["BMW","Audi","Volkswagen"]
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 }

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return cars.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("carCell", forIndexPath: indexPath) as! UITableViewCell

    cell.textLabel!.text = cars[indexPath.row]

    return cell
}

}

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.