iOS 中常量文件的写法

一、方式一(.h.m文件)

(1).h文件

#import 

/** 通用的间距值 */
UIKIT_EXTERN CGFloat const Margin;
/** 通用的小间距值 */
UIKIT_EXTERN CGFloat const SmallMargin;

/** 公共的URL */
UIKIT_EXTERN NSString * const CommonURL;

/** XMGUser - sex - male */
UIKIT_EXTERN NSString * const UserSexMale;

/** XMGUser - sex - female */
UIKIT_EXTERN NSString * const UserSexFemale;

(2).m文件

#import 

/** 通用的间距值 */
CGFloat const Margin = 10;
/** 通用的小间距值 */
CGFloat const SmallMargin = Margin * 0.5;

/** 公共的URL */
NSString * const CommonURL = @"http://api.budejie.com/api/api_open.php";

/** XMGUser - sex - male */
NSString * const UserSexMale = @"m";

/** XMGUser - sex - female */
NSString * const UserSexFemale = @"f";

方式二

直接把值写到.h文件中

//输出相关信息
#ifndef __OPTIMIZE__
#define BXLog(x, ...) NSLog(@"%s   %d行: " x, __FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#define BXLog(...) /* */
#endif


//断言宏
#ifdef __OPTIMIZE__
#define BXAssert(x) if(!(x)) {int a=1;int b=0;int c=a/b;c++;}
#else
#define BXAssert(x) if(!(x)) {}
#endif

#define cityWithName @"馆陶"

// 对字符串做特殊的宏,即保证返回的值不为空
#define ISNIL(x) ((x) == nil ? @"" : (x))
#define ISNILDefault(x, y) ((x) == nil ? y : (x))
#define ISNULL(x) ((x) == nil || [(x) isEqualToString:@"NULL"] || [(x) isEqualToString:@"null"] ? @"" : (x))

// 语言本地化
#define BXLocalizedString(x) NSLocalizedStringFromTable((x),@"LocationString",nil)


// 设备
#define iOS7_Version [[[UIDevice currentDevice]systemVersion]floatValue] >= 7.0

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
#define IS_IPHONE_4 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )480 ) < DBL_EPSILON )
//颜色设置

#define UIColorFromIntRBG(RED, GREEN, BLUE) [UIColor colorWithRed:RED/255.0 green:GREEN/255.0 blue:BLUE/255.0 alpha:1.0]

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]


//主色
#define BASIC_BLACK_COLOR     UIColorFromIntRBG(38, 53, 58)
#define BASIC_GREEN_COLOR     UIColorFromIntRBG(0, 131, 143)
#define BASIC_GRAY_COLOR      UIColorFromIntRBG(222, 223, 224)
#define BASIC_RED_COLOR       UIColorFromIntRBG(221, 72, 92)
//点击色
#define BASIC_GREEN_COLOR_HIGHT  UIColorFromRGB(0x66b5bc)
#define BASIC_RED_COLOR_HIGHT    UIColorFromRGB(0xeb919d)

#define CLICK_GREEN_COLOR     UIColorFromIntRBG(102, 181, 188)
#define CLICK_RED_COLOR       UIColorFromIntRBG(235, 145, 157)
#define NORMAL_TXT_COLOR      UIColorFromIntRBG(85, 86, 83)
//secondary
//UIColorFromRGB(0xf0f0ee)

#define NORMAL_BACK_COLOR       UIColorFromRGB(0xf0f0ee)
#define SECONDARY_TXT_COLOR     UIColorFromIntRBG(153, 154, 152)
#define BACKVIEW_COLOR          UIColorFromIntRBG(223, 228, 227)
#define PURE_WHITE_COLOR        UIColorFromIntRBG(255, 255, 255)
#define PURE_BLACK_COLOR        UIColorFromIntRBG(0, 0, 0)
#define UIClearColor   [UIColor clearColor]

方式三

全部写到PrefixHeader.pch文件中

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

/*** 如果希望某些内容能拷贝到任何源代码文件(OC\C\C++等), 那么就不要写在#ifdef __OBJC__和#endif之间 ***/


/***** 在#ifdef __OBJC__和#endif之间的内容, 只会拷贝到OC源代码文件中, 不会拷贝到其他语言的源代码文件中 *****/
#ifdef __OBJC__

#import "UIView+Extension.h"
#import "UIBarButtonItem+Extension.h"
#import "UITextField+Extension.h"
#import "Const.h"
#import "NSString+Extension.h"
#import "NSDate+Extension.h"
#import "NSCalendar+Extension.h"
#import "UIImage+Extension.h"
#import "UIImageView+Extension.h"

/*** 评论-头部控件-字体 ***/
#define CommentSectionHeaderFont [UIFont systemFontOfSize:15]

/*** 当前系统版本判断 ***/
#define iOS(version) ([UIDevice currentDevice].systemVersion.doubleValue >= (version))

/*** 将服务器返回的数据写入plist ***/
#define WriteToPlist(data, filename) [data writeToFile:[NSString stringWithFormat:@"/Users/xiaomage/Desktop/%@.plist", filename] atomically:YES];

/*** 日志 ***/
#ifdef DEBUG
#define MGLog(...) NSLog(__VA_ARGS__)
#else
#define MGLog(...)
#endif

#define MGLogFunc XMGLog(@"%s", __func__);

/*** 颜色 ***/
#define ColorA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]
#define Color(r, g, b) XMGColorA((r), (g), (b), 255)
#define RandomColor XMGColor(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))
#define GrayColor(v) XMGColor((v), (v), (v))
#define CommonBgColor XMGrayColor(206)

#endif
/***** 在#ifdef __OBJC__和#endif之间的内容, 只会拷贝到OC源代码文件中, 不会拷贝到其他语言的源代码文件中 *****/

#endif

你可能感兴趣的:(iOS 中常量文件的写法)