Stay weird. Stay different.

0%

IOS常用宏定义&工具方法

总结一些工作中常用的宏定义和工具方法

  1. 宏定义
  2. 工具方法

宏定义

  • NSLog宏定义
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifdef NEED_DEBUG

#define NSLog(format, ...) \
do { \
NSLog(@"<%@ : %d : %s>-%@", \
[[NSString stringWithUTF8String:__FILE__] lastPathComponent], \
__LINE__, \
__FUNCTION__, \
[NSString stringWithFormat:format, ##__VA_ARGS__]); \
} while(0)
#else
#define NSLog(format, ...) do{ } while(0)
#endif
  • 纪录程序执行时间宏定义
1
2
#define TICK   NSDate *startTime = [NSDate date]
#define TOCK NSLog(@"Time: %f", -[startTime timeIntervalSinceNow])
  • 字符串相关判断
1
2
3
4
5
6
//判断字符串是否为空或者为空字符串
#define StringIsNullOrEmpty(str) (str==nil||[str isEqualToString:@""])
//判断字符串不为空并且不为空字符串
#define StringNotNullAndEmpty(str) (str!=nil && ![str isEqualToString:@""] && ![str isEqualToString:@"null"])
//对象转字符串
#define StringFromObject(object) [NSString stringWithFormat:@"%@",object]
  • 程序主代理
1
2
/// 程序主代理
#define IMAPP_DELEGATE ((AppDelegate *)[UIApplication sharedApplication].delegate)
  • 获取图片
1
2
/// 取图片
#define GET_IMAGE(imageName) [UIImage imageNamed:(imageName)]
  • RGB颜色
1
#define ColoreWithRGB(r,g,b)        [UIColor colorWithRed:(CGFloat)r/255 green:(CGFloat)g/255 blue:(CGFloat)b/255 alpha:1]
  • 屏幕宽高
1
2
#define SCREEN_HEIGHT	([UIScreen mainScreen].bounds.size.height)
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
  • 单例宏定义
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//* 单例interface
#define CLASS_SINGLETON_INTERFACE(classname) \
\
+ (classname *)share##classname;

//* 单例implementation
#define CLASS_SINGLETON_IMPLEMENTATION(classname) \
static classname* shared##classname = nil; \
\
+ (classname *)share##classname \
{ \
@synchronized(self) \
{ \
if (shared##classname == nil) \
{ \
shared##classname = [[self alloc] init]; \
} \
} \
return shared##classname; \
} \

工具方法

  • Alert
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
CG_INLINE void AlertlogError (NSString* message)
{
static UIAlertView *alertView = nil;
if (!alertView)
{
alertView = [[UIAlertView alloc] initWithTitle: @""
message: message
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil,
nil];
[alertView show];
}
else if ([alertView isVisible])
{

[alertView dismissWithClickedButtonIndex:0 animated:NO];
#if !__has_feature(objc_arc)
[alertView release];
#else
#endif

alertView = nil;
alertView = [[UIAlertView alloc] initWithTitle: @""
message: message
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil,
nil];
[alertView show];
}
else
{
[alertView dismissWithClickedButtonIndex:0 animated:NO];
#if !__has_feature(objc_arc)
[alertView release];
#else
#endif
alertView = nil;
alertView = [[UIAlertView alloc] initWithTitle: @""
message: message
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil,
nil];
[alertView show];
}
}
  • URL编码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
CG_INLINE NSString* URLEncode(NSString *string)
{
#if !__has_feature(objc_arc)
NSString *newString = NSMakeCollectable([(NSString *)CFURLCreateStringByAddingPercentEscapes(
kCFAllocatorDefault,
(CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"),
CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)) autorelease]);
#else
NSString *newString = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,( __bridge CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"),CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
#endif
if (newString) {
return newString;
}
return string;
}

CG_INLINE NSString* URLDecode(NSString *string)
{
#if !__has_feature(objc_arc)

NSString *newString = NSMakeCollectable([(NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,(__bridge CFStringRef) string,CFSTR(""),CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)) autorelease]);
#else
NSString *newString = (__bridge NSString *) CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,(__bridge CFStringRef) string,CFSTR(""),CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));;
#endif
if (newString) {
return newString;
}
return string;
}
  • MD5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
CG_INLINE NSString* MD5FromString(NSString *sourceString)
{
const char *original_str = [sourceString UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(original_str, strlen(original_str), result);
NSMutableString *hash = [NSMutableString string];
for (int i = 0; i < 16; i++)
[hash appendFormat:@"%02X", result[i]];
return [hash lowercaseString];
}

CG_INLINE NSString *MD5ForFile(NSString *path)
{
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
if( handle== nil )
{
return nil;
}
CC_MD5_CTX md5;
CC_MD5_Init(&md5);
BOOL done = NO;
while(!done)
{
@autoreleasepool
{
NSData* fileData = [handle readDataOfLength:256];
CC_MD5_Update(&md5, [fileData bytes], [fileData length]);
if( [fileData length] == 0 ) done = YES;
fileData = nil;
}
}
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5_Final(digest, &md5);
NSString* s = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
digest[0], digest[1],
digest[2], digest[3],
digest[4], digest[5],
digest[6], digest[7],
digest[8], digest[9],
digest[10], digest[11],
digest[12], digest[13],
digest[14], digest[15]];
return s;
}