objective-c - C99 指定初始化程序或 CGMake 宏?

标签 objective-c c99

C99 指定的初始化器或各种 CGSizeMake、CZRectMake 等宏作为现代 Objective-C 中的约定更可取吗?

这似乎是一种个人风格偏好,但我看到 C99 风格的一个优势是值(value)观的意图清晰明确。

CGRect rect = CGRectMake(x, y, width, height) 

如果你混淆值的顺序会让你心痛,例如:

CGRect rect = (CGRect){.origin.x = x, .origin.y = y, .size.width = width, .size.height = height};

毫无疑问,.height 正在获得您赋予它的值(value)。人们继续使用现有宏的原因是什么?

Cocoa 编码公约文档中没有关于此的任何内容,我遇到的其中一个样式指南指出它是 GitHub 样式指南:https://github.com/github/objective-c-conventions

最佳答案

来自 Apple's CGGeometry reference :

All functions described in this reference that take CGRect data structures as inputs implicitly standardize those rectangles before calculating their results. For this reason, your applications should avoid directly reading and writing the data stored in the CGRect data structure. Instead, use the functions described here to manipulate rectangles and to retrieve their characteristics.

因此,这些 CGRect 函数(例如 CGRectGetWidth)将确保高度和宽度始终为非负值。 (CGRect 函数“将 CGRect 数据结构作为输入”,如 CGRectMake,不标准化矩形的尺寸。)

此外,现有函数可以在数据结构发生变化的情况下(诚然极不可能)发生变化。

最后,尽管您提到了 Github 风格指南,New York Times Objective-C Style Guide建议使用 CGRect 函数(出于我上面提到的相同原因)。

虽然在从 CGRect 获取值时可以使用内联函数,并在创建它们时直接访问结构成员,但这种不一致可能会损害代码的可读性。显然,正如您提到的,这是一个风格问题。

基本上,没关系,但使用功能。

关于objective-c - C99 指定初始化程序或 CGMake 宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21541960/

相关文章:

C 程序无法正常工作(集合、数组、循环)

ios - UILabel 字体大小大于标签高度

objective-c - 用于 Objective C 的免费 UML 图工具

c - 数组初始化会导致内存泄漏吗?

c - 更好地理解 c 中可变参数的类型提升

c - 带有 -std=c99 的 GCC 提示不知道 struct timespec

objective-c - UIImage 与 NSImage : Drawing to an off screen image in iOS

ios - 当 WKWebView 尝试呈现 WKActionSheet 时,长按会导致错误/警告

objective-c - 当返回值是不可变对象(immutable对象)时返回可变对象是不好的做法吗?

c - 为什么memset在函数上的工作方式不同?