zoukankan      html  css  js  c++  java
  • o_c runtime

    When you send a message like [NSObject alloc] you are actually sending a message to the class object and that class object needs to be an instance of the MetaClass which itself is an instance of the root meta class

    The basic implementation of a class in Objective-C looks like 

    interface MyClass : NSObject {

    //vars

    NSInteger counter;

    }

    //methods

    -voiddoFoo;

    end

    but the runtime has more than that to keep track of 

    if !__OBJC2__

        Class super_class                                        OBJC2_UNAVAILABLE;

        const char *name                                         OBJC2_UNAVAILABLE;

        long version                                             OBJC2_UNAVAILABLE;

        long info                                                OBJC2_UNAVAILABLE;

        long instance_size                                       OBJC2_UNAVAILABLE;

        struct objc_ivar_list *ivars                             OBJC2_UNAVAILABLE;

        struct objc_method_list **methodLists                    OBJC2_UNAVAILABLE;

        struct objc_cache *cache                                 OBJC2_UNAVAILABLE;

        struct objc_protocol_list *protocols                     OBJC2_UNAVAILABLE;

    endif

    Objective-C Associated Objects
    One thing recently introduced in Mac OS X 10.6 Snow Leopard was called Associated References. Objective-C has no support for dynamically adding on variables to objects unlike some other languages that have native support for this. So up until now you would have had to go to great lengths to build the infrastructure to pretend that you are adding a variable onto a class. Now in Mac OS X 10.6 the Objective-C Runtime has native support for this. If we wanted to add a variable to every class that already exists like say NSView we could do so like this... 

    import < Cocoa/Cocoa.h> //Cocoa

    include < objc/runtime.h> //objc runtime api’s

    interface NSView CustomAdditions

    propertyretain NSImage *customImage;

    end

    implementation NSView CustomAdditions

    static char img_key; //has a unique address identifier

    -NSImage *customImage

    {

        return objc_getAssociatedObjectself&img_key;

    }

    -voidsetCustomImage:NSImage *image

    {

        objc_setAssociatedObjectself&img_keyimage

                                 OBJC_ASSOCIATION_RETAIN;

    }

  • 相关阅读:
    交换排序------冒泡法 及其优化
    [编程题]最大和子矩阵
    [编程题] N阶楼梯上楼问题
    2017年东北大学计算机专业考博 面试编程题(某教授 实验室)
    幸运的袋子 (牛客网 16年网易内推 编程题)
    OpenvSwitch代码分析之bridge和port
    阅读书籍---程序员必读系列
    嵌入式开发之davinci--- 8168 电源调试总结
    嵌入式开发之davinci--- 8148/8168/8127 中的添加算饭scd 场景检测 文档简介
    嵌入式开发之davinci--- 8148/8168/8127 中的图像缩放sclr、swms之后出现图像视频卡顿、屏幕跳跃的问题
  • 原文地址:https://www.cnblogs.com/zengyou/p/2766204.html
Copyright © 2011-2022 走看看