Xcode 命令-有时只有注释才能使用的斜杠快捷方式

当使用快捷键注释或取消注释代码 + /时,Xcode 是相当不可靠的。我似乎不能找到一个真正的模式,当它打破,但它通常发生后,建立和运行我的代码在模拟器上。除非我在任何一个代码文件中编辑某些内容,否则它不会再次工作。即使我切换到主视图中的另一个文件,通过快捷方式注释也不起作用。其他键盘快捷键,如缩进 + [继续工作正常。

其他人有这个问题吗? 如果有,你找到解决办法了吗?我知道这似乎是一个小抱怨,但当运行,然后迅速尝试注释出一块代码来检查效果,这是一个有点不便。

我正在 OXS Yosemite 10.10(14A389)上使用 Xcode 6.1(6A1052d) ,如果有必要的话,我还在用 Objective-C 开发 iOS。

33616 次浏览

Hit +] once and then +/ will start working again.

You can hit +[ to undo the indentation effect.

Update:

Please note- This solution was provided prior to Xcode 8 launch so it may not work for everyone. If you are still facing problem, kindly refer to @CryingHippo's answer instead of downvoting.

For an Xcode 8, solution is

sudo /usr/libexec/xpccachectl

and restart your computer.

None of the above solutions worked for me with Xcode 8.0 (final). If you also have Xcode 8.1 beta installed like I do, try this: simply rename /Applications/Xcode-beta.app to something else, then restart Xcode 8. Oddly enough, that did the trick.

I am using Xcode 8.1 (not beta) and I had the same problem, not able to cmd+\ to comment. Restart Xcode not working for me but RESTART my Mac SOLVED the problem without a need to remove cache.

I had this problem in Xcode 8.0. Even though I already move Xcode 8.1-beta somewhere else (It's at Application/Xcode otherversions/Xcode 8.1-beta.app). I have to delete it to the trash, restart Xcode 8.0, and cmd+/ somehow works again.

I didn't have to do any of the above. A simple reboot fixed it.

Nothing above worked, even after trying multiple times.

Just reinstalled new xcode using AppStore app, previously I installed xcode after downloading from developer.apple.com/downloads. that is .xip file

Wait for new update and install.

Another reason may be that xcode isn't in Application directory.

Just quit xcode completely and restart. Worked for xcode 8.2

I have done this numerous times with Xcode 8. Latest being Xcode 10.1 where I would just quit Xcode and go to Applications and rename Xcode to something else, e.g. Xcode 2, launch it, quit again and rename back. It starts working after that. No Mac restart or Terminal commands required.

It still works as of Xcode 10.1. I cannot believe Apple still hasn't fixed it properly.

A simple workaround works good for me with following steps:

  1. Comment any line i.e. simply add "//" in front of a line.
  2. Press Command + "/" and it starts working.

It might help someone.

In my code I had this sequence:
/*" BLABLA MY COMMENTS */
All the code under this was not "commentable".

Removed the " and it was ok !

Hope it helps!

Oscahie's answer actually works for non-beta versions of Xcode as well. Here are the steps to follow, based on Sam's comment:

  1. Close Xcode
  2. In the Application folder, rename Xcode.app (or similar) to Xcode2.app. You might need to type in your password.
  3. Open Xcode, then close it.
  4. Rename Xcode2.app back to what it was before.
  5. (Optional) Spend 5 minutes wondering why this worked.

No need to reboot!

I was also facing this issue, when having multiple Xcode installed.

In which Xcode version you are you facing this problem, to solve this, the Xcode should be in the Application Folder. If it resides in the subfolder than move it to the Application folder directly.

I just had the same issue after setting up a new Mac with Xcode 10.1 using the German keyboard layout.

The solution was to disable the Keyboard shortcut in system settings for the help menu.

System settings > Keyboard > Shortcuts > App shortcuts.

There was a shortcut for all apps to show the help menu. If you have that, click the checkbox to disable it

Screenshot of Keyboard shortcuts in Mac system preferences

I found a pattern where toggle comments do not work. In fact, uncomment works, but comment don't, from keyboard as well as from the Editor menu. This happens in a specific file, suggesting there is something in text that prevents from working. I tested on XCode 10.1ß and on XCode10.2, on different machines.

Text before this line can be commented, text after cannot (I kept text exactly as is)

let alert = UIAlertController(title: "" /*"Teste texte"*/, message: "Love\nWe'll recommend more for you" /* Entrez des chiffres"*/, preferredStyle: .alert)

It appears that the /* … */ inside the UIAlertController() is the cause. If I suppress both comments inside, everything works OK.

I filed a bug report. # 49907361