C++ Objective-C 中的管道

发布于 2024-09-19 23:40:58 字数 281 浏览 4 评论 0原文

我不久前从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗心人 2024-09-26 23:41:03

您想要的是流操作。

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

淡忘如思 2024-09-26 23:41:01

您确实应该习惯像 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 function NSLocalizedString for that. But for C++’s stream operators you probably have to write different code for every language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文