Problem with NSMutableArray - iphone

I declared a NSMutable array and assigned some values to it.
.h
NSMutableArray *imageDetailsFromCategory;
#property (nonatomic, retain) NSMutableArray *imageDetailsFromCategory;
.m
#synthesise imageDetailsFromCategory
in ViewDidLoad:
imageDetailsFromCategory = [[NSMutableArray alloc]init];
//assigning object to Array..working fine.showing two images.
imageDetailsFromCategory = [self getImageDetailsFromCategory:generatedString];
Now my app is loading... I am doing some UI changes with the array. However I want to pass this array on another button click to another class. But when click event is triggered the array shows 0x76779e0"{(int)$VAR Count} like this in the same class I declared the arry. I can't get the array count after the button click.
Can any one tell me how can I access my array. What is the problem?

The method [self getImageDetailsFromCategory:generatedString]; I think returns a autoreleased array. Try using the proper setter for retaining it, like
self.imageDetailsFromCategory = [self getImageDetailsFromCategory:generatedString];

You are overriding your imageDetailsFromCategory variable that you alloc'd in the first line with your second line.
So imageDetailsFromCategory = [[NSMutableArray alloc] init] creates a mutable array… but imageDetailsFromCategory = [self getImageDetailsFromCategory:generatedString]; replaces the previously alloced mutable array with a brand new object.
THat's as if you did int i=5; then i = [self someMethod];: the value 5 would be lost.

Related

NSMutableArray empty after leaving View

I have a Mutable Array and I fill it with some strings. But when I leave the view (Navigation Controller) and reopen it, the Array is empty. My Mutable Array gets emptied, everytime I leave the view.
if([name_Recipe isEqualToString:#""])
{
NSLog(#"Nothing");
}
else
{
[favoritesArrayDetail addObject:name_Recipe];
NSLog(#"%#", [NSString stringWithFormat:#"%#", favoritesArrayDetail]);
}
This is because when you leave the view, the view controller is being released along with all of its member variables. If you want to still be able to access the array, you need yo declare it somewhere else, or make it static.
EDIT: Since viewDidLoad is called every time you go back to the view, it is resetting the array back to an empty one because of [NSMutableArray new]. You want to only call that line of code when the array has not yet been created.
For example, you could do:
if (!favoritesArrayDetail) //Only do this code if the array is nil.
favoritesArrayDetail = [NSMutableArray new];
Declare your array as a property.
#property (nonatomic, copy) NSMutableArray *favoritesArrayDetail;
And in viewDidLoad:
if (!favoritesArrayDetail)
{
favoritesArrayDetail = [NSMutableArray new];
}

removeAllObjects for NSMutableArray that populates UITableView crashes with ARC

I have a property of
#property (nonatomic, strong) NSMutableArray *timesArray;
It is used to populate the data in my UITableView. When I want to clear my view, I do this:
- (void)clearView {
self.nameField.text = #"";
self.noteField.text = #"";
if ([_timesArray count] > 0) {
[self.timesArray removeAllObjects];
[self.customTableView reloadData];
}
}
The removeAllObjects causes a crash. I am not sure why. I looked around and a lot of posts talk about an object being overreleased. How is that happening if I'm using ARC and not calling retain/release on any objects. My _timesArray just holds NSDate objects I get from a UIDatePicker.
My stack trace looks like:
My insertPill looks like:
- (void)insertPill:(id)sender {
//[[NSNotificationCenter defaultCenter] postNotificationName:InsertPillNotification object:self];
[self clearView];
}
If I don't removeAllObjects, and just do:
NSMutableArray *emptyArray = [[NSMutableArray alloc] initWithCapacity:0];
self.timesArray = emptyArray;
This works. But I'd still like to know why by removing the objects it does not work.
Edit: I initialize the array in viewDidLoad:
_timesArray = [[NSMutableArray alloc] initWithCapacity:0];
When I want to add a new object to the array, I do this:
NSMutableArray *tempUnsortedArray = [[NSMutableArray alloc] initWithArray:_timesArray];
[tempUnsortedArray addObject:_datePicker.date];
self.timesArray = tempUnsortedArray;
I'm not sure if the way I'm adding data the array is causing the issue or not.
You're getting a doesNotRecognizeSelector: exception. This probably means that the object you think is a NSMutableArray is not really one. It is probably an NSArray. Where are you assigning the object?
To start debugging the issue, po the object before calling removeAllObjects. What type of object is it reported as?
Otherwise it could be possible that there is a non NSObject element in timesArray.
#property (nonatomic, strong) NSMutableArray *timesArray;
if ([_timesArray count] > 0)
It seems, you syntesize your property like this:
#syntesize timesArray = _timesArray;
You chacking count of _timesArray, but removing object from timesArray. I never set new name for my properties and don't sure how it works, but I dont like it.

Sharing NSMutableArray

Hey everbody, im trying to share a NSMutableArray to another xib. The cartList.codigo is a NSMutableArray from a shared class, according to James Brannan's tutorial (http://www.vimeo.com/12164589).
When i count it, it gives me 1. But when i load the another view, gives me 0.
Whats wrong?
View that adds:
self.produtoCodigo = [[NSMutableArray alloc]init];
[self.produtoCodigo addObject:#"aa"];
CartViewController *carrinho = [[CartViewController alloc] initWithNibName:#"CartViewController" bundle:[NSBundle mainBundle]];
CartList *lista = [[CartList alloc] init];
carrinho.cartList = lista;
carrinho.cartList.codigo = self.produtoCodigo;
NSLog(#"QTD %i", [carrinho.cartList.codigo count]);
View that wants to load the item added:
- (void) viewDidAppear:(BOOL)animated {
self.produtoCodigo = self.cartList.codigo;
NSLog(#"%i", [self.produtoCodigo count]);
[super viewDidLoad];
}
Im loading the CartList class in both.
Thanks!
My guess is that you are not retaining the array, but only assigning it.
Do you retain the array in the view, something like this -
#property (nonatomic, retain) NSMutableArray * produtoCodigo;
the "retain" gives your view controller an ownership on the array and you can use it.
Since I don't have your code that is the best I can do to help.
shani
UPDATE -
Ok. you do retain it but now i see that the "produtoCodigo" array is retained but you dont pass the array in the view controller.
is seems that you can do 2 things:
in your view controller pass the viewController "produtoCodigo" to the view "produtoCodigo".
carrinho.produtoCodigo= self.produtoCodigo;
if you already pass the array to the "cartList" then remove the view "produtoCodigo" array, geter and seter (#synthesize & #property). and in the view did load you can:
produtoCodigo =[NSMutableArray arraryWithArray: self.cartList.codigo];
or if you need to use it later you can :
produtoCodigo =[NSMutableArray alloc]initWithArray: self.cartList.codigo];
and don't forget to release it later in the second option.
Hope this time it will help
shani

iOS - resetting NSMutable Array causes crash

I have an NSMutableArray I'm trying to reload after an async call. The first time it loads like this:
self.sessionProcList = [NSMutableArray arrayWithArray:[result records]];
After the user does some interaction, the same line will be reached to reload the NSMutableArray. This causes the crash
Header file has:
#interface...
NSMutableArray *sessionProcList;
... }
#property (nonatomic, retain) NSMutableArray *sessionProcList;
Say you do:
NSMutableArray *a = [NSMutableArray arrayWithObject: [[NSObject alloc] init]];
NSObject *o = [a objectAtIndex: 0];
[a removeAllObjects];
[o description]; // *BOOM*
The above will [generally -- sometimes not but only by coincidence] crash because o has been deallocated by the time the description method is invoked.
If you have a reference to an object in an array, but have not retained said reference, then said object may be deallocated out from under you when you empty the array.
(And nonatomic vs. atomic is irrelevant.)
If I had to guess, I would say that elements in that array are being referenced from somewhere else. Resetting the array causes the items using the references to crash.
I would check your application for other variables, properties, or UI elements using those variables that have not been release before resetting it.
Because arrayWithArray is a convenience method it gets initialised with an autorelease flag.
You haven't mentioned what the crash / error message is but I am guessing your NSMutableArray is getting released before your second iteraction with it starts.
Try and retain the array for however long you need it with:
self.sessionProcList = [NSMutableArray arrayWithArray:[result records]];
[sessionProcList retain];
And then release it when you're done with it:
[sessionProcList release];
I hope it helps.
Rog

NSMutableArrays

I have an NSMutableArray as a member variable for a class.
In the .h file:
#interface bleh {
NSMutableArray *list;
}
#property (readonly, assign) NSMutableArray *list;
#end
In the .m file:
#implementation bleh
#synthesize list;
-(void)init;
{
list = [NSMutableArray arrayWithCapacity:30];
}
#end
Now, I'm not really an objective-C programmer, so maybe I'm missing some of the nuances, but when I do the following:
NSMutableString *listItem = [NSMutableString stringWithString:#"Foobar"];
[list addObject:listItem];
I'm getting strange behavior. Namely, I'm using this to keep a list of files that I eventually want to attach to an email and then open the picker. I'm getting a SIGABRT, and upon debugging, I find out that whenever I operate on list, I'm getting nothing. addObject messages don't increase the size of the NSMutableArray at all.
Am I missing something? Can someone show me a full implementation of setting up an NSMutableArray to be manipulated within a class in Objective C?
Thanks.
PS - Assume that I'm smart enough to put the manipulations of the NSMutableArray inside of a member function for the class containing the member variable.
in the latest release of the SDK arrayWithCapacity is bad practice.
but in your code you creating a array that no one is owner , clam your array properly.
don't forget initialize your array
NSMutableArray *array = [[NSMutableArray alloc] init];
fix the (readonly,assign),
How are you actually creating your array? Is it possible that it's being autoreleased and going away? Remember, if you create it with a convenience method (like array or something) you need to retain it.
You're creating the array with arrayWithCapacity:, which returns an array you don't own, and you're never claiming ownership over it. Use a property accessor to retain the array:
self.list = [NSMutableArray arrayWithCapacity:30];
I would recommend reading the Cocoa memory management docs. Once you know the rules in there, it will be clear what to do in this sort of situation. They're not very hard, but they are very necessary if you're going to be programming Cocoa.
Your list variable has been auto-released and de-allocated, therefore your program crashes when you try to access it.
There are two ways to create objects in Cocoa:
NSMutableArray* array1 = [[NSMutableArray alloc] initWithCapacity:10];
NSMutableArray* array2 = [NSMutableArray arrayWithCapacity:10];
array1 was created using alloc+init, therefore you own it. It will stick around until you release it.
array2 was not created using alloc+init, therefore you do not own it. You're not responsible for releasing it, but it will go away on its own. You must retain array2 if you want it to stick around.
Your list property declaration is keeping you from properly retaining the NSMutableArray. By calling arrayWithCapacity you're effectively putting the array in an autorelease pool, which means it could be deallocated at any time if no object interested in keeping it around. While you are, the way you have things declared doesn't reflect that:
#property (readonly, assign) NSMutableArray *list;
The above declaration simply sets this pointer to be a copy of another pointer - it does no memory management for you. Instead it should read:
#property (readonly, retain) NSMutableArray *list;
... and you should assign the list like so:
self.list = [NSMutableArray arrayWithCapacity:64];
Because you specify the retain attribute for the property, whenever it is assigned a new value the retain message will be sent to that new value, communicating to the memory manager that you don't want this object deallocated. In order to bring this full circle, you'll need to release the object when you containing class is deallocated:
- (void)dealloc
{
[list release];
[super dealloc];
}
Are you initializing your list properly? Ie do you have something like the following in your code?
list = [[NSMutableArray alloc] init];
Problem ehre (assuming you initing your array properly) could be that #"Foobar" assings an NSString not an NSMutableString so its failing because if distinct types you should do
NSMutableString *listItem = [NSMutableString stringWithString:#"Foobar"];
[list addObject:listItem];
or
NSString *listItem =#"FooBar";
[list addObject:listItem];
It doesn't look as though you've actually initialized the NSMutableArray.
In the init event of the object, just say
[self setList:[[NSMutableArray alloc] initWithCapacity:10]]];
(I would just say init, but I don't remember if that works. It doesn't matter what capacity you start with)
Before actually allocating the array, the variable "list" will have a value of nil.