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];
}
Related
I need to sort array (in IOS) like this...
let us suppose that i have an NSMutableArray with : 1,2,3,4,5
i need to build a new array with the last object in the middle and the middle will replace the last : 1.2.5.4.3...
please help:
this is what i doing for now, i got only the 1.2.5 but i not success to put the 4.3 ...
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:#1,#2,#3,#4,#5, nil];
int arrCount = (int)arr.count/2;
NSMutableArray *arr2 = [[NSMutableArray alloc] init];
for (int i = 0;i < arr.count; i++) {
if (i < arrCount) {
[arr2 addObject:arr[i]];
}
else{
index = i;
[arr2 addObject:arr.lastObject];
}
}
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:#1,#2,#3,#4,#5, nil];
NSMutableArray *arr2 = [[NSMutableArray alloc] initWithArray:arr];
int middleIndex = (int)arr.count/2;
id middleObject = arr2[middleIndex];
[arr2 replaceObjectAtIndex:middleIndex withObject:arr2.lastObject];
[arr2 replaceObjectAtIndex:arr2.count-1 withObject:middleObject];
NSLog(#"%#", arr2);
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.
Hi I have a grouped tableview the first section contains a list of emails and the second section just has two rows which are add email manually and select email from contacts.
The log in ManualEmail.m keeps logging 0 for the count and the array in EmailViewController is never modified, but I can't figure out what's wrong
This is my current set up
EmailViewController.h
#property (nonatomic, retain) IBOutlet NSMutableArray *dataArray;
EmailViewController.m
#synthesize dataArray;
- (void)viewDidLoad {
[super viewDidLoad];
dataArray = [[NSMutableArray alloc] init];
NSMutableArray *listItems = [[NSMutableArray alloc] initWithObjects:nil];
[listItems addObject:[ObjectArrays productWithType:#"test" Eemail:#"test#website.com" Eselected:YES]];
NSDictionary *firstItemsArrayDict = [NSDictionary dictionaryWithObject:listItems forKey:#"data"];
[dataArray addObject:firstItemsArrayDict];
NSArray *secondItemsArray = [[NSArray alloc] initWithObjects:#"Add Email Address From Contacts", #"Add Email Address Manually", nil];
NSDictionary *secondItemsArrayDict = [NSDictionary dictionaryWithObject:secondItemsArray forKey:#"data"];
[dataArray addObject:secondItemsArrayDict];
[tableView reloadData];
}
ManualEmail.m
EmailViewController *emailPVC = [[EmailViewController alloc] init];
NSDictionary *dictionary = [emailPVC.dataArray objectAtIndex:0];
NSArray *array = [dictionary objectForKey:#"data"];
NSMutableArray *emailArray = [NSMutableArray arrayWithArray:array];
[emailArray addObject:[ObjectArrays productWithType:name.text Eemail:email.text Eselected:YES]];
[emailPVC.dataArray removeObjectAtIndex:0];
NSDictionary *firstItemsArrayDict = [NSDictionary dictionaryWithObject:emailArray forKey:#"data"];
[emailPVC.dataArray insertObject:firstItemsArrayDict atIndex:0];
NSLog(#"%d", [emailPVC.dataArray count]);
In ManualEmail.m, you're creating the EmailViewController in code. But you aren't ever calling anything that would call its -viewDidLoad method, so the dataArray isn't ever getting created or filled out. You need to call emailPVC.view = <some view you created>; in order for -viewDidLoad to get called.
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.
How to return all Array value if it created and initial in separate class of type NSMutableArray.
This is my simple code.
In FirstClassViewController
- (void)viewDidLoad
{
[super viewDidLoad];
MyArrayClass *thisArray = [[MyArrayClass alloc]init];
NSLog(#"%#",thisArray.getArray);
}
In MyArrayClass.h
#interface MyArrayClass : NSMutableArray{
NSMutableArray *courseArray;
}
-(NSString*)getArray;
-(void) setArray;
#end
In MyArrayClass.m
-(NSString*) getArray{
return courseArray;
}
-(void) setArray{
courseArray = [[NSMutableArray alloc]init];
// AR111:
[courseArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:
#"AR111",#"courseCode",
#"Arabic Communication Skills (I)",#"courseName",
#"3",#"creditHours",
nil]];
// AR112:
[courseArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:
#"AR112",#"courseCode",
#"Arabic Communication Skills (II)",#"courseName",
#"3",#"creditHours",
nil]];
}
I have this output message:
2012-07-19 10:36:18.344 test[622:f803] (null)