Global array loses values after exiting method - objective-c

At the top of my .m file I have
static NSMutableArray *name;
I load a bunch of values into my *name array inside my viewDidLoad method.
I have a slider that can modify the values inside this array. The slider method is only called when the value of the slider changes. However, I ran this code and every time my program exits the viewDidLoad method I lose the values that were added to the global variable name. I can see that they were there before exiting the viewDidLoad method.
What am I doing wrong?
EDIT: Inside viewDidLoad
if (name == nil)
name = [NSMutableArray array];
UITextField *nameTemp = [[UITextField alloc] initWithFrame:CGRectMake(20,20,20,20)];
nameTemp.returnKeyType = UIReturnKeyDone;
etc
[self.view addSubview: nameTemp];
[name addObject:nameTemp]
[nameTemp release];

[NSMutableArray array] creates an autoreleased array that apparently is being released at the end of your viewDidLoad method. Try using [[NSMutableArray alloc] init] or [[NSMutableArray array] retain] and see if the values persist after viewDidLoad returns.

Related

unable to store an image in an NSMutableArray

I am trying to save an image in an NSMutable array and it is not working
here is what I am doing
[imagesList addObject:[UIImage imageNamed:#"b.png"]];
after executing this line I noticed that the number of objects remains 0
any reason ?
Thanks
I repeate this code in several areas :
Globally I declare :
NSMutableArray *imagesList;
NSUserDefaults *imagesHistory;
in my viewdidload method I write:
imagesHistory=[NSUserDefaults standardUserDefaults]; //imagesHistory is created globallt as NSUserDefault imagesHistory
[imagesHistory setObject:imagesList forKey:#"images history"];
UIImage *image;
image=[UIImage imageNamed:#"b.png"];
[imagesList addObject:image];
imagesHistory=[NSUserDefaults standardUserDefaults];
[imagesHistory setObject:imagesList forKey:#"images history"];
and in the didFinishLaunchingWithOptions I write : (even though I don t need to do it when I am adding strings ...)
imagesList = [[NSMutableArray alloc] init];
Regardless of whether it is a global variable or not, you still need to call alloc and init SOMEWHERE for the object. If you intend to use it throughout your app, then appDidFinishLaunchingWithOptions is a decent place to add this call:
imagesList = [[NSMutableArray alloc] init];
Is your empty array being retained? If you're not using Automatic Reference Counting, there's a good chance you're initializing the array with the following
imagesList = [[NSMutableArray alloc] init];
but it's not being retained. You'll want to retain the empty array so it gets appended to further on in your code.
imagesList = [[[NSMutableArray alloc] init] retain];
Just don't forget to release the array when you're all done with it, in viewDidUnload or wherever is appropriate.
You're allocating/initing imagesList AFTER you try to add an object. You need to alloc/init imageList before you add anything to it.
To make sure it's there, try something like this:
if (!imagelist) imageList = [[NSMutableArray alloc] init];
If it exists, and you're still having this problem, it's possible that you're allocating/initing a new NSMutableArray and assigning it to imageList after you've added the object. This means the old array would be discarded and you'd be left with a new array with zero items.
try UIImageView *image;
image=[UIImage imageNamed:#"b.png"];
also, in #property(nonatomic,retain) use 'strong' instead of 'retain'

NSLog statement does not print my object

If i add an object to a property "dreamsArr", which is NSMutableArray:
#synthesize dreamsArr;
-(void) viewDidLoad
{
Dream *d1 = [[Dream alloc] init];
d1.title = [[NSString alloc] initWithString:#"my 1st dream"];
self.dreamsArr = [[NSMutableArray alloc] init];
[self.dreamsArr addObject:d1];
Dream *dr = [self.dreamsArr objectAtIndex:0];
NSString *title=dr.title;
const char *chTitle = [title UTF8String];
NSLog(#"%s", chTitle);
}
Why does NSLog print "null"?
when i trace this code, after [self.dreamsArr addObject:d1] , dreamsArr still empty, why?
Because you're using char array placeholder (%s) in NSLog statement instead of object's which should be used to print NSString. Try this:
NSLog("%#", chTitle);
Also, unless you're using ARC, you have a leak on 2nd and 4th lines of viewDidLoad method.
The line below has a leak
d1.title = [[NSString alloc] initWithString:#"my 1st dream"];
The leak comes from the assignment of title property which (i presume) is #property with retain modifier. The alloc calls creates an object with +1 retain count while assignment makes it +2. Since you don't balance it out with release call below in the method - you get a leak. Same goes for the 4th line:
self.dreamsArr = [[NSMutableArray alloc] init];
which creates leak in the very same manner.
Leaks are valid only if you're not releasing your dreamsArr instance variable in viewDidUnload method and title instance variable in the dealloc method of your Dream class.
problem was in the other part of code

NSMutableArray becomes empty when I call any IBAction

Please help me i have a NSMutableArray which contains data until viewDidLoad is finished.
When I click any button I try to get the data in it and it just disappears.
If you created your array with a variant of [NSMutableArray array] it will be autoreleased. Assuming myArray is a property of your class, you should use
myArray = [[NSMutableArray] alloc] init]; //or initWithCapacity, etc.
If you still use reference counting, you need to add [myArray retain]; unless it is already retained in the #property declaration and release it in viewDidUnload:.

NSMutableArray vs NSArray

i have created a script, witch load the XML from server parse it an save it to NSMutableArray
[kategorienArray addObject:catName];
catName ist a String, if i NSLog the Array everything works fine.
After that i have created a tebleview, and reload Data
[kategorienAuswahl reloadData];
KategorienAuswahl is my TableView
and now a get the Problems
if i Use a "normal" array
NSArray *array = [[NSArray alloc] initWithObjects:#"iPhone", #"iPod", #"iPad",nil];
self.listData = array;
[array release];
the will be displayed, but if i use
cell.textLabel.text = [kategorienArray objectAtIndex:row];
i get EXC_BAD_ACCESS
instead it works fine with
cell.textLabel.text = [listData objectAtIndex:row];
I Add nwo
kategorienArray = [[NSMutableArray alloc] init];
kategorienArray = [NSMutableArray arrayWithCapacity:10];
but now i get no data in my Tableview
You might not have initialized your kategorienArray.
Do you have a kategorienArray = [[NSMutableArray alloc] init]; in your code? You should have done this before adding objects to it and then accessing them.
Also, make sure your array is retained at the point where it's created, so that the system doesn't reclaim its memory before you access its items. The way I use to make sure things are going right is to declare the mutable array as a property. In your viewcontroller's .h file put
#property (nonatomic, retain) NSMutableArray *kategorienArray;
and in your .m file put
#synthesize kategorienArray;
at the top and then
self.kategorienArray = [NSMutableArray array];
in viewDidLoad or somewhere, before adding items to it.
The problem may be that 'kategorienArray' has been (auto)released.
Is 'kategorienArray' an ivar ?
Check out if when you created the array you correctly retained it (particularly if you created it with a class method).

How to release objects stored in an array?

Please look at the code below and suggest the best approach. I can't quite tell whether the code is correct. When adding objects to arrays, do they get a retain count? In the second function, am I releasing the local variable "mySubview" or the original object?
// this is a class property
myArray = [[NSMutableArray alloc] init];
- (void)createSubview
{
UIView *mySubview = [[UIView alloc] init];
[self addSubview:mySubview];
[myArray addObject:mySubview];
}
-(void)eventHandler:(NSNotification *) notification
{
UIView *mySubview = [notification object];
[myArray removeObjectIdenticalTo:mySubview];
[mySubview removeFromSuperview];
[mySubview release];
}
When adding objects to arrays, do they
get a retain count?
Yes.
In the second function, am I releasing
the local variable "mySubview" or the
original object?
UIView *mySubview;' defines a local variable, mySubview, which is a pointer to -- a reference to -- an instance of the UIView class. There is no such thing as a "local object" or "stack object" in Objective-C (save for blocks, but that is beyond the scope of this question).
So, no, when you call [mySubview release] you are sending -release to the instance of of UIView included in notification.
That release is balancing the retain implied by the alloc. Which isn't the right pattern at all. You should do something like:
- (void)createSubview
{
UIView *mySubview = [[UIView alloc] init];
[self addSubview:mySubview];
[myArray addObject:mySubview];
[mySubview release];
}
-(void)eventHandler:(NSNotification *) notification
{
UIView *mySubview = [notification object];
[myArray removeObjectIdenticalTo:mySubview];
[mySubview removeFromSuperview];
}
Oh, by "class property", I'm assuming you mean "instance variable"?