0
votes

looking for help with NSKeyedUnarchiver in Monotouch

Storying an Array of Dictionary items in an SQLite blob field

IOS Code

NSData     *data = [[NSData alloc] initWithBytes:ptr length:size];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSMutableArray *myArray = [unarchiver decodeObjectForKey:@"pricing"];
[unarchiver finishDecoding];
NSMutableArray *MutData = [[NSMutableArray alloc] init];
for (NSDictionary *obj in myArray) {
     NSMutableDictionary *element = [[NSMutableDictionary alloc] initWithDictionary:obj];
     [MutData addObject:element];
}

Trying to convert to MonoTouch, so far I have

MonoTouch Code

NSData data = NSData.FromString(item.pricing);

NSKeyedUnarchiver unarchive =  new NSKeyedUnarchiver(data);
unarchive.DecodeObject();
unarchive.FinishDecoding();

NSMutableArray array = unarchive.GetNativeField("pricing") as NSMutableArray;
NSMutableArray mutArray = new NSMutableArray();

for (uint count=0; count < array.Count; count++)
{
    NSMutableDictionary mutDict = new NSMutableDictionary(array.ValueAt(count));
    mutArray.Add(mutDict);
}

Produces and Error at runtime

Unhandled managed exception: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: * -[NSKeyedUnarchiver initForReadingWithData:]: incomprehensible archive (0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30) (MonoTouch.Foundation.MonoTouchException) at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:monotouch_IntPtr_objc_msgSend_IntPtr (intptr,intptr,intptr) at MonoTouch.Foundation.NSKeyedUnarchiver..ctor (MonoTouch.Foundation.NSData data) [0x00027] in /Developer/MonoTouch/Source/monotouch/src/Foundation/NSKeyedUnarchiver.g.cs:88 Stacktrace:

From the new NSKeyedUnarchiver(data) line

One can someone suggest tutorials / examples for MonoTouch NSKeyedUnarchiver ?

Two can someone help with this error ?

Last Is this the best way in light that the project is to be cross platform and work under Android (eventually)

Thanks in advance

1

1 Answers

1
votes

The problem is likely with the how you create the initial NSData.

This is what NSData.FromString does, which doesn't quite look like what you're doing in Objetive-C.

If you have a pointer available in C#, try using NSData.FromBytes instead.