MonoTouch NSKeyedUnarchiver

时间:2013-04-12 13:35:17

标签: xamarin.ios xamarin nskeyedarchiver

在Monotouch中寻找NSKeyedUnarchiver的帮助

在SQLite blob字段中编写一个字典项数组

IOS代码

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];
}

尝试转换为MonoTouch,到目前为止我已经

MonoTouch代码

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);
}

运行时产生和错误

  

未处理的托管异常:抛出Objective-C异常。名称:   NSInvalidArgumentException原因: * - [NSKeyedUnarchiver   initForReadingWithData:]:难以理解的存档(0x62,0x70,0x6c,   0x69,0x73,0x74,0x30,0x30)   (MonoTouch.Foundation.MonoTouchException)at(包装器   托管到本机)   MonoTouch.ObjCRuntime.Messaging:monotouch_IntPtr_objc_msgSend_IntPtr   (intptr,intptr,intptr)at   MonoTouch.Foundation.NSKeyedUnarchiver..ctor   (MonoTouch.Foundation.NSData数据)[0x00027] in   /Developer/MonoTouch/Source/monotouch/src/Foundation/NSKeyedUnarchiver.g.cs:88   堆栈跟踪:

从新的NSKeyedUnarchiver(数据)行

有人可以推荐MonoTouch NSKeyedUnarchiver的教程/示例吗?

有人可以帮助解决此错误吗?

最后这是最好的方式,该项目是跨平台的,并在Android下工作(最终)

提前致谢

1 个答案:

答案 0 :(得分:1)

问题可能在于您如何创建初始NSData。

This是NSData.FromString所做的,它看起来不像你在Objetive-C中所做的那样。

如果您在C#中有指针,请尝试改为使用NSData.FromBytes