T
T
tomzarubin2014-09-23 23:50:08
Objective-C
tomzarubin, 2014-09-23 23:50:08

Why do Objective-C need Selectors(@selector) in real life?

Hello.
I'm learning a language, the question arose - why do we need selectors? In the real life.
Well, that is, to pass the name of a method or to check whether an object supports a method—everything is clear with this. But, say, if we call a method on an object that does not support it, we will get an error 1 time at the compilation or execution stage. We'll fix it and it won't be there anymore. What case should happen so that we need to check if an object responds to a selector: if ([self respondsToSelector:selector]) ?
Share, please, cases from practice.
Thank you.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
Denis Morozov, 2014-09-24
@morozovdenis

1. When using protocols for delegates. The protocol has the ability to mark @optional methods and then the method does not have to be implemented in the class that implements the protocol. The class whose delegate it is already checks if the object matches the desired selector or not. if it
responds , then call it, if not, then default behavior

if ([target respondsToSelector:selector]) {
     [target performSelector:selector];
}

S
s0L, 2014-09-24
@s0L

It is far from always known the class of an object to statically determine the accessibility of a method. The id type is ubiquitous. Many more methods in the SDK take a selector as a parameter, this allows you to use objects of any class. Well, for example, methods for working with collections.

I
IbrahimKZ, 2014-09-24
@IbrahimKZ

Using a selector, you can call a method after a certain time, for example
Or when you want the ViewController to load faster and not wait for all the methods in viewDidLoad or others to complete when loading. First, we show that the transition took place on the ViewController, and then the most difficult methods can be called through the selectors. In general, there are many applications. Everything will come with practice.

N
Nikita Zinoviev, 2014-10-02
@zinovyev_n

With support for multiple versions of iOS, it is often necessary to check classes for method accessibility.

V
Vanya Ivanov, 2015-01-04
@mr_cloud

https://vk.com/videos-58860049?section=search&z=vi...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question