views:

178

answers:

2

I would like to fill in the class variables in a loop from an dictionary. What I want to do is having the dictionary key as a class variable and assign the class variable (the dictionary key) the value from dictionary... something like this :

+(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;
}

It does not work, because I do not know how to assign the class variable from the string.

A: 

Your question is very very unclear and I have no idea what you're trying to do or why. But just looking at your code I can tell you already that it's definitely not doing what you want.

//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;
}

Your code will just assign classInstance.classVariable to be equal to [allKeys objectAtIndex:[allKeys count]-1]. Your loop is pointless.

After I actually annotated your code though I think I have some idea of what you want. Basically you want to assign the variables with names matching the keys in the dictionary the values in the dictionary. i.e. if there is a key called "superKey" then you want to find the variable within classInstance (classInstance.superKey) and assign it the value in the dictionary that matches superKey. That's what you want, right?

Well, the only way I know of to do that is to use a big switch statement or a bunch of if statements. Make some function within MyClass like this:

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

Then you just call [classInstance assignProperty:classVariable toValue:[doc objectForKey:classVariable]] and the job will be done.

But having told you all that... Why would you ever want to do what you're doing? Want to know a much better way of doing this? Give MyClass its own NSDictionary. Basically all you are doing is defeating the entire purpose of the dictionary. Why? They are incredibly fast to access and can store whatever you want. There is no reason not to use one. So just do this:

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

    classInstance.dictionary = dic;

    return classInstance;
}

Voila.

Eli
Good post! One small issue: generally `init` methods are instance methods, not class methods, since the instance was already created by a call to `alloc`. Your methods should therefore probably be `-(id)initWithDictionary:(NSDictionary *)dic` rather than `+(id)...`
Tim
Good catch! I've updated that.
Eli
A: 

Hello, Thanks for answer, I am returning a JSON string that gives me a NSDiccionary with keys and values. I am trying to fill this values to my class lets say DetailObject. I want to use later in the project the DetailObjec.id, DetailObject.description etc ... I would like to do it in a loop, becouse now I have to write this:

  • (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; }

What I want is to parse the dictionary from JSON to my object and respective variables and values that comes from dictionary in a loop, becouse if the JSON diccionary changes, I just add the new class variable with the same name of the returned diccionary key...

I do not know if I explain me well...

Jan
Edit your question. This is not an answer to what you asked.
Novelocrat