NSDictionary index value?????

Discussion in 'iOS Programming' started by AbhishekApple, Sep 20, 2010.

  1. AbhishekApple macrumors member

    Joined:
    Aug 5, 2010
    #1
    The arry has value as==>
    arry = 1~Below - $0.99,2~$1.00 - $1.99,3~$2.00 - $2.99,4~$3.00 - Above;

    i want NSDictionary to be filled with above values in sequential order (ASC/DSC) with key value pair...
    but the ordering is not proper!!!!!!!!!!!


    Also Checkout the index value of NSDicitonary why are they not incrementing by 1 like 0,1,2,3,4....

    Code:
    - (void)requestFinished:(ASIHTTPRequest *)request
    {
    	
    NSString *responseString = [request responseString];
    NSArray *arry;
    arry= [responseString componentsSeparatedByString:@","];
    for (int i=[arry count]-1; i>=0; i--) {
    NSArray *arryValue = [[arry objectAtIndex:i] componentsSeparatedByString:@"~"];
    [dictPrice setObject:[arryValue objectAtIndex:1] forKey:[arryValue objectAtIndex:0]];
    }
    }
    
    the Log file shows following
    count=4

    when i=3;
    Code:
    Printing description of dictPrice:
    <CFDictionary 0xc038cfc0 [0x1e3bca0]>{type = mutable, count = 2, capacity = 3, pairs = (
    	[COLOR="Red"]0 [/COLOR]: <CFString 0xcc820ff0 [0x1e3bca0]>{contents = "4"} = 
    <CFString 0xcc822fe0 [0x1e3bca0]>{contents =[COLOR="Red"] "$3.00 - Above"[/COLOR]}
    	[COLOR="red"]3[/COLOR] : <CFString 0xcc86eff0 [0x1e3bca0]>{contents = "3"} =
     <CFString 0xcc870fe0 [0x1e3bca0]>{contents =[COLOR="red"] "$2.00 - $2.99"[/COLOR]}
    )}
    when i=2;
    Code:
    Printing description of dictPrice:
    <CFDictionary 0xc038cfc0 [0x1e3bca0]>{type = mutable, count = 3, capacity = 3, pairs = (
    	[COLOR="red"]0[/COLOR] : <CFString 0xcc820ff0 [0x1e3bca0]>{contents = "4"} =
     <CFString 0xcc822fe0 [0x1e3bca0]>{contents =[COLOR="red"] "$3.00 - Above"[/COLOR]}
    	[COLOR="red"]1[/COLOR] : <CFString 0xcc8ceff0 [0x1e3bca0]>{contents = "2"} = 
    <CFString 0xcc8d0fe0 [0x1e3bca0]>{contents =[COLOR="red"] "$1.00 - $1.99[/COLOR]"}
    [COLOR="red"]	3 [/COLOR]: <CFString 0xcc86eff0 [0x1e3bca0]>{contents = "3"} =
     <CFString 0xcc870fe0 [0x1e3bca0]>{contents = [COLOR="red"]"$2.00 - $2.99[/COLOR]"}
    )}
    
    when i=1;
    Code:
    Printing description of dictPrice:
    <CFDictionary 0xc038cfc0 [0x1e3bca0]>{type = mutable, count = 4, capacity = 6, pairs = (
    	[B][COLOR="Red"]0 [/COLOR][/B]: <CFString 0xcc820ff0 [0x1e3bca0]>{contents = "4"} = 
    <CFString 0xcc822fe0 [0x1e3bca0]>{contents =[COLOR="Red"] "$3.00 - Above"[/COLOR]}
    	[B][COLOR="red"]1[/COLOR][/B] : <CFString 0xcc8ceff0 [0x1e3bca0]>{contents = "2"} = 
    <CFString 0xcc8d0fe0 [0x1e3bca0]>{contents =[COLOR="red"] "$1.00 - $1.99"[/COLOR]}
    	[B][COLOR="red"]4 [/COLOR][/B]: <CFString 0xcc924ff0 [0x1e3bca0]>{contents = "1"} = 
    <CFString 0xcc926fe0 [0x1e3bca0]>{contents = [COLOR="red"]"Below - $0.99"[/COLOR]}
    	[B][COLOR="red"]7 [/COLOR][/B]: <CFString 0xcc86eff0 [0x1e3bca0]>{contents = "3"} = 
    <CFString 0xcc870fe0 [0x1e3bca0]>{contents =[COLOR="red"] "$2.00 - $2.99[/COLOR]"}
    )}
    
    
     
  2. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #2
    To put it simply, an NSDictionary is not sorted.
     
  3. adrian.oconnor macrumors 6502

    Joined:
    Jan 16, 2008
    Location:
    Nottingham, England
    #3
    You need to use an NSArray (or NSMutableArray) to get things stored in order. If you want key/value pairs you need to encapsulate the data within a class. Once you've done that you can even use NSMutableArray sortUsingSelector to order the results if you need to.
     
  4. AbhishekApple thread starter macrumors member

    Joined:
    Aug 5, 2010
    #4
    can u give example on this....

    how to create instance of class dynamically, and later use it?

    like here if i want 10 instance i can define it statically but how to define dynamically so that later i can get the instance name to fetch its variables

    ClassName *obj[10];
     
  5. adrian.oconnor macrumors 6502

    Joined:
    Jan 16, 2008
    Location:
    Nottingham, England
    #5
    I don't have the time to give you a detailed example, but try searching Google for NSMutableArray. You should be able to find plenty of help there.
     

Share This Page