1

I’m new to learning Swift and practicing UITableView tutorials that involves a todo-list app. The best way I can describe my problem is that the rawValue is supposed to be from the ‘enum Priority { }’ definition inside TodoList.swift , but it’s not being accessed by ‘func itemDetailViewController’ inside ChecklistviewController.swift.

TodoList.swift

protocol CaseCountable {
    static var caseCount: Int { get }
}

extension CaseCountable where Self: RawRepresentable, Self.RawValue == Int {
    internal static var caseCount: Int {
        var count = 0
        while let _ = Self(rawValue: count) {
             count += 1
        }
        return count
    }
}

class TodoList {

    enum Priority: Int, CaseCountable {
      case high = 0, medium = 1, low = 2, no = 3   
    }
    .
    .
    .

}

ChecklistViewController.swift

class ChecklistViewController: UITableViewController {

    var todoList: TodoList

    private func priorityForSectionIndex(_ index: Int) -> TodoList.Priority? {
       return TodoList.Priority(rawValue: index)
    }

    .
    .
    . 

}

extension ChecklistViewController: ItemDetailViewControllerDelegate {
    .
    .
    .
    func itemDetailViewController(_ controller: ItemDetailViewController, didFinishEditing item: ChecklistItem) {

       for priority in 0...(TodoList.Priority.caseCount-1) {
         let currentList = todoList.todoList(for: TodoList.Priority(rawValue: priority)!)
         if let index = currentList.index(of: item) {
             let indexPath = IndexPath(row: index, section: priority.rawValue) //COMPILER ERROR
             if let cell = tableView.cellForRow(at: indexPath) {
                configureText(for: cell, with: item)
             }
         }
    }
    navigationController?.popViewController(animated: true)
}

I tried following another post (How do I get the count of a Swift enum?) that showed a technique to make your own protocol called CaseCountalbe, in order to customize an enum to behave as if it were conforming to CaseIterable like in Swift 4.2. Unfortunately, I’m still confused about how data is passed between files. In this case, how would it be possible to get the rawValue from enum Priority to silence the compiler warning?

JCoder
  • 17
  • 7
  • 2
    Try `IndexPath(row: index, section: priority) // remove .rawValue` priority is an Int – Scriptable Oct 16 '18 at 12:51
  • Awesome! This gets rid of the error message. Now I have to see why the debugger won't stop at a breakpoint set on this line so that I can see the value of priority. – JCoder Oct 16 '18 at 21:25

1 Answers1

0

You are trying to access the rawValue of the priority object. HOWEVER, that priority is actually an Int.

If you change the line let indexPath = IndexPath(row: index, section: priority.rawValue)

to

let indexPath = IndexPath(row: index, section: currentList.priority.rawValue) it would probably work, assuming the currentList that has a TodoList which is an enum.

Let's go back to the basics of enum in Swift.

If for example we have an enum called PhoneType which has a rawValue type of Int:

enum PhoneType: Int {
    case iPhone5s = 568
    case iPhone8 = 667
    case iPhone8Plus = 736
    case iPhoneX = 812
}

then we can make an instance of PhoneType by passing a rawValue of Int, and use the enum in a switch or if-else statements, like so:

let screenHeight = Int(UIScreen.main.bounds.height)

if let type = PhoneType(rawValue: screenHeight) {
    switch type {
    case .iPhone5s: print("we are using iPhone5s and similar phones like SE/5C/5")
    case .iPhone8: print("we are using iPhone 8 and similar phones")
    case .iPhone8Plus: print("we are using iPhone 8plus or 7plus or 6 plus.")
    default: print("and so on...")
    }
}

I hope this helps.

Glenn Posadas
  • 12,555
  • 6
  • 54
  • 95
  • Thanks for the response, but unfortunately it gives a different error now stating that Value of type '[ChecklistItem]' has no member 'priority'. I hope my question provides enough code to decipher what's going on. I am still trying to navigate through this app that I'm practicing with. – JCoder Oct 16 '18 at 12:23
  • That error means you're likely referring to an array of `CheckListItem` and you have not specified an index. If you provide an index for the `CheckListItem` you may get around that. You may be looking for `let indexPath = IndexPath(row: index, section: item.priority.rawValue)` or just remove the .rawValue so you have `let indexPath = IndexPath(row: index, section: priority)` since the IndexPath.init(row:section:) expects two ints and the priority is an int. – jlowe Oct 16 '18 at 14:16
  • Thanks! This works to stop the error message by just typing priority after section. Now I need to sort out why the debugger doesn't stop at a breakpoint set on this line to see the value of priority. – JCoder Oct 16 '18 at 21:29