Im trying to filter through an array and get rid of all the duplicate strings.
NSMutableArray *initialWomensCategoryArray = [NSMutableArray new];
for(NSArray *womensCategoryInnner in [objects valueForKey:#"womensCategory"])
{
for(id object in womensCategoryInnner)
{
[initialWomensCategoryArray addObject:object];
womensDataArray = [[NSMutableArray alloc]init];
for (id obj in initialWomensCategoryArray)
{
if (![womensDataArray containsObject:obj])
{
[womensDataArray addObject: obj];
}
}
}
}
Above Error want to say "You have getting NULL value" when you have add Object into array. So you have two possibility for this error
1) [initialWomensCategoryArray addObject:object]
2) [womensDataArray addObject: obj]
I think you have to check for the first one([initialWomensCategoryArray addObject:object])
Related
This is my code for getting duplicate Device Contacts and i want to know how to get the index of the duplicate contacts
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:[Contact valueForKey:#"emailAddress"]];
for(id name in set){
if([name isEqual:[NSNull null]] || [name isEqualToString:#""]){
} else {
array=[[NSMutableArray alloc]init];
duplicateEmailDictionary =[NSMutableDictionary new];
if ([set countForObject:name]>1) {
for (int i=0; i<[set countForObject:name]; i++) {
[array addObject:name];
}
[duplicateEmailDictionary setObject:array forKey:#"emailAddress"];
[finalDuplicateEmail addObject:duplicateEmailDictionary];
}
}
}
A set is an unordered pool of unique objects. Since it's unordered, there is no integer index you can use to access specific elements of a set.
If you need index then use NSArray.
I have two array, called array1 and array2. I would like to remove every object from array1 that's value of the "nameId" key can be find in both array. Actually I'm trying it in a for loop, but it doesn't make sense. It doesn not crash, it just simply calls the log in the else statement, that I don't understand why happens. Maybe somebody could show me the right solution.
NSMutableArray *newArray = [self.array1 mutableCopy];
for (PFObject * object in newArray) {
PFObject *placeholderObject = object;
for (PFObject *object2 in self.array2) {
if ([placeholderObject[#"nameId"] isEqualToString:object2[#"nameId"]]) {
[self.array1 removeObject:object];
NSLog (#"EXISTING OBJECT FOUND %#", object);
} else {
NSLog(#"UNIQUE OBJECT FOUND %#", idO[#"hirCime"]);
}
}
}
When creating a mutableCopy of an array you create a new array with a copy of every object in it but they aren't the same ones, so object is a member of newArray but is not a member of self.array1 so you can't remove it from that array.
This should work:
// Creates a new empty mutable array
NSMutableArray *newArray = [#[] mutableCopy];
for (PFObject *object in self.array) {
BOOL found = NO;
for (PFObject *object2 in self.array2) {
if ([object[#"nameId"] isEqualToString:object2[#"nameId"]]) {
found = YES;
NSLog (#"EXISTING OBJECT FOUND %#", object);
break;
} else {
NSLog(#"UNIQUE OBJECT FOUND %#", idO[#"hirCime"]);
}
}
if (!found) {
[newArray addObject:[object copy]];
}
}
// And maybe you want this
self.array = newArray;
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];
I'm trying to add an dictionary to an array to create an array on dictionaries, but when I test the app, the array is still empty. Take a look on my code:
NSMutableArray *listaEstabelecimentosPorCategoria;
for (NSDictionary *tempDict in listaEstabelecimentos) {
if ([[tempDict objectForKey:#"category"] integerValue] == 1) {
[listaEstabelecimentosPorCategoria addObject:tempDict];
NSLog(#"TEST");
}
}
NSLog(#"%#", listaEstabelecimentosPorCategoria);
The app prints this:
2013-08-18 12:16:38.802 Petrópolis[10157:c07] TEST
2013-08-18 12:16:38.802 Petrópolis[10157:c07] TEST
2013-08-18 12:16:38.803 Petrópolis[10157:c07] (null)
when I test the app, the array is still empty.
This is because you did not initialize listaEstabelecimentosPorCategoria:
NSMutableArray *listaEstabelecimentosPorCategoria = [NSMutableArray array];
NSMutableArray *listaEstabelecimentosPorCategoria=[[NSMutableArray alloc] init]; //initialize
for (NSDictionary *tempDict in listaEstabelecimentos) {
if ([[tempDict objectForKey:#"category"] integerValue] == 1) {
[listaEstabelecimentosPorCategoria addObject:tempDict];
NSLog(#"TEST");
}
}
NSLog(#"%#", listaEstabelecimentosPorCategoria);
NSMutableArray *listaEstabelecimentosPorCategoria this code hasn't been allocated to memory so it will be null value
NSMutableArray *listaEstabelecimentosPorCategoria = [NSMutableArray array];
then listaEstabelecimentosPorCategoria add the object.
I am new to iOS development, I encounter error when replaceObjectAtIndex. Any wrong with my codes? Please help.Thanks.
self.myArray =array;
for (NSDictionary *data in array) {
NSString *fbid = [data objectForKey:#"id"];
for (int index = 0; index < self.myPersonArray.count; index ++) {
for (IP_PERSON *person in self.myPersonArray) {
if ([person.UserDef2 isEqualToString:fbid]) {
[self.myArray replaceObjectAtIndex:index withObject:person];
break;
}
}
}
Error is :
Terminating app due to uncaught exception NSGenericException, reason: '*** Collection <__NSArrayM: 0xa34f6c0> was mutated while being enumerated.
You cannot use fast enumeration and mutate collection at the same time, hence the error message. You can resort to using an usual for-loop.
You're iterating over array, which is equal to self.myArray.
Further down, you're editing this array when you do: [self.myArray replaceObjectAtIndex:index withObject:person];
To resolve this, just make self.array a mutableCopy of the original array:
self.myArray = [array mutableCopy];
You can take another temporary array and iterate over that array, so you are enumerating and mutating different array.
NSArray *tempArray = [yourArray copy];
for (IP_PERSON *person in tempArray) {
if ([person.UserDef2 isEqualToString:fbid]) {
[self.myArray replaceObjectAtIndex:index withObject:person];
break;
}
}
[tempArray release];
Alternatively you can iterate without an enumerator, you can use regular for loop with starting index, exit condition and increment as you have done in outer loop.
You can..
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]
self.myArray =array;
for (NSDictionary *data in array) {
NSString *fbid = [data objectForKey:#"id"];
for (int index = 0; index < self.myPersonArray.count; index ++) {
for (IP_PERSON *person in self.myPersonArray) {
if ([person.UserDef2 isEqualToString:fbid]) {
[dictionary setObject:person forKey:#(index)]; //Notice this line
break;
}
}
}
}
And then..
for(id key in dictionary) {
[self.myArray replaceObjectAtIndex:[key intValue] withObject:[dictionary objectForKey:key]];
}