tags:

views:

465

answers:

3

ok i have a plist like this

`
<dict>
<key>Rows</key>
<array>
 <dict>
  <key>WireSize</key>
  <string>16 AWG</string>
  <key>Children</key>
  <array>
   <dict>
    <key>Cooper 60°C (140°F)</key>
    <string>0</string>
   </dict>
   <dict>
    <key>Cooper 75°C (167°F)</key>
    <string>0</string>
   </dict>
   <dict>
    <key>Cooper 90°C (194°F)</key>
    <string>14</string>
   </dict>
   <dict>
    <key>Aluminum 60°C (140°F)</key>
    <string>0</string>
   </dict>
   <dict>
    <key>Aluminum 75°C (167°F)</key>
    <string>0</string>
   </dict>
   <dict>
    <key>Aluminum 90°C (194°F)</key>
    <string>0</string>
   </dict>
  </array>
 </dict>
 <dict>
  <key>WireSize</key>
  <string>16 AWG</string>
  <key>Children</key>
  <array>
   <dict>
    <key>Cooper 60°C (140°F)</key>
    <string>0</string>
   </dict>
   <dict>
    <key>Cooper 75°C (167°F)</key>
    <string>0</string>
   </dict>
   <dict>
    <key>Cooper 90°C (194°F)</key>
    <string>14</string>
   </dict>
   <dict>
    <key>Aluminum 60°C (140°F)</key>
    <string>0</string>
   </dict>
   <dict>
    <key>Aluminum 75°C (167°F)</key>
    <string>0</string>
   </dict>
   <dict>
    <key>Aluminum 90°C (194°F)</key>
    <string>0</string>
   </dict>
  </array>
 </dict>

</array>

`

and been trying to read the values from it but not success

i am using this code

enter NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"Table 310-16" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

for (id key in dictionary) {
 NSArray *array = [dictionary objectForKey:key];
 NSLog(@"key: %@, value: %@", key, [array objectAtIndex:0]);
} here

and the results are

key: Rows, value: {
Children =     (
            {
        "Cooper 60\U00b0C (140\U00b0F)" = 0;
    },
            {
        "Cooper 75\U00b0C (167\U00b0F)" = 0;
    },
            {
        "Cooper 90\U00b0C (194\U00b0F)" = 14;
    },
            {
        "Aluminum 60\U00b0C (140\U00b0F)" = 0;
    },
            {
        "Aluminum 75\U00b0C (167\U00b0F)" = 0;
    },
            {
        "Aluminum 90\U00b0C (194\U00b0F)" = 0;
    }
);
WireSize = "16 AWG";

}

but still don't know how to get and specific value for example Aluminum 60°C (140°F) or 14 or 16 AWG any help would be appresiated

HP

A: 

NSDictionary keys must be NSString objects. Given that the example for loop uses id key, I'll bet you're used to a language like ruby that allows keys to be an arbitrary object type. Since the keys must be NSStrings, the following will always work:

for (NSString *key in dictionary) {
    NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}

You can pass the string directly. So, to get a specific value such as your example:

// Edit: objectForValue: should have been objectForKey:
NSLog(@"Value for Aluminum 60°C (140°F): %@", [dictionary objectForKey:@"Aluminum 60°C (140°F)"]);
John Franklin
NSDictionary keys can be more than just NSStrings, they simply need to conform to the NSCopying protocol. Also, as has been pointed out, you probably meant -valueForKey: instead of objectForValue:.
Brad Larson
Brad: That is correct. From the Apple docs: "In general, a key can be any object (provided that it conforms to the NSCopying protocol—see below), but note that when using key-value coding the key must be a string (see Key-Value Coding Fundamentals)."
John Franklin
A: 

john when i try to use this code

enter NSLog(@"Value for Aluminum 60°C (140°F): %@", [dictionary objectForValue:@"Aluminum 60°C (140°F)"]);

here

i got this warning: 'NSDictionary' may not respond to '-objectForValue:'

and when i run the app it crash with this message

2009-12-19 12:50:14.718 Ampacity[3680:20b] * -[NSCFDictionary objectForValue:]: unrecognized selector sent to instance 0x4159140 2009-12-19 12:50:14.719 Ampacity[3680:20b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFDictionary objectForValue:]: unrecognized selector sent to instance 0x4159140' Blockquote

still trying to figure it out thanks for your help

quky
I think he made a typo. NSDictionary doesn't have a `objectForValue`. You want to use `objectForKey:`. This dictionary is somewhat unusual in that it has very complex keys and very simple values. I think it is easy to reverse the two in one's head. I did the first time I read the OP.
TechZen
any suggestion on how would look like the plist according to you.thanks any help would be appreciated
quky
A: 

If you want to do arbitrary lookups from a given key, you'll simply need to write some helper methods to iterate through your plist data structure, or perhaps a bit cleaner is to create a couple classes to encapsulate those data structures and create a tree of those classes when the app starts up.

It also seems like you could simplify the data structure a bit. something like this perhaps?

<array>
    <dict>
     <key>16 AWG</key>
     <array>
       <dict>
         <key>Cooper 60°C (140°F)</key>
         <string>0</string>
       </dict>
       <dict>
         <key>Cooper 75°C (167°F)</key>
         <string>0</string>
       </dict>
     </array>
    </dict>
    <dict>
     <key>12 AWG</key>
     <array>
       <dict>
         <key>Cooper 60°C (140°F)</key>
         <string>0</string>
       </dict>
       <dict>
         <key>Cooper 75°C (167°F)</key>
         <string>0</string>
       </dict>
     </array>
    </dict>
</array>

So you can then presume that all the keys in the top-level array of dicts are wire gauges, and the values for each key are the wire options.

wkw
thanks wkw i when that route already appreciated your help
quky