IOS:定义常量

来源:互联网 发布:115网盘mac客户端 编辑:程序博客网 时间:2024/05/29 07:43

新建一个Constants.h文件:

////  Constants.h//  HelloWorld////声明常量//export global constantsFOUNDATION_EXPORT NSString *const KEY_USER_NAME;FOUNDATION_EXPORT const int RETRY_COUNT;


新建Constants.m

//export global constantsNSString *const KEY_USER_NAME = @"user_name";const int RETRY_COUNT = 3;

HelloWorld-Prefix.pch文件中增加:

//// Prefix header for all source files of the 'HelloWorld' target in the 'HelloWorld' project//#import <Availability.h>#ifndef __IPHONE_5_0  #warning "This project uses features only available in iOS SDK 5.0 and later."#endif#ifdef __OBJC__  #import <UIKit/UIKit.h>  #import <Foundation/Foundation.h>  #import "Constants.h"  //增加改行,引入全局常量#endif

引用常量的地方直接使用常量即可:

- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.        NSLog(@"key is %@,count is %d",KEY_USER_NAME,RETRY_COUNT);}

2013-06-30 22:48:06.633 HelloWorld[1665:c07] key is user_name,count is 3




原创粉丝点击