0

I have a project, with a file that I call 'Keys.h'

In that file, I declare strings and integers that are used across the project, some of which are integers, some of which are strings.

All of the strings work fine; however, if I use integers, I get an unused variable warning.

For a string, (lfPrefs is a dictionary of user preferences)

static NSString * kUserLFPrefs = @"lfPrefs";

This works fine, and does not produce any errors.

For an integer, (I have integers to define the current mode because it seems a bit snappier than comparing strings all the time).

static int kModeLiveFeed = 1001;
static int kModeEventFeed = 2002;

These work just fine, except that they are showing an unused entity warning.

I'd prefer to use the integers over strings, mostly because I read that comparisons are much faster, takes up less memory, etc.

My question is how can I stop the warnings while still getting access to my integer keys?

(Or, should I just use strings)

Logan
  • 52,262
  • 20
  • 99
  • 128

2 Answers2

1

I can suggest two different methods.

If you want to keep such variables in .h file, you may prefer using define if you will not be changing the value run time like;

#define kModeLiveFeed 1001

If you will be changing the variable value run time, I suggest keeping them in a .m file instead of in a .h file and creating only one instance of the .m file by using singleton. Then, even if you continue to get a warning from the .m file, you can disable it by the steps below:

  • Select your project from the left navigator to open project settings view.
  • Then, select your target.
  • Go to Build Phases tab and open compile resources area.
  • Click to the right side of your .m file to add a compiler flag as -w

I hope it helps.

emreoktem
  • 2,409
  • 20
  • 36
1

You may be misunderstanding the meaning of static in C/Objective-C (this question should help). You should use const rather than static to define constants, and you should define the value of an integer/string constant in a .m file, with a corresponding declaration in the .h file. Or better yet, use an enum if you have a related set of integer constants.

Here is Apple's documentation on constants, which includes the above information as well as naming recommendations (e.g., PRConstant is preferred over the classic Mac OS-style kConstant).

Community
  • 1
  • 1
Nicholas Riley
  • 43,532
  • 6
  • 101
  • 124