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
Related
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
I have a problem where an attribute turns to nil after an iteration:
NSMutableArray * lojas = [[NSMutableArray alloc] init];
for (int x = 0; x < lojaResultado.count; x++) {
NSDictionary * listaAtributos = [lojaResultado objectAtIndex: x];
Loja * loja = [[Loja alloc] init];
NSMutableArray * produtosLista = [[NSMutableArray alloc] init];
[loja setName: [listAtributos objectForKey: #"Loja"]];
NSArray * produtosResultado = [[NSArray alloc] initWithArray: [listaAtributos objectForKey: #"Produtos"]];
for(int y = 0; y < produtosResultado.count; y++){
NSDictionary * produtoAtributos = [produtosResultado objectAtIndex:y];
Produto * produto = [[Produto alloc] init];
[produto setNome: [produtoAtributos objectForKey:#"Nome"]];
getNumber = [produtoAtributos objectForKey: #"Tipo"];
[produto setTipo: [getNumber intValue]];
getNumber = [produtoAtributos objectForKey: #"Tamanho"];
[produto setTamanho: [getNumber intValue]];
[produtosLista addObject: produto];
}
loja.produtos = produtosLista;
[lojas addObject: loja];
}
During the iteration I can see, at the debug mode, that my objetc loja receive the correct name on the method setName and the correct list (loja.produtos = produtosLista).
After add the object loja into my array lojas I can see the correct object, but when the second iteration starts, the object at the first array position has its attribute produtos (array) setted to nil.
Has someone had this problem before? Or can someone say what I am doing wrong?
Loja .h file:
#property (nonatomic) NSString * name;
#property (strong, nonatomic) NSMutableArray * produtos;
I saw in your code that [produtosLista addObject: tires]; but tires is not created in the method block, its seems like in second iteration tires is flushed out.
as your implementation its seems like you want to add product, can you try this code.
Update:
I updated the code snippet with using fast enumeration and removed alloc/init for array allocation and used autorelease concept.
NSMutableArray * lojas = [[NSMutableArray alloc] init];
for (NSDictionary *listaAtributos in lojaResultado) {
Loja * loja = [[Loja alloc] init];
[loja setName:[listAtributos objectForKey: #"Loja"]];
NSMutableArray * produtosLista = [NSMutableArray array];
for(NSDictionary * produtoAtributos in [listaAtributos objectForKey: #"Produtos"]){
Produto * produto = [[Produto alloc] init];
[produto setNome:[produtoAtributos objectForKey:#"Nome"]];
[produto setTipo:[[produtoAtributos objectForKey: #"Tipo"] intValue]];
[produto setTamanho:[[produtoAtributos objectForKey:#"Tamanho"] intValue]];
[produtosLista addObject:produto];
}
[loja setProdutos:produtosLista];
[lojas addObject: loja];
}
In my code, I set array inside a NSMutableDictionary as
if (array.count > 0) {
[self.filters setValue:array forKey:[self getKey:[[NSNumber numberWithInt:indexPath.section] intValue]]];
}
where array is
NSMutableArray *array = [[NSMutableArray alloc] init];
When the receiving code receives it, I tried to join the values in array as
if ([item objectForKey:#"category_filter"] != nil) {
NSArray *array = [NSArray arrayWithObjects:[item objectForKey:#"category_filter"], nil];
NSString *categories = [array componentsJoinedByString:#","];
NSLog(#"value:%#", categories);
}
where item is (NSMutableDictionary *)item
When I see log, I see as
2014-06-24 17:43:12.520 yelp[69744:70b] value:(
"Bagels (bagels)",
"Bakeries (bakeries)"
)
so they are not joined yet. What am I doing wrong here?
As Hot Licks commented,
I had to make change as
NSArray *array = [NSArray arrayWithArray:[item objectForKey:#"category_filter"]];
instead of
NSArray *array = [NSArray arrayWithObjects:[item objectForKey:#"category_filter"]];
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]];
}];