Multiple Arrays [closed]
Is there a way to access/store multiple arrays or data which could be compared against and return a value to ensure a program is faster?
I will have two variables with multiple possibilities I want to compare these two variables to all the possibilities and 开发者_如何学Goreturn a value but that will slow down the speed of the app is there a way to do it faster?
Sounds like you want to check for existence of an object within a set. Sets have a very fast look-up time but do not retain the order of the objects. They are particularly useful for checking whether a value is within a particular set of values. For example, the following snippet acquires a string of text from the user and ensures that it is either @"Abc"
, @"Def"
, or @"Ghi"
. This trivial case involves only three elements but of course you can easily add more.
NSSet *possibleValues = [NSSet setWithObjects:@"Abc", @"Def", @"Ghi", nil];
NSString *userProvidedInput = /* obtain from user somehow */;
if ([possibleValues containsObject:userProvidedInput])
{
NSLog(@"The user provided correct input!");
}
NSSet
objects can contain any NSObject
subclass, including NSString
, NSNumber
, etc. If you have a lot of elements in the set, consider storing them in a plist file (use an array), and use something like:
[NSSet setWithArray:[NSArray arrayWithContentsOfFile:@"some.plist"]];
精彩评论