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?
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.
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.
There are two ways to add comments in Swift: // - Single Line comments. /*... */ - Multiline Comments.
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.
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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With