2016-04-19 12 views
0

Ich habe benutzerdefinierte Klasse wie folgt:Vielleicht Name von ObjectType in NSArray?

@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 

Ich möchte Objecttype Name der Immobilie objA (die ObjArea ist), aber es nur NSArray zurück. Ich hatte Name der Klasse anderer Eigenschaft erhalten, einschließlich objb. Wie kann ich es in Laufzeit bekommen? Ich habe Funktion get list Eigenschaft und Klasse von ihnen erweitern (Kategorie) NSObject wie folgt.

- (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; 
} 

und:

- (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

Die Frage ist zu verwirrend zu beantworten. – trojanfoe

+0

@trojanfoe: Ich hatte Update-Frage. Hoffe du verstehst. Danke für die Unterstützung. – dleviathan

+0

Nein. Ich habe keine Ahnung, was du willst. – trojanfoe

Antwort

0

Sie gerade Sie in dem Array benutzerdefinierte Objekt gesetzt sollte dann rufen

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

oder Sie können ein benutzerdefiniertes Objekt vom Typ NSArray wie folgt erstellen: enter image description here dann tun Sie dies: Dann tun Sie, was Sie wollen mit y unser benutzerdefiniertes Array