for (int i=0; i<array.count; i++)
{
NSString *name = [NSString stringWithFormat:#"array%i",i];
NSMutableArray *stringName = [[NSMutableArray alloc] init];
}
I don't know its right or wrong i just want to create count number of times NSMutableArray Should create with different names like
the name string represent
name = array0
name = array1
name = array2.....name=array10
I want to Create
NSMutableArray *array0 = [[NSMutableArray alloc]init];
NSMutableArray *array1 = [[NSMutableArray alloc]init];
NSMutableArray *array2 = [[NSMutableArray alloc]init];
.
.
.
NSMutableArray *array10 = [[NSMutableArray alloc]init];
Like this
NSMutableArray *myArray = [NSMutableArray new];
for(int i=0;i<numOfSubArrays;i++){
NSMutableArray *tempArray = [NSMutableArray new];
[myArray addObject: tempArray];
}
[myArray[0] addObject:object00];
[myArray[0] addObject:object01];
.
.
[myArray[1] addObject:object10];
[myArray[1] addObject:object11];
//etc
//myArray[1][1] is object11
Related
I'm getting the n value from the user, according to it I want to create n number of NSMutableArray .
Yes you can, just add the arrays to another NSMutableArray. Use this code:
NSMutableArray *outerArray = [NSMutableArray arrayWithCapacity:n];
for (int i=0; i<n; i++) {
NSMutableArray *innerArray = [[NSMutableArray alloc] init];
[outerArray addObject:innerArray];
}
int n= 5; // or whatever user gives you
count = 0;
NSMutableArray *arrayOfArrays = [NSMutableArray array];
while(count < n)
{
NSMutableArray *anArray = [NSMutableArray array];
[arrayOfArrays addObject: anArray];
count ++;
}
By using an array of arrays:
NSMutableArray *arrays = [NSMutableArray new];
while (gotMoreInput) {
NSMutableArray *array = [NSMutableArray new];
[array addObject:#"Some data"];
[arrays addObject:array];
}
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);
Why can I not access the 4th element of an NSMutableArray when I initialise with a NSString object rather than input the text manually?
For Example:
NSString * d = #"d";
self.arr = [[NSMutableArray alloc] initWithObjects:
[NSMutableArray arrayWithObjects:#"A",#"B",#"C",d,nil],
[NSMutableArray arrayWithObjects:#"A",#"B",#"C",#"d",nil],
nil];
This causes a beyond bounds error (NSString):
NSMutableArray *subArray = [self.arr objectAtIndex:0];
question = [subArray objectAtIndex:3];
This doesn't (#"d"):
NSMutableArray *subArray = [self.arr objectAtIndex:1];
question = [subArray objectAtIndex:3];
Why does this happen? Surely they are the same thing? I was hoping to reduce the amount of times I would have to write #"d".
They should be same... i tried following in my project. it works well.
NSString * d = #"d";
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:
[NSMutableArray arrayWithObjects:#"A",#"B",#"C",d,nil],
[NSMutableArray arrayWithObjects:#"A",#"B",#"C",#"d",nil],
nil];
NSLog(#"0 array 4th:%#",[[array objectAtIndex:0] objectAtIndex:3]);
NSLog(#"1 array 4th:%#",[[array objectAtIndex:1] objectAtIndex:3]);
output is as followings:
2013-12-16 17:05:30.092 Demo[1005:60b] 0 array 4th:d
2013-12-16 17:05:30.094 Demo[1005:60b] 1 array 4th:d
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.
I have this...
allTableData = [[NSMutableArray alloc] initWithObjects:
[[Food alloc] initWithName:#"1" andDescription:#"Hi man!"],
[[Food alloc] initWithName:#"2" andDescription:#"Hi woman"],
nil ];
How can I do something like that?
NSArray *array = [NSArray arrayWithObjects:#"Hi man",#"Hi woman",nil];
allTableData = [[NSMutableArray alloc] initWithObjects
for(i=0; i<2; i++)
{
NSString *aString = [NSString stringWithFormat:#"%d", i];
[[Food alloc] initWithName:aString andDescription:#"array[i]"];
}
...as well as enter through a cycle?
[[Food alloc] initWithName:aString andDescription:#"array[i]"];
should be
[[Food alloc] initWithName:aString andDescription:array[i]];
In the first case you were adding the string "array[i]", where instead you want to access the i-th element of the array.
Also, there's a shorter way for converting an int to NSString
[NSString stringWithFormat:#"%d", i];
can be replaced by
#(i).stringValue
The # syntax for numbers is a feature introduced by llvm 3.3, that allows you to create NSNumbers literals. NSNumber has then the method stringValue that you can use to get the desired NSString.
Try this
NSArray *array = [NSArray arrayWithObjects:#"Hi man",#"Hi woman",nil];
allTableData = [NSMutableArray array];
for(i=0; i<[array count]; i++){
NSString *aString = [#(i) stringValue];
Food *food = [[Food alloc] initWithName:aString andDescription:array[i]];
[allTableData addObject:food];
}
NSArray *array = [NSArray arrayWithObjects:#"Hi man",#"Hi woman",nil];
NSMutableArray* tableData = [NSMutableArray arrayWithCapacity:array.count];
[array enumerateObjectsUsingBlock:^(id obj,NSUInteger index,BOOL* stop){
[tableData addObject:[[Food alloc] initWithName:[#(index) stringValue] andDescription:obj]];
}];