Instancjowanie klasy niestandardowej z NSDictionary
Mam wrażenie, że to głupie pytanie, ale i tak zapytam...
Mam kolekcję obiektów NSDictionary
, których pary klucz/wartość odpowiadają niestandardowej klasie, którą utworzyłem, nazwij ją MyClass
. Czy jest dla mnie łatwa lub "najlepsza praktyka" metoda, aby w zasadzie zrobić coś takiego jak MyClass * instance = [
map NSDictionary
właściwości do MyClass
];
? Mam przeczucie, że muszę coś zrobić z NSCoding
lub NSKeyedUnarchiver
, ale zamiast potykać się o to sama, myślę, że ktoś może być w stanie wskazać mi w we właściwym kierunku.
5 answers
Metoda-setValuesForKeysWithDictionary:, wraz z-dictionaryWithValuesForKeys:, jest tym, czego chcesz użyć.
Przykład:
// In your custom class
+ (id)customClassWithProperties:(NSDictionary *)properties {
return [[[self alloc] initWithProperties:properties] autorelease];
}
- (id)initWithProperties:(NSDictionary *)properties {
if (self = [self init]) {
[self setValuesForKeysWithDictionary:properties];
}
return self;
}
// ...and to easily derive the dictionary
NSDictionary *properties = [anObject dictionaryWithValuesForKeys:[anObject allKeys]];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-02-07 17:20:09
Nie ma allKeys
Na NSObject. Musisz utworzyć dodatkową kategorię na NSObject jak poniżej:
NSObject+PropertyArray.h
@interface NSObject (PropertyArray)
- (NSArray *) allKeys;
@end
NSObject+PropertyArray.m
#import <objc/runtime.h>
@implementation NSObject (PropertyArray)
- (NSArray *) allKeys {
Class clazz = [self class];
u_int count;
objc_property_t* properties = class_copyPropertyList(clazz, &count);
NSMutableArray* propertyArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count ; i++) {
const char* propertyName = property_getName(properties[i]);
[propertyArray addObject:[NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]];
}
free(properties);
return [NSArray arrayWithArray:propertyArray];
}
@end
Przykład:
#import "NSObject+PropertyArray.h"
...
MyObject *obj = [[MyObject alloc] init];
obj.a = @"Hello A"; //setting some values to attributes
obj.b = @"Hello B";
//dictionaryWithValuesForKeys requires keys in NSArray. You can now
//construct such NSArray using `allKeys` from NSObject(PropertyArray) category
NSDictionary *objDict = [obj dictionaryWithValuesForKeys:[obj allKeys]];
//Resurrect MyObject from NSDictionary using setValuesForKeysWithDictionary
MyObject *objResur = [[MyObject alloc] init];
[objResur setValuesForKeysWithDictionary:objDict];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-24 08:42:12
Zakładając, że twoja klasa jest zgodna z protokołem kodowania klucz-wartość, możesz użyć następującego: (zdefiniowanego jako kategoria na NSDictionary dla wygody):
// myNSDictionaryCategory.h:
@interface NSDictionary (myCategory)
- (void)mapPropertiesToObject:(id)instance
@end
// myNSDictionaryCategory.m:
- (void)mapPropertiesToObject:(id)instance
{
for (NSString * propertyKey in [self allKeys])
{
[instance setValue:[self objectForKey:propertyKey]
forKey:propertyKey];
}
}
A oto jak byś go użył:
#import "myNSDictionaryCategory.h"
//...
[someDictionary mapPropertiesToObject:someObject];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-05-06 17:23:13
Jeśli Twoje robienie tego typu rzeczy jest szansą, że masz do czynienia z JSONEM i prawdopodobnie powinieneś rzucić okiem na Mantle https://github.com/Mantle/Mantle
Otrzymasz wtedy wygodną metodę dictionaryValue
[anObject dictionaryValue];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-01 23:17:11
Po prostu Dodaj kategorię dla NSObject, aby uzyskać dictionaryRepresentation z własnych obiektów (w moim przypadku używając tylko w serializacji JSON):
// NSObject+JSONSerialize.h
#import <Foundation/Foundation.h>
@interface NSObject(JSONSerialize)
- (NSDictionary *)dictionaryRepresentation;
@end
// NSObject+JSONSerialize.m
#import "NSObject+JSONSerialize.h"
#import <objc/runtime.h>
@implementation NSObject(JSONSerialize)
+ (instancetype)instanceWithDictionary:(NSDictionary *)aDictionary {
return [[self alloc] initWithDictionary:aDictionary];
}
- (instancetype)initWithDictionary:(NSDictionary *)aDictionary {
aDictionary = [aDictionary clean];
self.isReady = NO;
for (NSString* propName in [self allPropertyNames]) {
[self setValue:aDictionary[propName] forKey:propName];
}
//You can add there some custom properties with wrong names like "id"
//[self setValue:aDictionary[@"id"] forKeyPath:@"objectID"];
self.isReady = YES;
return self;
}
- (NSDictionary *)dictionaryRepresentation {
NSMutableDictionary *result = [NSMutableDictionary dictionary];
NSArray *propertyNames = [self allPropertyNames];
id object;
for (NSString *key in propertyNames) {
object = [self valueForKey:key];
if (object) {
[result setObject:object forKey:key];
}
}
return result;
}
- (NSArray *)allPropertyNames {
unsigned count;
objc_property_t *properties = class_copyPropertyList([self class], &count);
NSMutableArray *rv = [NSMutableArray array];
unsigned i;
for (i = 0; i < count; i++) {
objc_property_t property = properties[i];
NSString *name = [NSString stringWithUTF8String:property_getName(property)];
[rv addObject:name];
}
//You can add there some custom properties with wrong names like "id"
//[rv addObject:@"objectID"];
//Example use inside initWithDictionary:
//[self setValue:aDictionary[@"id"] forKeyPath:@"objectID"];
free(properties);
return rv;
}
@end
Widać również, że moje rozwiązanie nie będzie działać z obiektami niestandardowymi z zagnieżdżonymi obiektami lub tablicami. Dla tablic-wystarczy zmienić linie kodu w metodzie dictionaryRepresentation
:
if (object) {
if ([object isKindOfClass:[NSArray class]]) {
@autoreleasepool {
NSMutableArray *array = [NSMutableArray array];
for (id item in (NSArray *)object) {
[array addObject:[item dictionaryRepresentation]];
}
[result setObject:array forKey:key];
}
} else {
[result setObject:object forKey:key];
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-30 13:06:38