2013-04-29 24 views
3
self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleWidth; 

源代码如上所示。 不知道什么符号“|”代表? 会真的很感谢你的回复,并且谢谢你的回复;)常用符号“|” in Objective-C

回答

12

总之:那是一个bitwise OR的操作。

它最初用于生成位掩码。

使用此操作,您可以将标志组合成二进制数。

例如:UIViewAutoresizing可能的标记有:

enum { 
    UIViewAutoresizingNone     = 0,   // = 0b 0000 0000 = 0 
    UIViewAutoresizingFlexibleLeftMargin = 1 << 0,  // = 0b 0000 0001 = 1 
    UIViewAutoresizingFlexibleWidth  = 1 << 1,  // = 0b 0000 0010 = 2 
    UIViewAutoresizingFlexibleRightMargin = 1 << 2,  // = 0b 0000 0100 = 4 
    UIViewAutoresizingFlexibleTopMargin = 1 << 3,  // = 0b 0000 1000 = 8 
    UIViewAutoresizingFlexibleHeight  = 1 << 4,  // = 0b 0001 0000 = 16 
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5  // = 0b 0010 0000 = 32 
}; 
typedef NSUInteger UIViewAutoresizing; 

声明:

self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleWidth; 

是esentially相同:

self.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

(因为两个操作数都相同) 。

如果你会问:

self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; 

这将设置self.autoresizingMask到:

(1<<1)|(1<<4)=(0b 0000 0010)|(0b 0001 0000)=0b 0001 0010 = 9 

位或不通过与简单真/假代数中使用Logical OR混淆。

这两者之间有一些关系(按位或可以理解为逻辑或位于同一位置上的位),但这是关于它的。

+2

+1很好的答案。 – Popeye 2013-04-29 08:48:53

+0

@Popeye:谢谢! – 2013-04-29 08:49:54

4

The |字符表示包含或按位操作。它在匹配两个对象的位串的前提下运行。

如果你有一个位串1101和另一个1001包含或两个会产生1011.基本上,如果两个串中的当前位是相同的,那么在它的位置输出1,否则为0。