zoukankan      html  css  js  c++  java
  • 初窥runtime的作用

    运用到runtime需要导入相应的头文件

    #import <objc/runtime.h>

    1.获取私有成员变量名

        unsigned int count;
        
        //需要导入头文件:你要导出的私有成员的类
        Ivar *ivars = class_copyIvarList([UIButton class], &count);
        
        for (NSInteger i = 0; i < count; i ++) {
        
        Ivar ivar = ivars[i];
        
        const char *name = ivar_getName(ivar);
            
        NSString *ocname = [NSString stringWithUTF8String:name];
            
        NSLog(@"%ld====%@",i,ocname);
        
        }
        //释放内存指针
        free(ivars);
        ivars = NULL;

    打印的结果为:

    2016-04-06 10:53:17.389 ZhangHongAd[851:211282] 0====_externalFlatEdge
    
    2016-04-06 10:53:17.389 ZhangHongAd[851:211282] 1====_contentLookup
    
    2016-04-06 10:53:17.389 ZhangHongAd[851:211282] 2====_contentEdgeInsets
    
    2016-04-06 10:53:17.390 ZhangHongAd[851:211282] 3====_titleEdgeInsets
    
    2016-04-06 10:53:17.390 ZhangHongAd[851:211282] 4====_imageEdgeInsets
    
    2016-04-06 10:53:17.390 ZhangHongAd[851:211282] 5====_backgroundView
    
    2016-04-06 10:53:17.390 ZhangHongAd[851:211282] 6====_floatingContentView
    
    2016-04-06 10:53:17.390 ZhangHongAd[851:211282] 7====_contentBackdropView
    
    2016-04-06 10:53:17.392 ZhangHongAd[851:211282] 8====_imageView
    
    2016-04-06 10:53:17.392 ZhangHongAd[851:211282] 9====_titleView
    
    2016-04-06 10:53:17.392 ZhangHongAd[851:211282] 10====_initialized
    
    2016-04-06 10:53:17.392 ZhangHongAd[851:211282] 11====_lastDrawingControlState
    
    2016-04-06 10:53:17.392 ZhangHongAd[851:211282] 12====_selectGestureRecognizer
    
    2016-04-06 10:53:17.392 ZhangHongAd[851:211282] 13====_buttonFlags
    
    2016-04-06 10:53:17.392 ZhangHongAd[851:211282] 14====_effectiveContentView
    
    2016-04-06 10:53:17.393 ZhangHongAd[851:211282] 15====_maskAnimationView
    
    2016-04-06 10:53:17.393 ZhangHongAd[851:211282] 16====_selectionView
    
    2016-04-06 10:53:17.393 ZhangHongAd[851:211282] 17====_lazyTitleViewFont
    
    2016-04-06 10:53:17.393 ZhangHongAd[851:211282] 18====_contentConstraints
    
    2016-04-06 10:53:17.393 ZhangHongAd[851:211282] 19====_internalTitlePaddingInsets
    打印结果

    2.获取类的私有属性名

        objc_property_t *properties = class_copyPropertyList([UIButton class], &count);
        
        for (NSInteger i = 0; i < count; i ++) {
            
            objc_property_t property = properties[i];
            
            const char *name = property_getName(property);
            
            NSString *ocname = [NSString stringWithUTF8String:name];
            
            NSLog(@"%ld====%@",i,ocname);
            
        }
        //释放内存指针
        free(properties);
        properties = NULL;

    这个方法跟获取私有成员变量名差不多,直接上代码

    2016-04-06 11:06:29.451 ZhangHongAd[859:213999] 0====contentEdgeInsets
    2016-04-06 11:06:29.451 ZhangHongAd[859:213999] 1====titleEdgeInsets
    2016-04-06 11:06:29.451 ZhangHongAd[859:213999] 2====reversesTitleShadowWhenHighlighted
    2016-04-06 11:06:29.451 ZhangHongAd[859:213999] 3====imageEdgeInsets
    2016-04-06 11:06:29.451 ZhangHongAd[859:213999] 4====adjustsImageWhenHighlighted
    2016-04-06 11:06:29.451 ZhangHongAd[859:213999] 5====adjustsImageWhenDisabled
    2016-04-06 11:06:29.451 ZhangHongAd[859:213999] 6====showsTouchWhenHighlighted
    2016-04-06 11:06:29.451 ZhangHongAd[859:213999] 7====tintColor
    2016-04-06 11:06:29.452 ZhangHongAd[859:213999] 8====buttonType
    2016-04-06 11:06:29.452 ZhangHongAd[859:213999] 9====currentTitle
    2016-04-06 11:06:29.453 ZhangHongAd[859:213999] 10====currentTitleColor
    2016-04-06 11:06:29.453 ZhangHongAd[859:213999] 11====currentTitleShadowColor
    2016-04-06 11:06:29.453 ZhangHongAd[859:213999] 12====currentImage
    2016-04-06 11:06:29.453 ZhangHongAd[859:213999] 13====currentBackgroundImage
    2016-04-06 11:06:29.453 ZhangHongAd[859:213999] 14====currentAttributedTitle
    2016-04-06 11:06:29.453 ZhangHongAd[859:213999] 15====titleLabel
    2016-04-06 11:06:29.453 ZhangHongAd[859:213999] 16====imageView
    2016-04-06 11:06:29.453 ZhangHongAd[859:213999] 17====_currentImageColor
    2016-04-06 11:06:29.453 ZhangHongAd[859:213999] 18====_externalFlatEdge
    2016-04-06 11:06:29.453 ZhangHongAd[859:213999] 19====_wantsAccessibilityUnderline
    2016-04-06 11:06:29.453 ZhangHongAd[859:213999] 20====_contentConstraints
    2016-04-06 11:06:29.453 ZhangHongAd[859:213999] 21====_internalTitlePaddingInsets
    2016-04-06 11:06:29.455 ZhangHongAd[859:213999] 22====hash
    2016-04-06 11:06:29.455 ZhangHongAd[859:213999] 23====superclass
    2016-04-06 11:06:29.455 ZhangHongAd[859:213999] 24====description
    2016-04-06 11:06:29.455 ZhangHongAd[859:213999] 25====debugDescription
    打印结果

    获取了系统类的私有成员变量跟属性,我们就可以用KVC去改变系统的属性名,用自定义的控件覆盖系统的控件。

    3.获取类的全部方法名

    class_copyMethodList(__unsafe_unretained Class cls, unsigned int *outCount)

    4.获取类的全部协议名

    class_copyProtocolList(__unsafe_unretained Class cls, unsigned int *outCount)

  • 相关阅读:
    TCP/IP讲解
    Android开发的技术层次
    页面右下角弹出类似QQ或MSN的消息提示
    C# winform 自定义鼠标图标
    C#遍历指定文件夹中的所有文件
    C#操作Word
    关于数据绑定的一些小技巧
    Silverlight遍历本地文件夹
    ckeditor+ckfinder+syntaxhighlight实现上传和插入代码高亮(for .NET)
    wpf 动画效果
  • 原文地址:https://www.cnblogs.com/CrazySL/p/5358479.html
Copyright © 2011-2022 走看看