2016-04-26 4 views
1

私はdtoという名前のオブジェクトを持っています。このオブジェクトは別のものの子であり、彼の父の最高クラスはNSObjectです。私はdtoとそのすべてのスーパークラスのすべてのプロパティを取得したいと思います。実際に私は私のbucleのpropertyNameを見たいときにEXC_BAD_ACCESSを取得します。どうもありがとうございました!objc_property_tを連結する

unsigned int i, superPropertyCount, propertyCount; 
objc_property_t *properties = class_copyPropertyList([dto class], &propertyCount); 

if ([[dto class] isSubclassOfClass:[NSObject class]]) 
{ 
    memcpy(properties, properties, propertyCount * sizeof(objc_property_t)); 
    id superClass = [[dto class] superclass]; 
    while ([superClass isSubclassOfClass:[NSObject class]]) 
    { 
     objc_property_t *superProperties = class_copyPropertyList(superClass, &superPropertyCount); 

     properties = malloc((propertyCount + superPropertyCount) * sizeof(objc_property_t)); 

     if (properties != NULL) 
     { 
      memcpy(properties, properties, propertyCount * sizeof(objc_property_t)); 
      memcpy(properties+propertyCount, superProperties, superPropertyCount * sizeof(objc_property_t)); 
     } 
     propertyCount = propertyCount + superPropertyCount; 
     superClass = [superClass superclass]; 
    } 
} 

for (i = 0; i < propertyCount; i++) 
{ 
    objc_property_t property = properties[i]; 
    NSString *propertyName = [NSString stringWithUTF8String: property_getName(property)]; 
} 
+2

ランタイムの深さを測るには非常に面白いですが、この種のイントロスペクションは生産コードでは避けなければなりません。それは脆弱で蔓延している。 Objective-Cは、完全に動的なランタイム中心の言語ではありません。 – bbum

答えて

3

あなたは、コードの

properties = malloc((propertyCount + superPropertyCount) * sizeof(objc_property_t)); 

次の行でプロパティでポインタを失うされています

memcpy(properties, properties, propertyCount * sizeof(objc_property_t)); 

は影響しません。

BTW。割り当てられたメモリをclass_copyPropertyListmallocで解放することを忘れないでください。

+0

ありがとうございます! :) – Patrick

関連する問題