objective c - Accessing Dictionaries inside a Dictionary by Index in Obj-C -


i'm trying learn objective-c , i've stumbled across little problem. want build ipad application collecting simple numbers,which user enters, date , time. thought of structure:

  • dictionary("main")
    • dictionary("27012013") //this holds data 27th of january 2013
      • index = 0
      • 3:33pm = 123 //this mean @ 3:33pm there value of 123
      • ..other values follow
    • dictionary("28012013") //and on

so there 1 big dictionary called "main" holds dictionaries days hold index , recorded values.

i value's uialertview input calls the

(void)alertview:(uialertview *)alertview clickedbuttonatindex:(nsinteger)buttonindex 

delegate method in current time , date saved in nsstring's this:

nsdateformatter *format = [[nsdateformatter alloc] init]; [format setdatestyle:nsdateformatternostyle]; [format settimestyle:nsdateformattershortstyle]; nsstring *time = [format stringfromdate:[nsdate date]]; nslog(@"time: %@ , entered text:%@",time,returnvalue.text);  //which date have [format setdatestyle:nsdateformattershortstyle]; [format settimestyle:nsdateformatternostyle]; nsstring *date = [format stringfromdate:nsdate.date]; nslog(@"found date:%@",date); nsstring *identifier =    [[date componentsseparatedbycharactersinset:                  [nscharacterset punctuationcharacterset]] componentsjoinedbystring:@""]; nslog(@"identifier:%@",identifier); 

where returnvalue.text holds entered text. check if dictionary called nsstring identifier exist , if not add main:

        if([main objectforkey:identifier] == nil){         //no dict available current date create one:         //there should no more 30 entries per day         nsmutabledictionary *d = [[nsmutabledictionary alloc] initwithcapacity:30];         [d setobject:identifier forkey:@"name"];         nsnumber* tmpi = [nsnumber numberwithinteger:main.count];         [d setobject:tmpi forkey:@"index"];         //and store recieved value in         [d setobject:returnvalue.text forkey:time];         [main setobject:d forkey:identifier];     }     else{         nsmutabledictionary *d = [main objectforkey:identifier];         [d setobject:returnvalue.text forkey:time];      } 

the first question here is: have use main.count or main.count+1?

furthermore want display information in tableview in each day should have own section. method

- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section 

i therefore need address dictionaries inside main dictionary index or e.g. filter them index key return number of values inside of them. use giant nsarray main make method determining if dictionary day exist more complicated , rather not want this. please me?

thanks in advance

first answer: depends on "main.count" return number of objects in dictionary may it's zero. if want start 0 it's otherwise +1.

second answer: here function of nsdictionary "allkeys" return keys "nsarray" can each key index if don't know "key" using index array.


Comments