PDA

View Full Version : assigning values from static object




sujithkrishnan
Jul 9, 2008, 09:54 AM
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;
}

+(void)setCart:(cart*)newCart
{
[staticCartObject release];
[newCart retain];
staticCartObject=newCart;
}


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???



aLoC
Jul 9, 2008, 10:01 AM
Maybe try this:

localCartObjectInFirstViewController = [[cart getCartObject] copy];

sujithkrishnan
Jul 15, 2008, 05:34 AM
Maybe try this:

localCartObjectInFirstViewController = [[cart getCartObject] copy];

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];


OR

NSString str1 = [originalString mutableCopy];

What the difference between two ??

If both are fine, why we want to allocate, adding burden of release and all??

aLoC
Jul 15, 2008, 06:33 AM
NSString str1 =[[NSString alloc]init];
str1 = [originalStr mutableCopy];


OR

NSString str1 = [originalString mutableCopy];

What the difference between two ??


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.

sujithkrishnan
Jul 16, 2008, 01:11 AM
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.

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.

aLoC
Jul 16, 2008, 04:06 AM
I want both as separate entities, as those change in fav list dont reflect itemClass.

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;
}

@end


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.