This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface People : NSObject | |
- (People *(^)())run; | |
- (People *(^)())study; | |
- (People *(^)(NSString* name))name; | |
@end | |
@implementation People | |
- (People *(^)())run{ | |
return ^{ | |
NSLog(@"run"); | |
return self; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///前言 | |
// 1)实际上 RunLoop 就是这样一个函数CFRunLoopRunSpecific,其内部是一个 do-while 循环。 | |
// 2)当你调用 CFRunLoopRun() 时,线程就会一直停留在这个循环里;直到超时或被手动停止,该函数才会返回。 | |
///一、 用DefaultMode启动 | |
void CFRunLoopRun(void) { | |
CFRunLoopRunSpecific(CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, 1.0e10, false); | |
} | |
///二、 用指定的Mode启动,允许设置RunLoop超时时间 | |
int CFRunLoopRunInMode(CFStringRef modeName, CFTimeInterval seconds, Boolean stopAfterHandle) { | |
return CFRunLoopRunSpecific(CFRunLoopGetCurrent(), modeName, seconds, returnAfterSourceHandled); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//获取当前的currentRunLoop,设置sourceCtx;运行runLoop--- | |
//一、source0的例子: 只包含了一个回调(函数指针),它并不能主动触发事件。 | |
// 1)使用时,你需要先调用 `CFRunLoopSourceSignal(source)`,将这个 Source 标记为待处理, | |
// 2)然后手动调用 `CFRunLoopWakeUp(runloop)` 来唤醒 RunLoop,让其处理这个事件perform。 | |
- (void) knrun { | |
@autoreleasepool { | |
//获取当前的NSRunLoop | |
self.runloop = [NSRunLoop currentRunLoop];// | |
//构造CFRunLoopSourceContext数据结构,以便创建CFRunLoopSourceRef | |
CFRunLoopSourceContext sourceCtx = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 全局的Dictionary,key 是 pthread_t, value 是 CFRunLoopRef | |
static CFMutableDictionaryRef loopsDic;//线程和 RunLoop 之间是一一对应的,其关系是保存在一个全局的 Dictionary 里 | |
/// 访问 loopsDic 时的锁 | |
static CFSpinLock_t loopsLock; | |
/// 获取一个 pthread 对应的 RunLoop。 | |
CFRunLoopRef _CFRunLoopGet(pthread_t thread) { | |
OSSpinLockLock(&loopsLock); | |
if (!loopsDic) {//线程刚创建时并没有 RunLoop,如果你不主动获取,那它一直都不会有 | |
// 第一次进入时,初始化全局Dic,并先为主线程创建一个 RunLoop。 | |
loopsDic = CFDictionaryCreateMutable(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define HMAppImageFile(url) [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[url lastPathComponent]] | |
#import "HMAppsViewController.h" | |
#import "HMApp.h" | |
@interface HMAppsViewController () | |
/** | |
* 所有的应用数据 | |
*/ | |
@property (nonatomic, strong) NSMutableArray *apps; | |
/** | |
* 存放所有下载操作的队列 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 单例模式的作用 | |
可以保证在程序运行过程,一个类只有一个实例,而且该实例易于供外界访问;从而方便地控制了实例个数,并节约系统资源 | |
// 单例模式的使用场合 | |
在整个应用程序中,共享一份资源(这份资源只需要创建初始化1次) | |
//1、 单例模式在ARC\MRC环境下的写法有所不同,需要编写2套不同的代码 | |
// 1)可以用宏判断是否为ARC环境 | |
#if __has_feature(objc_arc) | |
// ARC | |
#else |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//0、 GCD的队列可以分为2大类型 | |
// 1)并发队列(Concurrent Dispatch Queue) | |
可以让多个任务并发(同时)执行(自动开启多个线程同时执行任务);并发功能只有在异步(dispatch_async)函数下才有效 | |
// 2)串行队列(Serial Dispatch Queue) | |
让任务一个接着一个地执行(一个任务执行完毕后,再执行下一个任务) | |
//1、 容易混淆的术语:有4个术语比较容易混淆:同步、异步、并发、串行 | |
//1) 同步和异步主要影响:能不能开启新的线程 | |
同步:在当前线程中执行任务,不具备开启新线程的能力 | |
异步:在新的线程中执行任务,具备开启新线程的能力 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 方式一: 使用open | |
# open -b com.sublimetext.3 ${postName} | |
# Opens with the specified application bundle identifier. com.uranusjr.macdown | |
open -b com.uranusjr.macdown ${postName} | |
# 方式二: 使用 /usr/local/bin/macdown,前提是设置Macdown的terminal属性 | |
devzkndeMacBook-Pro:kunnan.github.io.git devzkn$ macdown _posts/GithubPages/2018-05-04-Github_Pages_Useful_Tool.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//可以用于日志记录和 Mock 测试。例如上报用户打开的界面所在VC的名称,就可以使用swizzling 统一处理 | |
void Swizzle(Class c, SEL orig, SEL new) { | |
Method origMethod = class_getInstanceMethod(c, orig); | |
Method newMethod = class_getInstanceMethod(c, new); | |
if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))//给一个方法添加新的方法和实现;Adds a new method to a class with a given name and implementation. | |
//若返回Yes说明类中没有该方法,然后再使用 `class_replaceMethod()` 方法进行取代 | |
class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));//取代了对于一个给定类的实现方法 | |
else | |
//YES if the method was added successfully, otherwise NO (for example, the class already contains a method implementation with that name). | |
method_exchangeImplementations(origMethod, newMethod);//交换两个类的实现方法 |
NewerOlder