Reputation: 2431
I am having a delegate function which asks for UIActivityIndicatorViewStyle
from delegate.
@protocol StatusViewCustomizationDelegate <NSObject>
@optional
-(UIActivityIndicatorViewStyle)activityIndicatorStyle;
@end
Inside a private function I check if delegate responds to this selector and if it responds I call the method. Below is the code:
-(void)configureView
{
UIActivityIndicatorViewStyle activityIndicatorStyleFromDelegate;
if ([self.delegate respondsToSelector:@selector(activityIndicatorViewStyle)])
{
activityIndicatorStyleFromDelegate = [self.delegate activityIndicatorStyle];
}
}
What is the correct way to check the enum value I received in activityIndicatorStyleFromDelegate
variable is a valid UIActivityIndicatorViewStyle
enum value?
Edit:
UIActivityIndicatorViewStyle
is an iOS defined enum.
typedef NS_ENUM(NSInteger, UIActivityIndicatorViewStyle) {
UIActivityIndicatorViewStyleWhiteLarge,
UIActivityIndicatorViewStyleWhite,
UIActivityIndicatorViewStyleGray,
};
Upvotes: 2
Views: 4447
Reputation: 53082
Generally, when checking enum values you're limited to one of two methods.
If the enum values are in a contiguous sequence, you can check the value is within its range:
if (activityIndicatorStyle >= UIActivityIndicatorViewStyleWhiteLarge &&
activityIndicatorStyle <= UIActivityIndicatorViewStyleGray) {
NSLog(@"Good");
} else {
NSLog(@"Bad");
}
Otherwise you'll just have to test check against each value in turn:
switch (activityIndicatorStyle) {
case UIActivityIndicatorViewStyleGray:
case UIActivityIndicatorViewStyleWhite:
case UIActivityIndicatorViewStyleWhiteLarge:
NSLog(@"Good");
break;
default:
NSLog(@"Bad");
}
Upvotes: 6
Reputation: 122381
Use this code:
if (style >= UIActivityIndicatorViewStyleWhiteLarge &&
style <= UIActivityIndicatorViewStyleGray) {
// valid
} else {
// invalid
}
Upvotes: 1
Reputation: 5536
You can take the last value of the enum and compare if the supplied enum is less or equal than the last value of the enum, so:
- (void)configureView
{
UIActivityIndicatorViewStyle activityIndicatorStyleFromDelegate;
if ([self.delegate respondsToSelector:@selector(activityIndicatorViewStyle)])
{
if ([self.delegate activityIndicatorStyle] >= UIActivityIndicatorViewStyleWhiteLarge &&
[self.delegate activityIndicatorStyle] <= UIActivityIndicatorViewStyleGray) {
activityIndicatorStyleFromDelegate = [self.delegate activityIndicatorStyle];
}
}
}
Also, a common convention is to add one more enumeration that is called num_types
and you can check if the enums are less than num_types
, but this is not possible in your context.
Upvotes: 0