在循环中填充NSDictionary中的类变量

时间:2009-09-08 18:19:37

标签: iphone cocoa-touch

我想从字典中填充循环中的类变量。我想要做的是将字典键作为类变量并将类变量(字典键)分配给字典中的值...类似这样的事情:

+(void) initWithDictionary:(NSDictionary *)dic {
  MyClass *classInstance  = [[[self alloc] init] autorelease];
  NSArray *allKeys = [dic allKeys];
  for(NSUInteger i = 0; i < [allKeys count]; i++)
  {
    id classVariable = [allKeys objectAtIndex:i];
    classInstance.classVariable = [dic objectForKey:[allKeys objectAtIndex:i]];
  }
  return classInstance;
}

它不起作用,因为我不知道如何从字符串中分配类变量。


感谢您的回答,我将返回一个JSON字符串,该字符串为NSDictionary提供了键和值。我想把这个值填到我的班级,让我们说DetailObject。我希望稍后在项目中使用DetailObject.idDetailObject.description等。我想在循环中完成它,因为现在我必须写这个:

+ (id) initWithDiccionary :(NSDictionary *)dic//;
{
    //Instantiating an object of this class... that's okay.
    DetailObject *classInstance = [[[self alloc] init] autorelease];

    classInstance.id = [dic objectForKey@"id"];
    classInstance.desc = [dic objectForKey@"desc"];
    etc... etc... 


    return classInstance;
}

我想要的是将字典从JSON解析为我的对象以及循环中来自字典的相应变量和值,因为如果JSON字典发生更改,我只需添加与返回的相同名称的新类变量字典键......

我不知道我是否已经解释得很好......

2 个答案:

答案 0 :(得分:1)

您的问题非常不明确,我不知道您要做什么或为什么。但只要查看你的代码,我就可以告诉你,它绝对不是你想要的。

//There should be no semicolon after "dic" below.
//Also, you should be returning a MyClass or an id.
- (id) initWithDiccionary :(NSDictionary *)dic//;
{
    //Instantiating an object of this class... that's okay.
    MyClass *classInstance = [[[self alloc] init] autorelease];

    //Getting all the keys from the dictionary, seems fine...
    NSArray *allKeys = [dic allKeys];

    //Looping through all the keys in the dictionary, seems okay...
    for(NSUInteger i = 0; i < [allKeys count]; i++)
    {
        //Storing the current key.
        id classVariable = [allKeys objectAtIndex:i];

        //Assigning the class's property "classVariable" to match the current key's value.
        //No reason to say "[allKeys objectAtIndex:i]" again, though.
        classInstance.classVariable = [dic objectForKey:classVariable];
    }

    //Returning something when you have no return type above (void) is wrong.
    return classInstance;
}

您的代码只会将classInstance.classVariable指定为等于[allKeys objectAtIndex:[allKeys count] -1]。你的循环毫无意义。

我实际上注释了你的代码后,我想我已经知道你想要什么了。基本上,您希望使用与字典中的键匹配的名称来为变量分配字典中的值。即如果有一个名为“superKey”的键,那么你想在classInstance(classInstance.superKey)中找到该变量,并在字典中为superKey赋值。这就是你想要的,对吧?

嗯,我知道这样做的唯一方法是使用一个大的switch语句或一堆if语句。在MyClass中创建一些函数,如下所示:

- (void) assignProperty:(id)property toValue:(id)value
{
    if (property == @"superKey")
    {
        self.superKey = value;
    }
    else if (property == @"lameKey")
    {
        self.lameKey = value;
    }
    //etc.
}

然后你只需调用[classInstance assignProperty:classVariable toValue:[doc objectForKey:classVariable]],就可以完成这项工作。

但是告诉过你们所有...... 你为什么要做你正在做的事情?想知道更好的方法吗?为MyClass提供自己的 NSDictionary。基本上你所做的就是打败字典的整个目的。为什么?它们访问速度极快,可以存储您想要的任何内容。没有理由不使用它。所以这样做:

- (id) initWithDiccionary :(NSDictionary *)dic
{
    MyClass *classInstance = [[[self alloc] init] autorelease];

    classInstance.dictionary = dic;

    return classInstance;
}

瞧。

答案 1 :(得分:1)

输入Key-Value Coding。以下是如何实现预期结果的示例:

@interface MyClass : NSObject
@property (nonatomic, retain) NSString *aString;
@property (nonatomic, retain) NSNumber *aNumber;
@property (nonatomic, retain) NSString *yetAnother;
- (id)initWithDictionary:(NSDictionary *)dictionary;
@end

@implementation MyClass
@synthesize aString;
@synthesize aNumber;
@synthesize yetAnother;

- (id)initWithDictionary:(NSDictionary *)dictionary {
  if ((self = [super init])) {
    [self setValuesForKeysWithDictionary:dictionary];
  }

  return self;
}

// dealloc is left as an exercise for the reader

@end

您可以按如下方式使用此课程:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"my string", @"aString", 
                            [NSNumber numberWithInt:42], @"aNumber", 
                            @"strings!", @"yetAnother", nil];

MyClass *myClass = [[MyClass alloc] initWithDictionary:dictionary] autorelease];
// yay!

你可以感谢Objective-C的活力。 :)