assigning values from static object

Discussion in 'iOS Programming' started by sujithkrishnan, Jul 9, 2008.

  1. sujithkrishnan macrumors 6502

    May 9, 2008
    Hi all..

    In my iPhone app,
    i make use of a static object...

    The getter and setter method for the static object is as follows...
    +(cart *)getMyCart
    	if(staticCartObject == nil)
    		staticCartObject = [[cart alloc]init];
    	    staticCartObject.productArray = [[NSMutableArray alloc]init];	
    	return cartObject;
         [staticCartObject release];
         [newCart retain];
    Hope the above code is OK....

    In one of my viewController i am doing an assignment like

    localCartObjectInFirstViewController = [cart getCartObject];
    I know its a "copy by reference"...

    But i want the "localCartObjectInFirstViewController" to have a separate memory containing the return value of [cart getMyCart];

    If i allocate memory for localCartObjectInFirstViewController and do the above will result in a memory leak , right???

    So whats the solution???
  2. aLoC macrumors 6502a

    Nov 10, 2006
    Maybe try this:

    localCartObjectInFirstViewController = [[cart getCartObject] copy];
  3. sujithkrishnan thread starter macrumors 6502

    May 9, 2008
    Still... both are at same memory....

    I tried for strings as well..

    But "mutableCopy" worked fine...
    Then my question is which of the folllowing is right???

    NSString str1 =[[NSString alloc]init];
    str1 = [originalStr mutableCopy];


    NSString str1 = [originalString mutableCopy];

    What the difference between two ??

    If both are fine, why we want to allocate, adding burden of release and all??
  4. aLoC macrumors 6502a

    Nov 10, 2006
    There are two string classes in Cocoa, one where the text can't change after it is initialized (NSString) and one where is can (NSMutableString). If you have ever used Java, it is the same as String and StringBuffer.

    The first example creates an NSString, then makes an NSMutableString object with the same text. Both of these objects will have to be released at some point.

    The second example just creates a mutable copy of a String given you already have one. The reason you get the same memory when you use [NSString copy] is that since NSString's text can't be changed, it is ok for everyone to share the same instance.
  5. sujithkrishnan thread starter macrumors 6502

    May 9, 2008
    Okie aLoC...

    Still a doubt....

    first example creates a string with some memory allocated ,right??

    So my question is the mutableCopy i am assigning will go to the same memory which it allocated in previous line? OR that alloc will cause a leak
    as mutableCopy returns same string with some address defined by them?

    Thing is that i am having a collection of items (item name, item porice, item description etc...) in ItemClass.

    But user will select some items , (all will store in a class FavoriteList).

    I am just assigning it during he add this items to his favorite list.
    But i found that those items in FavoriteList are sharing the same address with its location in ItemClass.

    I want both as separate entities, as those change in fav list dont reflect itemClass.
  6. aLoC macrumors 6502a

    Nov 10, 2006
    You need to add a method to your Item class to make it return a copy of itself, e.g.
    @implementation Item
    - (id)copyWithZone:(NSZone *)zone
        Item *copyOfMe = [[Item allocWithZone:zone] init];
        // OR: if your superclass already has an allocWithZone method, do this instead:
        // Item *copyOfMe = [super copyWithZone:zone];
        [copyOfMe setName:[[self name] copy]];
        [copyOfMe setPrice:[[self price] copy]];
        [copyOfMe setDescription:[[self description] copy]];
        // etc.
        return copyOfMe;
    Then when you want to copy an object (e.g. before adding it to the favorites) do it like so:
    Item item2 = [item1 copy];
    This will give two wholly independent Item objects.

Share This Page