Matching Strings In Array with an other Array from NsUserDefaults - ios

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

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
}

Related

Creating Different Permutations from different Arrays Values

I am trying to write a method which allows me to create a different String permutations, based on the values I have stored within 3 different arrays. Currently I have a NSMutableDictionary that contains 3 different keys, and associated to each key we have an NSMutableArray array that contains a list of different String objects.
How woulld I loop through each of my NSMutableArray arrays which are stored within my NSMutableDictionary, and build a string value for each node from the arrays. So essentially, something like the following: A1[0] = "Hello", A2[0] = "There", A3[0] = "Guys", which would essentially build me a string like this: "HelloThereGuys".
Any suggestions, or possibly different approaches to this problem will be appreciated.
First of all I want to agree with hochl. ;-) But I think, that you want to create a string containing a word from each array in the same index position?
NSMutableArray *enums = [NSMutableArray new];
for( NSString *key in dictionary )
{
NSEnumerator *enum = [dictionary[key] objectEnumerator]; // Forgot that in prior version
[enums addObject:enum];
}
while( YES )
{
NSMutableString *line = [NSMutableString new];
BOOL done = NO;
for( NSEnumerator *enum in enums )
{
NString *word = [enum nextObject];
if( word == nil )
{
done = YES;
break;
}
[line addString:word];
}
if (done)
{
break;
}
NSLog (#"%#", line );
}
Did I get you right?

How to filter search within a set of letters in search bar so that each letter typed will reduce the results in objective -c

i have implemented a search bar that searching trough an array of countries(presented in a picker view), the problem is that the user need to type the full country name that it will find it and i want him to be able to type even one letter and it will show the first country that starts with that letter and if types another than it sorts even further etc etc.
Anyone have any ideas??
for(int x = 0; x < countryTable.count; x++){
NSString *countryName = [[countryTable objectAtIndex:x]objectForKey:#"name"];
if([searchedStr isEqualToString:countryName.lowercaseString]){
[self.picker selectRow:i inComponent:0 animated:YES];
flag.image = [UIImage imageNamed:[[countryTable objectAtIndex:i]objectForKey:#"flag"]];
}
}
There's a method on NSArray called filteredArrayUsingPredicate: and a method on NSString called hasPrefix:. Together they do what you need...
NSString *userInput = //... user input as lowercase string. don't call this countryName, its confusing
NSPredicate *p = [NSPredicate predicateWithBlock:^BOOL(id element, NSDictionary *bind) {
NSString countryName = [[element objectForKey:#"name"] lowercaseString];
return [countryName hasPrefix:userInput];
}];
NSArray *filteredCountries = [countryTable filteredArrayUsingPredicate:p];
If you're on iOS 8 or OS X Yosemite, you can do:
NSString *country = countryName.lowercaseString; //"england"
NSString *needle = #"engl";
if (![country containsString:needle]) {
NSLog(#"Country string does not contain part (or whole) of searched country");
} else {
NSLog(#"Found the country!");
}
Else, if on versions below iOS 8:
NSString *country = countryName.lowercaseString; //"england"
NSString *needle = #"engl";
if ([country rangeOfString:needle].location == NSNotFound) {
NSLog(#"Country string does not contain part (or whole) of searched country");
} else {
NSLog(#"Found the country!");
}
Lastly, just iterate through all possible countries and apply this to them all. There might exist more robust solutions out there (like danh's solution with some smaller modifications), but this is by far the easiest to start with.

Display string from Nsarray only if it contains

I am using the following query to check if the strings within a NSArray contain a certain word.
I am struggling to then break it down to display only the string that has the rangeOfString word.
As you can see it currently displays all the results if the NSArray contains the word. How would I single it down further to display only the specific string that contains the "BOLDME" ?
// change this line only for the concatenation
NSString * resultConditions = [legislationArray componentsJoinedByString:#"\n"];
NSString *word = #"BOLDME";
//if string contains
if ([resultConditions rangeOfString:word].location != NSNotFound) {
cell.dynamicTextView.text = resultConditions;
}
Not only will this get you directly to each instance within the array that contains the text you're searching for, but it will also perform significantly better than the code in the question.
for (NSString *testWord in legislationArray) {
if ([testWord rangeOfString:#"BOLDME"].location != NSNotFound) {
// testWord contains "BOLDME"
cell.dynamicTextView.text =
[cell.dynamicTextView.text stringByAppendingString:testWord];
}
}
As written, this will append the found string to whatever text is already in the text view. It may be that you only want one word in the text view. If this is the case, then you should break; as soon as you find the first one.
You can iterate over NSArray and check each string if it contains specific word:
for(NSString *value in array) {
if([value rangeOfString:word] != NSNotFound) {
/* we have found that string*/
}
}

select indexOfObject from NSArray in Xcode by comparing string

Hi I have NSarray values in Xcode. I need to get array indexOfObject by comparing string values.
my array values are
(
{
firstName = lord;
lastname = krishna;
},
{
firstName = priya;
lastname = amirtha;
}
)
If I type first name in textfield and click button means last name want to display in another textfield.
thank you.
To answer the title of your question:
NSString *compareString = #"something";
NSMutableArray *indexesOfMatches = [[NSMutableArray alloc]init];
for (NSString *string in theArray) {
if ([string isEqualToString:compareString]) {
NSNumber *index = [[NSNumber numberWithInterger:[theArray indexOfObject:string]];
[indexOfMatches addObject:index];
}
}
//indexOfMatches will now contain NSNumber objects that represent the indexes of each of the matching string objects in the array
I think that using an NSDictionary would be better for you though. Then you can simply keep the first and last names as Key Value pairs.
NSDictionary *names = #{#"lord" : #"krishna", #"priya" : #"amirtha" };
Then you can just do value for key when you get the first name:
NSString *firstName = #"lord";
NSString *lastName = [names valueForKey:firstName];
Store firstNameArray and lastNameArray a mutable array NSMutableArray.
Using Fast Enumeration. Suppose array is the array you are provided with
for (NSDictionary *item in array) {
[firstNameArray addObject:[item objectForKey:#"firstName"]];
[lastNameArray addObject:[item objectForKey:#"lastName"]];
}
After entering the data in firstNameTextField click the button
Button action method implementation
-(IBAction)btnClicked:(id)sender {
NSInteger index = [firstName indexOfObject:[firstNameTextField text]];
[lastNameTextField setText:[lastName objectAtIndex:index]];
}

How can I access one value among some values

{
"id"=12
"genres_en" = "thriller,movies,action";
}
{
"id"=13
"genres_en" = "thriller,horror";
}
Hi everyone ,
I have one json like this..I mean i have one content and all movie has their genre ..And i need to do one categorization ..For example I need to check which genre of movie is horror..Or which one is thriller..I m getting all value like this:
--> "horror,movies"
But how can i do the controllation to check whether the genre of this movie includes horror or not..??what is your suggesstion?
Thank you
You could use componentsSeparatedByString
NSDictionary *dic = PARSE The JSON;
NSString *values = [dic objectForKey:"genres_en"];
NSString *firstValue = [[values componentsSeparatedByString:","] objectAtIndex:0];
I don't have Xcode nearby, so the following code may contain an error(or two :) ).
Try it like that:
NSDictionary *yourDict = //your parsed json
NSString *genres = [yourDict objectForKey:#"genres_en"];
if ([genres rangeOfString:#"horror"].location != NSNotFound) {
return YES;
} else {
return NO;
}
Hope it helps
for(int j=0;j< GenresArray.count; j++)
{
NSString *value=[[genre componentsSeparatedByString: #","]objectAtIndex:j];
if([value isEqualToString:#"movies"])
{
[genreMovies addObject:value];
}
}
Thank you for your help guys ..Now i have my result:)
You can try to check if the string contains the substring you want . Like this:
NSString *typesString = [dic objectForKey:"genres_en"];
NSRange range = [typesString rangeOfString:#"horror"];
if(range.location != NSNotFound)
{
//it contains the substring "horror"
}
Or , the better way would be to get the array of types.
NSArray *types = [[typesString componentsSeparatedByString:","];
Now you can store this array for each entry and whenever you want to check if it belongs to a certain gender , just loop through the types array ( of each entry ) and compare the strings.
Hope this helps.
Cheers!

Resources