views:

65

answers:

2

I made the transition from C++ to objective-C a while ago, and am now finding NSLog() tiresome. Instead, still in Objective-C, I would like to be able to write something like stdout << "The answer is " << 42 << "\n"; (I know that NSLog prints to stderr, I could put up with writing stderr << "Hello world";)

Basically, I just want to be able to use the C++ pipe syntax in Objective-C.

I don't care about speed (within reason) or if the only method uses precompiler macros or other hack-ish things.

A: 

What you're wanting is stream operations.

There isn't a really 'good' way to do this in Cocoa, I have a library that I never really fleshed out that would allow you to do something 'near' this, but still wouldn't get a lot of the benifits.

http://github.com/jweinberg/Objective-Curry/blob/master/OCFileStream.m

Starting from there you would be able to write a class that did

[[[stdOutStream write:@"10"] write:[bleh description]] write:@"more stuff"];

Joshua Weinberg
What I really want to do is be able to write stdOutStream << @"10" << [bleh description] << @"more stuff";
cool_me5000
Right....you can't.
Joshua Weinberg
Not even with preprocessor macros? something like #define << something that will work?
cool_me5000
+3  A: 

You really should get used to format strings as in NSLog. The C++ style syntax may be easy to write, but it is a nightmare to maintain. Think about internationalization. A format string can easily be loaded at runtime. Cocoa provides the function NSLocalizedString for that. But for C++’s stream operators you probably have to write different code for every language.

Sven