英语轻松读发新版了,欢迎下载、更新

Objective C: Blocks vs. Selectors vs. Protocols

freecunix 发布于 2013年09月10日 | 更新于 2013年09月10日
无人欣赏。

I think...

1: Informal protocol - I don't really see any advantage of doing this over a formal protocol.

2: Passing SELs - I don't think this is an established pattern in Cocoa. I personally wouldn't consider it as better than the delegate approach, but if it fits your thinking better, then go for it. You're not really getting rid of state; you're just transforming into something else. Personally, I'd prefer to have an ivar that I can set and check without having to use selector types.

3: Passing blocks - This is sort of a new-age approach, and it has some merit. I think you need to be careful though because, in my opinion, it doesn't scale really well. For example, if NSTableView's delegate and data source methods were all blocks, I would personally find that somewhat annoying. Imagine if you wanted to set 10 different blocks, your -awakeFromNib (or whatever) method would be pretty big. Individual methods seem more appropriate in this case. However, if you're sure that you're never going to go beyond, say, two methods, then the block approach seems more reasonable.

Welcome to discuss. :D

暂无回复
登录 或者 注册