Mahesh Mahesh - 9 months ago 27
iOS Question

Matching Strings In Array with an other Array from NsUserDefaults

Array From Defaults

NSMutableArray* saveSelectedPlaces;

NSArray* titleArray = @[@"Akshardham",
@"Charminar",
@"Golden Temple",
@"Indian Gate",
@"Kedartnath Temple",
@"Ladakh",
@"Manasasarovar",
@"Mumbai",
@"Ooty",
@"Tajmahal",
@"Thar Desert",];
Title = [titleArray mutableCopy];


My logic to compare strings

for (NSString* currentString in saveSelectedPlaces) {
if (detailcell.DetailTableCellTitle.text == currentString){
detailcell.favouriteButton.selected = YES;
[detailcell.favouriteButton setBackgroundImage:[UIImage imageNamed:@"HeartSelectedSmall"] forState:UIControlStateNormal];
}
}


I have a array of titles saved in defaults , i want to compare those strings with an other array " Title . Then if it matches i want to highlight the images.

Simply, am trying to favoriting a cell.

Please HELP ME

Thanks


[1]: https://i.stack.imgur.com/VQ0RJ.png

Answer Source

Try this:

Replace if (detailcell.DetailTableCellTitle.text == currentString) with if ([detailcell.DetailTableCellTitle.text isEqualToString: currentString])

The == is simply compares the pointers, which will usually be different even if their contents are the same. The isEqualToString method compares their contents.

More Simple way : You can check if an NSArray contains an object with containsObject.

BOOL contains = [saveSelectedPlaces containsObject: detailcell.DetailTableCellTitle.text];
if (contains)
{
 \\ add your code 
}