Objective C 如何填充 NSArray 的 rangeOfString?
我想知道是否可以填充 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“我想从主列表的小列表中过滤掉对象。”
如果我理解正确的话,您想从另一个数组中删除一个项目数组。您不想在 n^2 循环内做那么多工作和分配。
这会从另一个数组中删除一个数组中的项目。根据数组的大小,您可能需要进一步优化,但这有效:
输出:
"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:
This outputs:
我自己想出了办法并解决了这个问题:)
它几乎完美地工作。
我的代码:
I figured it out by myself and solved this :)
It works almost perfectly.
My codes: