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 8875969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:06:26+00:00 2026-06-14T19:06:26+00:00

I seem to be having difficulty understanding how to use Class static consts in

  • 0

I seem to be having difficulty understanding how to use Class static consts in Objective-C.

I come form a PHP background, and really what im trying to do is the equivalent of this:

MyObject.php

<?
class MyObject {

    const A_VALUE = 1;
    const ANOTHER_VALUE = 2;

}
?>

Which can be used like

MyObject::A_VALUE

and inside the class like

self::A_VALUE

I want to kind of replicate this behaviour in Obj-C, but I understand theres a FAR different syntax.

in Objective-C

I figured it would look something similar to this

MyObject.h

static int A_VALUE = 1;
static int ANOTHER_VALUE = 2;

@interface MyObject : NSObject;

This TECHNICALLY works, but it throws a GREAT deal of Warnings about unused variables

HOWEVER

MyObject.h

static NSString *A_VALUE = @"1";
static NSString *ANOTHER_VALUE = @"2";

@interface MyObject : NSObject;

THIS works without error. Despite not being used anywhere.

Well, I dont want to use a large series of strings, not only does this not feel very efficient at all (especially when theres many), but I hate looking at my code nd seeing a bunch of

if( [value isEqualToString:A_VALUE] ) {

Just. Gross. Could be worse, but eww. I would prefer:

if( value == A_VALUE ) {

However what i have works for right now, but ive been at a bit of a loss.

What is the proper way in Objective C to make consts (specifically int)

Cheers everyone!

  • 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-06-14T19:06:27+00:00Added an answer on June 14, 2026 at 7:06 pm

    You should define your integer constant this way:

    static const NSInteger kYourConstString = 4;
    

    and for your string:

    static NSString *const kYourConstString = @"An example";
    

    in your .m file. The static keyword will make the variable scope local to the compilation unit: your .m file.

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

Sidebar

Related Questions

I seem having difficulty in understanding the reason behind the need of having many
I'm having serious issues understanding PHP classes from a book. They seem very difficult.
I've been learning how to use GDI but am having difficulty understanding how Invalidate()
I seem to be having a very frustrating time with an inherited class calling
Okay, I seem to be having a problem. I'm trying to create a twicker
I'm having some difficulty in understanding the following WHERE clause in a T-SQL (SQL
I'm having a really difficult time understanding how this all works together. I've fiddled
This would seem to be a simple task but I'm having difficulty executing it
Having some difficulty with what I thought would be straight forward. I am trying
So I managed to use SOAP no problem but I am having difficulty using

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.