Trouble initialising NSMutableArray with NSString - ios

I'm having problem initialising an NSMutableArray called _entryArray with 2 NSString objects #"00:00:00". I'm getting the following error:
Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array'
When I try to initialise it without any objects in it, I get no errors. What do I do?
Thank you very much
code:
#implementation MainViewController{
#private
int n;
NSMutableArray *_entryArray;
NSMutableArray *_timeSinceLastEntryArray;
NSMutableArray *_timeInterval;
NSMutableArray *_timeInBackup;
NSMutableArray *_timeOutBackup;
BOOL whichButton;
}
-(void)viewWillAppear:(BOOL)animated {
}
- (void)viewDidLoad
{
[super viewDidLoad];
n=0;
_brain = [[Brain alloc] init];
_entryArray = [[NSMutableArray alloc] initWithObjects:#"00:00:00",#"00:00:00",nil];
_timeSinceLastEntryArray = [[NSMutableArray alloc] init];
_timeInBackup = [[NSMutableArray alloc] initWithObjects:(NSNumber *)0, nil];
_timeOutBackup = [[NSMutableArray alloc] initWithObjects:(NSNumber *)0, nil];
_timeInterval = [[NSMutableArray alloc] init];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}

These two lines result in arrays with zero objects:
_timeInBackup = [[NSMutableArray alloc] initWithObjects:(NSNumber *)0, nil];
_timeOutBackup = [[NSMutableArray alloc] initWithObjects:(NSNumber *)0, nil];
You are not adding the number 0, you are adding the pointer 0. This is the same as nil. In other words, this code is really this:
_timeInBackup = [[NSMutableArray alloc] initWithObjects:nil, nil];
_timeOutBackup = [[NSMutableArray alloc] initWithObjects:nil, nil];
If your intent is to add the number 0, do this:
_timeInBackup = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0], nil];
_timeOutBackup = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0], nil];
Better yet, do this:
_timeInBackup = [#[ #0 ] mutableCopy];
_timeOutBackup = [#[ #0 ] mutableCopy];
You also have other empty arrays:_timeSinceLastEntryArray and _timeInterval. None of the code you have posted is actually causing the error. You need to provide more details to know for sure which array is really causing you the problem.

Related

When I add Key value Dictionary in Key Value Dictionary of NsMutableArray at Position of 0. it will be throw in Exception

Below is my Code, When I Add Dictionary into NSMutableArray that time it works well but when I Put NSLog It will throw an Exception. So, Please Help me
//-->
arrMain = [[NSMutableArray alloc] init];
NSMutableArray *arrMainMenu = [[NSMutableArray alloc] init];
NSMutableArray *arrMainMenu2 = [[NSMutableArray alloc] init];
for(int i = 0; i < arrParent.count; i++)
{
NSMutableDictionary *dictMain = [arrParent objectAtIndex:i];
if([[dictMain valueForKey:E_UNDER] isEqualToString:#""])
{
NSString *titleMain = [dictMain valueForKey:E_PARAMETER_VALUE_NAME];
NSMutableArray *arrSubMenu = [[NSMutableArray alloc] init];
for (int j = 0; j <arrSub.count; j++)
{
NSMutableDictionary *dictSub = [[arrSub objectAtIndex:j] mutableCopy];
if([titleMain isEqualToString:[dictSub valueForKey:E_UNDER]])
[arrSubMenu addObject:dictSub];
}
if([arrSubMenu count])
{
[dictMain setValue:#"1" forKey:IS_EXAPANDABLE];
[dictMain setValue:#"0" forKey:IS_EXPANDED];
[arrSubMenu insertObject:dictMain atIndex:0]; //Here Add Key-Value Dictionary in Array
[dictMain setValue:arrSubMenu forKey:LIST_ID];
arrSubMenu = [[NSMutableArray alloc] init];
}
else
[dictMain setValue:#"0" forKey:IS_EXAPANDABLE];
[arrMainMenu addObject:dictMain];
}
}
NSLog(#"%#", arrMainMenu); //Here Throw Bad Access exception
[obj_DataModel setFilterMenu:arrMainMenu];
//-->

How to replace NSMutableArray List data

I am very new for IOS. In my app, I have inserted two UIButtons on my mainViewController
When I click first button I am adding some data in my Main NSMutableArray and when I click "second" button I want remove previous array details and replace new data.
My code:
- (void)viewDidLoad {
[super viewDidLoad];
mainArray = [[NSMutableArray alloc] init];
}
- (IBAction)button1:(id)sender{
NSMutableArray * data1 = [[NSMutableArray alloc]
initWithObjects:#"1",#"2",#"3", nil];
[mainArray replacementObject:data1];
}
- (IBAction)button2:(id)sender{
NSMutableArray * data2 = [[NSMutableArray alloc]
initWithObjects:#"1",#"2",#"3",#"4",#"5", nil];
[mainArray removeObjectAtIndex:data2];
}
Please help.
Try this code
- (IBAction)button1:(id)sender{
NSMutableArray * data1 = [[NSMutableArray alloc]
initWithObjects:#"1",#"2",#"3", nil];
[mainArray addObject:data1];
}
- (IBAction)button2:(id)sender{
NSMutableArray * data2 = [[NSMutableArray alloc]
initWithObjects:#"1",#"2",#"3",#"4",#"5", nil];
[mainArray replaceObjectAtIndex:0 withObject:data2];
}
I assumed your mainArray contains only one element. If there are more elements you need to specify correct index
Try This one:
- (IBAction)button1:(id)sender{
[mainArray addObjectsFromArray:[[NSMutableArray alloc]
initWithObjects:#"1",#"2",#"3", nil]];
}
- (IBAction)button2:(id)sender{
[mainArray replaceObjectsInRange:NSMakeRange(0, mainArray.count) withObjectsFromArray:[[NSMutableArray alloc]
initWithObjects:#"6",#"2",#"3",#"4",#"5", nil]];
}
- (void)viewDidLoad {
[super viewDidLoad];
mainArray = [[NSMutableArray alloc]init];
}
- (IBAction)button1:(id)sender{
mainArray = [[NSMutableArray alloc]
initWithObjects:#"1",#"2",#"3", nil];
}
- (IBAction)button2:(id)sender{
mainArray = [[NSMutableArray alloc]
initWithObjects:#"1",#"2",#"3",#"4",#"5", nil];
}

Is there a way to duplicate the number of NSDictionaries in an array?

I currently have this array of NSDictionarys in my table view, but I need to duplicate the number of dictionaries in the array as the table view scrolls down. Is there a way to achieve this, and also specify or know what the dictionary names will be?
NSMutableDictionary *d0 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *d1 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *d2 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *d3 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *d4 = [[NSMutableDictionary alloc] init];
objects = [NSMutableArray arrayWithObjects:d0, d1, d2, d3, d4, nil];
First of all, it seams to be easier to create the dictionaries in a loop:
NSMutableArray *objects = [NSMutableArray new];
for( int i = 0; i < 5; i++ )
{
[objects addObject:[NSMutableDictionary new]];
}
If you want to "duplicate" the dictionaries, you can do that with a loop, too:
for( NSMutableDictionary *dictionary in [objects copy])
{
[objects addObject:[dictionary mutableCopy]];
}
But I cannot get rid of the feeling that your whole approach is wrong. What is the idea behind that?

how add empty object on 2D array

I have 2D NSMutableArray with my class objects.
Example:
NSMutableArray *myArray2D = [[NSMutableArray alloc] init];
for (int i=0;i<10;i++) {
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObject:cardView];
[myArray addObject:cardView];
[myArray addObject:cardView];
[myArray2D addObject:myArray];
}
[[myArray2D lastObject] removeAllObjects];
My last object in myArray2D is has "0 objects"
Then I need remove last object in my myArray2D
// .... some code
Then I want to add this object with "0 object" back
How I can do this?
I'm not sure what you mean by "this object", because the empty array that you delete from myArray2D will be deallocated after you remove it. If you want to add an empty array to myArray2D, just alloc init a new array and add it:
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray2D addObject:myArray];
I'm not sure but maybe this is your answer
NSMutableArray *myArray2D = [[NSMutableArray alloc] init];
for (int i=0;i<10;i++) {
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObject:cardView];
[myArray addObject:cardView];
[myArray addObject:cardView];
[myArray2D addObject:myArray];
}
[[myArray2D lastObject] removeAllObjects];
NSMutableArray *tmpArray = [myArray2D lastObject];
[myArray2D removeLastObject];
[myArray2D addObject:tmpArray];
You create an empty array like this: NSMutableArray *array = [NSMutableArray array];
You cannot add nil objects to an NSArray.

Can I release an object which was added to a NSMutableArray or does this affect a crash?

I've got one question about the memory management of the NSMutableArray. I create an object of my custom class and add this object to a NSMutableArray. Is this automatically retained, so that I can release my created object?
Thanks!
Yes, it is automatically retained. You should release your object after adding it to the array (or use autorelease)
For example:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[[[MyClass alloc] init] autorelease]];
// or
MyClass * obj = [[MyClass alloc] init];
[array addObject:obj];
[obj release];
Yes, you can do this for example:
NSMutableArray *array = [[NSMutableArray alloc] init];
NSString *someString = #"Abc";
[array addObject:someString];
[someString release];
NSLog(#"Somestring: %#", [array objectAtIndex:0]);

Resources