2011-09-02 64 views

回答

9

是的,考慮到確切的上下文,可以有多種方法來解決這個問題。

我現在可以想到的一點是首先獲取源對象的所有屬性,然後使用setValue:value forKey:key來設置目標對象上的值。

代碼檢索所有自定義屬性:

-(NSSet *)propertyNames { 
    NSMutableSet *propNames = [NSMutableSet set]; 
    unsigned int outCount, i; 
    objc_property_t *properties = class_copyPropertyList([self class], &outCount); 
    for (i = 0; i < outCount; i++) { 
    objc_property_t property = properties[i]; 
    NSString *propertyName = [[[NSString alloc] 
     initWithCString:property_getName(property)] autorelease]; 
    [propNames addObject:propertyName]; 
    } 
    free(properties); 

    return propNames; 
} 

您可能希望簽出Key-Value Coding Programming Guide以獲取更多信息。

+1

我是否正確推斷必須有'#import '? – Rob

相關問題