Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constant in objective-c

I would like to add some constant keys for my application, these constants can be accessed anywhere in program. So I declare the constants in interface file:

#import <UIKit/UIKit.h>
NSString * MIN_INTERVAL_KEY = @"MIN_INTERVAL_KEY";
NSString * MAX_TOBACCO_KEY = @"MAX_TOBACCO_KEY";
NSString * ICON_BADGE = @"ICON_BADGE";

@interface SmokingViewController : UIViewController {
}

And I would like to access them from the MinIntervalViewController class:

- (void)viewDidAppear:(BOOL)animated {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if (user) {
        self.selectedValue = [user objectForKey:MIN_INTERVAL_KEY];
    }
    [super viewDidAppear:animated];
}

But the application shows an error in the MinIntervalViewController class:

error: 'MIN_INTERVAL_KEY' undeclared (first use in this function)

Do I miss something? Any help would be appreciated.

Thanks

like image 376
haisergeant Avatar asked Aug 18 '10 09:08

haisergeant


People also ask

What is constant in C with example?

A constant is a value or variable that can't be changed in the program, for example: 10, 20, 'a', 3.4, "c programming" etc.

What is constant in OOP?

Constants cannot be changed once it is declared. Class constants can be useful if you need to define some constant data within a class. A class constant is declared inside a class with the const keyword. Class constants are case-sensitive. However, it is recommended to name the constants in all uppercase letters.

How do you declare constant?

You use the Const statement to declare a constant and set its value. By declaring a constant, you assign a meaningful name to a value. Once a constant is declared, it cannot be modified or assigned a new value. You declare a constant within a procedure or in the declarations section of a module, class, or structure.


2 Answers

Constants.h

#import <Cocoa/Cocoa.h>

@interface Constants : NSObject {   

}

extern int const kExampleConstInt;
extern NSString * const kExampleConstString;

@end

Constants.m

#import "Constants.h"


@implementation Constants

int const kExampleConstInt = 1;
NSString * const kExampleConstString = @"String Value";

@end

To use:

#import "Constants.h"

Then, simply call the specific variable you wish to use.

NSString *newString = [NSString stringWithString:kExampleConstString];
like image 155
Philip Regan Avatar answered Sep 22 '22 13:09

Philip Regan


In the .h file:

extern NSString * const MIN_INTERVAL_KEY;

In one (!) .m file:

NSString * const MIN_INTERVAL_KEY = @"MIN_INTERVAL_KEY";

And what you seemed to have missed is to actually import the header file declaring MIN_INTERVAL_KEY ;-) So if you declared it in SmokingViewController.h but like to use it in MinIntervalViewController.m, then you need to import "SmokingViewController.h" in your MinIntervalViewController.m. Since Objective-C is really more or less an extension to C all C visibility rules apply.

Also, what helps to debug things like that is to right-click on the .m file in Xcode and select "Preprocess". Then you see the file preprocess, i.e. after CPP has done its work. This is what the C compiler REALLY is digesting.

like image 38
DarkDust Avatar answered Sep 19 '22 13:09

DarkDust