How to comment a block of code

Apologies for asking a very very basic question.

How to comment on a block of code in Xcode 15?

Many thanks

In the following sample of code the comments are prefixed by two forward slashes.

//
//  ContentView.swift
//  Test
//

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            // Image uses SF Symbols
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
        }
        .padding()
    }
}

#Preview {
    ContentView()
}

You can create a lengthy comment by using this method:

struct ContentView: View {
    var body: some View {
        VStack {
            /*
             Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
             */
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
        }
        .padding()
    }
}

Whilst it is a single line comment above, it will line wrap in the Xcode editor window and look like this:

If you mean that you want to comment out a block of code then what you would do is select the block of code in the editor window like this:

and then press Command + / and that entire block will have double forward slashes added to it like this:

Thank you so much for your help and responsing so quickly

1 Like