내 자신의 개체 컬렉션을 NSDictionary (JSON 용)로 깊이 파싱하려고합니다.ObjC valueForKey가 NSNumber 또는 UDT에 대해 작동하지 않습니다.
나는 나의 모델의 모든 연장 기본 객체 클래스를 가지고 있고, 차례로이 baseobject는 NSObject의 확장 : 방법에있어서
@interface BaseObj : NSObject <DICTMaker>
- (NSMutableDictionary *) toDICT;
@end
을, 나는 목록을 가져올 수 objc-런타임을 사용 속성의. [self valueForKey:]
을 사용하는 것에 대한 참조를 얻을 수있는 모든 속성은 내 사전에 속성의 이름과 값을 삽입합니다.
그러나 내가 지금까지 알아 차린 것은 NSNumber
과 사용자 정의 클래스가 사전에 추가되지 않는다는 것입니다. 내 파서가 가장 확실하게 식별합니다. 로그에 모든 것을 뱉어 내기 때문입니다. [self valueForKey:]
은 모두 NSNumbers
및 사용자 정의 개체에 nil
을 반환합니다. 그 결과 사전은 이메일, 비밀번호, password_confirm에 대한 항목을 포함
@interface UserRegistrationLocation : BaseObj {
NSString *address, *street, *street_2, *city;
NSNumber *addr_state, *addr_zip;
}
@interface UserRegistrationContact : BaseObj {
NSString *first_name, *last_name;
NSString *p_phone_area, *p_phone_first_3, *p_phone_last_4;
NSString *s_phone_area, *s_phone_first_3, *s_phone_last_4;
}
@interface UserRegistration : BaseObj {
NSString *email, *password, *password_confirm;
NSNumber *referral;
UserRegistrationContact *primary, *secondary;
UserRegistrationLocation *address;
}
NSMutableDictionary *mydict = [myUserRegistration toDICT];
: 여기
- (NSMutableDictionary *)toDICT {
NSMutableDictionary *props = [NSMutableDictionary dictionary];
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];
// Both of these work, I promise:
NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];
NSString *propertyType = [NSString stringWithUTF8String:getPropertyType(property)];
NSLog(@"%@ of Type: %@", propertyName, propertyType);
id propertyValue = [self valueForKey:propertyName];
if ([ propertyValue respondsToSelector:@selector(toDICT:) ])
[ props setObject:[propertyValue toDICT] forKey:propertyName ];
else if (propertyValue)
[ props setObject:propertyValue forKey:propertyName ];
else
NSLog(@"Unable to get ref to: %@", propertyName);
}
free(properties);
return props;
}
내가 만든 던져 샘플 개체입니다
[11012:f803] Unable to get ref to: referral
[11012:f803] Unable to get ref to: primary
[11012:f803] Unable to get ref to: secondary
[11012:f803] Unable to get ref to: address
[11012:f803] {"user":{"password":"haxme123","password_confirm":"haxme123","email":"[email protected]"}}
모든 지원하시기 바랍니다 = }!
얼마나 내 당황스럽고 내면의 물체에 데이터가 할당 되었는가.그러나 클래스 자체가 제대로 초기화되지 않았습니다! -10 프로그래머 pnts –