Rupesh
Rupesh

Reputation: 2051

NSMutable array, do not retain the added object

I have a UITextField called txtvwEmail. i am adding the text from txtvwEmail.text to the Array pastUrls but after adding the next text it remove the first text. Im using the code

if (![pastUrls containsObject:txtvwEmail.text]) {
    [pastUrls addObject:txtvwEmail.text];
}

Upvotes: 0

Views: 150

Answers (2)

//this should be outside of ur all loops
NSMutableArray *pastUrls=[[NSMutableArray alloc]init];

//remove this line
//NSMutableArray *pastUrls=[NSMutableArray array];

if (![pastUrls containsObject:txtvwEmail.text]) {
    [pastUrls addObject:txtvwEmail.text];
}

NSLog(@"pastUrls : %@ \n\n",pastUrls);

Upvotes: 1

Gobra
Gobra

Reputation: 4261

You should rely on the basics of the language and frameworks. The array DOES RETAIN the object, however, it could be:

  1. pastUrls is nil -> no retain
  2. somewhere in the code .text is released (or autoreleased) and the count is yet the same

Also, you can't really be sure [obj retainCount] return the correct value. To diagnose the real problem, revise the code or post it here so we can help.

Upvotes: 2

Related Questions