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?