开发者

What is the shortest way to write rangeOfString with list of names, dictionary, king james bible, etc?

This is embarrassing. I don't want to use coredata or text file. I need shortest way to write this code.

names ("John", "Matthew", "thoma开发者_运维技巧s", "isaac", "bible", "Mayan", "2012", more names)

This code takes extremely long and wasting too much memory, download time, and waste of space. Not recommend it. 10,000 lines.

 NSString *selectives = Name.text;
 if ([selectives rangeOfString:@"John"].location != NSNotFound) {
    //
 }
 if ([selectives rangeOfString:@"Matthew"].location != NSNotFound) {
     //
 }

Second. This code doesn't work. I don't what I'm doing wrong here. I'm lost. I need help!

    NSString *string = Name.text;
    NSString *NameMe = [NSString stringWithString:@"Jake", "miller", "thomas", "isaac"];

    if([string rangeOfString:NameMe].location !=NSNotFound) 
    {   
       // 
    }

Is there a better way to write this? How do you write? I'm not good at this.


In the first case you can use fast enumeration to shorten your code.

NSArray *searchStrings = // Set up your search strings however you want
NSString *selectives = Name.text

for (NSString *searchString in searchStrings) {
    if [selectives rangeOfString:searchString].location != NSNotFound {
        // Your processing here
    }
}

In the second case this line is wrong

NSString *NameMe = [NSString stringWithString:@"Jake", "miller", "thomas", "isaac"];

You aren't passing in a properly formed string. @"Jake" is valid, the rest, because they are outside the @"..." section are not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜