iOS/Objective-C 开发常用代码(1)

判断iOS设备是否是iPad

1
#define IS_IPAD ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] && [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)

iOS中URL编码

1
2
NSString* escapedURLString = [unescapedString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]


1
2
3
NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes
(NULL, (CFStringRef)yourtext, NULL,
(CFStringRef)@”!*’();:@&=+$,/?%#[]“, kCFStringEncodingUTF8);

iOS中不定参数(可变参数)的方法

常见于NSArray初始化方法中,比如:

1
2
3
4
5
@interface NSArray (NSArrayCreation)
+ (id)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
- (id)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
//...
@end

NS_REQUIRES_NIL_TERMINATION 是一个宏,用于编译时非nil结尾的检查。自定义不定参数的方法与C/C++一样,示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (id)initWithColumns: (NSString*)firstColumnName, ... {
if (self = [self init]) {
NSMutableArray* arrays = [NSMutableArray array];
va_list argList;
if (firstColumnName) {
[arrays addObject:firstColumnName];
va_start(argList, firstColumnName);
id arg;
while ((arg = va_arg(argList, id))) {
[arrays addObject:arg];
}
}
self.columnNames = [NSArray arrayWithArray:arrays];
}
return self;
}