2012-08-10 29 views
0

我試圖在iOS應用程序中使用GCDAsyncSocket。我一直在關注CocoaAsyncSocket的wiki中提供的所有步驟。下面是我在做什麼:嘗試在iOS 5.1上使用GCDAsyncSocket寫數據的錯誤

GCDAsyncSocket socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; 

    NSError *err = nil; 
    if (![socket connectToHost:@"192.168.0.129" onPort:2811 error:&err]) // Asynchronous! 
    { 
     // If there was an error, it's likely something like "already connected" or "no delegate set" 
     NSLog(@"I goofed: %@", err); 
    } 

    uint8_t buffer[2] = "1\n"; 

    NSData *data = [NSData dataWithBytes: &buffer length: sizeof(buffer)]; 
    [socket writeData:data withTimeout:10 tag:1]; 

我已經包含了太多的依賴構架:安全& CFNetwork的,幷包括在我的課各自的代表。我是否需要其他配置來使用它?

當我運行這個例子中,我得到這個錯誤:

[NSMallocBlock bytes]: unrecognized selector sent to instance 0x6b7abe0 'NSInvalidArgumentException', reason: '-[NSMallocBlock bytes]: unrecognized selector sent to instance 0x6b7abe0'

而且它發生在這條線GCDAsyncSocket.m的

const uint8_t *buffer = (const uint8_t *)[currentWrite->buffer bytes] + currentWrite->bytesDone; 

回答

0

嘗試利用你的數據轉換,從字符串數據

+(NSData *) DataToHex: (NSString *) string 
{ 
    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    int stringLength = string.length; 
    NSMutableData *hexStringArray = [[NSMutableData alloc] init]; 
    [hexStringArray setLength:0]; 
    unsigned char whole_byte; 

    char byte_chars[3] = {'\0','\0','\0'}; 
    int i; 
    for (i=0; i < stringLength/2; i++) 
    { 
     byte_chars[0] = [string characterAtIndex:i*2]; 
     byte_chars[1] = [string characterAtIndex:i*2+1]; 
     whole_byte = strtol(byte_chars, NULL, 16); 
     [hexStringArray appendBytes:&whole_byte length:1]; 
    }                  //this is auto way 

    return hexStringArray; 
} 
相關問題