anonim
anonim

Reputation: 2544

Xcode comment uncomment issue

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?

Upvotes: 8

Views: 9671

Answers (6)

Brian Hong
Brian Hong

Reputation: 1084

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.

Upvotes: 2

matrix3003
matrix3003

Reputation: 398

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

Upvotes: 4

user3226094
user3226094

Reputation: 11

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.

Upvotes: 1

Senior
Senior

Reputation: 2259

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.

Upvotes: 13

trojanfoe
trojanfoe

Reputation: 122391

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.

Upvotes: 1

woz
woz

Reputation: 10994

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.

Upvotes: 1

Related Questions