C++ Objective-C 中的管道
我不久前从 C++ 过渡到 Objective-C,现在发现 NSLog() 很烦人。相反,仍然在 Objective-C 中,我希望能够编写类似的东西 标准输出<< “答案是”<<第42话“\n”; (我知道 NSLog 打印到 stderr,我可以忍受编写 stderr << "Hello world";)
基本上,我只是希望能够在 Objective-C 中使用 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 hack-ish things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要的是流操作。
在 Cocoa 中没有一个真正“好”的方法来做到这一点,我有一个我从未真正充实过的库,它可以让你做一些“接近”的事情,但仍然不会得到很多好处。
http://github.com/jweinberg/Objective-Curry/blob/ master/OCFileStream.m
从那里开始,您将能够编写一个执行
[[[stdOutStream write:@"10"] write:[bleh description]] write:@"more stuff" 的 类];
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"];
您确实应该习惯像
NSLog
中那样格式化字符串。 C++ 风格的语法可能很容易编写,但维护起来却是一场噩梦。考虑国际化。格式字符串可以在运行时轻松加载。 Cocoa为此提供了函数NSLocalizedString
。但对于 C++ 的流运算符,您可能必须为每种语言编写不同的代码。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 functionNSLocalizedString
for that. But for C++’s stream operators you probably have to write different code for every language.