NOTE
As per CodaFi comments below, this appears to be an issue with the implementation of NSLocale
.
NSCoder
produces an NSCFLocale
instance, whereas straight allocation returns an NSLocale
.
Due to the fact that NSLocale
and NSCFLocale
perform different equality checks (the former appears to be using CFEqual()
, the latter isEqualToString:
) this leads to the incongruences exposed below.
ORIGINAL POST
This is not exactly an answer, since I don't know what's going on, but I couldn't fit this in a comment. Here's the odd results of some testing
If you "manually" declare an instance of NSLocale
everything works as expected
NSLocale * l1 = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:l1];
NSLocale *l2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"%i", [l1 isEqual:l2]); // => 1, aka YES
But if you declare l1
as [NSLocale currentLocale]
, things go wrong
NSLocale * l1 = [NSLocale currentLocale];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:l1];
NSLocale *l2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"%i", [l1 isEqual:l2]); // => 0, aka NO
Frankly I am as baffled as you are.
Extra info:
Here's the result of another test
NSLocale * l1 = [NSLocale currentLocale];
NSLocale * l3 = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:l1];
NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:l3];
NSLocale *l2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLocale *l4 = [NSKeyedUnarchiver unarchiveObjectWithData:data1];
NSLog(@"\n%p\n%p", l1, l2); // => 0x9849da0
NSLog(@"\n%p\n%p", l3, l4); // => 0x9866770
NSLog(@"%i", [l1 isEqual:l2]); // => 0x9866770
NSLog(@"%i", [l3 isEqual:l4]); // => 0x9866770
Surprisingly enough, the instance returned after unarchiving l1
is the same as l3
!