何时使用像(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.

何时使用像(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.