Working with Notifications and NSFileHandle
Working with Notifications and NSFileHandle
- Subject: Working with Notifications and NSFileHandle
- From: "Wan, Nathan (CIV)" <email@hidden>
- Date: Fri, 27 Jun 2008 13:32:33 -0700
- Thread-topic: Working with Notifications and NSFileHandle
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@hidden)
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:
This email sent to email@hidden