2011-10-13 18 views
0

我想知道是否可以填充NSArray的rangeOfString對象。因爲我在rangeOfString: 之後有一長串對象,NSArray biglist的數量高於列表的數量。Objective C如何填寫NSArray的rangeOfString?

我想過濾主列表的小列表中的對象。

請告訴我,如果這不明確。

我的代碼如下:

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循環在main 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!"); 

回答

1

我想通了我本人和解決了這個:) 它的工作原理幾乎完全。

我的代碼:

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

「我想從主力名單的小單子過濾掉的對象。」

如果我理解正確,你想從另一個數組中刪除一個項目的數組。你不想在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 
+0

該解決方案是非常適合的對象的100%匹配。我有像「幫助 - >這裏的幫助定義......」,「什麼 - >定義」等對象,我想刪除該單詞與另一個數組中的單詞匹配的行。這就是爲什麼我必須使用'rangeOfString'。 – wagashi