Finding a NSString in an Array in a Master Array

Total Post:20

 1288  View(s)
Rate this:

I have a string called alpha.

alpha= @"pie"

I have 3 arrays that were added into a master array called "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];

So now I am doing a for loop like this

 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????           



My question is, how can I determine which array the pie came from. in the app people might have new words that I might not know, but i want to be able to detect where that string what array is it from?

Anybody knows how I can do this??

  1. Post:214

    Re: Finding a NSString in an Array in a Master Array

    You should not merge array as per your description. instead you should use something like below.


    @interface aViewController ()
    @property (nonatomic, strong) NSMutableArray *allwords;

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


    Using this you can simply find your array which contains specific string.


      Modified On Apr-10-2018 01:22:30 AM