UInt8/NSString with CFWriteStreamWrite
Trying to create an app that does some socket communication (Writing only).
I can get it to work with a static val开发者_开发问答ue:
UInt8 buf[] = "play";
int bytesWritten = CFWriteStreamWrite(writeStream, buf, strlen((char*)buf));
I'm a relative neophyte to C/Objective-C and I'm trying to figure out how to pass an NSString to a function and get the value into "buf".
-(void) sendData: (NSString *) command {
UInt8 buf[] = ????;
int bytesWritten = CFWriteStreamWrite(writeStream, buf, strlen((char*)buf));
}
Obviously more to it than that...just isolating the relevant code.
You can access the bytes behind an NSString using the UTF8String
method.
CFWriteStreamWrite(writeStream,
(const UInt8 *)[command UTF8String],
[command lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
精彩评论