Im new to iphone programming and im kinda stuck with my current project. I got a text file with a lot of data in it. Data is sorted like this:
51398030 10003254 80211593/94 1301281744 TOMTOM100
51398030 10003254 80211593/94 1301281744 TOMTOM101
51398030 10003254 80211593/94 1301281745 TOMTOM102
51398030 10003254 80211593/94 1301281745 TOMTOM103
51398033 10003254 80211595 1301281744 TOMTOM100
51398033 10003254 80211595 1301281744 TOMTOM101
51398033 10003254 80211595 1301281745 TOMTOM102
51398033 10003254 80211595 1301281745 TOMTOM103
51398029 10003254 80211597/98 1301281744 TOMTOM100
51398029 10003254 80211597/98 1301281744 TOMTOM101
51398029 10003254 80211597/98 1301281745 TOMTOM102
51398029 10003254 80211597/98 1301281745 TOMTOM103
I already imported this txt file and created a NSMutableArray that contains each line as object. Now i have a textfield where the user can enter a number. the app should now look trough the text file and and sort out the lines containing this number.
I already looked at https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Strings/Articles/Scanners.html#//apple_ref/doc/uid/20000147-BCIEFGHC
but i dont know how to start.
This is how far i got:
NSData *tmpData = [NSData dataWithContentsOfURL:[NSURL URLWithString:trackTraceFileUrlName] ];
NSLog(@"%@",trackTraceFileUrlName);
//convert data to string
NSString *tmpString = [[NSString alloc] initWithData:tmpData encoding:NSUTF8StringEncoding];
NSArray *piecesArray = [tmpString componentsSeparatedByString:@"\n"];
NSMutableArray *trackTraceContent = [NSMutableArray arrayWithArray:piecesArray];
NSLog(@"*%@*",trackTraceContent);
Try the below code
NSString *str = <string on which you want to search>;
NSRange r;
if ((r = [str rangeOfString:@"the string you want to search"]).location != NSNotFound)
//string found..
I think you have an array so try the modified one
for (NSString *str in <array>)
{
NSRange r;
if ((r = [str rangeOfString:@"the string you want to search"]).location != NSNotFound)
//string found..
}
Since you probably want to search over all the lines in the array, and assuming that you array just contains the strings as NSString objects:
NSArray *stringArray; // assuming that you already have these variables
NSString *numberEnteredByUser;
NSPredicate *pred = [NSPredicate predicateWithBlock:^(id obj, NSDictionary *bindings) {
NSString *str = (NSString *)obj;
return [str rangeOfString:numberEnteredByUser].location != NSNotFound;
}];
NSArray *results = [stringArray filteredArrayUsingPredicate:pred];
Of course, you can also create the target array without using NSPredicate, by just iterating over stringArray
and adding the right objects to a NSMutableArray results
.
If the number entered by the user is the primary search into your data, and it's unique , then hold your data in a dictionary, not an array, using the number as the key:
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
MyObject obj = ...;
[dictionary setObject:obj forKey:obj.number];
// etc.
Look-up will be much faster, as this is precisely what dictionaries (hash tables) are for.
Note: Objective-C collection classes can only hold objects, so you will need to represent the number using an NSNumber
object, not a primitive type.
If the number is not unique then you can still use this technique, but you'll need to hold a dictionary of arrays, using the number as the key into that array, which complicates things somewhat. However you will still gain performance benefit doing this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.