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

SOLVED: Xcode is taking forever to compile this code

Forums > SwiftUI

import SwiftUI

struct HalfPie : Shape
{
   func path( in rectangle: CGRect ) -> Path
   {
      var path = Path()
      path.move( to: CGPoint(
                            x: rectangle.midX,
                            y: rectangle.midY))

      path.addArc(center: CGPoint(
                            x: rectangle.midX,
                            y: rectangle.midY ),
                          radius: rectangle.width / 2,
         startAngle: Angle(degrees: 40),
         endAngle: Angle(degrees: 140),
         clockwise: false )

      path.closeSubpath()

    return path
   }
}

struct Triangle: Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()

        path.move(to: CGPoint(x: rect.midX, y: rect.minY))
        path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))
        path.addLine(to: CGPoint(x: rect.maxY, y: rect.maxY))
        path.closeSubpath()

        return path
    }
}

struct ContentView: View {

    @State private var selectedIndex = 0
    let shapes = ["Square","Triangle","Circle","PieSlice"]

    var body: some View {
        VStack(alignment:.center){
            Text("Select Shape")
                .font(.largeTitle)

            Group{

                if selectedIndex == 0{
                    Rectangle()
                        .stroke()
                        .frame(width:200,height:100)
                }

                else if selectedIndex == 1{
                    //   Rectangle()
                    Triangle()
                        .stroke()

                }

                else if selectedIndex == 2{
                    Circle()
                        .stroke()

                }

                else if selectedIndex == 3 {
                    HalfPie()
                        .foregroundColor(.green)
                        .stroke(Color.blue)
                }
            }
            .frame(width: 200, height: 200)
            .padding()

            Picker("Select Shape", selection: $selectedIndex){
                ForEach(0..<shapes.count, id: \.self){
                    Text(shapes[$0])
                }
            }.pickerStyle(SegmentedPickerStyle())
            .padding()
        }
    }
}

When I`m commenting any 1 condition its compiling. Doesnt matter which if/else it seems that its refusing to compile if there are more than 3 conditions.

3      

Try using a switch instead ot if

4      

And whats the explanation of the solution? 4 if/else statements are enough for xcode to get confused?

4      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot 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.