Friday, September 28, 2012

How we import (constant) global variables and #define VS global variables


How we import (constant) global variables and #define VS global variables


One of them informs the preprocessor to do a textual substitution. The other actually declares a (constant) C variable.


#define VS global variables - enum


define informs the preprocessor to do a textual substitution (a macro)
global variables - enum - declares a (constant) variable.


so its like macro VS const variable. 


The global variables - enum

  • they obey the language's scoping rules 
    you can see them in the debugger 
    you can take their address if you need to 
    you can pass them by const-reference if you need to 
    they don't create new "keywords" in your program.



you should use global variables and enum for constants, not #define.


How we import global variables?



  • 1) Add 2 new files to your project: "GlobalValues.h" and "GlobalValues.m".
  • 2) Open "GlobalValues.h", and declare all your needed variables.
//name of the 'service' used for storing items in the user's keychain:
extern NSString const *MYKeychainServiceName;

//name of the PasteBoard data type that application uses for document data:
extern NSString const *MYPboardDataType;
  • 3) Open "GlobalValues.m", and start the new file by importing "GlobalValues.h".
  • 4) Then, assign values to the variables you declared in the header file:
#import "GlobalValues.h"

//name of the 'service' the application uses for storing items in the user's keychain:
NSString const *MYKeychainServiceName = @"MyApplication Password";
//name of the PasteBoard data type that application uses for document data:
NSString const *MYPboardDataType = @"MyApplication DocData";
In the implementation files of the classes that need to use these variables, you would put - at the very beginning:
#import "GlobalValues.h"


#source:
Big Nerd Ranch Guide - Objective C programming
http://www.cocoadev.com/index.pl?GlobalVariablesInCocoa 

No comments:

Post a Comment