最近积累的几个Objective-C知识点
1、__unused修饰符
__unused宏(事实上是attribute((unused))这个GCC的编译器属性,更多请参考mattt大神的博客)告诉编译器“如果我没用到这个变量,不要警告我”。
2、三元表达式的简写
?:是C中唯一一个三目运算符,用来替代简单的if-else语句,同时也是可以两元使用:1
2NSString *string = inputString ?: @"default";
NSString *string = inputString ? inputString : @"default"; // 等价
3、如果block的参数列表为空的话,相当于可变参数(不是void)。
4、@compatibility_alias:允许现有类有不同的名称做别名。比如PSTCollectionView使用@compatibility_alias来显著提高对UICollectionView的向后兼容的直接替换的使用体验。(具体可参考NSHipster的文章)
1 | // 允许代码使用UICollectionView如同它可以在iOS SDK 5使用一样。 |
5、attribute((constructor))
若函数被设定为constructor属性,则该函数会在main()函数执行之前被自动的执行
6、看到QQ群里讨论,GCD最多能创建多少线程
66=64(GCD线程池的最大值) + 主线程 + 其他随机非GCD线程。来源
7、在Darwin层建立Notification监听的方法
在锁屏和解锁的时候,iOS系统会发送通知,经过搜索,得知大概有下面3种通知:
(1)、com.apple.iokit.hid.displayStatus
锁屏后通知会发出消息,在屏幕变亮后,没有滑动解锁,系统也会发出该通知。
(2)、com.apple.springboard.lockstate
在系统锁屏和滑动解锁后,会发出该通知
(3)、com.apple.springboard.lockcomplete
锁屏后,发出该通知。这个通知总会在(2)通知后发出。
判断屏幕是否有锁的方法1
2
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, displayStatusChanged, CFSTR("com.apple.iokit.hid.displayStatus"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
1 | static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { |
3、宏定义
1 | #define nil __DARWIN_NULL |
nil用于表示空的实例对象
1 | #define Nil __DARWIN_NULL |
Nil用于表示空类对象