0

I am getting trouble in one of my app . I have 36 images & 36 sound and 36 text this all name are same now i want to Declare array of this all means array of images array of text and array of sound and now randomly want to display 9 images from this on button tag and on same page 9 sound play one bye one with text and identify that image.

so here i declare images array and also array of text i also done arc4random and getting images but now the problem is how i can use dictionary over here so that i can store the data in this dictionary so how i can do this plz help me ontis.

3 Answers3

1

You should create an array of dictionary objects, with each dictionary object containing one image, one sound and one text object (the same name ones). So the array will contain 36 dictionary objects and each dictionary object will contain 3 objects each.

NSMutableArray *combinedArray = [[NSMutableArray alloc] init];
for(i=0;i<36;i++) {
    NSDictionary *dict = [[NSDictionary dictionaryWithObjectsAndKeys:[imageArray objectAtIndex:i], @"image", [textArray objectAtIndex:i], @"text", [soundArray objectAtIndex:i], @"sound", nil]];
    [combinedArray addObject:dict];
}

Now you can use arc4Random on the combinedArray, and you'll have all the 3 corresponding objects at the selected random index. Hope this helps

Dhruv Goel
  • 2,715
  • 1
  • 17
  • 17
  • i put this code it gives me error expected identifier is there anything missing. – Ravi Vaghela Jan 29 '13 at 06:54
  • here is my code NSMutableArray *combinedArray = [[NSMutableArray alloc] init]; for(i=0;i<36;i++) { NSDictionary *dict = [[NSDictionary dictionaryWithObjectsAndKeys:[imageArray objectAtIndex:i], @"image", [textArray objectAtIndex:i], @"text", [soundArray objectAtIndex:i], @"sound", nil]]; [combinedArray addObject:dict]; } – Ravi Vaghela Jan 29 '13 at 06:55
  • i done thing in a same way but it give me errors here is my .h and .m file plz tell me i m facing problem – Ravi Vaghela Jan 29 '13 at 07:18
  • use : for(int i=0; i<36; i++) .. and I hope you have initialized and populated imageArray, textArray and soundArray before using this code – Dhruv Goel Jan 29 '13 at 07:38
  • You should first populate all the 3 arrays, then run the above loop, then run the arc4Random function on the **combinedArray**. Then from the random dictionary object, extract the 3 objects by using :[dict objectForKey:@"image"], [dict objectForKey:@"text"], [dict objectForKey:@"sound"] – Dhruv Goel Jan 29 '13 at 07:48
  • is that possible through plist – Ravi Vaghela Jan 29 '13 at 11:04
  • yes off course, if you have a static data set, you can use plists – Dhruv Goel Jan 29 '13 at 11:53
  • now i make plist file and set images , images name and images text in the plist now what should i have to do to fetch the data means images on that 9 button randomly and sound play one bye one now thn user have to identify the images according to sound. – Ravi Vaghela Jan 29 '13 at 12:16
  • hello here i m not having right to uplad images but here i can give you brief about that plist – Ravi Vaghela Jan 30 '13 at 07:06
  • Root / imagetext / image / imagesound these all having 36 itmes same name of all – Ravi Vaghela Jan 30 '13 at 07:09
  • so here i get three array in that – Ravi Vaghela Jan 30 '13 at 07:09
  • hell nirav please tell me how i can access value from the plist please – Ravi Vaghela Jan 30 '13 at 11:03
  • helo nirav for the plist how can i diclare the plist means in whihc flow i declare the data i have 36 images , 36 image sound and text for that these three i have so now i want to declare the plist please tellme .. – Ravi Vaghela Feb 01 '13 at 05:04
0

if you want to store array into dictinoary then write below code.What you do is create 3 different array and then create for loop.

          for(int i=0;i<some value:i++)
          {
            dictMut=[[NSMutableDictionary alloc]init];
            [dictMut setObject:[dataArrayA objectAtIndex:w] forKey:@"A"];
            [dictMut setObject:[dataArrayB objectAtIndex:w] forKey:@"B"];
            [dictMut setObject:[dataArrayC objectAtIndex:w] forKey:@"C"];
          }

let me know it is working or not!!!

Happy Coding!!!!

NiravPatel
  • 3,260
  • 2
  • 21
  • 31
0

ok i saw that...Now what you did is you have created textarray, soundarray and imagearray.

Now after create these 3 arrays write code like below..

        for(int i=0;i<36:i++)
      {
        dictMut=[[NSMutableDictionary alloc]init];
        [dictMut setObject:[textarray objectAtIndex:w] forKey:@"A"];
        [dictMut setObject:[soundarray objectAtIndex:w] forKey:@"B"];
        [dictMut setObject:[imagearray objectAtIndex:w] forKey:@"C"];
      }

and your array values will be stored in dictionary.

let me know it is working or not!!!!

HaPPY Coding!!!!

NiravPatel
  • 3,260
  • 2
  • 21
  • 31
  • hello can it be possible using plist? – Ravi Vaghela Jan 29 '13 at 11:40
  • okay nirav i have made plist and define three array in imagename , imagesound and imagetext now how we can get the random 9 from all and store in to dictionary – Ravi Vaghela Jan 30 '13 at 04:59
  • i have posted some links, you can refer from those links.http://www.edumobile.org/iphone/iphone-programming-tutorials/how-to-use-plist-in-iphone/ – NiravPatel Jan 30 '13 at 05:57
  • http://stackoverflow.com/questions/1801070/use-a-plist-dictionary-for-app-settings – NiravPatel Jan 30 '13 at 05:57
  • http://mobiledevelopertips.com/data-file-management/reading-a-plist-into-an-nsarray.html – NiravPatel Jan 30 '13 at 05:58
  • yes that i made i also made plist but now how to access from that that i dont know plz can you help me on this give me ur skype id so ican add and explain you in detais @nirav – Ravi Vaghela Jan 30 '13 at 06:15
  • hey , right now i am in company .so i cant share my skype id to you dude..but i will post code for excessing data from plist file.just show me screenshot of plist file that you have created. – NiravPatel Jan 30 '13 at 06:28