[NSLayoutConstraint constraintsWithVisualFormat:options:metrics:views:] 的选项参数作为 NULL 值传递的内容

What to pass as a NULL value for the options parameter of [NSLayoutConstraint constraintsWithVisualFormat:options:metrics:views:]

我认为很容易回答:如果我不想使用 [NSLayoutConstraint constraintsWithVisualFormat:options:metrics:views:] 的选项参数,应该传递什么传递 nil 确实有效,但编译器抱怨明显错误的类型(void* 而不是 NSLayoutFormatOptions 这是一个 NSUInteger typedef)。当我不知道框架中使用的枚举的值时传递 0 是否安全?

此致!

使用 0。

这是来自 Working with Auto Layout Programmatically 的示例:

NSArray *constraints =
        [NSLayoutConstraint constraintsWithVisualFormat:@"[button1]-[button2]"
                            options:0 metrics:nil views:viewsDictionary];

Apple's documentation可以看出,传0其实和NSLayoutFormatDirectionLeadingToTrailing是一样的:

enum { /* choose only one of these */ NSLayoutFormatAlignAllLeft = NSLayoutAttributeLeft,
NSLayoutFormatAlignAllRight = NSLayoutAttributeRight,
NSLayoutFormatAlignAllTop = NSLayoutAttributeTop,
NSLayoutFormatAlignAllBottom = NSLayoutAttributeBottom,
NSLayoutFormatAlignAllLeading = NSLayoutAttributeLeading,
NSLayoutFormatAlignAllTrailing = NSLayoutAttributeTrailing,
NSLayoutFormatAlignAllCenterX = NSLayoutAttributeCenterX,
NSLayoutFormatAlignAllCenterY = NSLayoutAttributeCenterY,
NSLayoutFormatAlignAllBaseline = NSLayoutAttributeBaseline,

NSLayoutFormatAlignmentMask = 0xFF, /* choose only one of these three */
NSLayoutFormatDirectionLeadingToTrailing = 0 << 8, // default
NSLayoutFormatDirectionLeftToRight = 1 << 8,
NSLayoutFormatDirectionRightToLeft = 2 << 8,
NSLayoutFormatDirectionMask = 0x3 << 8,
};

typedef NSUInteger NSLayoutFormatOptions;

这被认为是默认状态,因此传递 0 是安全的。