Getting the right objects in an Array

  • Hello everybody,

    I hope someone can help me with the following; I'm trying to use a
    plist to fill a NSPopupbutton menu item with strings from the plist.
    The plist looks as follows:

    <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd
    ">
          <plist version='1.0'>
                  <dict>
                          <key>List</key>
                          <array>
                                  <dict>
                                          <key>name</key>
                                          <string>Joe</string>
                                          <key>uuid</key>

    <string>A4640553-1C93-4FCD-98FE-917AF1700C6B</string>
                                  </dict>
                                  <dict>
                                          <key>name</key>
                                          <string>Jane</string>
                                          <key>uuid</key>
                                          <string>B6DBECAC-3472-4692-
    B0FC-6143A46A7B53</string>
                                  </dict>
                          </array>
                  </dict>
          </plist>

    I'm using the following code to fill a NSMutableArray with strings
    from the NSDictionary object. The for loop gets the instances of the
    keys 'name' and puts the string in the mutable array object
    'firstNames'. In my program it uses the firstNames-array to fill a
    NSPopupButton with the Array of NSStrings. When that happens the
    program exits with a “EXC_BAD_ACCESS”-error. It looks to me that the
    objects in the returned mutable array are not NSString objects. When I
    debug, Xcode shows my NSString-value as 'invalid'.
    .....

    NSArray *myArray = [dict objectForKey: @"List"];
    firstNames = [NSMutableArray array];

    int i, myNameList = [myArray count];

    for ( i = 0; i < myNameList; i++ ) {

      NSDictionary *nameEntry = [myArray objectAtIndex: i];

      NSString *myName;
      myName = [[NSString alloc] initWithString: [nameEntry valueForKey:
    @"name"]];

      [firstNames addObject: myName];

    }

    return firstNames;
    .....

    Can anybody give me some clues on what I'm doing horribly wrong here?
    Thanks,

    Marcel
  • On 11/5/07, Marcel Borsten <marcel...> wrote:

    > I'm using the following code to fill a NSMutableArray with strings
    > from the NSDictionary object. The for loop gets the instances of the
    > keys 'name' and puts the string in the mutable array object
    > 'firstNames'. In my program it uses the firstNames-array to fill a
    > NSPopupButton with the Array of NSStrings. When that happens the
    > program exits with a "EXC_BAD_ACCESS"-error.

    When what happens? What it the stack trace at the time of the crash?

    > It looks to me that the
    > objects in the returned mutable array are not NSString objects. When I
    > debug, Xcode shows my NSString-value as 'invalid'.

    Consider using NSLog to help trace what is taking place however the
    debugger is usually the best way. Are you sure you are using a debug
    build of you application and stopping at a point in time that ensures
    the vars you are interested in are valid (in scope).

    For example try using the following code...

    ...
        NSArray *myArray = [dict objectForKey: @"List"];
        NSLog(@"myArray=%@", myArray);

        firstNames = [NSMutableArray array];

        int i, myNameList = [myArray count];
        for ( i = 0; i < myNameList; i++ ) {
            NSDictionary *nameEntry = [myArray objectAtIndex: i];
            NSLog(@"nameEntry=%@", nameEntry);

            NSString *name = [nameEntry valueForKey:@"name"];
            NSLog(@"name=%@", name);

            [firstNames addObject: name];
        }
    ...

    If the above logs what you expect then your problem is someplace
    else... As a guess I would say you aren't managing the life of
    firstNames correctly and it is disappearing before you get a chance to
    update your view object with the data.

    You don't list how you populate the NSPopupButton.

    -Shawn
  • I don't know about wrong, but you are making it difficult on yourself.
    Assuming your 'dict' is the property list dictionary, you should be
    able to simply say

    firstNames = [NSMutableArray arrayWithArray:[dict
    valueForKeyPath:@"List.name"]];

    pg

    On Nov 5, 2007, at 9:59 AM, Marcel Borsten wrote:

    > Hello everybody,
    >
    > I hope someone can help me with the following; I'm trying to use a
    > plist to fill a NSPopupbutton menu item with strings from the plist.
    > The plist looks as follows:
    >
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd
    > ">
    > <plist version='1.0'>
    > <dict>
    > <key>List</key>
    > <array>
    > <dict>
    > <key>name</key>
    > <string>Joe</string>
    > <key>uuid</key>
    >
    > <string>A4640553-1C93-4FCD-98FE-917AF1700C6B</string>
    > </dict>
    > <dict>
    > <key>name</key>
    > <string>Jane</string>
    > <key>uuid</key>
    > <string>B6DBECAC-3472-4692-
    > B0FC-6143A46A7B53</string>
    > </dict>
    > </array>
    > </dict>
    > </plist>
    >
    >
    > I'm using the following code to fill a NSMutableArray with strings
    > from the NSDictionary object. The for loop gets the instances of the
    > keys 'name' and puts the string in the mutable array object
    > 'firstNames'. In my program it uses the firstNames-array to fill a
    > NSPopupButton with the Array of NSStrings. When that happens the
    > program exits with a “EXC_BAD_ACCESS”-error. It looks to me that the
    > objects in the returned mutable array are not NSString objects. When
    > I debug, Xcode shows my NSString-value as 'invalid'.
    > .....
    >
    > NSArray *myArray = [dict objectForKey: @"List"];
    > firstNames = [NSMutableArray array];
    >
    > int i, myNameList = [myArray count];
    >
    > for ( i = 0; i < myNameList; i++ )    {
    >
    > NSDictionary *nameEntry = [myArray objectAtIndex: i];
    >
    > NSString *myName;
    > myName = [[NSString alloc] initWithString: [nameEntry valueForKey:
    > @"name"]];
    >
    > [firstNames addObject: myName];
    >
    > }
    >
    > return firstNames;
    > .....
    >
    > Can anybody give me some clues on what I'm doing horribly wrong
    > here? Thanks,
    >
    > Marcel
  • Thank for your reply. It looks like you were right about me not
    managing the life of the objects correctly. I had another look at that
    part of the code and changed the sequence, now it works perfectly,
    Thanks!

    > On 11/5/07, Marcel Borsten <marcel...> wrote:
    >
    >> I'm using the following code to fill a NSMutableArray with strings
    >> from the NSDictionary object. The for loop gets the instances of the
    >> keys 'name' and puts the string in the mutable array object
    >> 'firstNames'. In my program it uses the firstNames-array to fill a
    >> NSPopupButton with the Array of NSStrings. When that happens the
    >> program exits with a "EXC_BAD_ACCESS"-error.
    >
    > When what happens? What it the stack trace at the time of the crash?
    >
    >> It looks to me that the
    >> objects in the returned mutable array are not NSString objects.
    >> When I
    >> debug, Xcode shows my NSString-value as 'invalid'.
    >
    > Consider using NSLog to help trace what is taking place however the
    > debugger is usually the best way. Are you sure you are using a debug
    > build of you application and stopping at a point in time that ensures
    > the vars you are interested in are valid (in scope).
    >
    > For example try using the following code...
    >
    > ...
    > NSArray *myArray = [dict objectForKey: @"List"];
    > NSLog(@"myArray=%@", myArray);
    >
    > firstNames = [NSMutableArray array];
    >
    > int i, myNameList = [myArray count];
    > for ( i = 0; i < myNameList; i++ ) {
    > NSDictionary *nameEntry = [myArray objectAtIndex: i];
    > NSLog(@"nameEntry=%@", nameEntry);
    >
    > NSString *name = [nameEntry valueForKey:@"name"];
    > NSLog(@"name=%@", name);
    >
    > [firstNames addObject: name];
    > }
    > ...
    >
    > If the above logs what you expect then your problem is someplace
    > else... As a guess I would say you aren't managing the life of
    > firstNames correctly and it is disappearing before you get a chance to
    > update your view object with the data.
    >
    > You don't list how you populate the NSPopupButton.
    >
    > -Shawn
    >
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today