Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6107781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:13:32+00:00 2026-05-23T14:13:32+00:00

I am trying to implement a simple method, however I am still quite a

  • 0

I am trying to implement a simple method, however I am still quite a newbie on objective-c.

I have this simple method which is trying to convert from an id to a specific value in enum, if matched.

This is the enum

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

And this is the relevant 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];
    } 
}

The very first check I would to implement is if the id can be converted to an int, then see if it falls in the two relevant categories group or datasource, or return a default value if not. Could you tell me if the switch/case I implemented is a proper solution or not ?
I would like also this method not to causing crash of an application, so what could be advisable to check, keeping in mind that in any case the default value is to be returned.

thanks

[EDIT]
I forgot to say that this value is going to be stored in a field of a NSManagedObject, which by CoreData restriction can be an NSNumber, so probably there’s a better solution instead of an enum.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:13:32+00:00Added an answer on May 23, 2026 at 2:13 pm

    It might be a good idea to include this code to check if the id can be used:

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

    However, if you’ll always pass a NSNumber go ahead and declare the method as:

    - (DXPropertyType)typeFromObject:(NSNumber)_type;
    

    In your code, you’re returning a NSNumber. I don’t think that’s what you really
    want, as you’d be doing nothing with the NSNumber passed. Return the enum
    item:

    -(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;
        } 
    }
    

    And then this can be simplified to:

    - (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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still learning about Objective-C memory management. I'm trying to implement several simple classes
I'm trying to implement a simple method to read new lines from a log
I've been trying to implement a simple component-based game object architecture using Objective-C, much
My first try of MVC. Am trying to implement a simple example. Inspiration from
I'm trying to implement a circular buffer in C, and have come across this
I am trying to implement simple xhr abstraction, and am getting this warning when
I am trying to implement a simple plist example from Beginning iPhone 3 Development
Hi I have been trying to implement auto complete in my site from 2
I'm learning Matplotlib, and trying to implement a simple linear regression by hand. However,
I'm trying to implement a simple blog, which contains topics models/Topic.cs public class Topic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.