使用姓名列表、字典、詹姆斯国王圣经等编写 rangeOfString 的最短方法是什么?

发布于 2024-12-02 12:34:09 字数 694 浏览 0 评论 0原文

这很尴尬。我不想使用 coredata 或文本文件。我需要最短的方法来编写这段代码。

名字(“约翰”,“马太”,“托马斯”,“以撒”,“圣经”,“玛雅”,“2012”,更多名字)

这段代码花费了非常长的时间,浪费了太多的内存、下载时间和浪费空间。不推荐它。 10,000 行。

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

第二。这段代码不起作用。我不知道我在这里做错了什么。我迷路了。我需要帮助!

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

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

有更好的方法来写这个吗?你怎么写?我不擅长这个。

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

names ("John", "Matthew", "thomas", "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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小兔几 2024-12-09 12:34:09

在第一种情况下,您可以使用快速枚举来缩短代码。

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
    }
}

在第二种情况下,这一行是错误的,

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

您没有传递正确格式的字符串。 @"Jake" 有效,其余部分则无效,因为它们位于 @"..." 部分之外。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文