Use runtime to get Apple private properties

Source: Internet
Author: User

Case

  -When we use the navigation bar controller Uinavgationcontroller, and when using the System return button, the system will also have a uiscreenedgepangesturerecognizer gesture, And often sometimes we customize a nice back button, and then we need to customize a button and add a gesture. It's easy, isn't it? Yes, but what should you do with the target and @selector of your gestures with Apple? That's what we're going to do today.

  

Steps

1. By printing, we can see the value of the Action,target, but how to remove the target?

-KVC: Prerequisite: Must know property name

-Run time

2. To use the runtime, you must first import a header file objc/runtime.h

#import <objc/runtime.h>

3. Get the property name using runtime

-Traverse all properties within a class Ivar: Represents member properties

-Copyivarlist can only get the property under which class, and does not go out of bounds (the property of its parent class is not traversed)

-Class gets the member properties of which classes

-Count: Tells you the total number of member properties within the current class

UnsignedintCount =0; //returns an array of member propertiesIvar *ivars = Class_copyivarlist ([uigesturerecognizerclass], &count);  for(inti =0; I < count; i++) {        //Remove member VariableIvar Ivar =Ivars[i]; //Get Property nameNSString *ivarname =@ (Ivar_getname (Ivar)); NSLog (@"%@", Ivarname); }

4. Print each member property, you can see _targets this looks like a property, next we to verify.

5. Get the value of this key, by printing the discovery system rewrite the descriptions property, we do not get the desired properties, however, there is no egg, we can get through the breakpoint.


Gets the value of this property
Nsarray *targets = [Self.interactivepopgesturerecognizer valueforkeypath:@ "_targets" ];
ID OBJC = [targets firstobject]; NSLog (@ "%@", OBJC); ID target = [OBJC Valueforkeypath:@ "_target"];

-It's not what we want.

-OK, that's what we want, to successfully get _target this property

-print target, that's what we want.

Conclusion: All properties under the current class can be removed by the runtime. I'm going to talk about it today, and next time I'll write about how to do the dictionary-to-model through the runtime, because sometimes our dictionary key is sometimes a keyword, so we may not be able to define it directly as a property, as detailed below.

Use runtime to get Apple private properties

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.