Ao abrir a tela de adicionar uma meal, ela não abre e dar um erro na linha que passa o os itens para o array de itens, do metodo viewDidLoad. Erro: Could not cast value of type 'eggplant_brownie.Item' (0x1000df258) to 'NSArray' (0x1a054d418). (lldb)
Codigo da classe abaixo
import UIKit
protocol AddMealDelegate {
func add(meal:Meal)
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, AddAnItemDelegate {
@IBOutlet var nameField: UITextField!;
@IBOutlet var happinessField: UITextField!;
@IBOutlet var tableView:UITableView?
var delegate:AddMealDelegate?
var items = Array<Item>()
var selecionados = Array<Item>()
override func viewDidLoad() {
let newItemButton = UIBarButtonItem(title: "New Item",
style: UIBarButtonItemStyle.Plain,
target: self,
action: #selector(ViewController.showNewItem))
navigationItem.rightBarButtonItem = newItemButton
let dir = getUserDir()
let archive = "\(dir)/eggplant-brownie-items"
if let loaded = NSKeyedUnarchiver.unarchiveObjectWithFile(archive) {
items = loaded as! Array
}
}
func showNewItem(){
let newItem = NewItemViewController(delegate: self)
if let navigation = navigationController{
navigation.pushViewController(newItem, animated: true)
}else{
Alert(controller:self).show()
}
}
func getUserDir() -> String {
let userDir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let dir = userDir[0] as String
return dir
}
func add(item:Item){
items.append(item)
if let table = tableView{
let dir = getUserDir()
let archive = "\(dir)/eggplant-brownie-items"
NSKeyedArchiver.archiveRootObject(item, toFile: archive)
table.reloadData()
}else{
Alert(controller: self).show("Unexpected Error, but the item was added")
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let row = indexPath.row
let item = items[row]
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
cell.textLabel?.text = item.name
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath){
let item = items[indexPath.row]
if cell.accessoryType == UITableViewCellAccessoryType.None {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
selecionados.append(item)
print("Nome: \(item.name)")
}else{
cell.accessoryType = UITableViewCellAccessoryType.None
if let position = selecionados.indexOf(item) {
print("Objeto encontrado \(selecionados[position].name)")
selecionados.removeAtIndex(position)
}else{
Alert(controller: self).show() }
}
}else{
Alert(controller: self).show()
}
}
func getMealFromForm() -> Meal?{
if nameField == nil || happinessField == nil {
return nil
}
let name = nameField.text!;
let happiness = Int(happinessField.text!)
if happiness == nil {
return nil
}
let meal = Meal(name: name, happiness: happiness!)
meal.items = selecionados
return meal
}
@IBAction func add(){
if let meal = getMealFromForm(){
if let meals = delegate{
meals.add(meal)
if let navigation = self.navigationController{
navigation.popViewControllerAnimated(true)
}else{
Alert(controller: self).show("Unexpected erro, but the meal was added")
}
return
}
}
Alert(controller: self).show()
}
}