有没有一种方法来动态确定在可可/可可触摸运行时类的实例变量?(Is there a way to d

2019-09-01 14:10发布

有没有一种方法,以获得像一类的所有键值对的字典?

Answer 1:

你必须使用推出自己的Objective-C运行时函数 。 下面是一些很基本的示例代码。 需要注意的是得到一个类的实例变量没有得到它的超类的实例变量。 你需要做的是明确的,但功能都没有在运行。

#import <objc/objc-runtime.h>
#include <inttypes.h>
#include <Foundation/Foundation.h>

@interface Foo : NSObject
{
    int i1;
}
@end
@implementation Foo
@end

@interface Bar : Foo
{
    NSString* s1;
}

@end
@implementation Bar
@end

int main(int argc, char** argv)
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    unsigned int count;
    Ivar* ivars = class_copyIvarList([Bar class], &count);
    for(unsigned int i = 0; i < count; ++i)
    {
        NSLog(@"%@::%s", [Bar class], ivar_getName(ivars[i]));
    }
    free(ivars);


    [pool release];
}


Answer 2:

我不知道只是高德,但如果你让他们定义的属性就可以访问类的可用属性。

我一直在使用SQLitePersistentObjects了几个项目,它提供了一些有用的代码,得到的类中定义搞清楚系列化,并从源码时使用的属性。

它使用功能class_copyPropertyList工作再上一个类属性的可用列表。

进一步来说:

+(NSDictionary *)propertiesWithEncodedTypes
{

    // DO NOT use a static variable to cache this, it will cause problem with subclasses of classes that are subclasses of SQLitePersistentObject

    // Recurse up the classes, but stop at NSObject. Each class only reports its own properties, not those inherited from its superclass
    NSMutableDictionary *theProps;

    if ([self superclass] != [NSObject class])
        theProps = (NSMutableDictionary *)[[self superclass] propertiesWithEncodedTypes];
    else
        theProps = [NSMutableDictionary dictionary];

    unsigned int outCount;


    objc_property_t *propList = class_copyPropertyList([self class], &outCount);
    int i;

    // Loop through properties and add declarations for the create
    for (i=0; i < outCount; i++)
    {
        objc_property_t * oneProp = propList + i;
        NSString *propName = [NSString stringWithUTF8String:property_getName(*oneProp)];
        NSString *attrs = [NSString stringWithUTF8String: property_getAttributes(*oneProp)];
        NSArray *attrParts = [attrs componentsSeparatedByString:@","];
        if (attrParts != nil)
        {
            if ([attrParts count] > 0)
            {
                NSString *propType = [[attrParts objectAtIndex:0] substringFromIndex:1];
                [theProps setObject:propType forKey:propName];
            }
        }
    }

  free(propList);

    return theProps;    
}

这将返回属性的字典 - 你需要做的,你回来的结果的一些调查,但你应该能够得到你所需要的,如果你正在使用的特性。



Answer 3:

是的,完全有可能:

int numIvars = 0;
Ivar * ivars = class_copyIvarList([anInstanceOfAClass class], &numIvars);
NSMutableDictionary * pairs = [NSMutableDictionary dictionary];
for (int i = 0; i < numIvars; ++i) {
  Ivar ivar = ivars[i];
  NSString * ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];
  id ivarValue = [anInstanceOfAClass valueForKey:ivarName];
  [pairs setObject:ivarValue forKey:ivarName];
}
free(ivars);
NSLog(@"%@", pairs);


文章来源: Is there a way to dynamically determine ivars of a class at runtime in Cocoa / Cocoa Touch?