枚举定义Objective-C中的表单 [英] Enum defining forms in Objective-C

查看:121
本文介绍了枚举定义Objective-C中的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  typedef枚举{
...
}名称之间有什么区别?

 枚举{
...
};
typedef NSUInteger名称;

?如果功能相同,第二种形式是什么?不是不必要的凌乱吗?

解决方案

枚举 C ,因此是 Objective-C 的一部分。
它只是一个 int 类型的显式编码。这对于调试是非常有用的,大多数更新的编译器可以根据它进行优化。 (你应该完全忽略)。最有用的方法是使您的代码更易于阅读(给任何人或睡觉后给自己)。

  typedef枚举{
...
} NameType;

之后是

  NameType name; 

,这通常是typedef的首选样式,



您的第二个例子将将typedef绑定到您要指定的值只能是给定类型。



注意这不妨碍你执行

  name = 10244; //枚举中没有列出的一些非有效值

但是有些编译器可能会在此生成一个警告案例,






我昨天碰到了苹果对以下的使用情况:

 枚举{
NSFetchedResultsChangeInsert = 1,
NSFetchedResultsChangeDelete = 2,
NSFetchedResultsChangeMove = 3,
NSFetchedResultsChangeUpdate = 4
} ;
typedef NSUInteger NSFetchedResultsChangeType;

他们这样做是因为他们真的想要 NSFetchedResultsChangeType 是他们定义为 NSUInteger 的类型。这个可以是 int ,但它也可以是别的东西。而且值为1,2,3和4,这与我们有些无关,是什么类型的。但是,由于它们是一个工具提供程序,因此它们的编码水平不同。



您不应该看看Apple对编码样式提示。如果你看到一些看起来像更干净/更好的代码方式,通常是。正如Kevin所说,API的稳定性对他们至关重要。






编辑(2013年1月)如果您可以访问WWDC 2012会议视频,则应该观看会话405 - 现代Objective-C 6:00-10:00。在新的编译器中有一个新的语法的讨论,允许显式地调整类型的大小并将值紧密地绑定到类型。 (从C ++ 11借来)

 枚举NSFetchedResultsChangeType:NSUInteger {
NSFetchedResultsChangeInsert = 1,
NSFetchedResultsChangeDelete = 2 ,
NSFetchedResultsChangeMove = 3,
NSFetchedResultsChangeUpdate = 4
};


What is the difference between

typedef enum {
    ...
} Name;

and

enum {
    ...
};
typedef NSUInteger Name;

? If functionality is the same, what is the second form good for? Isn't it unnecessarily messy?

解决方案

enum is as old as C, therefore a part of Objective-C. It is just explicit coding of an int type. It's quite useful for debugging and most newer compilers can make optimizations based on it. (Which you should totally ignore). It's most useful in making your code more readable (to anyone else, or to yourself after you've slept).

typedef enum {
    ...
} NameType ;

would be followed by

NameType name;

and that's typically the preferred style of a typedef,

your second example will not tie the typedef to the values you want to specify should only be of the given type.

Note that this does not prevent you from executing

name = 10244; // some non-valid value not listed in the enumeration

but some compilers might generate a warning in that case,


I ran across Apple's use of the following today:

enum {
NSFetchedResultsChangeInsert = 1,
NSFetchedResultsChangeDelete = 2,
NSFetchedResultsChangeMove = 3,
NSFetchedResultsChangeUpdate = 4
};
typedef NSUInteger NSFetchedResultsChangeType;

They do this because they really want the NSFetchedResultsChangeType to be of the type they have defined as NSUInteger. This can be an int but it can also be something else. And with values of 1, 2, 3, and 4, it's somewhat irrelevant to us what the type is. But they are coding to a different level of abstraction because they are a tools provider.

You should never look to Apple for coding style hints. If you see something that looks like it's cleaner/better way to code, it usually is. As Kevin mentioned, API stability is of paramount importance for them.


Edit (Jan 2013) If you have access to the WWDC 2012 Session Videos, you should watch Session 405 - Modern Objective-C 6:00-10:00. There is discussion a new syntax in the newer compiler that allows explicit sizing of a type and tight bonding of values to types. (borrowed from C++ 11)

enum NSFetchedResultsChangeType : NSUInteger {
NSFetchedResultsChangeInsert = 1,
NSFetchedResultsChangeDelete = 2,
NSFetchedResultsChangeMove = 3,
NSFetchedResultsChangeUpdate = 4
};

这篇关于枚举定义Objective-C中的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆