0

I am filling array in view did load method but the problem is that each time view is loaded it adds again and again same content i want that if first time view is loaded then it should add contents in array not in the second time repeatedly. Here is my code:

    for (int i=0;i<5;i++) {

            Coffee*obj=[appDelegate.coffeeArray objectAtIndex:i];

            NSLog(@"This is working %d",i);

            [appDelegate.arrayOne addObject:obj];   
    }


    for (int a=5;a<8;a++) {


        Coffee*obj=[appDelegate.coffeeArray objectAtIndex:a];


        [appDelegate.arrayTwo addObject:obj];   
    }   

two array i am filling from another array but it repeats values if we move from one view to another and again come back

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70

2 Answers2

4

First check that here it have any value then remove that all data from array like bellow..

   if ([appDelegate.arrayOne count]>0) {
         [appDelegate.arrayOne removeAllObjects];
   }

   if ([appDelegate.arrayTwo count]>0) {
         [appDelegate.arrayTwo removeAllObjects];
   }

   for (int i=0;i<5;i++) {

            Coffee*obj=[appDelegate.coffeeArray objectAtIndex:i];

            NSLog(@"This is working %d",i);

            [appDelegate.arrayOne addObject:obj];   
    }


    for (int a=5;a<8;a++) {


        Coffee*obj=[appDelegate.coffeeArray objectAtIndex:a];


        [appDelegate.arrayTwo addObject:obj];   
    }

and the add in that array..

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • can you please edit in my code – Jdeveloper Iphone Apr 16 '13 at 09:29
  • Why remove all objects if they are the same that would be added later? Skip the adding mechanics if these arrays do have values. That results in the same values being in the array but at lower resource consume because they are not added repeatedly. – TRD Apr 16 '13 at 09:32
  • @JdeveloperIphone i edit my answer whole your whole code... :) wel-come plzz accept answer and upvote me if its useful to you... – Paras Joshi Apr 16 '13 at 09:32
  • @TRD if every time or after some time here we got different data at that time this concept is useful so i do that... – Paras Joshi Apr 16 '13 at 09:33
  • @ParashJoshi one another general question that my audio file is play perfectly on simulator but it is not playing on device what would be the issue – Jdeveloper Iphone Apr 16 '13 at 09:38
  • @ParasJoshi If this data really does change since when it was added then I would prefer to keep the changes instead of revising them to some default values. But may be this case is just more a philosophical topic and not what the starter intended. I just wanted to point out that there is a possibility to "ensmart" that piece :) – TRD Apr 16 '13 at 09:39
  • if you use this type of code then i should work perfactly.. NSString *path = [[NSBundle mainBundle] pathForResource:@"adriantnt_release_click" ofType:@"mp3"]; AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; theAudio.delegate=self; [theAudio play]; – Paras Joshi Apr 16 '13 at 09:40
  • @JdeveloperIphone also check setting of your device also check out volume of device.. :) – Paras Joshi Apr 16 '13 at 09:41
  • @TRD yes dude you are right and some of data i use with this point like Country Array or something like that which not changed in future at that time i use that concept.. :) thanx friend – Paras Joshi Apr 16 '13 at 09:42
  • @ParasJoshi here is the link i have posted code please help me out in this – Jdeveloper Iphone Apr 16 '13 at 09:45
  • http://stackoverflow.com/questions/16033723/audio-file-not-working-on-device-but-it-is-working-on-simulator – Jdeveloper Iphone Apr 16 '13 at 09:45
4

Assuming you are using NSMutableArray use removeAllObjects method avilable for Array before adding objects to it. Checkout Documentation

Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99