Sorted NSDictionary through Class

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

  1. AbhishekApple macrumors member

    Aug 5, 2010
    I want to create a key value pair like NSDictionary but as it is not Sorted
    am usiing a class for it....

    my question is wher shld i deallocate KeyValue kvObj in my code????
    This will surely create mem leaks.....plz suggest

    @interface KeyValue : NSObject {
    	NSString *_key;
    	NSString *_value;
    @property(nonatomic,retain)NSString *_key;
    @property(nonatomic,retain)NSString *_value;

    #import KeyValue
    @implementation Dictionary
    static NSMutableArray *arryColor;
    - (void)requestFinished:(ASIHTTPRequest *)request
    	NSString *responseString = [request responseString];
    		NSArray *arry;
    		arry= [responseString componentsSeparatedByString:@","];
    for (int i=0; i<[arry count]; i++) {
    	NSArray *arryValue = [[arry objectAtIndex:i] componentsSeparatedByString:@"~"]; 
    // arryValue = 1~abc,2~xyz,3~pqr;
    [COLOR="red"]KeyValue kvObj= [[KeyValue alloc] init];[/COLOR]
    kvObj._key=[arryValue objectAtIndex:0];   //key like 1,2,3,...
    kvObj._value=[arryValue objectAtIndex:1];   //value like abc,xyz,pqr....
    [COLOR="Red"][arryColor addObject:kvObj];[/COLOR]		
  2. Luke Redpath macrumors 6502a

    Nov 9, 2007
    Colchester, UK
    You have created it using alloc so if you no longer need to retain ownership of it once you've added it to the array (and as its a local variable you probably don't) then yes, you should release it.

    If this wasn't clear, it might be time to reread the Apple Ojective C Memory Management Guide again (you have read it, right?).

    As it stands, your code doesn't make a whole lot of sense. You are assigning a instance of KeyValue to kvObj in your first highlighted line, then assigning a different object to kvObj on the two lines following it, so you've already lost a reference to the KeyValue instance anyway, so it will leak regardless.

    Is the code above the actual code? If it is, it doesn't work as you think it does. If it isn't, then please post real code otherwise you just confuse matters.
  3. AbhishekApple thread starter macrumors member

    Aug 5, 2010
    wht i conclude from below code is array copies the object and stores in new location thus you can delete the local object after allocating it to array..
    Please comment if i am wrong..:apple:
    			for (int i=0; i<[arry count]; i++) {
    				NSArray *arryValue = [[arry objectAtIndex:i] componentsSeparatedByString:@"~"];
    				KeyValue *keyvalObj = [[KeyValue alloc] init];
    				keyvalObj._key = [arryValue objectAtIndex:0];
    				keyvalObj._value = [arryValue objectAtIndex:1];
    				[arryPrice addObject:keyvalObj];
    				[keyvalObj release];
  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    That is not correct. There is no copying or deleting going on. Basically, by adding the object to your array, it is adding a pointer to that instance in the array and then retaining that instance (by incrementing the retain count). Then, when you release the ivar, the retain count is decremented.

    I take it you have not yet read the Memory Management Programming Guide. You should.

Share This Page