Convert id to enum using objective-c

I'm trying to implement a simple method, however, I'm still pretty new to objective-c.

I have this simple method that tries to convert from id to a specific value in an enumeration, if one matches.

This listing

typedef enum {
   DXTypeUnknown = 0,
   DXDatasource = 1,
   DXGroup = 2
} DXPropertyType;

And this is the appropriate method:

-(DXPropertyType)typeFromObject:(id)_type {
    int _t = [_type intValue];

    switch (_t) {
        case DXDatasource:
            return [NSNumber numberWithInt:DXDatasource];
        case DXGroup:
            return [NSNumber numberWithInt:DXGroup];


        default:
            return [NSNumber numberWithInt:DXTypeUnknown];
    } 
}

, , - int, , , , , . , /, , ? , , , , .

[EDIT] , NSManagedObject, CoreData NSNumber, , , .

+5
3

, , id:

if (![_type respondsToSelector:@selector(intValue)])
    return nil;

, NSNumber :

- (DXPropertyType)typeFromObject:(NSNumber)_type;

NSNumber. , , NSNumber. :

-(DXPropertyType)typeFromObject:(id)_type {

    if (![_type respondsToSelector:@selector(intValue)])
        return nil;

    int _t = [_type intValue];

    switch (_t) {
        case DXDatasource:
            return DXDatasource;
        case DXGroup:
            return DXGroup;


        default:
            return DXTypeUnknown;
    } 
}

:

- (DXPropertyType)typeFromObject:(id)_type {

    if ([_type respondsToSelector:@selector(intValue)]) {

        int t = [_type intValue];
        DXPropertyType property_t;

        if (t >= 1 && t <= 2)
            property_t = t;
        else
            property_t = DXTypeUnknown;

        return property_t;
    }
    return nil;
}
+4

switch .

NSNumber, .

-(NSNumber)typeFromObject:(id)_type
0

, , . , NSNumber *.

enum, , , , . [NSNull null].

0
source

All Articles