在主阵列中的数组中查找NSString

时间:2015-07-15 00:49:40

标签: ios objective-c arrays

我有一个非常有趣但很难玩的问题。

我有一个名为alpha的字符串。

alpha = @“pie”

我有3个数组被添加到一个名为“ALL WORDS”的主数组中

array 1 = [nsarray alloc]initwithobjects @"cake", @"donut"];
array 2 = [nsarray alloc]initwithobjects @"cream", @"pie"];
array 3 = [nsarray alloc]initwithobjects @"rice", @"flour"];


    allwords = [NSMutableArray array];
    [allwords addObjectsFromArray:array1];
    [allwords addObjectsFromArray:array2];
    [allwords addObjectsFromArray:array3];

所以现在我正在做这样的for循环

 for (int i = 0; i < [allWords count]; i++)
 {
        NSString *takestring =[allWords objectAtIndex:i];
        if ( [takestring is equal to alpha]) {
           //tell which array alpha or takestring came from????            
        }        
 }

我的问题是,如何确定馅饼来自哪个阵列。在应用程序中,人们可能会有我可能不知道的新单词,但我希望能够检测到该字符串的位置是什么?

有人知道我该怎么办吗?

2 个答案:

答案 0 :(得分:1)

您不应该根据您的描述合并数组。相反,你应该使用类似下面的东西。

@interface aViewController ()

@property (nonatomic, strong) NSMutableArray *allwords;

@end

@implementation aViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.



    NSArray *array1 = [[NSArray alloc] initWithObjects:@"cake",@"donut", nil];
    NSArray *array2 = [[NSArray alloc] initWithObjects:@"cream", @"pie", nil];
    NSArray *array3 = [[NSArray alloc] initWithObjects:@"rice", @"flour", nil];
    self.allwords = [[NSMutableArray alloc] initWithObjects:array1, array2, array3, nil];

    [self findMyArray:self.allwords];
}


- (void) findMyArray:(id)array {


    if ([array isKindOfClass:[NSArray class]] || [array isKindOfClass:[NSMutableArray class]]) {
        for (NSArray *temp in array) {
            for (NSString *str in temp) {
                if ([str isEqualToString:@"pie"]) { // define @"pie" as per your requirement
                    NSLog(@"THE ARRAY WHICH CONTAIN PIE IS %@", temp);
                }
            }
        }
    }


}
@end

使用此功能,您只需找到包含特定字符串

的数组即可

答案 1 :(得分:0)

解决方案1:跟踪我们找到单词的数组的索引,将数组添加到主数组,而不是添加每个数组的对象。

NSString* alpha= @"pie";

NSArray  *array1 = [[NSArray alloc]initWithObjects: @"cake", @"donut", nil];
NSArray  *array2 = [[NSArray alloc]initWithObjects: @"cream", @"pie", nil];
NSArray  *array3 = [[NSArray alloc]initWithObjects: @"rice", @"flour", nil];

NSMutableArray*  allwords = [NSMutableArray array];

[allwords addObject:array1];
[allwords addObject:array2];
[allwords addObject:array3];

NSUInteger indexOfArray = -1;

for (NSArray* currentArray in allwords)
{
    for (NSString* word in currentArray)
    {
        if ([word isEqualToString:alpha])
        {
            indexOfArray = [allwords indexOfObject:currentArray];
            break; // if you don't want to loop after you find the value
        }
    }
}

// in which array I have found the word @"pie" ? ahh! I have the index of the array so

if (indexOfArray != -1) // we found the word
{
    NSArray* containerArray = [allwords objectAtIndex:indexOfArray];
    NSLog(@"%@",containerArray);
}