`

Enum defining forms in Objective-C

    博客分类:
  • ios
 
阅读更多

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?

===============================================================================

The former defines a type name to refer to an enum. This is the way most enums are named in C. The latter is a bit different though, and it's prevalent in the Cocoa frameworks. There's two reasons to use the latter. The first is if your enum defines a bitfield, and you'd want it here because when you're providing a "Name" value you'll be providing a combination of the enum values. In other words, if you say something like

[self doSomethingWithBitfield:(Enum1 | Enum2)]

you're not passing a value of Name but rather an integer that's a combination of the two.

However, Cocoa frameworks use this idiom even for non-bitfield values, for a very good reason: API stability. According to the C standard, the underlying integral type of an enum is requires to be able to contain all values in the enum, but is otherwise chosen by the compiler. This means that adding a new enum value could change the integral type of the enum (e.g. adding -1 can make it signed, adding 6 billion can make it into a long long, etc). This is a bad thing from an API stability standpoint, because the type encoding of methods which take values of this enum could change unexpectedly and potentially break existing code and binaries. In order to prevent this, the Cocoa frameworks generally define the type as being an NSUInteger (or NSInteger if they need negative numbers), so the API and type encodings stay stable.

转载自:http://stackoverflow.com/questions/7421516/enum-defining-forms-in-objective-c

 

分享到:
评论

相关推荐

    [Objective-C编程(第6版)]Programming in Objective-C

    ### Objective-C编程(第6版) —— Programming in Objective-C #### 书籍概述 《Objective-C编程(第6版)》是由Stephen G. Kochan编写的权威指南,旨在为程序员提供学习Objective-C语言所需的全部基础知识。该书由...

    禅与Objective-C编程艺术

    《禅与Objective-C编程艺术》是一本专注于iOS开发领域中Objective-C编程语言的书籍。Objective-C是苹果公司开发的一款编程语言,主要用于开发macOS、iOS和watchOS应用。本书以其独特的视角,将编程艺术与禅的哲学...

    Objective-C switch 语句

    Objective-C 是苹果公司开发的一种面向对象的编程语言,它基于C语言并添加了类、协议、消息传递等特性。在Objective-C中,`switch`语句是一种条件控制结构,用于根据不同的情况执行不同的代码块。它与C语言中的`...

    Objective-C再学习demo1

    Objective-C是一种强大的面向对象编程语言,主要用于开发Apple的iOS和macOS平台上的应用程序。"Objective-C再学习demo1"是一个项目,旨在复习和巩固Objective-C的基础知识和基本数据类型。这个压缩包可能包含了用于...

    NSHIPSTER:Obscure Topics In Cocoa & Objective-C

    标题“NSHIPSTER: Obscure Topics In Cocoa & Objective-C”指出了本书的内容方向,即深入探讨Cocoa和Objective-C中的不那么明显但重要的主题。Cocoa是苹果公司为其Mac OS X操作系统开发的应用程序框架,而Objective...

    PyPI 官网下载 | cpp-enum-class-string-idl-0.0.1.tar.gz

    标题中的"PyPI 官网下载 | cpp-enum-class-string-idl-0.0.1.tar.gz"表明这是一个从Python Package Index(PyPI)官方源下载的软件包,名为"cpp-enum-class-string-idl",版本号为0.0.1。PyPI是Python社区用来分发和...

    swift-iOSSwiftObjective-C心得

    在iOS应用开发的世界里,Swift、Objective-C是两种主要的编程语言。Swift是由Apple于2014年推出,旨在成为Objective-C的现代化替代品,它吸取了Objective-C的精华,同时引入了许多新特性,提高了代码的可读性和安全...

    python-enum34-1.0.4-1.el7.noarch.rpm

    离线安装包,亲测可用

    objective-c-style-guide-master.zip

    Objective-C是一种面向对象的编程语言,它是Apple的iOS和macOS平台上的主要开发语言。"Objective-C Style Guide"是一份详细的编码规范,旨在提高代码的可读性、可维护性和团队协作效率。这份指南通常包括类命名、...

    Google Objective-C Sytle Guide 中文版

    **谷歌Objective-C编程风格指南中文版** Objective-C是一种强大的面向对象的编程语言,尤其在苹果的iOS和Mac OS X开发中占据主导地位。为了确保代码的一致性、可读性和可维护性,谷歌公司制定了一份详尽的Objective...

    Objective-C和Swift的转换速查手册(推荐)

    Objective-C中定义枚举使用`NS_ENUM`,而在Swift中,枚举更加灵活,可以指定关联的类型并使用`case`来定义各个值。 5. 字符串操作 Objective-C的字符串连接使用`stringByAppendingString:`方法,Swift中则可以通过...

    NSHipster Obscure Topics in Cocoa & Objective C

    《NSHipster Obscure Topics in Cocoa & Objective-C》是Mattt Thompson撰写的一本书,它深入探讨了Cocoa和Objective-C编程中一些不为人知的主题和高级概念。这本书不仅涵盖了Objective-C的基础知识点,还涉及了...

    Python库 | aenum-2.0.1-py2-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:aenum-2.0.1-py2-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    cpp-enum-class-string-idl

    cpp-enum-class-string-idl python3 -m cpp_enum_class_string_idl MyEnums.yaml MyEnums.yaml interfaces : - MyEnum.yaml - MyOtherEnum.yaml MyEnum.yaml name : MyEnum type : int values : - Value0 -...

    Swift和OC代码的相互使用

    在iOS和macOS开发中,Apple提供了两种主要的编程语言:Objective-C(简称OC)和Swift。自从2014年Swift发布以来,它迅速获得了开发者们的青睐,但Objective-C作为苹果生态系统的老牌语言,仍然在很多现有项目中占据...

    rust-demo-enum-struct-mod-pub-super-crate-if-match-20210518.txt

    关于enum、mod、struct、match、方法、函数相关的demo 源码已经在如下博客中开源: https://blog.csdn.net/yujianliam/article/details/116992163

    10道常用的Swift面试题(附答案).md

    1、Swift和Objective-C有什么区别? 1)Swift是强类型(静态)语言,有类型推断,Objective-C弱类型(动态)语言 2)Swift面向协议编程,Objective-C面向对象编程 3)Swift注重值类型,Objective-C注重引用类型 4)...

    Python库 | enum34-1.1.3-py2.py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:enum34-1.1.3-py2.py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Student-Partner-Generator.zip_Objective-C_

    1. **类与对象**:Objective-C是面向对象的编程语言,它以C语言为基础,引入了Smalltalk的类和消息传递概念。在这个项目中,`Student Partner Generator.c`可能定义了一个或多个类,如`Student`和`Classroom`,用于...

Global site tag (gtag.js) - Google Analytics