我想知道是否有可能填充一个NSArray的rangeOfString对象。因为在rangeOfString:
之后我有一个很长的对象列表NSArray biglist ' s count大于list ' s count。
我想从主列表的小列表中过滤掉对象。
如果不清楚,请告诉我。
我的代码如下:
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);
}
但是我不确定把它放在哪里,在mainlist的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!");
"我想从主列表的小列表中过滤掉对象。"
如果我理解正确的话,您想要从另一个数组中删除一个数组的项。你不希望在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
我自己想出来并解决了这个问题:)它几乎可以完美地工作。
我的代码:
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);