Archiving NSError

  • Hello to mac developers :)
    If I don't misunderstand things NSError objects can be archived
    because NSError implements NSCoding protocol.
    I try to archive it as follows:

    NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
    NSError * error = [[NSError alloc] initWithDomain: @"TestDomain"
    code: 1 userInfo: nil];

    [dict setObject:error forKey:@"Error"];
    [dict setObject:@"OperationName" forKey:@"Operation"];
    [dict writeToFile:@"/Users/ivira/temp" atomically:YES];

    Without placing error into dict it work's ok. What am I doing wrong?

    --
    With regards, Vera Tkachenko
    [ICQ#230923300]
    [web http://vera.org.ua]
  • Le 16 nov. 08 à 15:41, Vera Tkachenko a écrit :

    > Hello to mac developers :)
    > If I don't misunderstand things NSError objects can be archived
    > because NSError implements NSCoding protocol.
    > I try to archive it as follows:
    >
    > NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
    > NSError * error = [[NSError alloc] initWithDomain: @"TestDomain"
    > code: 1 userInfo: nil];
    >
    > [dict setObject:error forKey:@"Error"];
    > [dict setObject:@"OperationName" forKey:@"Operation"];
    > [dict writeToFile:@"/Users/ivira/temp" atomically:YES];
    >
    > Without placing error into dict it work's ok. What am I doing wrong?

    You try to serialize it not archiving it.

    See this guide for the difference between the two technics:

    http://developer.apple.com/documentation/Cocoa/Conceptual/Archiving/Archivi
    ng.html
  • Have a look at the documentation for -writeToFile:atomically: In
    particular this quote:

    "This method recursively validates that all the contained objects are
    property list objects (instances of NSData, NSDate, NSNumber,
    NSString, NSArray, or NSDictionary) before writing out the file, and
    returnsNO if all the objects are not property list objects, since the
    resultant file would not be a valid property list."

    What is your real goal? Do you want a plist where the Error key is an
    archived NSError object, or just an archived version of the
    dictionarty? I'm going to guess the former in which case you would
    change your code to do:

    [dict setObject:[NSKeyedArchiver archivedDataWithRootObject:error]
    forKey:@"Error"];

    On 16 Nov 2008, at 14:41, Vera Tkachenko wrote:

    > Hello to mac developers :)
    > If I don't misunderstand things NSError objects can be archived
    > because NSError implements NSCoding protocol.
    > I try to archive it as follows:
    >
    > NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
    > NSError * error = [[NSError alloc] initWithDomain: @"TestDomain"
    > code: 1 userInfo: nil];
    >
    > [dict setObject:error forKey:@"Error"];
    > [dict setObject:@"OperationName" forKey:@"Operation"];
    > [dict writeToFile:@"/Users/ivira/temp" atomically:YES];
    >
    > Without placing error into dict it work's ok. What am I doing wrong?
    >
    > -- With regards, Vera Tkachenko
    > [ICQ#230923300]
    > [web http://vera.org.ua]