views:

100

answers:

1

Hello I have a problem with "CFDataRef. I get the "data" field from a "kCFSocketDataCallBack. "data" should correspond to a string received on the socket. How do I convert, for example, in a NSString so I can put my text in a textbox??

Thank you very much

 static void
 AcceptDataCallback(CFSocketRef s,
 CFSocketCallBackType type, CFDataRef
 address, const void *data, void *info)
 {

 //my code for the textBox

 }
+2  A: 

You could first try converting to NSData by casting it:

NSData * someData = (NSData*)address;

Then convert the NSData to NSString:

NSString * someString = [[NSString alloc] initWithData:someData encoding:NSASCIIStringEncoding];

Or do it all at once:

NSString * someString = [[NSString alloc] initWithData:(NSData*)address encoding:NSASCIIStringEncoding];

You may have to mess around with the encoding.

Tom Irving
And if i have a NSString and i must to save this in the CFDataRef???
zp26
NSString * myString = @"My string"; CFDataRef myDataRef = (CFDataRef)[myString dataUsingEncoding:NSASCIIStringEncoding];
Tom Irving