Hi all
I'm new to Objective-C and Cocoa and I am having trouble with the notifications
system. This I thought was just a small project to write a native mac program
to continuously read and write data from a serial port to a file. It was
suggested I use NSFileHandle to maintain safe threads, as it can read the
serial port asynchronously; I had hoped this program would read the text file,
and as I changed it, there would be an output to the console. Something simple
to see the notification system in action:
//CODE
#import <Cocoa/Cocoa.h>
#import <Foundation/NSFileHandle.h>
#import <Foundation/NSString.h>
#import <Foundation/NSNotificationCenter.h>
#import <stdio.h>
@interface test : NSObject
-(void) writeDataReadInBackground:(NSNotification *)notification;
-(void) testWriteData:(NSData *)data;
@end
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData *aData = [@"Test" dataUsingEncoding: NSASCIIStringEncoding];
NSFileHandle *in = [NSFileHandle
fileHandleForReadingAtPath:@"/Users/_me_/test.txt"];
if(in == nil)
printf("somethig wrong\n");
test *t = [[test alloc] init];
[[NSNotificationCenter defaultCenter] addObserver: t
selector:@selector(writeDataReadInBackground:) name:
NSFileHandleDataAvailableNotification object: nil];
[t testWriteData: aData];
[in waitForDataInBackgroundAndNotify];
while(1)
sleep(5);
[t release];
[pool release];
return 0;
}
@implementation test
- (void) writeDataReadInBackground:(NSNotification *)notification {
printf("Notification recieved\n");
}
- (void) testWriteData:(NSData *)data {
NSFileHandle *stdOut = [NSFileHandle fileHandleWithStandardOutput];
[stdOut writeData:data];
}
@end
//END_CODE
Thanks for your help
Nathan
PS What's the proper way to work between NSString and NSData?
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [EMAIL PROTECTED]