简体   繁体   English

单是什么? 刻薄

[英]What does the single | mean in swift

I found some example code when building a menu bar item in OS X. It makes use of the single | 我在OS X中构建菜单栏项时发现了一些示例代码。 and I'm unsure what it actually means. 我不确定这到底意味着什么。

(What I'm trying to do is have a function called on left click of the menu item, but have it show the menu on right click) (我想做的是在菜单项的左键单击上调用一个函数,但在右键单击时显示菜单)

Here's my code 这是我的代码

//Get reference to main system status bar
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
statusItem.image = icon
statusItem.menu = menuBar
if let statusButton = statusItem.button
{
    statusButton.target = self
    statusButton.action = #selector(statusItemClicked)

    statusButton.sendActionOn(Int(NSEventMask.RightMouseUpMask.rawValue | NSEventMask.LeftMouseUpMask.rawValue))

}

Original Answer with code Left vs Right Click Status Bar Item Mac Swift 2 带有代码的原始答​​案左与右键单击状态栏项目Mac Swift 2

Bitwise OR, just like it does in most C-like languages. 像大多数类C语言一样,按位或。 In this context, it's being used to combine flags. 在这种情况下,它被用来组合标志。

That must be really old code. 那一定是很老的代码。 Nowadays, in modern Swift, NSEventMask is an Option Set . 如今,在现代Swift中, NSEventMask是一个Option Set You can just say [NSEventMask.rightMouseUp, NSEventMask.leftMouseUp] , and you don't need the Int cast at all. 您可以只说[NSEventMask.rightMouseUp, NSEventMask.leftMouseUp] ,并且根本不需要Int [NSEventMask.rightMouseUp, NSEventMask.leftMouseUp] (Or if you haven't updated to Swift 3 yet, the case names would start with capital letters.) (或者,如果您尚未更新到Swift 3,则案例名称将以大写字母开头。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM