UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Building a particular view for the Simulator

Forums > 100 Days of SwiftUI

Hi,

I'm trying to get Xcode to build a particular view to run in the Simulator or on the Phone, which is useful when Paul has multiple views in his xcode project. (Project6 in this case)

However, when I try to run the below code, I get an error: Command CompileSwiftSources failed with a nonzero exit code

Google hasnt helped me resolve it. Can anyone tell me what I am doing wrong?

import SwiftUI

@available(iOS 14.0, *)
@main
struct MyApp: App {
  var body: some Scene {
  WindowGroup {
    SnakeLettersContentView()
    }
  }
}

struct SnakeLettersContentView: View {
    let letters = Array("Hello SwiftUI")
    @State private var enabled = false
    @State private var dragAmount = CGSize.zero

    var body: some View {
        HStack(spacing: 0) {
            ForEach(0..<letters.count) { num in
                Text(String(self.letters[num]))
                    .padding(5)
                    .font(.title)
                    .background(self.enabled ? Color.blue : Color.red)
                    .offset(self.dragAmount)
                    .animation(Animation.default.delay(Double(num) / 20))
            }
        }
        .gesture(
            DragGesture()
                .onChanged { self.dragAmount = $0.translation }
                .onEnded { _ in
                    self.dragAmount = .zero
                    self.enabled.toggle()
                }
        )
    }
}

struct ContentView: View {
    @State private var animationAmount: CGFloat = 1

    var body: some View {
        Button("Tap Me") {
            self.animationAmount += 1
        }
        .padding(50)
        .background(Color.red)
        .foregroundColor(.white)
        .clipShape(Circle())
        .scaleEffect(animationAmount)
        .blur(radius: (animationAmount - 1) * 3)
        .animation(.default)
    }
}

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

2      

@randmtask doesn't have a clear view of the problem:

when I try to run the below code, I get an error: Command CompileSwiftSources failed with a nonzero exit code

First, you are asking the right questions. There are a number of programmers here eager to help you grok this material.

Next, take a step back from your code and look at it from the compiler's point of view.

I like to think of a View as a Lego kit full of parts that SwiftUI wants to assemble and push to the screen. Your @main group is going to set up a view named SnakeLettersContentView()

But your preview code is telling SwiftUI to assemble a view from a (Lego) kit named ContentView().

Your ContentView() kit only has one button that toggles a switch you've named animationAmount.

This is fine, but does nothing.

Toggling this boolean has no effect on the other Lego kit, SnakeLettersContentView().

This may not be the root of the problem, but I'd start there to eliminate unnecessary confusion.

Keep Coding

3      

One other thought.

You may find the compiler reminding you that the .animation() modifier has been deprecated. Sure, it works now, but it may not work after WWDC in a few weeks!

Instead, the newer version of this modifier asks you to include a value: parameter that it watches for changes. When this value changes, the .animation() modifier knows to do its animation magic.

Suggest changing the modifier to:

ForEach(0..<letters.count) { num in
    Text(String(self.letters[num])
    .padding(5)
    .font(.title)
    .background(self.enabled ? Color.blue : Color.red)
    .offset(self.dragAmount)
 // .animation(Animation.default.delay(Double(num) / 20))  // <-- Deprecated
    .animation(Animation.default.delay(Double(num) / 20), value: self.dragAmount) // <-- Use this instead
    }
}

3      

Finally, here's what I think about ContentView().

Please add your own thoughts to the discussion.

See --> Give your View a Proper Description

3      

If you want to have all the animation in one project

Make a NEW SwiftUI View file for SnakeLettersView

struct SnakeLettersView: View {
    let letters = Array("Hello SwiftUI")
        @State private var enabled = false
        @State private var dragAmount = CGSize.zero

        var body: some View {
            HStack(spacing: 0) {
                ForEach(0..<letters.count, id: \.self) { num in
                    Text(String(self.letters[num]))
                        .padding(5)
                        .font(.title)
                        .background(self.enabled ? Color.blue : Color.red)
                        .offset(self.dragAmount)
                        .animation(Animation.default.delay(Double(num) / 20), value: dragAmount)
                }
            }
            .gesture(
                DragGesture()
                    .onChanged { self.dragAmount = $0.translation }
                    .onEnded { _ in
                        self.dragAmount = .zero
                        self.enabled.toggle()
                    }
            )
            .navigationTitle("Snake Letters")
            .navigationBarTitleDisplayMode(.inline)
        }
}

struct SnakeLettersView_Previews: PreviewProvider {
    static var previews: some View {
        SnakeLettersView()
    }
}

and then make a NEW SwiftUI View file for ButtonView

import SwiftUI

struct ButtonView: View {
    @State private var animationAmount: CGFloat = 1

    var body: some View {
        Button("Tap Me") {
            self.animationAmount += 1
        }
        .padding(50)
        .background(Color.red)
        .foregroundColor(.white)
        .clipShape(Circle())
        .scaleEffect(animationAmount)
        .blur(radius: (animationAmount - 1) * 3)
        .animation(.default, value: animationAmount)
        .navigationTitle("Button")
        .navigationBarTitleDisplayMode(.inline)
    }
}

struct ButtonView_Previews: PreviewProvider {
    static var previews: some View {
        ButtonView()
    }
}

Then in the ContentView change to this

struct ContentView: View {

    var body: some View {
        NavigationStack {
            List {
                NavigationLink("Button", destination: ButtonView.init)
                NavigationLink("SnakeLetters", destination: SnakeLettersView.init)
                // keep adding new NavigationLink for each animation type
            }
        }
    }
}

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

This will not produce any errors.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Reply to this topic…

You need to create an account or log in to reply.

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.