Swift switch not working with C++ based enum - objective-c

I'm trying to learn Swift by converting an existing Objective-C app I wrote (a streaming audio player). However, the libraries I'm using don't have Swift equivalents, so I've created a bridging header and am referencing them successfully.
Now, I'm trying to do a switch on an enum from one of these libraries, but Swift is just complaining.
To make things even more confusing (for me, probably not for any of you), a series of if blocks works fine.
Here is the enum from the Objective-C library:
typedef enum {
kFsAudioStreamRetrievingURL,
kFsAudioStreamStopped,
kFsAudioStreamBuffering,
kFsAudioStreamPlaying,
kFsAudioStreamPaused,
kFsAudioStreamSeeking,
kFSAudioStreamEndOfFile,
kFsAudioStreamFailed,
kFsAudioStreamRetryingStarted,
kFsAudioStreamRetryingSucceeded,
kFsAudioStreamRetryingFailed,
kFsAudioStreamPlaybackCompleted,
kFsAudioStreamUnknownState
} FSAudioStreamState;
This works:
if (state == kFsAudioStreamRetrievingURL) {
}
if (state == kFsAudioStreamStopped) {
}
if (state == kFsAudioStreamBuffering) {
}
But this doesn't work:
switch state {
case kFsAudioStreamRetrievingURL:
case kFsAudioStreamStopped:
case kFsAudioStreamBuffering:
break;
}
The second one gives me the following error:
Binary operator '~=' cannot be applied to two 'FSAudioStreamState' operands
I would appreciate any assistance in helping me learn more Swift!

Try this:
switch state {
case .RetrievingURL:
case .Stopped:
case .Buffering:
...
}
---- EDITED!!! ----
I'm wrong, this is C++ enum. So try this:
switch state.value {
case kFsAudioStreamRetrievingURL.value:
case kFsAudioStreamStopped.value:
case kFsAudioStreamBuffering.value:
...
}

In obj-c
Enum definition should be
typedef NS_ENUM(NSInteger, FSAudioStreamState){
FSAudioStreamStateStopped,
...
}
So when they are imported into Swift
enum FSAudioStreamState{
case Stopped
...
}
I think it's C++ enum (Not Objective-C enum), so swift will not regard it as enum and it would be better to use if - else.

Related

How to properly log Vulkan result type strings

Logging VkResult enum values
The VkResult enum contains a lot of values. Unfortunately though, they are C enums which aliases an integer, so I cannot easily just log their names to the console. For this purpose, I am envisioning a function which does something like this:
void graphics::log_vk_result(VkResult result)
{
switch (result)
{
case VK_SUCCESS:
log_core_debug("VK_SUCCESS"); return;
case VK_NOT_READY:
log_core_debug("VK_NOT_READY"); return;
[...]
}
But some of the enum values are only supported by certain extensions, as indicated here. An example: The extension VK_EXT_debug_report introduces the following value to the enumeration: VK_ERROR_VALIDATION_FAILED_EXT. So my idea for (potentially) more portable code would be something like this:
void graphics::log_vk_result(VkResult result)
{
switch (result)
{
[...]
#if defined(VK_EXT_DEBUG_REPORT_EXTENSION_NAME)
case VK_ERROR_VALIDATION_FAILED_EXT:
log_core_debug("VK_ERROR_VALIDATION_FAILED_EXT");
#endif
}
I found this name by looking at the extension manual page. I cannot easily see whether or not VK_EXT_DEBUG_REPORT_EXTENSION_NAME is a macro or an enum - it is a const char* but stated under the section "New Enum Constants". So checking for this particular value, for this particular extension, was my default choice.
[I do realize this extension is marked as deprecated, and I'm not using it! I only use it as an example here.]
I have two questions:
Is this needed?
Is this the correct way of doing this?
Thanks a lot!
All of this is unnecessary, since Vulkan SDK already includes the desired functionality:
#include <vulkan/vk_enum_string_helper.h>
void graphics::log_vk_result( const VkResult result ){
log_core_debug( string_VkResult(result) );
}

Objective-C NS_ENUM not recognized in Swift

I am using the google VR SDK external framework in my swift project.
In my Bridging-Header.h I am importing the framework header
#import "GVRCardboardView.h"
In GVRCardboardView.h there is this:
typedef NS_ENUM(NSInteger, GVRUserEvent) {
kGVRUserEventBackButton,
kGVRUserEventTilt,
kGVRUserEventTrigger,
};
In my swift code
#objc func cardboardView(_ cardboardView: GVRCardboardView!, didFire event: GVRUserEvent) {
switch (event) {
case kGVRUserEventBackButton:
print("User pressed back button");
break;
case GVRUserEvent.kGVRUserEventTilt:
print("User performed tilt action");
break;
case .kGVRUserEventTrigger:
print("User performed trigger action");
break;
}
}
But I get compiler errors. You can see I'm trying three different ways to access the enum values in my switch case, and they all give errors. First,
Use of unresolved identifier 'kGVRUserEventTilt'
When I searched other similar questions online, I saw the solution was to include the name of the enum, like you see in the second case of my switch statement. But that gives me this error:
Type 'GVRUserEvent' has no member 'kGVRUserEventTilt'
And the third case gives this:
Pattern cannot match values of type 'GVRUserEvent'
Anyone know why? It recognizes GVRUserEvent as something -- I don't get errors in the argument line.
Thanks for any help!

How to avoid if else or switch case whe dealing with enums?

I have a member variable that tells units for a value I have measured like centimeters,kilometers,seconds,hours etc.
Now these are enums,
When I display a corresponding string, I have created a method that returns corresponding string for these enums.
Unlike Java, enums here cant have other properties associated with them.
So I have to explicitly do a if-else-if chain or a switch case to return the correct string.
I am new to Objective C. any good practice that I should be following in such scenarios ?
afaik Objective-C enums are just old-school C enums... so maybe you can use an integer value for them?
I guess if your enum values started at 0 and increased you could use some sort of array access:
const char *distanceUnitToString2(enum DistanceUnit unit)
{
const char *values[] = {
"cm",
"m",
"km"
};
// do some sanity checking here
// ...
return values[unit];
}
But this feels a little flaky to me. What if you have negative values, or you are using bitmask-style enum values like 1 << 8? You are going to end up using a very large array.
You also could use a switch and improve it a little with a macro. Something like this:
const char *distanceUnitToString(enum DistanceUnit unit)
{
#define CASE(UNIT, STRING) case (UNIT): return (STRING)
switch (unit) {
CASE(kCentimeters, "cm");
CASE(kMeters, "m");
CASE(kKiloMeters, "km");
default:
// should not get here
assert(0);
break;
}
#undef CASE
}
But you don't really save that much vs. not using the macro.
Martin James's comment is the right answer. And use a definition of the enum like:
enum units { cm = 0, m, km };
that way you can be sure that your enum translates to the correct index values.

Casting a basic CFTypeRef to a more specific CoreFoundation type

Is there a simple way to convert a CTypeRef to a specific CoreFoundation type? I'm not looking to cast inline as (CFStringRef)myObjectRef but would like to create a helper method to do this for me for all CoreFoundation types.
I know it's possible to use something like CFGetTypeID(myObjectRef) == CFStringGetTypeID() to find out whether a CTypeRef is a CFString. However creating a single method to do this could become very verbose and have a lot of if statements.
Is building out a method with a bunch of if statements against CFGetTypeID() the only way? Or is there a simpler way to do this?
UPDATE: with example
I'd like to make a helper function to work with some legacy code I can't change. Currently it produces one of CFDictionaryRef, CFStringRef or CFURLRef as a return value provided as a CTypeRef. I'm currently working around this by running CFGetTypeID() on the returned value but this isn't ideal. Rather than having C-style casts all over the place, I'd rather have a CastToCF() helper which handles this for me. This would help make testing easier in the future as well.
P.S. I'm not worried about mutable types.
there's no obvious point in doing this. a c style cast is not like other languages - it is a typecast which the address on the left will be identical to the address on the right. cftypes will not throw or return null if you do a bad cast (unlike other languages). iow, it's merely a decoration for you to specify a type, and a c compiler will assume your cast is valid.
or perhaps you can provide a better example of how you would use this, if that did not help.
Update
ok. since you tagged it objc++, i'd just create a helper class which had plenty of diagnostics and did all the noisy conversions (minimal illustration):
class t_helper {
public:
t_helper(CFTypeRef cf) : d_cf(cf), d_type(CFGetTypeID(cf)) { assert(this->d_cf); }
~t_helper() {}
/* type info */
bool isString() const { return CFStringGetTypeID() == this->type(); }
CFStringRef string() { assert(this->isString()); return this->cf_cast<CFStringRef>(); }
bool isDictionary() const { return CFDictionaryGetTypeID() == this->type(); }
CFDictionaryRef dictionary() { assert(this->isDictionary()); return this->cf_cast<CFDictionaryRef>(); }
...
/* and a trivial example of an operation */
void appendMutableCopyToArray(CFMutableArrayRef array) {
if (this->isString()) {
CFMutableStringRef cp(CFStringCreateMutableCopy(0,0,this->string()));
CFArrayAppendValue(array, cp);
CFRelease(cp);
}
...
}
...
private:
template < typename T > T cf_cast() { return reinterpret_cast<T>(this->d_cf); }
const CFTypeID type() const { return this->d_type; }
private:
CFTypeRef d_cf;
const CFTypeID d_type;
};
that's about as accurate as i can get get without a really specific example of the program you are dealing with.

Objective C switch statements and named integer constants

I have a controller which serves as a delegate to two scrollviews which are placed in view managed by aforementioned view controller.
To distinguish between two scroll views I'm trying to use switch statement (instead of simple pointer comparison with if statement). I have tagged both scroll views as 0 and 1 like this
NSUInteger const kFirstScrollView = 0;
NSUInteger const kSecondScrollView = 1;
When I try to use these constants in a switch statement, the compiler says that case statements are not constants.
switch (scrollView.tag) {
case kFirstScrollView: {
// do stuff
}
case kSecondScrollView: {
// do stuff
}
}
What am I doing wrong?
This can be solved through the use of an anonymous (though not necessarily so) enum type:
enum {
kFirstScrollView = 0,
kSecondScrollView = 1
};
switch (scrollView.tag) {
case kFirstScrollView: {
// do stuff
}
case kSecondScrollView: {
// do stuff
}
}
This will compile without errors.
This is because case statement requires constant expression. Now in C and thus in Obj-C making a variable const does not create a true constant. Thus you are getting this error. But if you use C++ or Obj-C++ then this will work.
Some more hint is available here and here.