类别和继承的区别和用途是什么? [英] What's the difference and use of categories and inheritance?

查看:93
本文介绍了类别和继承的区别和用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

目标-c中的继承与类别之间的差异

我什么时候应该使用子类?什么时候应该使用类别?

When should I use subclassing and when should I use categories?

推荐答案

要扩展功能时的子类由基数使用。

Subclass when you want to extend the functionality utilized by the base.

@interface MyObject: NSObject<SomeProtocol>

如果要为您可能无法控制的代码添加便捷方法,请添加类别。

Add a category when you want to add a convenience method to code you may not control.

@interface UIView (MyViewAdditions)

- (void)recursiveEnumerateSubviewsUsingBlock:(void (^)(UIView *view, BOOL *stop))block;

这篇关于类别和继承的区别和用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆