0

I am getting a crash Thread 21: EXC_BAD_ACCESS (code=1, address=0x101e) when trying to log a Swift enum in my Objective C code.

Enum passed in as the result of a completion handler on Swift function call from Obj C code:

 NSLog(@"Completed with operation: %@", operation);

Enum:

@objc public enum Operation: Int {
    case Unknown
    case Operation1
    case Operation2
    case Operation1And2
}

I tired the following 2 variations and the app failed to build. There is an issue with Xcode that prevents me from being able to see what the actual error was.

 NSLog(@"Completed with operation: %@", [operation description]);

 NSLog(@"Completed with operation: %@", [operation value]);

I want to do basic operation on this enum like log its value, do boolean comparisons. But I am a little lost on how to do that as fairly new to Objective C.

Parth
  • 2,682
  • 1
  • 20
  • 39
  • If it's an Int, you should use `%d`, not `%@`, that's why. See https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1 It's the same reason : https://stackoverflow.com/questions/23610509/exc-bad-access-when-building-nspredicate/27566124#27566124 – Larme Jun 02 '22 at 07:39
  • Got it and how would I do boolean comparisons on it? I am getting `error: receiver type 'Operation' (aka 'enum Operation') is not an Objective-C class` when doing `if (operation == [Operation Operation1])` – Parth Jun 02 '22 at 14:46
  • `if (operation == OperationOperation1) {}` – Larme Jun 02 '22 at 14:52
  • Thanks that built. Last question, is it possible to NSLog the string `OperationOperation1`. Using %d gave me an unhelpful `897745528` as output. – Parth Jun 02 '22 at 15:04
  • No, you need to add an extra method to transform the Int value into String... – Larme Jun 02 '22 at 15:06
  • But why is the int value not equal to 0, 1, 2? Why is it such a long number? – Parth Jun 02 '22 at 15:07
  • https://stackoverflow.com/questions/1094984/convert-objective-c-typedef-to-its-string-equivalent to print it. – Larme Jun 02 '22 at 15:07

0 Answers0