NSMutableArray adds same object each time - ios

I am adding object in NSMutableArray it adds but it adds the same object three time instead of different or new.
Calendar Sow Array has data Breedingdate and sow name.
for (SOWObject *object in appDelegate.calenderSowArray) {
temp = object.breedingDate;
NSLog(#"Date %#",temp);
[arrayNew removeAllObjects];
for (indxs = 0; indxs <countOfarray; indxs ++) {
SOWObject *neObject= (SOWObject *)[appDelegate.calenderSowArray objectAtIndex:indxs];
NSLog(#"Breeding Date %#",neObject.breedingDate);
if ([temp isEqualToString:neObject.breedingDate]) {
[arrayNew removeAllObjects];
[arrayNew addObject:neObject];
}
}
[testArray addObject:arrayNew];
}

Try this,
NSMutableArray *testArray = [[NSMutableArray alloc]init];
for (SOWObject *object in appDelegate.calenderSowArray)
{
[testArray addObject:object];
}
This alone should be enough if you want to add all the SOWObject's in appdelegate.calendarSowArray into the TestArray.

Check if the array contains the object before adding. By doing this a particular object will be added only once.
if(![arrayNew containsObject:neObject]) // if arrayNew does not contain neObject, add it to the array
{
add it to the array
}
or if your testArray is adding same objects, then,
if(![testArray containsObject:arrayNew]) // if testArray does not contain arrayNew, add it to the array
{
add it to the array
}

It looks like you are attempting to create an array of arrays, however the inner array always contains exactly one element. If this is what you want then:
for (SOWObject *object in appDelegate.calenderSowArray) {
temp = object.breedingDate;
NSLog(#"Date %#",temp);
for (indxs = 0; indxs <countOfarray; indxs ++) {
SOWObject *neObject= (SOWObject *)[appDelegate.calenderSowArray objectAtIndex:indxs];
NSLog(#"Breeding Date %#",neObject.breedingDate);
if ([temp isEqualToString:neObject.breedingDate]) {
[testArray addObject:#[ neObject ]]; // This creates a new inner-array each time
}
}
}
And if not (you just want an array) then:
[testArray addObject:neObject];

Related

Compare the unique index value with another array in objective C

There is an array having same objects in single array , i need to compare these array’s index with another array.. Give me a help.
Something like:
NSMutableArray *latArray =
[NSMutableArray arrayWithObjects:#“43.20,#“43.23”,#“43.24”,#“43.20”,nil];
NSMutableArray *lngArray =
[NSMutableArray arrayWithObjects:#“76.90”,#“76.94”,#“76.92”,#“76.90”,nil];
NSMutableArray *imagesArray =
[[NSMutableArray alloc] initWithObjects:#"1.jpg", #"2.jpg”,#“3.jpg”,#“4.jpg”,nil];
resultResult = #"1.jpg", #“4.jpg” // because the index 0 and index 3 same values in both array.
I would wrap your coordinates into location objects and use them as the keys in a dictionary. This would allow to check for duplicate coordinates, like this:
NSMutableDictionary *results = [[NSMutableDictionary alloc] init];
for (int i = 0; i < [imagesArray count]; i++)
{
// Wrap coordinates into a NSValue object
// (CLLocationCoordinate2D is a C-struct that cannot be used as a dictionary key)
// (CLLocation also does not implement required methods to be usable as a dictionary key)
NSValue *loc = [NSValue valueWithMKCoordinate:CLLocationCoordinate2DMake(
((NSNumber)[latArray objectAtIndex:i]).doubleValue,
((double)[lngArray objectAtIndex:i]).doubleValue)];
// 1. If you only want the first occurrence of a specific location, use this:
if ([results objectForKey:loc] == nil)
{
[results setObject:[imagesArray objectAtIndex:i] forKey:loc];
}
// 2. Or, if you want the last occurrence of a specific location, use this:
[results setObject:[imagesArray objectAtIndex:i] forKey:loc];
}
I think you are trying the check for the same objects in an array. If so do the following.
for(int i=0;i<yourarray.count;i++)
{
NSString *yourstring=[yourarray objectatindex:i];
for(int k=0;k<yourarray.count;k++)
{
if(i!=k)
{
NSString *yourstring2=[yourarray objectatindex:k];
if([yourstring isEqualtostring yourstring2])
{
//now you got equal objects. do what ever you want here
}
}
}
}

Multi dimensional array containing array of dictionaries

Trough my ios first app developpement i have to re-order an array containing dictionaires, parsed from a xml document, the purpose of re-ordering it is to send it to a function that build a collapsible, so it need a childCell index and a parentCell Index to print the strings of each child then pass to another parent. The problem is here : i'am able to fill my big array containing arrays of dictionaries, then i that array and do a loop to fill the childArray to contain multiple dictionaries, then i add this child array to my parent array, every thing seem to run but it gives me an empty array at the end. i put my code to show you how i tried to do this :
stories is the NSArray of dictionaries, childArray is the Array that should contain the dictionaries of stories, and parentArray is the Array that contains it all.
If someone who already did that can explain me were it goes wrong please it would be very much appreciated.
-(NSMutableArray *)orderChildsAndParents:(NSMutableArray *)fromArray
{
int varial = 0;
int catIndex = 0;
NSMutableArray *parentArray = [NSMutableArray array];
while(varial < [stories count])
{
NSString* cleanedString = [[[[stories objectAtIndex:varial] objectForKey:#"category"] componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
componentsJoinedByString:#""];
if ([cleanedString isEqualToString:[category objectAtIndex:catIndex] ])
{
if (!childArray || !childArray.count)
childArray = [NSMutableArray array];
[childArray addObject:[stories objectAtIndex:varial]];
varial++;
}
else{
[parentArray addObject:childArray];
[childArray removeAllObjects];
catIndex++;
}
}
NSLog(#"%#", parentArray);
return parentArray;
}
- (NSString *) labelForCellAtChildIndex:(NSInteger) childIndex withinParentCellIndex:(NSInteger) parentIndex {
NSMutableArray *orderedArray = [self orderChildsAndParents:stories];
NSLog(#"format string %#", [[[orderedArray objectAtIndex:parentIndex] objectAtIndex:childIndex] objectForKey:#"name"]); // empty :8
return [[[orderedArray objectAtIndex:parentIndex] objectAtIndex:childIndex] objectForKey:#"name"];
}

Strings end up as nulls

I have a sequence of code. In it there are custom objects: Item and ItemList. The relevant members of these are Item.code and ItemList.ItemDictionary. Item.code is simply a NSString code (Like "AVK") that uniquely identifies an Item. These Items are separated into categories. There are 4 categories (Named "CatOne", etc). The ItemDictionary is a dictionary with the category names as keys and an NSMutableArray as the object; the array is filled with Item objects that are part of the category.
The basic problem is that when I try to access the Item.code, the string comes out as (null).
The functionality is that I have an array of updated Items (updatedItems) and I want to update the ItemList.ItemDictionary with these new Items.
The following are all properties of the object, and are synthesized in the main file.
#synthesize ItemListFromFile;
#synthesize upDatedItems;
#synthesize tempPassedManifest;
And the code:
-(id) upDatedItems:(NSArray *)newItems manifest:(Manifest *)manifest {
ItemListFromFile = [[ItemList alloc] init];
ItemListFromFile.ItemDictionary = [[NSMutableDictionary alloc] init];
ItemListFromFile = [NSKeyedUnarchiver unarchiveObjectWithFile:manifest.ItemListSavePath];
updatedItems = newItems
tempPassedManifest = manifest;
[self UpdateItemList];
return self;
}
-(void)UpdateItemList {
NSMutableArray *newItemArray = [[NSMutableArray alloc] init];
NSMutableArray *oldItemArray = [[NSMutableArray alloc] init];
// Go through each category. "i" is category Number
for (int i=1; i <= [Number of Categories]; i++)
{
NSString *currentCategoryName = [Get Category Name]; //This works...
// ********* Debug statements ************
// This Loop is where NSLog shows something's not right
// These conditions work fine.
for (int j = 0; j < [[ItemListFromFile.ItemDictionary objectForKey:currentCategoryName] count]; j++)
{
// This Log outputs (null) when it should output the code from the Item
NSLog(#"Code from File: %#", [[[ItemListFromFile.ItemDictionary objectForKey:currentCategoryName] objectAtIndex:j] code]);
}
// ************** Debug ******************
if ([[ItemListFromFile.ItemDictionary allKeys] containsObject:currentCategoryName])
{
[oldItemArray setArray:[ItemListFromFile.ItemDictionary objectForKey:currentCategoryName]];
for (Item *anItem in oldItemArray)
{
NSLog(#"OldItemArray Code: %#", anItem.code);
}
}
else
{
[oldItemArray removeAllObjects];
}
[newItemArray removeAllObjects];
// Go through each Item in category i.
for (NSString *currentCode in [array of codes in category i (this works)])
{
// There's two options of where to grab each Item from: either upDatedItems or oldItemArray
BOOL inOldArray = YES;
for (Item *anItem in upDatedItems)
{
if ([anItem.code isEqualToString:currentCode])
{
[newItemArray addObject:anItem];
inOldArray = NO;
NSLog(#"%# was in updatedItems", currentCode);
break;
}
}
if (inOldArray)
{
// Checking in old Item Array
for (Item *anItem in oldItemArray)
{
NSLog(#"Checking %# against %#", anItem.code, currentCode);
// (anItem.code is null)
if ([anItem.code isEqualToString:currentCode])
{
[newItemArray addObject:anItem];
NSLog(#"%# was in oldItems", currentCode);
break;
}
}
}
}
//We've created the array, so add it to dictionary
[ItemListFromFile.ItemDictionary setObject:newItemArray forKey:currentCategoryName];
NSLog(#"New Dance Array: %#", newDanceArray);
}
[NSKeyedArchiver archiveRootObject:ItemListFromFile toFile:tempPassedManifest.ItemListSavePath];
}
I've tried to be as thorough as possible, but do not hesitate to ask for clarification.
Update
I've been trying a lot of solutions to this, but nothing seems to be working. Things I've tried:
Creating an object that takes the place of *anItem, so instead of a for loop that loops through the contents of the array, loops from i=0..[Count], and then sets the object at that index to the item I created before the loop. I thought this might help so that I could allocate and initialize the Item.code before setting it to whatever the Item has.
Using
oldItemArray = [ItemListFromFile.ItemDictionary objectForKey:currentCategoryName];
instead of
[oldItemArray setArray:[ItemListFromFile.ItemDictionary objectForKey:currentCategoryName]];
I tried updating all of the items in the ItemList.ItemDictionary so that when it archives, all of the items are fresh for the next trial run of the code.

How can I add elements to an NSDictionary in the following format?

I have an NSArray of names and ages. Now I am trying to create a new NSDictionary with item 0 holding the first name and first age and item 1 holding the second name and second age from the corresponding NSArray? Is that possible?
In my viewDidLoad:
NSMutableArray *Names = [[NSMutableArray alloc] init];
NSMutableArray *ages = [[NSMutableArray alloc] init];
for(int i = 0; i < 4; i++) {
[Names addObject:[candidates objectAtIndex:i]];
[ages addObject:[studenAge objectAtIndex:i]];
}
But how can I make an NSDictionary from this by order?
Final result
I want to write this NSDictionary into a .plist so it look like this:
Details
{
item 0
{
name:rahul
age:25
}
item 1
{
name:ram
age:26
}
item 2
{
name:aajy
age:20
}
item 4
{
name:raj
age:25
}
}
Dictionaries do not have an order.
Probably you want to add dictionaries to the array:
NSMutableArray *persons=[[NSMutableArray alloc]init];
for(int i=0;i<4;i++)
{
[persons addObject:# { #"name" : candidate[i], #"age" : studenAge[i] }];
}
You can sort this array with the sort-Methods of NSArray, NSMutableArray.
As mentioned by #Amin, dictionaries don't maintain order and since you seem to want an indexed access here is what I suggest as the final structure: an array of dictionaries:
NSMutableArray *details = [NSMutableArray array];
for (int i=0; i<4; i++) {
[details addObject:#{#"name": canditate[i], #"age":studentAge[i]}];
}
This will give you the following structure:
[{name:rahul, age:25}, {name:ram age:26},...]

Adding custom objects to NSMutable array not working, addObject returns empty array

I am making a number of custom objects. I want to add each object to an array, but after the code below is run the array is still empty.
for (int i = 0; i<= numberOfObjectsWanted; i++) {
CustomClass *object = [[CustomClass alloc]init];
[objectsArray addObject:object];
}
objectsArray is an NSMutableArray
So if your array is nil, no message sends will have any effect on it...
objectsArray = [[NSMutableArray alloc] init];
beforehand.
You're not adding the object that was created. Try...
for (int i = 0; i<= numberOfObjectsWanted; i++) {
CustomClass *object = [[CustomClass alloc]init];
[objectsArray addObject:object];
}
EDIT - The new code in the question looks better now. The next thing to check is whether you have a good mutable array to begin with. Try NSLog(#"array is %#", objectsArray);
inside the loop.

Resources