iOS开发 常用宏定义

#ifndef Public_h

#define Public_h

// 1.判断是否为iOS7

#define iOS7 ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0)

#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]

// 2.获得RGB颜色

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

#define RGB(r, g, b)                        RGBA(r, g, b, 1.0f)

// 随机色

#define RandomColor                 RGB(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))

// 3.是否为4inch

#define fourInch ([UIScreen mainScreen].bounds.size.height == 568)

// 4.屏幕大小尺寸

#define screen_width [UIScreen mainScreen].bounds.size.width

#define screen_height [UIScreen mainScreen].bounds.size.height

//重新设定viewY

#define setFrameY(view, newY) view.frame = CGRectMake(view.frame.origin.x, newY, view.frame.size.width, view.frame.size.height)

#define setFrameX(view, newX) view.frame = CGRectMake(newX, view.frame.origin.y, view.frame.size.width, view.frame.size.height)

#define setFrameH(view, newH) view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, newH)

#define setFrameW(view, newW) view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, newW, view.frame.size.height)

//view的坐标及长宽

#define W(view)    view.frame.size.width

#define H(view)    view.frame.size.height

#define X(view)    view.frame.origin.x

#define Y(view)    view.frame.origin.y

#define ViewBottomY(view) Y(view)+H(view)

#define ViewWidthX(view) X(view)+W(view)

//5.常用对象

#define APPDELEGATE ((AppDelegate *)[UIApplication sharedApplication].delegate)

#define APP_TOPWIMDOW [[UIApplication sharedApplication].windows lastObject]

//重写NSLog,Debug模式下打印日志和当前行数

#if DEBUG

#define NSLog(FORMAT, ...) fprintf(stderr,"line%d content:%s\n",__LINE__,[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);

//fprintf(stderr,"\nfunction:%s line:%d content:%s", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);

#else

#define NSLog(FORMAT, ...) nil

#endif

//----------------------其他----------------------------

// View 圆角和加边框

#define ViewBorderRadius(View, Radius, Width, Color)\

\

[View.layer setCornerRadius:(Radius)];\

[View.layer setMasksToBounds:YES];\

[View.layer setBorderWidth:(Width)];\

[View.layer setBorderColor:[Color CGColor]]

// View 圆角

#define ViewRadius(View, Radius)\

\

[View.layer setCornerRadius:(Radius)];\

[View.layer setMasksToBounds:YES]

// 是否iPad
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
// 是否iPad
#define someThing (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)? ipad: iphone
 
//获取系统版本
#define IOS_VERSION [[UIDevice currentDevice] systemVersion] floatValue]
#define CurrentSystemVersion [UIDevice currentDevice] systemVersion]

//方正黑体简体字体定义
#define FONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F]



#endif /* Public_h */


以及单例模式

// .h文件的实现
#define SingletonH(methodName) + (instancetype)shared##methodName;

// .m文件的实现
#if __has_feature(objc_arc) // 是ARC
#define SingletonM(methodName) \
static id _instace = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
if (_instace == nil) { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
} \
return _instace; \
} \
\
- (id)init \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super init]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##methodName \
{ \
return [[self alloc] init]; \
} \
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
} \
\
+ (id)mutableCopyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
}

#else // 不是ARC

#define SingletonM(methodName) \
static id _instace = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
if (_instace == nil) { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
} \
return _instace; \
} \
\
- (id)init \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super init]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##methodName \
{ \
return [[self alloc] init]; \
} \
\
- (oneway void)release \
{ \
\
} \
\
- (id)retain \
{ \
return self; \
} \
\
- (NSUInteger)retainCount \
{ \
return 1; \
} \
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
    return _instace; \
} \
 \
+ (id)mutableCopyWithZone:(struct _NSZone *)zone \
{ \
    return _instace; \
}

#endif  //Singleton.h

其中这个单例模式先在UserInfoModel.h中

SingletonH(UserInfoModel);
再在.m中

SingletonM(UserInfoModel);



初始化赋值

//UserInfoModel是个单例,此处初始化后,数据会一直随应用程序的启动而存在
 UserInfoModel *userInfoModel = [UserInfoModel mj_objectWithKeyValues:responseData.responseObject[0]];
//其中mj_objectWithKeyValues是mj方法的其中一种,详情请看本人另一篇文章介绍常用mj
调用获取单例中的值,比如获取id

[UserInfoModel sharedUserInfoModel].ID






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值