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];
}
Related
I have an NSMutableArray that contains several NSMutableDictionary object, each dict has a NSString and an NSMutableArray in it.
My problem is that I need to find the correct dict based on a string match and when found insert an object into the NSMutableArray in the same dict where the string match was found, I can only make this work the first time when the array is empty because after that I am unable to match the correct string:
Here is what I have tried so far, I tried using contains object but that wont get me inside the dict inside the array so I am stuck
NSMutableArray *variantOptions = [[NSMutableArray alloc] init];
for (NSDictionary *variant in variantInfo) {
NSMutableDictionary *variantOption = [[NSMutableDictionary alloc] init];
NSMutableArray *variantOptionValues = [[NSMutableArray alloc] init];
NSString *name = variant[#"name"];
NSString *value = variant[#"value"];
if(variantOptions.count > 0) {
// Need to loop through the array until name isEquaToString variantOptionName and when true insert value into variantOptionValuesArray in that same dict and if not exists create a new dict and insert in array
} else {
[variantOptionValues addObject:value];
[variantOption setObject:name forKey:#"variantOptionName"];
[variantOption setObject:variantOptionValues forKey:#"variantOptionValues"];
}
[variantOptions addObject:variantOption];
}
for (NSDictionary *variant in variantInfo) {
NSString *name = variant[#"name"];
NSString *value = variant[#"value"];
BOOL found = false;
for (NSMutableDictionary *v in variantOptions) {
if (v[#"name"] isequalToString:name]) {
NSMutableArray *values = v[#"variantOptionValues"];
[values addObject:value];
found = true;
break;
}
}
if (!found) {
// name was not found
NSMutableDictionary *variantOption = [[NSMutableDictionary alloc] init];
NSMutableArray *variantOptionValues = [NSMutableArray alloc] init];
[variantOptionValues addObject:value];
[variantOption setObject:name forKey:#"variantOptionName"];
[variantOption setObject:variantOptionValues forKey:#"variantOptionValues"];
[variantOptions addObject:variantOption];
}
}
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];
//-->
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);
When executing the below block. My friends_dict NSMutableDictionary value changes as friend changes. If I'm not wrong this is because the reference of the value in the dictionary is passed along to the friend. How can I pass the value instead of reference and prevent the dictionary from changing.
NSMutableArray *friendsArray = [[NSMutableArray alloc] initWithArray:[friends_dict valueForKey:selectedFriendId]];
for (NSObject *object in [response objectForKey:#"friend_nearby"]){
NSString *id = [NSString stringWithFormat:#"%#",[object valueForKey:#"id"]];
NSString *spotValue = [NSString stringWithFormat:#"%#",[object valueForKey:#"spot"]];
for(int i = 0; i < [friendsArray count]; i++){
FriendDataModel *friend = [[FriendDataModel alloc] init];
friend = [friendsArray objectAtIndex:i];
if ([friend.friendId isEqualToString:id] && ![friend.spot isEqualToString:spotValue] ) {
friend.spot = spotValue; //This is where the dictionary value changes
[friendsArray replaceObjectAtIndex:i withObject:friend];
spotChanged = YES;
}
}
}
Copy the array and pass in the copy.
NSArray *friendList = [friends_dict valueForKey:selectedFriendId];
NSMutableArray *friendsArray = [friendList mutableCopy];
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]];
}];