views:

116

answers:

2

So here is the problem.

I have a method (or message as they are called in obj-C) I am passing in a pointer to an object.

inside that method i am going to change what that pointer is pointing to, and release the old object. And i would like the instance variable (which is being passed into the method) to now reference the new value assigned to it. basically having the variable operate like an OUT parameter in languages like C#

-(NSDictionary *) GetListWithCommand:(NSString*) command andCache:(CachedMutableDictionary*) cache  
{  
    CachedMutableDictionary* Dictionary = [Getfrom somesource];
    CachedMutableDictionary* temp = cache;  
    cache = [Dictionary retain];  
    [temp release];  
}

so i believe i need to send in an address-of reference for the cache variable and then be able to refer to it both at the address level or at the object level.

it may also be wiser/easier to create a copy method on the CachedMutableDictionary class.

+1  A: 

Yes, it sounds like this method needs to take a CachedMutableDictionary** to which the caller passes &cache. The last three lines of the method become:

CachedMutableDictionary *temp = *cache;
*cache = [Dictionary retain];  
[temp release];

This smells funny to me though. Where does cache come from originally? Is it always an instance variable of an object? Why not just pass in the object?

Matt Kane
I agree it smells funny. what i am trying to do is pick between two sources (a cached plist and an XML source), as well as have the Dictionary saved to an object, so i can grab the dictionary without having to make the same decision over and over. I am also adding this in to my code, so am trying to avoid a major refactor.
Bluephlame
+5  A: 

You just have to add some stars here and there:

-(NSDictionary *) GetListWithCommand:(NSString*) command
                            andCache:(CachedMutableDictionary**) cache  
{  
    CachedMutableDictionary* Dictionary = [Getfrom somesource];
    CachedMutableDictionary* temp = *cache;  
    *cache = [Dictionary retain];  
    [temp release];  
}
mouviciel
You should ensure that cache isn't actually null before you dereference it.
Jason Coco