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

  • Home
  • SEARCH
  • 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 6900709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:34:50+00:00 2026-05-27T07:34:50+00:00

What would be the way to create a constant in objective-c that is called

  • 0

What would be the way to create a constant in objective-c that is called like a class property? (e.g. classA.KEY_FOR_ITEM1)

That is I see the advice re how to create a constant here http://stackoverflow.com/questions/538996/constants-in-objective-c This approach approach however seems to create a constant that is global and can be referenced anywhere.

I was more interested in a constant that you had to specify the context by using the class name too. So say you had an Event object, then you could have EventType constants specified (e.g. EVENTTYPE_DIRECT)

  EventType.EVENTTYPE_DIRECT

So the question is what would the *.h and *.m code segments be for this

  • 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-27T07:34:51+00:00Added an answer on May 27, 2026 at 7:34 am

    Sounds to me like you’re coming from a Java-style language background, as do I (sorry if I’m guessing wrong here). I wondered about this too for a while, and then I noticed how Apple defines enums and uses them to define constants like you’re describing.

    Let’s use the imaginary class Event as an example:

    In the header file, you want to define a new enum:

    typedef enum {
    
        kEventType_EventName1,
        kEventType_EventName2,
        kEventType_EventName3
    
    }EventType;  //this is the name of the new type
    

    where EventName1 etc, is replaced with the actual names of the events you want (ie. kEventType_Direct.

    Any other class that needs to see these event types just needs to import your Event.h file:

    #import "Event.h"
    

    Then you can use EventType as you would any other variable type (bearing in mind that it is not an NSObject, and does cannot be retained, released, etc – you would treat it like any other C type: int, float, etc)

    You can even have EventType typed variables as members of other classes, so long as those classes import your Event.h header.

    But that allows you to do things like this:

    -(void) handleEventOfType: (EventType) evtType {
    
        switch(evtType) {
            case kEventType_EventType1 :
                //stuff here
                break;
    
            //etc...
    
        }
    }
    

    That’s the best way I’ve seen to do it so far, and seems to be the way that is generally practiced abroad (at least in most of the projects that I’ve seen). Anyway, hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know a way to create an XML file using MySQL
I would like to know if there is a way to take create a
What would be the simplest way to create some graphical representations of the usage
What would be the correct way to create a fully URL-encoded file:// URI from
What would be the best way to create a JS chat client with GWT?
What would be the best way to create a circularly linked list in C#.
Is there a way to dynamically create constant variables on the fly? The idea
I would like to create a struct and use it inside an other struct
I would like to port an embedded application to x86 to create an emulator.
How would one create a char or string constant containing the single character ASCII

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.