Ed Wisseborn
Ed Wisseborn

Reputation: 3

Value of type String to type [String] in Swift

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)
    }
}

Upvotes: 0

Views: 508

Answers (6)

Ed Wisseborn
Ed Wisseborn

Reputation: 3

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
}

}

Upvotes: 0

richbrz
richbrz

Reputation: 92

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!

Upvotes: 0

Adam Evans
Adam Evans

Reputation: 2082

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.

Upvotes: 1

Eliko
Eliko

Reputation: 1137

Try this:

pickOption = String(carName.text)

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

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]

Upvotes: 2

ixx
ixx

Reputation: 32271

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).

Upvotes: 1

Related Questions