Objective-C provides a good type judgment and reflection mechanism. The foundation of OC isC LanguageAll objects are represented by pointers. The actual type information of a pointer may be blurred during transmission. You can use type queries to solve many programming problems. In addition, method calls in OC are message-based. It is also a common function to query whether a type contains a message response.
For example, an nsurlrequest object is obtained during HTTP network communication. When writing header information, you need to check whether it is mutablerequest:
If ([req ismemberofclass: [nsmutableurlrequest class])
Or
If ([req iskindofclass: [nsmutableurlrequest class])
The difference between the two statements is:
1. If the ismemberofclass type is identical, true is returned. If the instance is a subclass of the type, false is returned.
2. Determine whether iskindofclass contains sub-classes.
Obtain an nsurlresponse. to read the response header field, first determine whether it contains the allheaderfields method:
If ([Response responsestoselector: @ selector (allheaderfields)])
You can also write
If ([[response class] instancesrespondtoselect: @ selector (allheaderfields)]).
If you need to know whether the instance has implemented an interface, use:
If ([Request conformstoprotocol: nsprotocolfromstring (@ "nscopying")])