JCoder
JCoder

Reputation: 17

Xcode 9.4.1 Compiler error saying that Value of type ‘Int’ has no member ‘rawValue’

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?

Upvotes: 1

Views: 1762

Answers (1)

Glenn Posadas
Glenn Posadas

Reputation: 13290

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.

Upvotes: 0

Related Questions