tags:

views:

76

answers:

1

hi,

I'm trying to made a cocoa app that read-write to a .plist file. I can retrieve informations from the .plist, write into, but when a key (only with strings) is empty, the app don't write to the plist.

here a sample:

-

 (IBAction)saveBoot:(id)sender {
    NSString *errorDesc;
    NSString *bootPath = @"/myplist.plist";
    NSMutableDictionary *plistBootDict = 
        [NSMutableDictionary dictionaryWithObjects:
        [NSMutableArray arrayWithObjects:

                  Rescan,
           RescanPrompt,
           GUI,
           InstantMenu,
                  DefaultPartition,
           EHCIacquire,
           nil]

                  forKeys:[NSMutableArray arrayWithObjects: 

           @"Rescan",
           @"Rescan Prompt",
           @"GUI",
           @"Instant Menu",
           @"Default Partition",
           @"EHCIacquire",
           nil]];

      NSData *plistBootData = [NSPropertyListSerialization 
                        dataFromPropertyList:plistBootDict
               format:NSPropertyListXMLFormat_v1_0
            errorDescription:&errorDesc];




        if (bootPath) {
        [plistBootData writeToFile:bootPath atomically:NO];
    }
        else {
            NSLog(errorDesc);
            [errorDesc release];
        }


    }
    @end

I think i need a loop to check if each key is empty or not (and remove it if empty), but i've tried different (objectEnumerator, objectForKey:..etc) method whitout success.

If someone can help a beginner like me, thanks in advance.

Ronan.

A: 

The problem is probably that because nil is the terminator for variable argument lists, so if, say, RescanPrompt is nil, the object array will only contain up until that part (so you can't "remove if empty" since it won't exist in the dictionary in the first place). You should probably construct your dictionary piece by piece; something like:

NSMutableDictionary *plistBootDict = [NSMutableDictionary dictionary];

if (Rescan)
    [plistBootDisc setObject:Rescan forKey:@"Rescan"];
if (GUI)
    [plistBootDisc setObject:GUI forKey:@"GUI"];

// etc

(Also, there's no reason to be using NSMutableArray or NSMutableDictionary if you're never going to be mutating them later.)

Wevah
OK, will try in this way. Thanks for advices.
ronan
Just tried and it works as expected. Many thanks for your Help. Regards,
ronan