前言:有些代码偶尔用一次根本记不住,每次都要搜一下,在此做个记录。
1.以字符串的形式输出frame
NSLog(@"%@",NSStringFromCGRect(self.view.frame));
2.URL中有汉字的UTF-8转码
NSString*encodedString=[urlStrstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
3.weak修饰block
__weak typeof(self) weakSelf =self;
4.粘贴板
UIPasteboard*pasteboard= [UIPasteboardgeneralPasteboard];
pasteboard.string=@"赋值粘贴";
5.枚举的写法
typedef enum{
Spring =0,//春天
Summer =1,//夏天
Autumn =2,//秋天
Winter =3//冬天
} SeaconState;
6. NSLog打印控制
#ifdef DEBUG
#define NSLog(...) NSLog(__VA_ARGS__)
#define debugMethod() NSLog(@"%s", __func__)
#else
#define NSLog(...)
#define debugMethod()
#endif
7.含有%号的url的转码
NSString*encodeUrl = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)url,(CFStringRef)@"!{{TEXTPLACEHOLDER}}'()*+,-./:;=?@_~%#[]",NULL,kCFStringEncodingUTF8));
8.block
作为一个变量
returnType(^blockName)(parameterTypes) = ^returnType(parameters) {...};
作为一个属性
@property (nonatomic, copy,nullability)returnType(^blockName)(parameterTypes);
作为一个函数的参数
- (void)someMethodThatTakesABlock:(returnType(^nullability)(parameterTypes))blockName;
方法调用
[someObject someMethodThatTakesABlock:^returnType(parameters) {...}];
typedef
typedef returnType(^TypeName)(parameterTypes);
TypeNameblockName = ^returnType(parameters) {...};
网友评论