Objective C 如何填充 NSArray 的 rangeOfString?

发布于 2024-12-10 02:44:48 字数 2715 浏览 4 评论 0原文

我想知道是否可以填充 NSArray 的 rangeOfString 对象。因为我在 rangeOfString: 之后有一长串对象 NSArray biglist 的计数高于 list 的计数。

我想从主列表的小列表中过滤掉对象。

如果不清楚,请告诉我。

我的代码如下:

NSArray *biglist = [[NSArray alloc] initWithArray:
                        [[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"mainlist" ofType:@"txt"]
                                                   encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:@"\n"]];


NSArray *list = [[NSArray alloc] initWithArray:
                        [[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"smalllist" ofType:@"txt"]
                                                   encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:@"\n"]];

    for (NSString *listword in list);

    NSMutableArray *wordlist = [[NSMutableArray alloc] init];
    NSMutableArray *worindex = [[NSMutableArray alloc] init];
    NSMutableIndexSet *mindexes = [[NSMutableIndexSet alloc] init];
    NSMutableDictionary *mutdic = [[NSMutableDictionary alloc] init]; 
    NSMutableArray *mutarray = [[NSMutableArray alloc] init];

    for (NSString *s in mainlist)
    {

        NSRange ran = [s rangeOfString:listword];

        if (ran.location !=NSNotFound)
                {
                //my codes here
                }
        }

编辑:

我想我可以通过编写来解决这个问题

int i;
for (i = 0; i<[list count]; i++)
{
    NSString *same = [list objectAtIndex:i];
    NSLog (@"listword: %@", same);
}

但我不确定将其放置在哪里,在主列表中的 for 循环内部还是外部。


编辑:这个 for 循环在主 for 循环内工作。


编辑: 尝试了这些代码,但它不起作用。

NSArray *list = [[NSArray alloc] initWithArray:
                     [[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"small" ofType:@"txt"]
                                                encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:@"\n"]];


    NSArray *mainlist = [[NSArray alloc] initWithArray:
                        [[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"mainlist" ofType:@"txt"]
                                                   encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:@"\n"]];

    NSMutableArray *large = [NSMutableArray arrayWithArray:mainlist];


    NSArray *newlarge;

    for (NSString *listword in list)
    {

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SELF beginswith[c] %@)",listword];
    newlarge = [large filteredArrayUsingPredicate:predicate];
    }

    NSLog (@"large: %@", newlarge);
    NSLog (@"finished!");

I wonder if it is possible to fill rangeOfString objects of a NSArray. Because I have a long list of objects for after rangeOfString:
NSArray biglist´s count is higher than list´s count.

I want to filter away the objects from the small list of the main list.

Please tell me if this is not clear.

My codes below:

NSArray *biglist = [[NSArray alloc] initWithArray:
                        [[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"mainlist" ofType:@"txt"]
                                                   encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:@"\n"]];


NSArray *list = [[NSArray alloc] initWithArray:
                        [[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"smalllist" ofType:@"txt"]
                                                   encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:@"\n"]];

    for (NSString *listword in list);

    NSMutableArray *wordlist = [[NSMutableArray alloc] init];
    NSMutableArray *worindex = [[NSMutableArray alloc] init];
    NSMutableIndexSet *mindexes = [[NSMutableIndexSet alloc] init];
    NSMutableDictionary *mutdic = [[NSMutableDictionary alloc] init]; 
    NSMutableArray *mutarray = [[NSMutableArray alloc] init];

    for (NSString *s in mainlist)
    {

        NSRange ran = [s rangeOfString:listword];

        if (ran.location !=NSNotFound)
                {
                //my codes here
                }
        }

EDIT:

I think I can solve this by writing

int i;
for (i = 0; i<[list count]; i++)
{
    NSString *same = [list objectAtIndex:i];
    NSLog (@"listword: %@", same);
}

But I am not sure where to place it, inside the for loop s in mainlist or outside.


EDIT: This for loop works inside the main for loop.


EDIT:
Tried these codes, but it doesnt work somehow..

NSArray *list = [[NSArray alloc] initWithArray:
                     [[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"small" ofType:@"txt"]
                                                encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:@"\n"]];


    NSArray *mainlist = [[NSArray alloc] initWithArray:
                        [[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"mainlist" ofType:@"txt"]
                                                   encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:@"\n"]];

    NSMutableArray *large = [NSMutableArray arrayWithArray:mainlist];


    NSArray *newlarge;

    for (NSString *listword in list)
    {

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SELF beginswith[c] %@)",listword];
    newlarge = [large filteredArrayUsingPredicate:predicate];
    }

    NSLog (@"large: %@", newlarge);
    NSLog (@"finished!");

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

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

发布评论

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

评论(2

小…红帽 2024-12-17 02:44:48

“我想从主列表的小列表中过滤掉对象。”

如果我理解正确的话,您想从另一个数组中删除一个项目数组。您不想在 n^2 循环内做那么多工作和分配。

这会从另一个数组中删除一个数组中的项目。根据数组的大小,您可能需要进一步优化,但这有效:

NSArray *small = [NSArray arrayWithObjects:@"three", @"two", nil];
NSMutableArray *large = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil];

[large removeObjectsInArray:small];

// print
for (NSString *current in large)
{
    NSLog(@"item: %@", current);
}

输出:

2011-10-13 08:39:21.176 Craplet[5235:707] item: one
2011-10-13 08:39:21.178 Craplet[5235:707] item: four

"I want to filter away the objects from the small list of the main list."

If I understand correctly, you want to remove an array of items from another array. You don't want to do that much work and allocations inside an n^2 loop.

This removes an array of items from another array. Depending on how large your array is you may need to optimize further but this works:

NSArray *small = [NSArray arrayWithObjects:@"three", @"two", nil];
NSMutableArray *large = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil];

[large removeObjectsInArray:small];

// print
for (NSString *current in large)
{
    NSLog(@"item: %@", current);
}

This outputs:

2011-10-13 08:39:21.176 Craplet[5235:707] item: one
2011-10-13 08:39:21.178 Craplet[5235:707] item: four
浅唱ヾ落雨殇 2024-12-17 02:44:48

我自己想出了办法并解决了这个问题:)
它几乎完美地工作。

我的代码:

NSArray *big = [[NSArray alloc] initWithObjects:@"hello ->mache", @"heisann hoppsann ->hiya", @"nei men ->da", @"however ->what", @"may ->april", @"mai ->maj", nil];
NSArray *small = [[NSArray alloc] initWithObjects: @"heisann ", @"nei men ", @"however ", @"mai", nil];
NSMutableArray *smallwithh = [[NSMutableArray alloc] init];
NSMutableIndexSet *mindexes = [[NSMutableIndexSet alloc] init];

for (NSString *same in small)
{

    NSLog (@"listword: %@", same);

    for (NSString *s in big)
    {
        NSRange ran = [s rangeOfString:same];
        if (ran.location !=NSNotFound)

        {

            [smallwithh addObject:s];
            NSUInteger ind = [big indexOfObject:s];
            [mindexes addIndex:ind];

        }

    }

}

NSLog (@"smallwith: %@", smallwithh);

[smallwithh release];

NSMutableArray *newWords =[NSMutableArray arrayWithArray: big];
[newWords removeObjectsAtIndexes: mindexes];
[big release];      
[small release];

NSLog (@"newWords: %@", newWords);

I figured it out by myself and solved this :)
It works almost perfectly.

My codes:

NSArray *big = [[NSArray alloc] initWithObjects:@"hello ->mache", @"heisann hoppsann ->hiya", @"nei men ->da", @"however ->what", @"may ->april", @"mai ->maj", nil];
NSArray *small = [[NSArray alloc] initWithObjects: @"heisann ", @"nei men ", @"however ", @"mai", nil];
NSMutableArray *smallwithh = [[NSMutableArray alloc] init];
NSMutableIndexSet *mindexes = [[NSMutableIndexSet alloc] init];

for (NSString *same in small)
{

    NSLog (@"listword: %@", same);

    for (NSString *s in big)
    {
        NSRange ran = [s rangeOfString:same];
        if (ran.location !=NSNotFound)

        {

            [smallwithh addObject:s];
            NSUInteger ind = [big indexOfObject:s];
            [mindexes addIndex:ind];

        }

    }

}

NSLog (@"smallwith: %@", smallwithh);

[smallwithh release];

NSMutableArray *newWords =[NSMutableArray arrayWithArray: big];
[newWords removeObjectsAtIndexes: mindexes];
[big release];      
[small release];

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