开发者

NSMutable array, do not retain the added object

开发者 https://www.devze.com 2023-03-24 21:56 出处:网络
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

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


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.


//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);
0

精彩评论

暂无评论...
验证码 换一张
取 消