解码器通过蓝牙连接崩溃
我有一个对象想通过蓝牙发送到另一个设备。我已成功设置蓝牙连接并传输编码的 NSString;但是,我还没有弄清楚如何正确使用归档和编码工具来发送对象。
我想发送下面定义的名为 ChatMessage 的对象。它实现了 NSCoding 委托方法 initWithCoder 和encodeWithCoder,如下所示。
在第二个代码片段中,我有用于发送和接收数据的代码,即导致调用解码器的方法。
它在解码方法的最后一行不断崩溃。我一直在努力弄清楚出了什么问题。任何帮助将不胜感激!
@interface ChatMessage : NSObject <NSCoding> {
NSString *sender;
NSString *message;
}
@property (nonatomic, retain) NSString *sender;
@property (nonatomic, retain) NSString *message;
@end
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:sender forKey:@"sender"];
[coder encodeObject:message forKey:@"message"];
}
- (id)initWithCoder:(NSCoder *)coder {
sender = [[coder decodeObjectForKey:@"sender"] retain];
message = [[coder decodeObjectForKey:@"message"] retain];
return self;
}
在我看来,PeerPicker Delegate 函数的协议。
- (void) receiveData:(NSData *)data
fromPeer:(NSString *)peer
inSession:(GKSession *)session
context:(void *)context {
ChatMessage *aMsg = [[ChatMessage alloc] init];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData:data];
@try {
aMsg = [unarchiver decodeObjectForKey:@"myMessage"];
}
@catch (NSException *exception) {
NSLog(@"Error: %@", exception);
}
@finally {
}
if (!messages) messages = [[NSMutableArray alloc] init];
[messages addObject:aMsg];
// reload the table
[messageList reloadData];
[unarchiver finishDecoding];
[unarchiver release];
[data release];
}
I have an object that I want to send to another device via bluetooth. I have successfully setup the bluetooth connection and transferred an encoded NSString; however, I haven't figured out how to use the archiving and encoding tools correctly to send an Object.
I want to send the object defined below called ChatMessage. It implements the NSCoding delegate methods initWithCoder and encodeWithCoder as seen below.
In the second code snippet, I have the code for sending and receiving the data i.e. the methods that result in the de-encoder being called.
It keeps crashing on the last line of the decode method. I've been struggling to figure it out what is going wrong. Any help would be greatly appreciated!
@interface ChatMessage : NSObject <NSCoding> {
NSString *sender;
NSString *message;
}
@property (nonatomic, retain) NSString *sender;
@property (nonatomic, retain) NSString *message;
@end
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:sender forKey:@"sender"];
[coder encodeObject:message forKey:@"message"];
}
- (id)initWithCoder:(NSCoder *)coder {
sender = [[coder decodeObjectForKey:@"sender"] retain];
message = [[coder decodeObjectForKey:@"message"] retain];
return self;
}
In my View, the protocol for the PeerPicker Delegate functions.
- (void) receiveData:(NSData *)data
fromPeer:(NSString *)peer
inSession:(GKSession *)session
context:(void *)context {
ChatMessage *aMsg = [[ChatMessage alloc] init];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData:data];
@try {
aMsg = [unarchiver decodeObjectForKey:@"myMessage"];
}
@catch (NSException *exception) {
NSLog(@"Error: %@", exception);
}
@finally {
}
if (!messages) messages = [[NSMutableArray alloc] init];
[messages addObject:aMsg];
// reload the table
[messageList reloadData];
[unarchiver finishDecoding];
[unarchiver release];
[data release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
--- 代码崩溃了,因为我有
[数据发布];
我使用仪器工具发现了这一点。
--- The code was crashing because I had
[data release];
I found this using the instruments tool.