0

this is my code in ViewController.h in CoCoa to implement "CFsocket"

@interface ViewController : NSViewController
-(IBAction)start:(id)sender;
@property (strong, nonatomic) IBOutlet NSTextView  *CommandDisplay;

this is ViewController.m

@implementation ViewController
@synthesize CommandDisplay=_CommandDisplay;

void AcceptCallBack(CFSocketRef socket,CFSocketCallBackType type,CFDataRef address,const void *data,void *info)
{
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
// For a kCFSocketConnectCallBack that failed in the background, it is a pointer to an SInt32 error code; for a kCFSocketAcceptCallBack, it is a pointer to a CFSocketNativeHandle; or for a kCFSocketDataCallBack, it is a CFData object containing the incoming data. In all other cases, it is NULL.
CFSocketNativeHandle sock = *(CFSocketNativeHandle *) data;
CFStreamCreatePairWithSocket(kCFAllocatorDefault, sock, &readStream, &writeStream);


if (!readStream || !writeStream)
{
    close(sock);
    NSLog(@"CFStreamCreatePairWithSocket()Fail");
    return;
}

CFStreamClientContext streamCtxt = {0, NULL, NULL, NULL, NULL};
CFReadStreamSetClient(readStream, kCFStreamEventHasBytesAvailable, ReadStreamClientCallBack, &streamCtxt);
CFWriteStreamSetClient(writeStream, kCFStreamEventCanAcceptBytes, WriteStreamClientCallBack, &streamCtxt);

CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(),kCFRunLoopCommonModes);
CFWriteStreamScheduleWithRunLoop(writeStream, CFRunLoopGetCurrent(),kCFRunLoopCommonModes);

CFReadStreamOpen(readStream);
CFWriteStreamOpen(writeStream);
}

// readstream operatoion , use when client transmitted data
 void ReadStreamClientCallBack(CFReadStreamRef stream, CFStreamEventType eventType, void* clientCallBackInfo)
{
UInt8 buff[255];
CFReadStreamRef inputStream = stream;
CFReadStreamRead(stream, buff, 255);

_CommandDisplay.string=[self._CommandDisplay.string stringByAppendingString:[NSString stringWithFormat:@"SeverCreat failed\n"]];


NSLog(@"receive: %s",buff);
NSLog(@"%@",clientCallBackInfo);
CFReadStreamClose(inputStream);
CFReadStreamUnscheduleFromRunLoop(inputStream,CFRunLoopGetCurrent(),kCFRunLoopCommonModes);
inputStream = NULL;
 }

when i use c function , it can't recognize _CommandDisplay which i have synthesize ,but i need to print read data to NSTextView,how can i solve this problem?

林子勤
  • 29
  • 6

1 Answers1

0

In Objective-C a synthesized property foo is backed by an implicit instance variable _foo.

  • If you want to access the instance variable directly use _foo without self.

  • If you want to access the property by its synthesized getter and setter use self.foo (without the underscore)

Write

self.CommandDisplay.string = [self.CommandDisplay.string stringByAppendingString:@"SeverCreat failed\n"];

or

_CommandDisplay.string = [_CommandDisplay.string stringByAppendingString:@"SeverCreat failed\n"];

NSString stringWithFormat is not needed, there are no format parameters, and you can also delete the @synthesize line, it's not needed either.

A small side-note:

If the C-function was outside the scope of the implementation block, you would have to pass the reference to the NSTextView instance thru the info parameter of the function, but in this case it should work

vadian
  • 274,689
  • 30
  • 353
  • 361