A handy console output category .
A handy console output category .
- Subject: A handy console output category .
- From: "John C. Randolph" <email@hidden>
- Date: Tue, 15 Nov 2011 00:24:43 -0800
Hi All,
Recently, I was writing a command-line tool and I didn't feel like dropping down to printf() for console output, so I wrote this category.
Drop this in your project…
> @interface NSString (console)
>
> - (void) writeToFileHandle:(NSFileHandle *) handle;
> - (void) writeToStdOut;
> - (void) writeToStdErr;
>
> @end
>
> @implementation NSString (console)
>
> - (void) writeToFileHandle:(NSFileHandle *) handle { [handle writeData:[self dataUsingEncoding:NSUTF8StringEncoding]]; }
> - (void) writeToStdOut { [self writeToFileHandle:[NSFileHandle fileHandleWithStandardOutput]]; }
> - (void) writeToStdErr { [self writeToFileHandle:[NSFileHandle fileHandleWithStandardError]]; }
>
> @end
…and you'll be able to tell strings to output themselves like so:
[@"Hello, World!\n" writeToStdOut];
[@"Oh, no! Something broke!\n" writeToStdErr];
[[self description] writeToStdOut];
-jcr_______________________________________________
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