点击播放按钮时,它会在Xcode中崩溃并显示消息“Thread 6 GCDAsyncSocket 0 objc_msgSend”。有时它只是工作,但大多数时候我得到这个错误。任何线索?
快照:http://i.imgur.com/G2Nv4.png
以下是代码行:
- (void)viewDidLoad {
[self initSocket];
}
-(void) initSocket
{
dispatch_queue_t mainQueue = dispatch_get_main_queue();
asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];
[self connectToVLC];
}
-(void) connectToVLC
{
NSString *host = @"192.168.0.8";
uint16_t port = 8080;
NSError *error = nil;
if (![asyncSocket connectToHost:host onPort:port error:&error]){
NSLog(@"unable connect to VLC server");
}else{
NSLog(@"connected to VLC server");
}
}
-(IBAction) btnPlayTapped
{
[self writeCommand:@"stop" tag:TAG_NONE];
[self writeCommand:@"repeat off" tag:TAG_NONE];
[self writeCommand:@"loop off" tag:TAG_NONE];
[self writeCommand:@"add /abc.wav" tag:TAG_NONE];
[self writeCommand:@"play" tag:TAG_PLAY];
}
-(void) writeCommand:(NSString *)cmd tag:(long)theTag
{
NSMutableData *data = [NSMutableData dataWithData:[cmd dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:[GCDAsyncSocket CRLFData]];
[asyncSocket writeData:data withTimeout:-1 tag:theTag];
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
if(tag==TAG_PLAY){
//handle rest operation here
}
}
谢谢!
答案 0 :(得分:1)
通过在writeCommand方法中保留数据对象来解决:
-(void) writeCommand:(NSString *)cmd tag:(long)theTag
{
NSMutableData *data = [NSMutableData dataWithData:[cmd dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:[GCDAsyncSocket CRLFData]];
[data retain];//release it in dealloc
[asyncSocket writeData:data withTimeout:-1 tag:theTag];
}