如何在代码中命名iOS相关元素?

发布于 2024-10-26 05:12:53 字数 353 浏览 2 评论 0 原文

我最近做了很多 iPhone 开发,但我遇到了一个命名问题,我想不出一个好的解决方案。问题是有时我必须在变量、命名空间或类名中引用“iOS”。我该怎么做呢?假设我有一个专为 iOS 设计的“MyClass”版本。我应该将其命名为:

iOSMyClass吗?

这很糟糕。类名应该以大写字母开头。

IOS我的班级?

这很糟糕。现在我的类看起来像一个界面。

苹果我的班级?

这更好,但如果我为 Mac 创建该类的版本会怎么样?

AppleMobileMyClass?

这更好了,但它开始变得相当冗长。

有什么想法吗?我正在使用 C# 开发桌面软件。

I've been doing a lot of iPhone development lately, and I have a naming issue for which I can't think of a good solution. The problem is that somethimes I have to refer to "iOS" in variable, namespace, or class names. How should I do it? Suppose I have a version of "MyClass" that is designed for iOS. Should I call it:

iOSMyClass?

This is bad. Class names are supposed to start with a capital letter.

IOSMyClass?

This is bad. Now my class looks like an interface.

AppleMyClass?

This is better, but what if I create a version of the class for Macs?

AppleMobileMyClass?

This is better, but it's starting to get pretty verbose.

Any thoughts? I'm developing desktop software using C#.

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

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

发布评论

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

评论(2

夏夜暖风 2024-11-02 05:12:53

建议选择第二个选择,但稍作修改:IosMyClass。考虑采用 Pascal 大小写的 3 个字母首字母缩略词约定(即 System.Web.Mvc)。当然,Apple 在 iOS 的实现中扭转了这一局面。然而,听起来好像这是针对 .NET 空间的,并且希望遵循其约定。

还有一些其他类(甚至在 .NET Framework 中),以字母 I 开头。诚然,这并不理想,因为它非常接近使用 I 为接口添加前缀的约定。但是,接口以两个大写字母 (I[AZ]) 开头。这将它们标识为一个接口。因此,按照惯例,IosMyClass 不是一个接口,但 IIosFoo 却是。

Suggest going with your second choice, but a slight mod: IosMyClass. Consider the convention of 3 letter acronyms being Pascal cased (i.e. MVC in System.Web.Mvc). Of course, Apple flips that around in its implemention with iOS. However it sounds as if this is for the .NET space, and the desire is to follow its conventions.

There are handfuls of other classes (in the .NET Framework even), that start with the letter I. Admittedly, it's not ideal, as it's so close to the convention of prefixing interfaces with I. However, interfaces start with two upper case letters (I[A-Z]). That identifies them as an interface. So IosMyClass, by convention, is not an interface, but IIosFoo would be.

情释 2024-11-02 05:12:53

我知道您在这里指定 C# 作为您的语言,但我认为我应该指出标准的 Objective-C 命名约定,您在查看其他 iOS 代码时会看到这些约定。

Objective-C 缺乏名称空间的概念,因此它是 通常建议在类名前添加 2-3 个大写字母作为前缀,表示您的公司或框架,以避免命名冲突。 Apple 最近一直推荐使用 3 个字符,因为像 Core Plot 这样使用 2 个字母的框架已经开始与 Apple 框架中的私有类(如本例中的 CPImage)发生冲突。有关命名指南的另一种看法,请参阅此处

因此,我建议不要使用通用的 iOS 前缀,因为 Apple 可能会在内部使用该前缀,并导致您的应用程序崩溃。诚然,我不知道 MonoTouch C# 类如何与 Cocoa 框架类交互,但我希望避免出现此类问题。

再说一次,如果我可以指出 Core Plot 框架,该框架内有一些特定于 Mac 的代码和一些特定于 iOS 的代码,但并未针对每个平台使用单独的类。特定于平台的方法从主类文件中提取并放置在类别中。这些类别具有相同的名称,但 Mac 目标使用一个文件进行编译,而 iOS 目标使用另一个文件。同样,特定于每个平台的整个类都被赋予相同的名称和接口,但放置在不同的文件中。编译时使用哪个文件由平台目标决定。

因此,我建议不要给 iOS 特定的类一个唯一的名称,除非它们在您的其他目标平台上没有类似的名称。

I know you specify C# as your language here, but I thought I should point out the standard Objective-C naming conventions, which you'll see as you look at other iOS code.

Objective-C lacks the concept of namespaces, so it is generally recommended that you prefix your class names with 2-3 capital letters that signify your company or framework to avoid naming collisions. Apple has been recommending 3 characters lately, because frameworks like Core Plot that use 2 letters have started having collisions with private classes in Apple's frameworks (like CPImage, in this case). For another take on the naming guidelines, see here.

For that reason, I would recommend against the use of a generic iOS prefix, because Apple might use that internally for something and cause your application to break. Admittedly, I don't know how MonoTouch C# classes interact with the Cocoa framework classes, but I'd want to avoid any chance of a problem like this.

Again, if I may point to the Core Plot framework, there is some code within that framework that is specific to Mac and some to iOS, yet separate classes are not used for each platform. Platform-specific methods are extracted from the main class files and placed in categories. These categories have the same name, but the Mac target compiles using one file and the iOS target uses another. Similarly, entire classes that are specific to each platform are given the same name and interface, but put in different files. Which file is used at compilation is determined by the platform target.

Therefore, I'd recommend against giving iOS-specific classes a unique name unless they have no analogue on your other target platforms.

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