开发者

Objective C How to fill rangeOfString of a NSArray?

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"]开发者_JAVA百科
                                                   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!");


"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


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);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜