Mutable dictionary issue

Discussion in 'iOS Programming' started by ghousemd, Aug 24, 2011.

  1. ghousemd, Aug 24, 2011
    Last edited by a moderator: Aug 24, 2011

    ghousemd macrumors newbie

    Joined:
    Jun 25, 2011
    #1
    HI everybody i have an issue and couldn't able to solve it .so,any help from u guys would be really helpfull.issue is

    i am having a Mutabledictionary in the format of
    Bank
    id 2
    kd 4
    gd 5
    gang
    id 4
    kd 8
    gd 1
    like this and another dictionary in the format of
    Bank gd
    gang kd
    like that i need to comare this dictionary with the above one and retrieve their corresponding values .i have tried this code but it is giving error
    Code:
    int count = [mFavoritesArray count];
    for (NSMutableDictionary *dd in mTest) {
    for (int i=0; i < count; i++) {
    NSMutableDictionary *tmp = [dd valueForKey:[mFavoritesArray objectAtIndex:i]];
    
    error is NSUnknownKeyException', reason: valueForUndefinedKey this class is not key value coding-compliant for the key kd.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    valueForKey: is part of KVC system. You should not be using that for this purpose. You should be using objectForKey:.
     
  3. ghousemd thread starter macrumors newbie

    Joined:
    Jun 25, 2011
    #3
    i have used that one also but it is showing error
    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Then you have a memory management problem as you are sending the message to a string, not a dictionary.
     
  5. ghousemd thread starter macrumors newbie

    Joined:
    Jun 25, 2011
    #5
    yes u r right when i print dd dictionary it is having a single elment only .so could u tell me how to implement for each loop with dd dictionary and where i am doing wrong
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  7. ghousemd thread starter macrumors newbie

    Joined:
    Jun 25, 2011
    #7

    is a Dictionary

    and another issue is
    hi guys i am trying to read plist which is inthe form of
    Bug dictionary
    key value
    key value
    hint dictionary
    z value
    d value
    like this when i tried to read it using NSMutable dictionary it is reading properly but when i tried to read it using NSArray it is not reading the contents and code i am using are

    NSString *mm=[[NSBundle mainBundle] pathForResource:mad:"Score" ofType:mad:"plist"];
    NSArray *mTest=[[NSArray alloc] initWithContentsOfFile:mm];// here it is not working

    NSString *mm=[[NSBundle mainBundle] pathForResource:mad:"Score" ofType:mad:"plist"];
    NSMutable dictionary *mTest=[[NSMutableDictionary alloc] initWithContentsOfFile:mm];// here it is working
    the reason why i am trying to use NSarray is in apple periodic elements example it is working but it is not working in my code
    any suggestion are welcome
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    So what does iteration over a dictionary do? Hint it doesn't iterate over the values alone.
     
  9. ghousemd, Aug 25, 2011
    Last edited: Aug 25, 2011

    ghousemd thread starter macrumors newbie

    Joined:
    Jun 25, 2011
    #9
    Bank dictionary
    id 2
    kd 4
    gd 5
    gang dictionary
    id 4
    kd 8
    gd 1
    like this and another dictionary in the format of
    Bank gd
    gang kd

    i need to compare these two dictionaries and add all values
    the code i am using is and for adding i dont know how to that any code from you will be a great helpfull to me



    NSString *mm=[[NSBundle mainBundle] pathForResource:mad:"Score" ofType:mad:"plist"];
    NSMutableDictionary *mTest=[[NSMutableDictionary alloc] initWithContentsOfFile:mm];
    NSArray *aa=[mTest mutableCopy];
    for (NSDictionary *dd in aa)// here dd is not able to retrieve data
    {
    for (int i=0; i < count; i++) {

    NSMutableDictionary *tmp = [dd valueForKey:[mFavoritesArray objectAtIndex:i]];
    }
    }
     
  10. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    So if you want the values what options do you have? Can you get an array of values from a dictionary? What does the documentation have to say on this.

    Honestly I feel like I am having to excessively spoon feed you answers. The ability to read and understand the documentation is a core programming skill that I suggest you need to work on.
     
  11. ghousemd thread starter macrumors newbie

    Joined:
    Jun 25, 2011
    #11
     
  12. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #12
    You ignored my question completely. I am not going to continue this. Look at the documentation for a method that will return an array of values from a dictionary. You seem to expect someone else to write your code for you. That is not programming. It's just utter laziness.
     
  13. agwilson macrumors newbie

    Joined:
    Feb 5, 2008
    #13
    Code:
    for (NSDictionary *dd in aa)// here dd is not able to retrieve data
    {
           for (int i=0; i < count; i++) {
               
           NSMutableDictionary *tmp = [dd valueForKey:[mFavoritesArray objectAtIndex:i]];
    }
    
    Robbieduncan, what do you think "for (NSDictionary *dd in aa)" is doing?

    You said mTest is an NSMutableDictionary. So, for each dd, NSMutableDictionary, in mTest you want the following code inside the loop to execute? Does mTest contain NSMutableDictionaries? Is it unable to retrieve data or is it trying to retrieve the wrong type of data you are looking for?
     
  14. ghousemd thread starter macrumors newbie

    Joined:
    Jun 25, 2011
    #14

    mTest contain NSMutabaleDictionaries and it is retrieveing data and dd it should retrieve each dictionary from it but it is not working

    i found solution for my problem code is


    for (id key in mTest)
    {
    NSLog(@"%@",key);
    id value = [mTest objectForKey:key];
    id vv=[mFinalDictionary valueForKey:key];
    id gg=[value valueForKey:vv];
    NSLog(@"%@",value);
    NSLog(@"%@",vv);
    NSLog(@"%@",gg);
    }

    ----------



    ----------

    sorry for disturbing u anyhow thanks for ur replay
     

Share This Page