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!
You should define your integer constant this way:
and for your string:
in your .m file. The
statickeyword will make the variable scope local to the compilation unit: your .m file.