我有一個Object:aObject,它有屬性a,b,c。是否有可能使用ID訪問對象的所有屬性?
我可以使用訪問屬性:
aObject.a
aObject.b
aObject.c
是否有可能訪問使用ID的對象的所有屬性?如
[aObject indexOf:0]
[aObject indexOf:1]
[aObject indexOf:2]
或類似的東西。
歡迎任何評論
我有一個Object:aObject,它有屬性a,b,c。是否有可能使用ID訪問對象的所有屬性?
我可以使用訪問屬性:
aObject.a
aObject.b
aObject.c
是否有可能訪問使用ID的對象的所有屬性?如
[aObject indexOf:0]
[aObject indexOf:1]
[aObject indexOf:2]
或類似的東西。
歡迎任何評論
KVC(鍵值編碼)將允許您使用方法[anObject setValue:aValue forKey:@"a"]
設置屬性值。
如果你真的想迭代每個屬性,設置它的值,那麼你需要下拉到Objective-C運行時。
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList([anObject class], &outCount);
for (i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
const char * name = property_getName(property);
[anObject setValue:anyValue forKey:[NSString stringWithUTF8String:name]];
}
free(properties);
即使我從來沒有使用過它,現在看來,這可能與class_copyPropertyList方法。
free(properties); – 0xced