Objective-C Programming Summary (article 2) Type Determination

Source: Internet
Author: User

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")])

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.