简体   繁体   English

当我们可以使用子类时,为什么我们需要类别? 当我们可以使用函数时,为什么我们需要块?

[英]Why do we needed category when we can use a subclass? and Why we needed blocks when we can use functions?

These two questions are quite common when we search it but yet I need to get a satisfying answer about both.When ever we search a difference between say subclass and a category we actually get definition of both not the difference .I went to an interview to a very good MNC working on iOS and I was encountered with these two questions and I gave almost all the answers I have read here but the interviewer was not satisfied.He stuck to his questions and was that-这两个问题在我们搜索时很常见,但我需要得到一个令人满意的答案。当我们搜索说子类和类别之间的差异时,我们实际上得到了两者的定义而不是差异。我去采访一个非常优秀的跨国公司在 iOS 上工作,我遇到了这两个问题,我给出了我在这里读到的几乎所有答案,但面试官并不满意。他坚持他的问题,是 -

  1. Why do we needed category when we can use a subclass?当我们可以使用子类时,为什么我们需要类别?

  2. Why we needed blocks when we can use functions?当我们可以使用函数时,为什么我们需要块?

So please explain me what specific qualities blocks and category add in objective C that their counter part can't do.所以请解释一下他们的对应部分无法做到的目标C中添加的特定品质块和类别。

First... 第一...

Just reading the documentation "Subclassing Notes" for NSString shows why creating categories is sometimes better than subclassing. 只需阅读NSString文档“ Subclassing Notes”,就可以知道为什么创建类别有时比子类更好。

If you wanted to add a function -(void)reverseString (for instance) to NSString then subclassing it is going to be a massive pain in comparison to categories. 如果您想向NSString添加函数-(void)reverseString (例如),则子类化与类别相比将是一个巨大的痛苦。

Second... 第二...

Blocks are useful for capturing scope and context. 块对于捕获范围和上下文很有用。 They can also be passed around. 它们也可以传递。 So you can pass a block into an asynchronous call which then may be passed elsewhere. 因此,您可以将一个块传递到一个异步调用中,然后可以将其传递到其他地方。 TBH you don't care where the block is passed or where it is finally called from. TBH不管您在哪里传递块或从哪里最终调用它。 The scope captured at the time of creating the block is captured too. 创建块时捕获的范围也被捕获。

Yes, you can use methods too. 是的,您也可以使用方法。 But they both have different uses. 但是它们都有不同的用途。

Your questions are a bit odd. 您的问题有点奇怪。 It's like asking... 就像问...

Why do hammers exist when we can just use wrenches? 当我们只能使用扳手时,为什么会有锤子存在?

  1. You can't use subclassing when someone else is creating the objects. 当其他人创建对象时,不能使用子类化。 For instance, NSString is returned from hundreds of system APIs, and you can't change them to return MyImprovedString . 例如, NSString是从数百个系统API中返回的,您不能将它们更改为返回MyImprovedString

  2. Functions split up the logic; 函数将逻辑分开; blocks allow you to write it closer together. 块使您可以更紧密地书写它。 Like: 喜欢:

    [thing doSomethingAndWhenFinishedDo: ^{ some_other_thing; }];

the same code written with functions would put the second part of the logic several lines away in the file. 用函数编写的相同代码会将逻辑的第二部分放在文件中几行之遥。 If you have a few nested scopes in your logic then blocks can really clean it up. 如果逻辑中有一些嵌套作用域,则块可以真正清除它。

Why do we needed category when we can use a subclass? 当我们可以使用子类时,为什么需要分类?

Categories let you expand the API of existing classes without changing their type. 类别使您可以扩展现有类的API,而无需更改其类型。 Subclassing does the same thing but introduces a new type. 子类化做同样的事情,但是引入了一种新类型。 Additionally subclassing lets you add state. 另外,子类化可让您添加状态。

Why we needed blocks when we can use functions? 为什么可以使用函数时需要块?

Block objects are a C-level syntactic and runtime feature. 块对象是C级语法和运行时功能。 They are similar to standard C functions, but in addition to executable code they may also contain variable bindings to automatic (stack) or managed (heap) memory. 它们类似于标准C函数,但是除了可执行代码外,它们还可能包含到自动(堆栈)或托管(堆)内存的变量绑定。 A block can therefore maintain a set of state (data) that it can use to impact behavior when executed. 因此,一个块可以维护一组状态(数据),当执行时它可以用来影响行为。

You can use blocks to compose function expressions that can be passed to API, optionally stored, and used by multiple threads. 您可以使用块来构成函数表达式,这些函数表达式可以传递给API,可以选择存储并由多个线程使用。 Blocks are particularly useful as a callback because the block carries both the code to be executed on callback and the data needed during that execution 块作为回调特别有用,因为该块既包含要在回调上执行的代码,又包含执行期间所需的数据

Category : It is used if we want to add any method on a given class whose source is not known. Category:如果我们要在给定类中添加任何方法而其来源未知,则使用它。 This is basically used when we want to alter the behaviour of any Class. 当我们要更改任何类的行为时,基本上使用此方法。

For example : If we want to add a method on NSString to reverse a string we can go for categories. For example :如果我们想在NSString上添加一个方法来反转字符串,我们可以使用类别。

Subclassing : If we want to modify state as well as behaviour of any class or override any methods to alter the behaviour of the parent class then we go for subclassing. 子类化:如果我们想修改状态以及任何类的行为或重写任何方法来更改父类的行为,那么我们就进行子类化。

For example : We subclass UIView to alter its state and behaviour in our iOS code. For example :我们将UIView子类化,以在iOS代码中更改其状态和行为。

Reference : 参考:

When to use categories and when to use subclassing? 何时使用类别以及何时使用子类别?

What is the difference between inheritance and Categories in Objective-C Objective-C中的继承和类别有什么区别

  • We need new method but we don't need new class so we need category.我们需要新方法但我们不需要新的 class 所以我们需要类别。

  • We need function but we don't need named function so we need block.我们需要 function 但我们不需要命名 function 所以我们需要块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM