Shakmyster
Shakmyster

Reputation: 9

Using TextField to search from a txt file xcode

I am trying to make a simple iphone program that will allow me to search a keyword when entered into a textfield and then search for it by clicking a rect button. I have searched the site and there is nothing that relates to my current situation. to reiterate, I have a textfield where I would like to enter some text and search for it by clicking a rect button. I have the information i want to search for in a txt file. What is the appropriate way to to go about doing this in xcode for iphone. I appreciate the help, thanks.

Upvotes: 0

Views: 745

Answers (2)

Hemant Singh Rathore
Hemant Singh Rathore

Reputation: 2139

Try this:

-(void)searchWord:(NSString *)wordToBeSearched{
NSString* path = [[NSBundle mainBundle] pathForResource:@"wordFile"
                                             ofType:@"txt"];

NSString* content = [NSString stringWithContentsOfFile:path
                                          encoding:NSUTF8StringEncoding
                                             error:NULL];

NSString *delimiter = @"\n";
NSArray *items = [content componentsSeparatedByString:delimiter];
NSString *character = @" ";
BOOL isContain = NO;
for (NSString *wordToBeSearched in items) {
isContain = ([string rangeOfString:wordToBeSearched].location != NSNotFound);
}
return isContain; 

}

You can go here for more details

Upvotes: 0

Paul Peelen
Paul Peelen

Reputation: 10329

If I understand your question correctly, You are trying to find a user defined phrase in a text file.

What you should do is read the file to an NSString, and then search the string for the user defined string. http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/SearchingStrings.html

Googled it quickly and found an example (Not written by me!):

NSString *searchFor = @"home";
NSRange range;
for (NSString *string in stringList)
{
    range = [word rangeOfString:searchFor];
    if (range.location != NSNotFound)
    {
      NSLog (@"Yay! '%@' found in '%@'.", searchFor, string);
    }
}

If I missunderstood you, please update your question so it is better understandable.

Upvotes: 1

Related Questions