Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode comment uncomment issue

Tags:

xcode

ios

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

//      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

//     //      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?

like image 728
anonim Avatar asked Nov 15 '12 15:11

anonim


People also ask

How do I comment and uncomment in Xcode?

By selecting a few lines of code (pressing ⇧+↑ or ⇧+↓), you can comment a bunch of lines in a single shot with the ⌘ + / shortcut. If the lines are already comments, the same shortcut will uncomment them.

How do you uncomment a comment?

An alternative way to comment any block of code is to select it, press Alt+Enter and choose Comment selection. The same way works for uncommenting code inside a block comment - set the caret anywhere in the block comment, press Alt+Enter and choose Uncomment.

How do you comment multiple lines in Swift?

There are two ways to add comments in Swift: // - Single Line comments. /*... */ - Multiline Comments.


6 Answers

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.

like image 171
Senior Avatar answered Oct 05 '22 21:10

Senior


Editor->Structure->Uncomment Selection

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

comments must appear AT THE FIRST POSITION to be considered as such

like image 44
matrix3003 Avatar answered Oct 05 '22 23:10

matrix3003


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

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.

like image 23
Brian Hong Avatar answered Oct 05 '22 22:10

Brian Hong


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.

like image 40
woz Avatar answered Oct 05 '22 23:10

woz


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-out command on any IDE.

like image 42
trojanfoe Avatar answered Oct 05 '22 22:10

trojanfoe


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.

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.

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

Good luck all.

like image 36
user3226094 Avatar answered Oct 05 '22 23:10

user3226094