Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional enum in swift from objective-c

I have a protocol defined in objective-c. It has a property which is an enum. I want to create a swift class that implements this protocol and I want the swift class to have the enum field optional. I can't use nullable annotation for enum types. How to work around this issue?

like image 303
TpoM6oH Avatar asked Dec 07 '25 05:12

TpoM6oH


1 Answers

Enums in Objective-C are never nil because they always default to 0 as they are not reference types.

Thus, you cannot have the property never-nil to implement the protocol while making it optional (e.g. nullable) in swift.

If you need the property nullable nullable sometimes, then you probably have to redesign your architecture (e.g. the hierarchy of protocols to have one where it is optional).

like image 70
Tobi Nary Avatar answered Dec 09 '25 19:12

Tobi Nary