how add empty object on 2D array - ios

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.

Related

How to Create Arrays With Different Names Based on Some count times

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

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?

Accessing NSString within NSMutableArray

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

NSMutableArray adding into a cycle

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]];
}];

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