Pieter Kuijpers
Pieter Kuijpers

Reputation: 7317

Object has different class after archiving/unarchiving

I'm working on an Objective-C code base for an iPad project using iOS 6.

After I refactored the name of class 'ClassA' to 'ClassB', I find the following test failing:

NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:objectOfClassB];
ServiceOrderOld *decodedObject = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];

[[[decodedObject class] should] equal:[objectOfClassB class]];

The error message I get reads: "[FAILED], expected subject to equal ClassB, got ClassB"

What could be causing this strange behaviour? Any tips on debugging this?

Upvotes: 4

Views: 371

Answers (2)

Tom
Tom

Reputation: 1174

I hope I'm not too late ;)

I had the same problem while testing a framework. It turns out that the class in question was also in the test's target. I think it then mismatch (but compile!) with the framework's class. I removed the class from the test's target and the test passes.

Upvotes: 1

Mundi
Mundi

Reputation: 80271

It seems class is really irrelevant for the archiving mechanism. Did you try just re-casting the class when you unarchive?

DesiredClass *object = (DesiredClass*) [NSKeyedUnarchiver
   unarchiveObjectWithData:encodedObject];

Upvotes: 0

Related Questions