NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: posting code for review general question

Forums > SwiftUI

hi there,

apologies for this simple question, but how do i post my code here so it cn be viewed correctly.

when i cut and paste it in it just all maps together into one giant paragraph (see below)

thnx

import SwiftUI

struct ContentView: View {     @State private var game = ["Rock", "Paper", "Scissors"] //.shuffled()     @State private var correctAnswer = Int.random(in: 0...2)          @State private var winLose = [" Win", " Lose"]     @State private var correctAnswer1 = Int.random(in: 0...2)          @State private var showingScore = false // tells us if alert is showing or not     @State private var scoreTitle = "" // title inside the alert     @State private var score = 0          var body: some View {         ZStack {             LinearGradient(gradient: Gradient(colors: [.blue, .black]),                            startPoint: .top, endPoint: .bottom)             VStack(spacing: 30) {                 VStack {                     Text("Tap the correct move")                         .foregroundColor(.white)                     Text(game[correctAnswer] + winLose[correctAnswer1])                         .foregroundColor(.white)                         .font(.largeTitle)                         .fontWeight(.black)                                      }                                  ForEach(0 ..< 3) { number in                     Button(action: {                         self.gameTapped(number)                     }) {                         Text(self.game[number])                             .foregroundColor(.white)                             .clipShape(Capsule())                             .overlay(Capsule().stroke(Color.black, lineWidth: 1))                             .shadow(color: .black, radius: 2)                     }                 }                 Spacer()             }         }         .alert(isPresented: $showingScore) {             Alert(title: Text(scoreTitle), message: Text("Your score is (score)"), dismissButton: .default(Text("Continue")) {                 self.askQuestion()             })         }     }     func gameTapped(_ number: Int) {              if number != correctAnswer {                scoreTitle = "Correct"                    score += 100                              }              else {             scoreTitle = "Wrong. This was..... (game[number])"             score -= 100         }         showingScore = true              }     func askQuestion() {         //game.shuffle()         correctAnswer = Int.random(in: 0...2)     } }

struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } }

1      

You have to fence your code with three backticks ``` 0n the line before and three backticks ``` on the line after your codeblock.

You can either put in the backticks manually or you can highlight your codeblock and cliock the </> button in the formatting toolbar.

So this:

func printSomething(_ thing: String?) { if let thing = thing { print(thing) } else { print("nothing there") } }

will come out like this:

func printSomething(_ thing: String?) {
    if let thing = thing {
        print(thing)
    } else {
        print("nothing there")
    }
}

1      

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.