Object vanished before it is removed?

  • Hi all,

    I am trying to loop through an NSMutableArray of
    NSMutableDictionaries, extract archived NSColors, convert them to
    hexValues and then remove the NSColor from the NSArray. Here's the code:

    int row;
    for (row = 0; row < [theArray count]; row++) {
    NSMutableDictionary *testDic = [NSMutableDictionary
    dictionaryWithDictionary: [theArray objectAtIndex: row]];

    NSColor *bgColor = (NSColor *)[NSUnarchiver unarchiveObjectWithData:
    [testDic valueForKeyPath:
    @"OUTPUT_WINDOW_SETTINGS.NSBACKGROUND_COLOR"]];

    [[testDic valueForKey: @"OUTPUT_WINDOW_SETTINGS"]
      setObject: [bgColor hexadecimalValueOfAnNSColor]
                   forKey: @"BACKGROUND_COLOR"];

    NSLog(@"%@ has the value %@", [testDic valueForKey: @"NAME"] ,
    [testDic valueForKeyPath:
    @"OUTPUT_WINDOW_SETTINGS.NSBACKGROUND_COLOR"]);

    [[testDic valueForKey: @"OUTPUT_WINDOW_SETTINGS"] removeObjectForKey:
    @"NSBACKGROUND_COLOR"];
    }

    The problem is, after the first loop, the log reads:

    Testdata1 has the value <040b7374 7265616d 74797065 6481e803 84014084
    8484074e 53436f6c 6f720084 84084e53 4f626a65 63740085 84016301
    84046666 66660000 010186>
    Testdata2 has the value (null)
    *** -initForReadingWithData: nil argument

    But Testdata2 does exist and has the appropriate value (checked with
    NSLog). What am I missing?

    Cheers,

    Knut
  • Your first problem is that it is incorrect for keys to begin with
    anything other than lowercase letters.  From
    http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Co
    ncepts/BasicPrinciples.html#//apple_ref/doc/uid/20002170

    :

    "Keys must use ASCII encoding, begin with a lowercase letter, and may
    not contain whitespace.

    Some example keys would be payee, openingBalance, transactions and amount."

    Fix that and let's see what happens.

    --Kyle Sluder