何时使用像(id )self?(When to use something like (id)self?)

编程入门 行业动态 更新时间:2024-10-23 13:34:06
何时使用像(id )self?(When to use something like (id)self?)

我已经在我正在使用的一些代码领域看到了这一点。

searchBar.delegate = (id<UISearchBarDelegate>)self;

我的问题是...

为什么不通过在类的接口中添加<UISearchBarDelegate>来使当前类成为UISearchBar的委托?

有时候上面的代码比让班级成为代表更好吗?

这两种方式都适用于我,但我想知道为什么我可能想使用其中一种。

I've seen this in some areas of code that I am working with.

searchBar.delegate = (id<UISearchBarDelegate>)self;

My question is...

Why not just make the current class a delegate of UISearchBar by adding <UISearchBarDelegate> in the interface of the class?

Are there times when the above code is better than having the class be a delegate?.

Both ways work for me, but I'd like to learn why I may want to use one over the other.

更多推荐

本文发布于:2023-08-07 21:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465992.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:id

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!