开发者

C++ pipes in Objective-C

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 hac开发者_JAVA百科k-ish things.


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.


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"];

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜