NS_ENUM & NS_OPTIONS

时间:2022-09-02 12:27:18

When everything is an object, nothing is.

So, there are a few ways you could parse that, but for the purposes of this article, this is all to say: sometimes it's nice to be able to drop down to the C layer of things.

Yes--that non-objective part of our favorite Smalltalk-inspired hybrid language, C can be a great asset. It's fast, it's battle-tested, it's the very foundation of modern computing. But more than that, C is the escape hatch for when the Object-Oriented paradigm cracks under its own cognitive weight.

Static functions are nicer than shoe-horned class methods.
Enums are nicer than string constants.
Bitmasks are nicer than arrays of string constants.
Preprocessor directives are nicer than runtime hacks.

A skilled Objective-C developer is able to gracefully switch between Objective and Procedural paradigms, and use each to their own advantage.

And on that note, this week's topic has to do with two simple-but-handy macros: NS_ENUM andNS_OPTIONS.


Introduced in Foundation with iOS 6 / Mac OS X 10.8, the NS_ENUM and NS_OPTIONS macros are the new, preferred way to declare enum types.

If you'd like to use either macro when targeting a previous version of iOS or OS X, you can simply inline like so:

#ifndef NS_ENUM
#define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
#endif

enum, or enumerated value types, are the C way to define constants for fixed values, like days of the week, or available styles of table view cells. In an enum declaration, constants without explicit values will automatically be assigned values sequentially, starting from 0.

There are several legal ways that enums can be defined. What's confusing is that there are subtle functional differences between each approach, and without knowing any better, someone is just as likely to use them interchangeably.

For instance:

enum {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
};

...declares integer values, but no type.

Whereas:

typedef enum {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
} UITableViewCellStyle;

...defines the UITableViewCellStyle type, suitable for specifying the type of method parameters.

However, Apple had previously defined all of their enum types as:

typedef enum {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
}; typedef NSInteger UITableViewCellStyle;

...which ensures a fixed size for UITableViewCellStyle, but does nothing to hint the relation between the aforementioned enum and the new type to the compiler.

Thankfully, Apple has decided on "One Macro To Rule Them All" with NS_ENUM.

NS_ENUM

Now, UITableViewCellStyle is declared with:

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
};

The first argument for NS_ENUM is the type used to store the new type. In a 64-bit environment,UITableViewCellStyle will be 8 bytes long--same as NSInteger. Make sure that the specified size can fit all of the defined values, or else an error will be generated. The second argument is the name of the new type (as you probably guessed). Inside the block, the values are defined as usual.

This approach combines the best of all of the aforementioned approaches, and even provides hints to the compiler for type-checking and switch statement completeness.

NS_OPTIONS

enum can also be used to define a bitmask. Using a convenient property of binary math, a single integer value can encode a combination of values all at once using the bitwise OR (|), and decoded with bitwise AND (&). Each subsequent value, rather than automatically being incremented by 1 from 0, are manually given a value with a bit offset: 1 << 01 << 11 << 2, and so on. If you imagine the binary representation of a number, like 10110 for 22, each bit can be though to represent a single boolean. In UIKit, for example, UIViewAutoresizing is a bitmask that can represent any combination of flexible top, bottom, left, and right margins, or width and height.

Rather than NS_ENUM, bitmasks should now use the NS_OPTIONS macro.

The syntax is exactly the same as NS_ENUM, but this macro alerts the compiler to how values can be combined with bitmask |. Again, you must be careful that all of the enumerated values fit within the specified type.


NS_ENUM and NS_OPTIONS are handy additions to the Objective-C development experience, and reaffirm the healthy dialectic between its objective and procedural nature. Keep this in mind as you move forward in your own journey to understand the logical tensions that underpin everything around us.

NS_ENUM & NS_OPTIONS的更多相关文章

  1. iOS&colon; 枚举类型 enum&comma;NS&lowbar;ENUM&comma;NS&lowbar;OPTIONS

    一般情况下,我们采用C风格的enum关键字可以定义枚举类型. enum{ UIViewAnimationTransitionNone, UIViewAnimationTransitionFlipFro ...

  2. iOS&sol;object-c&colon; 枚举类型 enum&comma;NS&lowbar;ENUM&comma;NS&lowbar;OPTIONS

    一般情况下,我们采用C风格的enum关键字可以定义枚举类型. enum{ UIViewAnimationTransitionNone, UIViewAnimationTransitionFlipFro ...

  3. iOS中的枚举&colon;enum&comma; NS&lowbar;ENUM&comma; NS&lowbar;OPTIONS的使用区别

    1.enum可以声明一般类型和位掩码(bitmasked)类型 例如: enum Test{// 一般枚举 TestA, TestB, TestC, }; enum{// 匿名枚举 TestA, Te ...

  4. Object-C&colon; 枚举

    摘自:http://coffeeandsandwich.com/?p=8 在 iOS 6 和 Mac OS X 10.8 以前,定义枚举类型的方式如下: typedef enum the_enum_n ...

  5. SDWebImage之SDWebImageCompat

    SDWebImageCompat 是SDWebImage 的配置文件,里面利用条件编译对Apple 的各个平台进行了兼容.从源码中可以看到SDWebImage 支持当前的MAC/iOS/TV/WATC ...

  6. &lt&semi;笔记&gt&semi;Effective Objective-C 2&period;0 编写高质量iOS与

    1. 内存管理-引用计数 2. 非对象类型  int float double char 3.运行时--编译器(编译时)函数调用 4.@class  缩短编译时间,降低依赖,耦合 5.使用字面量而不是 ...

  7. iOS&period;C

    iOS开发中C语言的应用: 1. NS_ENUM & NS_OPTIONS http://nshipster.com/ns_enum-ns_options/

  8. clang 编译 OC

    clang -fobjc-arc -framework Foundation helloworld.m -o helloworld.out OVERVIEW: clang LLVM compiler ...

  9. YYModel源代码阅读--基础知识

    这段时间因为工作需要,阅读了YYModel这个开源框架,至于它能做什么,最直白的讲述就是JSON与Model之间的相互转化. 源代码在Github,大家可以自行git clone或者download. ...

随机推荐

  1. 七天学会ASP&period;NET MVC &lpar;四&rpar;——用户授权认证问题

    小编应各位的要求,快马加鞭,马不停蹄的终于:七天学会 Asp.Net MVC 第四篇出炉,在第四天的学习中,我们主要了学习如何在MVC中如何实现认证授权等问题,本节主要讲了验证错误时的错误值,客户端验 ...

  2. Android总结篇系列:Android开发环境搭建

    工欲善其事必先利其器. 1.安装并配置Java环境进入Java oracle官网,当前网址如下:http://www.oracle.com/technetwork/java/javase/downlo ...

  3. Python逐块读取大文件行数的代码 - 为程序员服务

    Python逐块读取大文件行数的代码 - 为程序员服务 python数文件行数最简单的方法是使用enumerate方法,但是如果文件很大的话,这个方法就有点慢了,我们可以逐块的读取文件的内容,然后按块 ...

  4. 杭电1233还是畅通project

    还是畅通project Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  5. shiro授权

    一.shiro-permission.ini shiro-permission.ini里面的内容相当于在数据库 #用户 [users] #用户zhang的密码是123,此用户具有role1和role2 ...

  6. 【easy】101&period; Symmetric Tree

    判断一棵二叉树是否对称 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left ...

  7. Centos Linux 下Pycharm 安装

    原文链接 :https://www.cnblogs.com/shaosks/p/9173806.html 可以通过浏览器访问http://www.jetbrains.com/pycharm/,选择需要 ...

  8. 如何在MacBook的以太网端口上成功运行DHCP服务器?

    我的目标是在我的MacBook以太网端口上安装一个以太网交换机,我将通过DHCP连接几个Raspberry Pi连接,每个都将运行VNC服务器进行远程访问,我希望我的互联网可以从我的MacBook的W ...

  9. u-boot启动第二阶段以及界面命令分析

    u-boot第一阶段完成了一些平台相关的硬件的配置,第一阶段所做的事情也是为第二阶段的准备,我们知道在第一阶段最后时搭建好C运行环境,之后调用了start_armboot(),那么很显然第二阶段从st ...

  10. HDU 5361 In Touch (2015 多校6 1009 最短路 &plus; 区间更新)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:最短路.求源点到全部点的最短距离.但与普通最短路不同的是,给出的边是某点到区间[l,r]内随意 ...