Question

I got a segmented control with 3 options. I didn't really know how to create a variable from the selected.segmentedIndex so I copy a method from another place.

typedef NS_ENUM(int, tipoServidor) {
SERVIDOR_ARG = 1,
SERVIDOR_EU = 2,
SERVIDOR_US = 3};
@property (nonatomic) tipoServidor servidorType;

<Another file>
if (self.persistentSettings.servidorType == SERVIDOR_ARG){
    self.servidorControl.selectedSegmentIndex= 0;
} else if (self.persistentSettings.servidorType == SERVIDOR_EU) {
    self.servidorControl.selectedSegmentIndex= 1;
} else if (self.persistentSettings.servidorType == SERVIDOR_US) {
    self.servidorControl.selectedSegmentIndex = 2;}

<Another file>
- (NSString *)servidorType {
AdSettings *settings = [[AdSettings alloc] init];

NSString *server = [settings servidorType];

The problem is that the last line throws this error:

Implicit conversion of 'tipoServidor' (aka 'enum tipoServidor') to 'NSString *' is disallowed with ARC

How is the correct way to make this work?

Thank you very much

Was it helpful?

Solution

The problem is that the last line throws this error: Implicit conversion of tipoServidor (aka enum tipoServidor) to NSString * is disallowed with ARC

ARC or not, an enum is not an NSString, so you cannot do it. In order to deal with it, either define an array of strings that represent your enum values, or write a small function that does the mapping, for example, like this:

NSString *DescriptionOfServidor(tipoServidor e) {
    switch (e) {
        case SERVIDOR_ARG: return @"SERVIDOR_ARG";
        case SERVIDOR_EU: return @"SERVIDOR_EU";
        case SERVIDOR_US: return @"SERVIDOR_US";
    }
    return nil;
}

All you need now is this call:

NSString *server = DescriptionOfServidor([settings servidorType]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top