Mahesh Mahesh - 1 year ago 83
iOS Question

Matching Strings In Array with an other Array from NsUserDefaults

Array From Defaults

NSMutableArray* saveSelectedPlaces;

NSArray* titleArray = @[@"Akshardham",
@"Golden Temple",
@"Indian Gate",
@"Kedartnath Temple",
@"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



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 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download