取消存档 NSMutableDictionary o NSData 时出现错误?
我正在尝试通过 wifi/蓝牙将信息字典从一台 iphone 发送到另一台 iphone,为此我实现了 CFnetwork 和 NSNetservice Concept。我在接收器端收到了数据。
我的代码如下......
**Sender Side**
samDict=[NSMutableDictionary dictionary];
[samDict setObject:@"Muhammed Musthafa" forKey:@"PP"];
[samDict setObject:@"John P" forKey:@"Jose"];
[samDict setObject:@"Lubaba" forKey:@"P"];
[samDict setObject:@"JINI" forKey:@"KS"];
[samDict setObject:@"Anusha" forKey:@"GS"];
[samDict setObject:@"Athulya" forKey:@"V"];
[samDict setObject:@"Riyas" forKey:@"MM"];
[samDict setObject:@"Raihanath" forKey:@"MH"];
[samDict setObject:@"Shabeer" forKey:@"poola"];
[samDict setObject:@"Rajisha" forKey:@"Raji"];
//NSLog(@" Dictionary values.............%@",samDict);
NSMutableData *ArchiveData=[[NSMutableData alloc]init];
NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:ArchiveData];
[archiver setOutputFormat: NSPropertyListXMLFormat_v1_0];
[archiver encodeObject:self.samDict forKey:@"Some Key Values"];
[archiver finishEncoding];
[archiver release];
const uint8_t *buffer=(const uint8_t *)[ArchiveData bytes];
NSInteger nWrtitten=[_outStream write:buffer maxLength:[ArchiveData legth]];
if (!nWrtitten)
{
NSLog(@"Eorror writting to Stream %@ : %@", _outStream, [_outStream streamError]);
}
else
{
NSLog(@" Write %ld bytes to Stream %@",(long)nWrtitten, _outStream);
}
接收方
NSInteger bytesRead;
uint8_t buffer[13000];
NSMutableData *data=[[NSMutableData data]retain];
if (stream==_inStream)
{
bytesRead=[_inStream read:buffer maxLength:sizeof(buffer)];
if (bytesRead==-1&& bytesRead==0)
{
NSLog(@"_inStream Error...................");
}
else
{
[data appendBytes:buffer length:bytesRead];
NSLog(@"Data Received has length :%d\n\n",[data length]);
NSLog(@"data............%@",data);
}
}
我在接收方获取了数据。数据为字节格式。我无法在接收器端取消存档此数据。如果我尝试将其取消存档为这样的字典格式
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSMutableDictionary* UnArchiveDictionary =[NSMutableDictionary dictionary];
UnArchiveDictionary = [[unarchiver decodeObjectForKey:@"Some Key Values"] retain];
NSLog(@"UnArchive Dictionary %@",UnArchiveDictionary);
,但不幸的是我无法以字典格式取消存档此数据。我收到行 NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
的错误
我找到了一些将字节数据转换为字符串格式的代码,如下所示
size_t length=[data length];
unsigned char aBuffer[length];
[data getBytes:aBuffer length:length];
aBuffer[length - 1]=0;
NSString *messageString =aBuffer;
NSLog (@"%s",messageString);
但我的问题是如何返回同一本词典。为什么在未归档时出现错误?
I am trying to send a dictionary of information from one iphone to another iphone through wifi/blutooth,for that i implement CFnetwork and NSNetservice Concept.I received Data in the Reciver side.
I did code as follows ....
**Sender Side**
samDict=[NSMutableDictionary dictionary];
[samDict setObject:@"Muhammed Musthafa" forKey:@"PP"];
[samDict setObject:@"John P" forKey:@"Jose"];
[samDict setObject:@"Lubaba" forKey:@"P"];
[samDict setObject:@"JINI" forKey:@"KS"];
[samDict setObject:@"Anusha" forKey:@"GS"];
[samDict setObject:@"Athulya" forKey:@"V"];
[samDict setObject:@"Riyas" forKey:@"MM"];
[samDict setObject:@"Raihanath" forKey:@"MH"];
[samDict setObject:@"Shabeer" forKey:@"poola"];
[samDict setObject:@"Rajisha" forKey:@"Raji"];
//NSLog(@" Dictionary values.............%@",samDict);
NSMutableData *ArchiveData=[[NSMutableData alloc]init];
NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:ArchiveData];
[archiver setOutputFormat: NSPropertyListXMLFormat_v1_0];
[archiver encodeObject:self.samDict forKey:@"Some Key Values"];
[archiver finishEncoding];
[archiver release];
const uint8_t *buffer=(const uint8_t *)[ArchiveData bytes];
NSInteger nWrtitten=[_outStream write:buffer maxLength:[ArchiveData legth]];
if (!nWrtitten)
{
NSLog(@"Eorror writting to Stream %@ : %@", _outStream, [_outStream streamError]);
}
else
{
NSLog(@" Write %ld bytes to Stream %@",(long)nWrtitten, _outStream);
}
Receiver side
NSInteger bytesRead;
uint8_t buffer[13000];
NSMutableData *data=[[NSMutableData data]retain];
if (stream==_inStream)
{
bytesRead=[_inStream read:buffer maxLength:sizeof(buffer)];
if (bytesRead==-1&& bytesRead==0)
{
NSLog(@"_inStream Error...................");
}
else
{
[data appendBytes:buffer length:bytesRead];
NSLog(@"Data Received has length :%d\n\n",[data length]);
NSLog(@"data............%@",data);
}
}
I got data in Receiver Side.that is in bytes format . I can't UnArchive this data in Receiver side.if i am trying to unarchived this to Dictionary format like this
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSMutableDictionary* UnArchiveDictionary =[NSMutableDictionary dictionary];
UnArchiveDictionary = [[unarchiver decodeObjectForKey:@"Some Key Values"] retain];
NSLog(@"UnArchive Dictionary %@",UnArchiveDictionary);
But unfortunately ican't Unarchived this data in Dictionary Format. i am getting Error of line NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
I have found some code for that converting bytes Data to String Format like this
size_t length=[data length];
unsigned char aBuffer[length];
[data getBytes:aBuffer length:length];
aBuffer[length - 1]=0;
NSString *messageString =aBuffer;
NSLog (@"%s",messageString);
But my problem is how to get back the same dictionary . Why getting error in Unarchived time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能在于条件:
将始终为假。一个变量不能同时有两个不同的值。您可能想要:
因此,如果出现错误(或没有传输数据),您的代码不会注意到,而是将堆栈中的垃圾字节附加到可变数据中。
然后,带密钥的解存档器尝试将这些垃圾字节解释为存档,并注意到它不是存档,因此它抛出异常。
The problem might be that the condition:
will always be false. A variable can't have two different values at once. You probably want:
So if there was an error (or no data was transmitted) your code wouldn't notice and instead append garbage bytes from the stack to your mutable data.
The keyed unarchiver then tries to interpret these garbage bytes as an archive and notices that it isn't, so it throws an exception.