2016-04-19 6 views
0

私は以下のようにカスタムクラスを持っている:私は(ObjAreaある)プロパティOBJAののObjectTypeの名前を取得したい多分NSArrayのObjectTypeの名前を取得できますか?

@interface TestObject : NSObject 
    @property (nonatomic, retain) NSArray<ObjA *> *obja; 
    @property (nonatomic, retain) NSString *status; 
    @property (nonatomic, retain) NSString *keyword; 
    @property (nonatomic, retain) ObjB *objb; 
@end 

、それだけにNSArrayを返します。私は他の物件の階級の名前を持っていました。objbが含まれています。実行時にを取得するにはどうすればいいですか?私は関数のリストプロパティを取得し、それらのクラス(カテゴリ)このようなNSObjectを拡張します。

- (NSArray *)propertyList { 
    Class currentClass = [self class]; 
    NSMutableArray *propertyList = [[NSMutableArray alloc] init]; 
    do { 
     unsigned int outCount, i; 
     objc_property_t *properties = class_copyPropertyList(currentClass, &outCount); 

     for (i = 0; i < outCount; i++) { 
      objc_property_t property = properties[i]; 

      NSString *propertyName = [NSString stringWithFormat:@"%s", property_getName(property)]; 
      [propertyList addObject:propertyName]; 
     } 
     free(properties); 
     currentClass = [currentClass superclass]; 
    } while ([currentClass superclass]); 

    return propertyList; 
} 

と:

- (Class) classOfPropertyNamed:(NSString*)keyPath { 
    Class class = 0; 

    unsigned int n = 0; 
    objc_property_t* properties = class_copyPropertyList(self.class, &n); 
    for (unsigned int i=0; i<n; i++) { 
     objc_property_t* property = properties + i; 
     NSString* name = [NSString stringWithCString:property_getName(*property) encoding:NSUTF8StringEncoding]; 
     if (![keyPath isEqualToString:name]) continue; 

     const char* attributes = property_getAttributes(*property); 
     if (attributes[1] == '@') { 
      NSMutableString* className = [NSMutableString new]; 
      for (int j=3; attributes[j] && attributes[j]!='"'; j++) 
       [className appendFormat:@"%c", attributes[j]]; 
      class = NSClassFromString(className); 
     } 
     break; 
    } 
    free(properties); 

    return class; 
} 
+0

質問はあまりにも答えが分かりません。 – trojanfoe

+0

@trojanfoe:更新に関する質問がありました。あなたは理解することを望みます。サポートしてくれてありがとう。 – dleviathan

+0

私はあなたが何を望んでいるのか分からない。 – trojanfoe

答えて

0

あなただけ

myCustomObject = [yourArray objectAtIndex:someIndex]; 
myCUstomObject.name // Then do what ever you want with it 

を呼び出すか、あなたはこのようにタイプにNSArrayのカスタムオブジェクトを作成することができ、その後、アレイにあなたのカスタムオブジェクトを設定する必要があります。 enter image description hereを これを行う: enter image description here 次に、y私たちのカスタム配列

関連する問題