[英]Eclipse SWT - How to convert from 'id' to a usable type
I'm trying to create an Android client for a client/server app that has previously only supported iPhone-based clients. 我正在尝试为以前仅支持基于iPhone的客户端的客户端/服务器应用程序创建Android客户端。 The server was not really designed with the intention of supporting other devices, and some of the data I need to be able to process is stored as a chunk of bytes that was generated by using an
NSKeyedArchiver
to serialize an NSDictionary
on the iPhone client. 该服务器并非真正为支持其他设备而设计的,我需要处理的一些数据存储为通过使用
NSKeyedArchiver
在iPhone客户端上序列化NSDictionary
生成的字节块。
To handle the deserialization I'm trying to use the Eclipse SWT, which includes Java bindings for Cocoa API calls and classes, including NSKeyedArchiver
and NSKeyedUnarchiver
. 为了处理反序列化,我尝试使用Eclipse SWT,其中包括Cocoa API调用和类的Java绑定,包括
NSKeyedArchiver
和NSKeyedUnarchiver
。 These seem to be working, but NSKeyedUnarchiver.unarchiveObjectWithData()
returns an id
type, and I haven't been able to figure out how to convert it into something usable (it should actually be an NSDictionary
). 这些似乎有效,但是
NSKeyedUnarchiver.unarchiveObjectWithData()
返回一个id
类型,我还无法弄清楚如何将其转换为可用的东西(它实际上应该是NSDictionary
)。 Obviousing in objective-c a simple cast would do the trick, but attempting the same in Java throws a ClassCastException
at runtime: 在Objective-c中显然可以实现简单的转换,但是在Java中尝试相同的转换会在运行时抛出
ClassCastException
:
Exception in thread "main" java.lang.ClassCastException: org.eclipse.swt.internal.cocoa.id cannot be cast to org.eclipse.swt.internal.cocoa.NSDictionary
线程“主”中的异常java.lang.ClassCastException:org.eclipse.swt.internal.cocoa.id无法转换为org.eclipse.swt.internal.cocoa.NSDictionary
The code I am testing with is: 我正在测试的代码是:
public static void main(String[] args) throws Exception {
NSAutoreleasePool pool = (NSAutoreleasePool)new NSAutoreleasePool().alloc().init();
//read the test data into a byte array
int read = 0;
byte[] buffer = new byte[1024];
FileInputStream in = new FileInputStream(INPUT);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
while((read = in.read(buffer)) != -1) {
bytes.write(buffer, 0, read);
}
buffer = bytes.toByteArray();
System.out.println("Read " + buffer.length + " bytes");
//load the byte array into an NSData instance
NSData data = NSData.dataWithBytes(buffer, buffer.length);
//deserialize the data
id result = NSKeyedUnarchiver.unarchiveObjectWithData(data);
//error here
NSDictionary dictionaryResult = (NSDictionary)result;
pool.release();
}
So what do I need to do to convert 'result' from id
to something more useful, like NSDictionary
? 那么,我需要怎么做才能将“结果”从
id
转换为更有用的东西,例如NSDictionary
?
Bah, nevermind, I figured it out. ah,没关系,我知道了。 The way to get from
id
to something usable in SWT is to construct a new instance of the desired concrete type, passing the id
as a parameter to the constructor. 从
id
到SWT中可用的东西的方法是构造所需具体类型的新实例,将id
作为参数传递给构造函数。 Like: 喜欢:
NSDictionary dictionaryResult = new NSDictionary(result);
Obvious enough, when you think about it. 显而易见,当您考虑它时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.