哪个方法名称最适合 Objective-C/Cocoa 约定?

发布于 2024-07-08 01:48:29 字数 284 浏览 8 评论 0原文

这是一个快速问题。 哪个方法名称对于 Objective-C Cocoa 应用程序最有意义?

-(void) doSomethingWithAnimation:(BOOL)animated

或:

-(void) doSomething:(BOOL)animated

甚至:

-(void) doSomethingAnimated:(BOOL)animated

Here's a quicky question. Which method name makes the most sense for an Objective-C Cocoa application?

-(void) doSomethingWithAnimation:(BOOL)animated

or:

-(void) doSomething:(BOOL)animated

or even:

-(void) doSomethingAnimated:(BOOL)animated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

护你周全 2024-07-15 01:48:29

我认为 Cocoa 约定将为您的示例提供以下语义(显然,忽略参数的 BOOL 类型):

-(void) doSomethingWithAnimation:(BOOL)animated

实际上会期望一个动画作为参数(即代表动画的东西。

-(void) doSomething:(BOOL)animated

会期望做一些事情。

-(void) doSomethingAnimated:(BOOL)animated

会,如诺亚回答说,用可选的动画做一些事情。

I think the Cocoa convention would give your examples the following semmantics (ignoring the BOOL type for the argument, obviously):

-(void) doSomethingWithAnimation:(BOOL)animated

would actually expect an Animation as the parameter (i.e. something that represents the animation.

-(void) doSomething:(BOOL)animated

would expect the Something to do.

-(void) doSomethingAnimated:(BOOL)animated

would, as Noah answered, do something with optional animation.

心在旅行 2024-07-15 01:48:29

-(void)doSomethingAnimated:(BOOL)animated 似乎最符合苹果的命名风格。 作为参考,请检查 iPhone UIKit 文档 - 例如 UINavigationController 的 -popToRootViewControllerAnimated: 方法。

-(void)doSomethingAnimated:(BOOL)animated seems most consistent with Apple's naming style. For reference, check the iPhone UIKit docs - UINavigationController's -popToRootViewControllerAnimated: method, for instance.

铃予 2024-07-15 01:48:29

这里还有另一个需要考虑的选项:创建两个方法,-doSomething-doSomethingWithAnimation

然后,如果您愿意,您可以让它们都尾部调用第三个私有方法,并为该方法指定您想要的任何名称。 :)

Here's another option to consider: make two methods, -doSomething and -doSomethingWithAnimation.

Then, if you want, you can have them both tail-call a third, private method, and give that method any name you want. :)

寄意 2024-07-15 01:48:29

清晰简洁地写下您的评论,例如:
//do Something with the Animation

然后根据这个注释写下你的方法名称,如下所示
doSomethingwithAnimation:(BOOL)动画

Objective-C 和 Cocoa 的设计是为了便于阅读,所以如果你的方法不能清晰地叙述你的代码,它可能没有被很好地命名。

write your comment clearly and tersely for example :
//do Something with the Animation

Then write your method name based on this comment, like below
doSomethingwithAnimation:(BOOL)animated

Objective-C and Cocoa are designed to read well, so if your method cannot be a clear narrative of your code, it might not be well named.

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