__kindof 与 __typeof

How to use __kindof in Objective-C?

Suppose we have this property:

The developer reading this code will have an idea that the array must contain UIViews, but it is not guaranteed that it will really contain UIViews, until he reads through all of the code and verifies that indeed only UIViews were inserted to the array. This case can be resolved using Objective-C’s new features, namely the Generics and __kindof.

Using Generics, we can declare the property as the following instead.

Anyone who reads this code will understand the array must contain UIViews. But the catch is, the array must contain only UIViews, but not instances of subclasses of UIViews such as UIWebViews or UIButtons. Trying to insert a non-UIWebView objectc will result to a compiler warning. This problem can be resolved using__kindof.

Using this construct, viewCollection can contain instances of UIViews and its subclasses such asUIWebViews and UIButtons.

Developers should use the new features of Objective-C if given the chance. The new __kindof construct will surely help developers improve their code by being specific with their intents. The Generics construct and__kindof seems strict but I think this is for the good of the developers and the apps that they are making.

=============================================

block对于其变量都会形成strong reference,对于self也会形成strong reference ,而如果self本身对block也是 strong reference 的话,就会形成 strong reference 循环,造成内存泄露,为了防止这种情况发生,在block外部应该创建一个week(__block) reference。


所以在block内如果有self的话,一般都会在block外面加一句_block typeof(self)bself = self;

__block typeof(self) bself = self;  
  
[self methodThatTakesABlock:^ {  
  
    [bself doSomething];  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值