// 添加一个监听者
- (void)addObserver {
// 1. 创建监听者
/**
* 创建监听者
*
* @param allocator#> 分配存储空间
* @param activities#> 要监听的状态
dispatch_async(dispatch_get_global_queue(0, 0), ^{
_link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
// NOTE: 子线程的runloop默认不创建; 在子线程获取 currentRunLoop 对象的时候,就会自动创建RunLoop
// 这里不加到 main loop,必须创建一个 runloop
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
[_link addToRunLoop:runloop forMode:NSRunLoopCommonModes];
// 必须 timer addToRunLoop 后,再run
[runloop run];
});
// this is the cell height cache; obviously you don't want a static size array in production
var itemHeights = [CGFloat](count: 1000, repeatedValue: UITableViewAutomaticDimension)
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if itemHeights[indexPath.row] == UITableViewAutomaticDimension {
itemHeights[indexPath.row] = cell.bounds.height
}
// fetch more cells, etc.
}
- (instancetype)initWithRequest:(NSURLRequest *)request
options:(YYWebImageOptions)options
cache:(nullable YYImageCache *)cache
cacheKey:(nullable NSString *)cacheKey
progress:(nullable YYWebImageProgressBlock)progress
transform:(nullable YYWebImageTransformBlock)transform
completion:(nullable YYWebImageCompletionBlock)completion NS_DESIGNATED_INITIALIZER;
- (instancetype)init UNAVAILABLE_ATTRIBUTE;
//实现
/// Network thread entry point.
+ (void)_networkThreadMain:(id)object {
@autoreleasepool {
[[NSThread currentThread] setName:@"com.xxx.xxx"];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
[runLoop run];
}
##Creating a timer with Grand Central Dispatch
At the following is the implementation file of a sample class that shows, how to make a timer with the help of Grand Central Dispatch. The timer fires on a global queue, just change the queue to the main queue or any custom queue and the timer fires on this queue and not on the global queue anymore.
#import <Foundation/Foundation.h>
@interface SampleClass : NSObject
- (void)startTimer;
NSOptation 是一个抽象类,主要使用NSOpration的两个子类NSBlockOperation, NSInvocationOperation
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"block 1 start");
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
button.frame = CGRectMake(0, 0, 50, 50);
button.tag = 1001;
[button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
//
// MenuChartsViewContainerController.m
// CardApp
//
// Created by tailang on 6/22/16.
// Copyright © 2016 2dfire.com. All rights reserved.
//
参考:
1.https://github.com/objc-zen/objc-zen-book
2.http://www.jianshu.com/p/c150f8fa4d21
3.https://github.com/QianKaiLu/Objective-C-Coding-Guidelines-In-Chinese
###基本
- 将Tab制表符设置为4个空格
- 每行长度最好能控制在80左右(非强制)
###条件语句
NewerOlder