9

I have an enum declaration.

enum OP_CODE {
    case addition
    case substraction
    case multiplication
    case division
}

And use it in a method:

func performOperation(operation: OP_CODE) {
        
}

We all know that how we can call this normally

self.performOperation(OP_CODE.addition)

But if I have to call it in some delegate where the integer value is not predictable, how do I call it?

For example:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     self.delegate.performOperation(indexPath.row)
}

Here, the compiler throws an error Int is not convertible to 'OP_CODE'. I've tried many permutations, but haven't been able to figure it out.

2 Answers 2

19

You need to specify the raw type of the enumeration

enum OP_CODE: Int {
    case addition, substraction, multiplication, division
}

addition will have a raw value of 0, substraction of 1, and so on.

and then you can do

if let code = OP_CODE(rawValue: indexPath.row) {
    self.delegate.performOperation(code)
} else {
   // invalid code
}

More info here: https://developer.apple.com/library/ios/documentation/swift/conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-XID_222


for older swift releases

In case you're using an older version of swift, raw enumerations work a bit different. In Xcode < 6.1, you have to use fromRaw() instead of a failable initializer:

let code = OP_CODE.fromRaw(indexPath.row)
Sign up to request clarification or add additional context in comments.

7 Comments

Tried it. Got an error OP_CODE cannot be constructed it has no accessible initializers- Can you please brief it.
I might have put a typo, try now
I tried the code and it works on my side, which version of Xcode are you on?
Xcode - Version 6.0.1 (6A317) & OS X Yosemite version 10.10
that's an old version, in that case you can use OP_CODE.fromRaw(indexPath.row), but you should really upgrade to 6.1
|
8

You can use raw values in your enum:

enum OP_CODE : Int{
    case addition = 0
    case substraction = 1
    case multiplication = 2
    case division = 3
}

and use the failable initializer taking a raw value as input:

let code = OP_CODE(rawValue: 2) // code == .multiplication

Note that code is an optional, because if the raw value doesn't map to a valid enum, the initializer returns nil.

In your case:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let code = OP_CODE(rawValue: indexPath.row)
    if let code = code {
        self.delegate.performOperation(code)
    }
}

Moreover, given an instance of the enum, you can obtain the associated raw value using the rawValue property.

Note: enums have changed a little bit in Xcode 6.1 - if you're using a previous version, read @GabrielePetronella's answer and related comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.