Xcode 注释取消注释问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13400879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:06:29  来源:igfitidea点击:

Xcode comment uncomment issue

iosxcode

提问by anonim

I'm new to Xcode and found something frustrating. I select a few lines of code and comment them out. for ex.

我是 Xcode 的新手,发现了一些令人沮丧的事情。我选择了几行代码并将它们注释掉。例如。

//      NSString* u = __txtUsername.text;
//      NSString* p = __txtPassword.text; 

then I may re-indent the code and the commented code goes like below

然后我可以重新缩进代码,注释代码如下所示

        //      NSString* u = __txtUsername.text;
        //      NSString* p = __txtPassword.text;

now if I try to uncomment the commented code Xcode produces something like below

现在,如果我尝试取消注释已注释的代码,Xcode 会生成如下所示的内容

//     //      NSString* u = __txtUsername.text;
//     //      NSString* p = __txtPassword.text;

in fact instead of removing //it adds more //at the beginning and removing //from commented re-indented code is really frustrating.

事实上,不是删除//它,而是//在开始时添加更多,并且//从注释重新缩进的代码中删除真的很令人沮丧。

Is there any solution to that or I've made something ridiculously?

有什么解决办法吗,或者我做了一些可笑的事情?

回答by Senior

You're correct, Xcode is stupid. If you get in this situation, keep hitting cmd-[ to bring the text all the way to the start of the line, then uncomment and it should work. Why Xcode doesn't just remove the first instance of // on a line is beyond me.

你是对的,Xcode 是愚蠢的。如果您遇到这种情况,请继续按 cmd-[ 将文本一直带到行首,然后取消注释,它应该可以工作。为什么 Xcode 不只是删除一行中 // 的第一个实例,这超出了我的理解。

回答by matrix3003

Editor->Structure->Uncomment Selection

编辑器->结构->取消注释选择

the option won't show up if lines WITHOUT COMMENTSincluded into selection

如果选择中包含没有注释的行,则不会显示该选项

comments must appear AT THE FIRST POSITIONto be considered as such

评论必须出现在第一个位置才能被视为这样

回答by user3226094

So long as you ONLY highlight the exact lines that are commented out, the "uncomment" function should work just fine. If you highlight the commented code beyond the confines of the uncommented code, then it treats it as if you're "adding to" the already-commented out code. Which, as you know re-indents and re-comments out the already-commented out code, if that makes sense. There shouldn't be any need to mess with indentation... Xcode should put everything right back in the correct place.

只要您只突出显示被注释掉的确切行,“取消注释”功能就可以正常工作。如果您突出显示超出未注释代码范围的注释代码,那么它会将其视为“添加到”已注释掉的代码。如您所知,如果有意义的话,可以重新缩进并重新注释掉已经注释掉的代码。没有必要弄乱缩进...... Xcode 应该把所有东西都放回正确的位置。

I like the "/*... */" concept from woz though. I'd like it more if there was a keyboard shortcut that would make that method a little faster. Quickly highlighting anywhere w/in the row, then pressing the "cmd /" keystroke seems a little less precise and faster for me.

不过我喜欢 woz 的“/*... */”概念。如果有一个键盘快捷键可以使该方法更快一点,我会更喜欢它。快速突出显示行中的任何地方,然后按“cmd /”键击对我来说似乎不太精确和更快。

Not sure if this has been fixed on Xcode since this posting, but thought I'd comment on it.

不确定自发布以来这是否已在 Xcode 上修复,但我想我会对此发表评论。

Good luck all.

祝大家好运。

回答by woz

Just leave the //all the way to the left, or the uncomment feature indeed will not work.

只需//一直向左移动,否则取消注释功能确实不起作用。

If you need multiline comment, your best bet might be to use this syntax:

如果您需要多行注释,最好的办法可能是使用以下语法:

/* 
    NSString* u = __txtUsername.text;
    NSString* p = __txtPassword.text;
*/

Then you only have to delete /*and */to uncomment the block of code.

然后你只需要删除/**/取消注释代码块。

回答by trojanfoe

If I have to comment-out a non-trivial amount of code I use:

如果我必须注释掉我使用的大量代码:

#if 0

code
code
code

#endif // 0

If it's a trivial amount of code I do it manually. This is hardly manual labour compared to some jobs, so I don't mind this.

如果代码量很小,我会手动完成。与某些工作相比,这几乎不是体力劳动,所以我不介意这一点。

I've never used the (un)comment-outcommand on any IDE.

我从未在任何 IDE 上使用过(un)comment-out命令。

回答by Brian Hong

For those who don't want to type three times, and just make normal cmd + /work, try this, I love it.

对于那些不想打三遍,只想正常cmd + /工作的人,试试这个,我喜欢它。

https://apps.apple.com/us/app/comment-here/id1406737173?mt=12

https://apps.apple.com/us/app/comment-here/id1406737173?mt=12

Download it, open it. It tells you how to install it. Also as the last step says, remove previous keymap for it, and assign a new one.

下载,打开。它告诉你如何安装它。同样如最后一步所说,删除它以前的键映射,并分配一个新的键映射。