总结iOS 开发的几个常用代码

近期总结了一些常用的ios代码。

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

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

@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++一样,示例如下:

- (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;
}

2、iOS中URL编码

NSString* escapedURLString = [unescapedString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

或

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

3、判断iOS设备是否是iPad

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



 

你可能感兴趣的:(总结iOS 开发的几个常用代码)