2017-09-07 13 views
-1

配列に項目を入れたときにXcodeがこのエラーを返し、ChecklistItemオブジェクトの変数を設定しています。私はエラーを返す行をコメントしました。あなたがEquatableプロトコルに準拠する必要が'(of:ChecklistItem)'型の引数リストを持つ "index"を呼び出せません

import UIKit 

class ChecklistViewController: UITableViewController, AddItemViewControllerDelegate { 

    var items: [ChecklistItem] 

    required init?(coder aDecoder: NSCoder) { 
     items = [ChecklistItem]() 

     let row0item = ChecklistItem() 
     row0item.text = "Walk the dog" 
     row0item.checked = false 
     items.append(row0item) 

     let row1item = ChecklistItem() 
     row1item.text = "Brush my teeth" 
     row1item.checked = true 
     items.append(row1item) 

     let row2item = ChecklistItem() 
     row2item.text = "Learn iOS development" 
     row2item.checked = true 
     items.append(row2item) 

     let row3item = ChecklistItem() 
     row3item.text = "Soccer practice" 
     row3item.checked = false 
     items.append(row3item) 

     let row4item = ChecklistItem() 
     row4item.text = "Eat ice cream" 
     row4item.checked = true 
     items.append(row4item) 

     super.init(coder: aDecoder) 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 


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

     return items.count 

    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "ChecklistItem", for: indexPath) 

     let item = items[indexPath.row] 

     let label = cell.viewWithTag(1000) as! UILabel 
     label.text = item.text 

     configureText(for: cell, with: item) 
     configureCheckmark(for: cell, with: item) 

     return cell 

    } 

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     if let cell = tableView.cellForRow(at: indexPath) { 
      let item = items[indexPath.row] 
      item.toggleChecked() 
      item.checked = !item.checked 

      configureCheckmark(for: cell, with: item) 
     } 
     tableView.deselectRow(at:indexPath, animated: true) 
    } 

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
     items.remove(at: indexPath.row) 

     let indexPaths = [indexPath] 
     tableView.deleteRows(at: indexPaths, with: .automatic) 
    } 

    func configureCheckmark(for cell: UITableViewCell, with item: ChecklistItem) { 

     let label = cell.viewWithTag(1001) as! UILabel 


     if item.checked { 
      label.text = "√" 
     } else { 
      label.text = " " 
     } 
    } 

    func configureText(for cell: UITableViewCell, with item: ChecklistItem) { 

     let label = cell.viewWithTag(1000) as! UILabel 
     label.text = item.text 

    } 

    func addItemViewControllerDidCancel(_ controller: AddItemViewController) { 
     dismiss(animated: true, completion: nil) 
    } 

    func addItemViewController(_ controller: AddItemViewController, didFinishAdding item: ChecklistItem) { 

     let newRowIndex = items.count 
     items.append(item) 

     let indexPath = IndexPath(row: newRowIndex, section: 0) 
     let indexPaths = [indexPath] 
     tableView.insertRows(at: indexPaths, with: .automatic) 

     dismiss(animated: true, completion: nil) 
    } 

    func addItemViewController(_ controller: AddItemViewController, didFinishEditing item: ChecklistItem) { 

     if let index = items.index(of: item) { // Cannot invoke "index" with an argument list of type'(of: ChecklistItem)' 
      let indexPath = IndexPath(row: index, section: 0) 
      if let cell = tableView.cellForRow(at: indexPath) { 
       configureText(for: cell, with: item) 
      } 
     } 
     dismiss(animated: true, completion: nil) 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "AddItem" { 

      let navigationController = segue.destination as! UINavigationController 
      let controller = navigationController.topViewController as! AddItemViewController 

      controller.delegate = self 
     } else if segue.identifier == "EditItem" { 

      let navigationController = segue.destination as! UINavigationController 
      let controller = navigationController.topViewController as! AddItemViewController 

      controller.delegate = self 

      if let indexPath = tableView.indexPath(for: sender as! UITableViewCell) { 
       controller.itemToEdit = items[indexPath.row] 
      } 


     } 
    } 



} 

ChecklistItem.swift

輸入財団

class ChecklistItem { 
    var text = "" 
    var checked = false 

    func toggleChecked() { 
     checked = !checked 
    } 
} 
+0

indexOfを使用するには、ChecklistItemがEquatableプロトコルを採用する必要があります。このプロトコルを採用することによってのみ、リストはアイテムを他のアイテムと比較して目的のインデックスを見つけることができます – arvidurs

答えて

0

は... どれでも多分、事前にグーグル。

+0

なぜこの質問へのリンクを投稿しましたか? – rmaddy

+0

そして、重複するものへのリンクを試みる(しようとする)代わりに、重複して閉じるよう投票してください。 – rmaddy

+0

おっと、それは重複したリンクでなければなりません。それを乱した。しかし、マーキングのためにありがとう! – arvidurs

関連する問題