RE: MutableArrays losing its contents?
RE: MutableArrays losing its contents?
- Subject: RE: MutableArrays losing its contents?
- From: Lorenzo <email@hidden>
- Date: Mon, 01 Dec 2003 15:13:29 +0100
Hi,
are you sure you don't access, and maybe delete, the objects in some other
point of your code? I suppose that, after you add the object, you set it to
a different value (e.g. an empty mutable string). You should initialize it
this way:
mArray = [[NSMutableArray alloc] init];
//here you don't need to retain it;
or you could even initialize it this way:
mArray = [[NSMutableArray array] retain];
/here you need to retain it;
then add objects this way:
NSMutableString *mString = [NSMutableString string];
// please note, don't retain the mString.
[mString setString:@"Hello"];
[mArray addObject:mString];
// now, if you write
[mString setString:@"Bye Bye"];
// your mArray will contain the object "Bye Bye" and not "Hello".
So, be sure you don't modify the objects you added using their pointers.
And, don't retain the objects you are going to add. The mArray will retain
them for you. Instead, create new objects, and add them to the mArray.
In the case I made above, I created the mString with
[NSMutableString string];
So I don't need to release it. Instead, if you created the mString this way:
NSMutableString *mString = [[NSMutableString alloc] init];
[mString setString:@"Hello"];
[mArray addObject:mString];
// then you have to release it, just after you added it to the mArray.
[mString release];
And last, in the dealloc method, dont' forget to release the mArray
- (void)dealloc
{
[mArray release];
}
Best Regards
--
Lorenzo
email: email@hidden
>
NSMutableArray not keeping its contents?
>
>
I have a NSMutableArray that keeps a list of NSURL. The problem I have
>
is that even though the array is in my class's header its contents are
>
not retained from the method that they are added from.
>
What I'm trying to say is that I have a open file method that gets the
>
url and adds it to the array then another method that is setup to get
>
the url from the array and open it.
>
The problem I'm having is that even if I retain the array and the
>
objects within it it doesn't exists when the second method tries to
>
access it. I know its getting added to the array because I can use it
>
from the array while it is in the first method (the one that gets the
>
URL and puts it into the array).
>
>
Any help?
>
>
Lee Morgan
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.