Alicia's Blog

[LEAF Photo]作者


  • Home

  • Tags

  • Archives

Source Code Learning - UITableView-FDTemplateLayoutCell

Posted on 2015-07-12

actionSheet 避开 switch case 写法

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    SEL selectors[] = {
        @selector(insertRow),
        @selector(insertSection),
        @selector(deleteSection)
    };

    if (buttonIndex < sizeof(selectors) / sizeof(SEL)) {
        void(*imp)(id, SEL) = (typeof(imp))[self methodForSelector:selectors[buttonIndex]];
        imp(self, selectors[buttonIndex]);
    }
}

利用判断条件或者在循环中调用某个方法的方式如上

已知函数名,有两种方式调用函数,之前在 SDWebImage 解析 中总结过可以使用 performSelector 或者 methodForSelector 两个函数。

do {} while(0) 在宏定义中的使用

static void __FD_TEMPLATE_LAYOUT_CELL_PRIMARY_CALL_IF_CRASH_NOT_OUR_BUG__(void (^callout)(void)) {
    callout();
}
#define FDPrimaryCall(...) do {__FD_TEMPLATE_LAYOUT_CELL_PRIMARY_CALL_IF_CRASH_NOT_OUR_BUG__(^{__VA_ARGS__});} while(0)
Read more »

Source Code Learning - SDWebImage

Posted on 2015-07-04

之前看源码的时候,有很多东西研究的不够透彻,所以重新整理学习下

NSURLSession 工作模式

NSURLSessionConfiguration 分为三种工作模式
default: 可以使用磁盘缓存、身份认证、Cookie
ephemeral(及时模式): 数据只能保存到内存,无法存储到磁盘。
background: 可以在后台执行上传或下载任务

后进先出

后进先出通过 NSOperation 的依赖实现

if (wself.executionOrder == SDWebImageDownloaderLIFOExecutionOrder) {
        // Emulate LIFO execution order by systematically adding new operations as last operation's dependency
        [wself.lastAddedOperation addDependency:operation];
        wself.lastAddedOperation = operation;
}

initialize 方法

即使创建多个实例,initialize 只会执行一次,如果希望仅运行一次的话调用该方法。

#import "Animal.h";
@implementation Animal
+(void) initialize {
    NSLog(@"Animal initialize");
}
-(void) init {
    NSLog(@"Animal init");
}
@end
// In Test class
- (void)test {
    Animal *animal1 = [[Animal alloc] init];
    Animal *animal2 = [[Animal alloc] init];
}

输出

Animal initialize
Animal init
Animal init

一般某个全局状态无法在编译期初始化,可以放在 initialize 里面。而 SDWebImageDownloader 中实现 initialize 方法,用来注册观察者对象

Read more »

GCD

Posted on 2015-05-29

dispatch_sync / dispatch_async 同步与异步

dispatch_sync 提交 block 以供同步执行。这个接口会等到 block 执行结束才返回,所以不需要复制 block。

可以利用串行队列实现锁的功能。比如多线程写同一数据库,需要保持写入的顺序和每次写入的完整性

sync 和 async 区别
同步函数不论是并发队列还是串行队列,都不会开线程。一个任务开始后等待任务完成后返回
异步函数并发队列能开启多个线程,串行队列开启一个线程。异步方法会立即返回,不等待任务完成。

dispatch_queue_create 队列的创建或获取

GCD的所有队列都是先进先出的,它的队列主要分三种,主队列 main_queue、全局并发队列 global_queue、自定义队列
串行队列:队列中的任务必须在前一个任务结束后才能执行,包括 DISPATCH_QUEUE_SERIAL 和 主队列
并发队列:队列中的任务必须在前一个任务开始后才能执行,包括 DISPATCH_QUEUE_CONCURRENT 和 全局并发队列

Read more »

Macro

Posted on 2014-09-23

尺寸宏

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height) 

打印宏

#ifdef DEBUG
#   define DLog(args, ...) NSLog((@"%s - [Line %d] -- " args), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#   define DLog(...)
#endif

func, FUNCTION, PRETTY_FUNCTION 可以用来打印函数名。
PRETTY_FUNCTION 和 func, FUNCTION 稍微有点不同的是,它在 C/C++ 函数中会打印出方法签名(即方法名称和一个参数列表)。

如果觉得上面的方式输入的内容还不够丰富,建议使用 喵神的代码

#define NSLog(format, ...) do { \
    fprintf(stderr, "<%s : %d> %s\n",  \
    [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], \
    __LINE__, __func__);  \
    (NSLog)((format), ##__VA_ARGS__);   \
    fprintf(stderr, "-------\n");       \
} while (0)

系统宏

//检查系统版本
#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

颜色宏

//带有RGBA的颜色设置
#define RGBCOLORA(r, g, b, a) [UIColor colorWithRed:4/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]

单例宏

#define singleH(name) +(instancetype)share##name;

#if __has_feature(objc_arc)

#define singleM(name) static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
+(instancetype)share##name\
{\
return [[self alloc]init];\
}\
-(id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
\
-(id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}

#else
#define singleM static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
+(instancetype)shareTools\
{\
return [[self alloc]init];\
}\
-(id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
-(id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
-(oneway void)release\
{\
}\
\
-(instancetype)retain\
{\
return _instance;\
}\
\
-(NSUInteger)retainCount\
{\
return MAXFLOAT;\
}
#endif

Mobile Client Development Notice

Posted on 2014-01-15

这是一篇关于移动客户端开发中容易出现的 Bug 及问题以及产品方面的大杂烩。

修改代码时应当注意的问题

  • 代码是否可能存在重复,修复一个地方,其他的地方没有修复完全,要把重复的代码抽到公共的地方
  • 修改前和修改后是否功能是一致的没有出现变化。
  • 属性是否应该是只读的,一旦把只读变成非只读,要考虑修改了内存中常驻的变量时是否会有功能影响
  • 有时候写完一个功能测试下没问题,再写另一个功能时可能影响了之前的功能,所以再提测前要对所有功能重新走一遍完整的自测流程。

提交代码前的自测应检查的内容

总结下通用的测试用例

Read more »
1…45
Alicia

Alicia

45 posts
23 tags
GitHub E-Mail Twitter weibo
© 2020 Alicia
Powered by Hexo
|
Theme — NexT.Mist v5.1.4