多值参数

时间:2011-06-02 07:42:44

标签: iphone objective-c parameter-passing

有人可以向我解释如何将多个值传递给objective-c中的参数或变量,如下所示,以及如何在方法内处理它:

view.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin 
                      | UIViewAutoresizingFlexibleTopMargin;

1 个答案:

答案 0 :(得分:2)

你所看到的是整数之间的简单分离,UIView自动调整掩码参数只是typedef'ed枚举值。您可以自己创建:

typedef enum {
    IceCreamChocolateSyrup = 1 << 1,
    IceCreamCaramelSyrup = 1 << 2,
    IceCreamMapleSyrup = 1 << 3,
    // etc. up to 31 flavors
} IceCreamSyrups;

然后定义一个接受它们作为参数的方法:

- (void)addIceCreamSyrups:(IceCreamSyrups)syrups {
    if (syrups & IceCreamChocolateSyrup)
        [self addChocolateSyrup];

    if (syrups & IceCreamCaramelSyrup)
        [self addCaramelSyrup];

    if (syrups & IceCreamMapleSyrup)
        [self addMapleSyrup];
}

并按如下方式调用此方法:

[self addIceCreamSyrups:(IceCreamChocolateSyrup | IceCreamMapleSyrup)];