Use of data within a plist in Xcode for iOS -


hoping out there can me. i'm new ios app development , trying put careers day event @ local school.

the background want able test students' ability accurately process , input data.

to test this, want app deploy onto ipads them try during day. simple stuff (i think!)

so far know how create app, put input boxes in , linked , have worked out how test input boxes correct against hard coded value in code this:

  self.dealid = self.dealidentry.text;     nsstring *check1 = @"no";     nsstring *dealidstring = self.dealid;     if ([dealidstring length] == 0) {         check1 = @"no deal id entered";     }     if([self.dealid isequal: @"12345678"]) {         check1 = @"yes";     }     self.dealidcheck.text = check1; 

what want though, have table of possible dealids, each 5-6 bits of associated data (customer, currency etc) , when student enters deal id , correct customer, currency etc app should check doing "lookup" of dealid in plist , checking customer value correct dealid.

i've tried doing means of dictionary:

nsbundle *bundle = [nsbundle mainbundle]; nsstring *plistpath = [bundle pathforresource:@"testtable" oftype:@"plist"]; nsdictionary *dict = [[nsdictionary alloc] initwithcontentsoffile:plistpath];    self.customername = self.customernameentry.text;     nsstring *check2 = @"no";     nsdictionary *dealid = [dict valueforkey:self.dealid];     nsstring *customernamestring = self.customername;     if ([customernamestring length] == 0) {         check2 = @"no customer name entered";     }     if([self.customername isequal: [dealid objectforkey:@"customername"]]) {         check2 = @"yes";     }     self.customernamecheck.text = check2; 

sadly it's not working, suspect either don't know how correctly load plist (or test if it's loaded) or i've done wrong!

edit 2 - matt's below, have code load plist directory of directories , can query relevant object given subdirectory. again matt!

//this loads array of data     nsbundle* bundle = [nsbundle mainbundle];     nsstring* plistpath = [bundle pathforresource:@"testtable" oftype:@"plist"];     //make large dictionary     nsdictionary *tradetable = [[nsdictionary alloc] initwithcontentsoffile:plistpath];     nslog(@"the count: %i", [tradetable count]);     //create relevant sub directory     nsdictionary *query = [tradetable valueforkey:@"12345678"];     nslog(@"the count: %i", [query count]);     //query subdirectory object in question     nsstring *name = [query objectforkey:@"customername"];     nslog(name); 

for interested, plist looks this:

<dict>     <key>12345678</key>     <dict>         <key>customerid</key>         <integer>98765</integer>         <key>customername</key>         <string>bigbank</string>         <key>customerbank</key>         <string>littlebank</string>         <key>amountinccy</key>         <integer>75000</integer>         <key>ccy</key>         <string>gbp</string>         <key>amountinusd</key>         <integer>115000</integer>         <key>date</key>         <integer>1</integer>         <key>points</key>         <integer>5</integer>     </dict>     <key>12345679</key>     <dict>         <key>customerid</key>         <integer>98754</integer>         <key>customername</key>         <string>carshop</string>         <key>customerbank</key>         <string>bigbank</string>         <key>amountinccy</key>         <integer>25123</integer>         <key>ccy</key>         <string>eur</string>         <key>amountinusd</key>         <integer>27000</integer>         <key>date</key>         <integer>0</integer>         <key>points</key>         <integer>4</integer>     </dict> </dict> </plist> 

sorry being wordy, wanted make sure gave info could.

very appreciate anyone's assistance. phill

the plist you've shown load nsarray of nsdictionary objects. load nsarray arraywithcontentsoffile:; walk nsarray until find nsdictionary want.

you using [[nsdictionary alloc] initwithcontentsoffile:plistpath], result (your dict) nil because not dictionary, it's array. docs on initwithcontentsoffile: predict this:

return value: initialized dictionary... or nil if ... contents of file invalid representation of dictionary.

(my italics)

well, that's problem. contents of file invalid representation of dictionary, because in fact valid representation of array.

now, i'm not saying array of dictionaries best way store data. think dictionary of dictionaries have been better (because grab right subdictionary instantly master key, such dealid). but, whatever reason, that's not how stored data. stored array, , must retrieve array.

edit: i'm suggesting use value of deal id key subdictionaries. this:

dictionary     key: 12345678     value: whole nother dictionary *else* answer     ---     key: 12345679     value: whole nother dictionary *else* answer     ---     etc. 

so start big dictionary of dictionaries (d), d[somedealid] correct subdictionary (subd), , can subd[@"customername"] or other value key. seems me sort of "lookup" describing.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -