help-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSKeyedUnarchiver


From: Parrish Myers
Subject: NSKeyedUnarchiver
Date: Sun, 8 Jan 2006 19:44:23 -0700

Can someone please help... this doesn't make sense:

The following code should print the NSMutableArray twice... but only does once... the NSKeyedUnarchiver doesn't seem to work on windows...

------------------------------------

#import <Foundation/Foundation.h>

void printArray(NSMutableArray *anArray)
{
  int length = [anArray count];
  int i;
  for (i = 0; i < length; i++) {
    NSLog(@"object %d : %@", i, [anArray objectAtIndex:i]);
  }
}

int main(int argc, const char **argv)
{
  int i;
  NSData *d1, *d2;
  BOOL result;
  NSMutableArray *theArray, *newArray;

  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  theArray = [[NSMutableArray alloc] init];

  for (i = 0; i < 100; i++) {
    [theArray addObject:[NSNumber numberWithInt:i]];
  }

  printArray(theArray);

  d1 = [NSKeyedArchiver archivedDataWithRootObject:theArray];
  result = [d1 writeToFile:@"archive.plist" atomically:YES];
  NSLog(@"Archived theArray with result: %d", result);

  NSLog(@"Attempting to resurect theArray");
  d2 = [[NSData dataWithContentsOfFile:@" archive.plist"] retain];
  if (d2) {
    NSLog(@"d2 has data");
  } else {
    NSLog(@"d2 is nil");
  }
  newArray = [[NSKeyedUnarchiver unarchiveObjectWithData:d2] retain];
  if (newArray) {
    printArray(newArray);
  } else {
    NSLog(@"I shouldn't be here!");
    NSLog(@"newArray is nil");
  }

  [pool release];
  return result;
}

------------------------------------
Thanks.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]