Since Swift 1.2 it's been possible to automatically convert enums in Swift to Objective-C. However, as far as I can tell, it is not possible to convert an array of enums. Is this true?
So, this is possible:
@objc public enum SomeEnumType: Int {
case OneCase
case AnotherCase
}
But this is not:
public func someFunc(someArrayOfEnums: Array<SomeEnumType>) -> Bool {
return true
}
Can anyone verify this? And how would you recommend working around this? One approach would be to have two method declarations e.g:
// This will not automatically get bridged.
public func someFunc(someArrayOfEnums: Array<SomeEnumType>) -> Bool {
return true
}
// This will automatically get bridged.
public func someFunc(someArrayOfEnums: Array<Int>) -> Bool {
return true
}
But this is polluting the Swift interface. Any way to hide the second function declaration for any Swift consumers?
So it is not possible to have a certain Enum extend and inherit the cases of another enum.
The name of an enum in Swift should follow the PascalCase naming convention in which the first letter of each word in a compound word is capitalized. The values declared in an enum — up , down , left and right — are referred to as enumeration case.
Finally, let's take a look at how enum cases relate to functions, and how Swift 5.3 brings a new feature that lets us combine protocols with enums in brand new ways. A really interesting aspect of enum cases with associated values is that they can actually be used directly as functions.
Objective-C Enumerators allow variables to be declared with a predefined list of valid values, each of which is assigned a name that can be used when setting the variable. This has the advantage of making the code easier to read and understand.
It seems, we cannot expose Array<SomeEnumType>
parameter to Obj-C even if SomeEnumType
is @objc
.
As a workaround, how about:
@objc(someFunc:)
func objc_someFunc(someArrayOfEnums: Array<Int>) -> Bool {
return someFunc(someArrayOfEnums.map({ SomeEnumType(rawValue: $0)! }))
}
func someFunc(someArrayOfEnums: Array<SomeEnumType>) -> Bool {
return true
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With