Swift Playgrounds

RSS for tag

Learn and explore coding in Swift through interactive learning experiences on the Swift Playgrounds app for iPadOS and macOS.

Swift Playgrounds Documentation

Posts under Swift Playgrounds tag

279 Posts
Sort by:
Post not yet marked as solved
1 Replies
178 Views
Hello there! I was trying to build an app with the new App feature in Swift Playgrounds for Mac. But I have an error message in my code, and I can't understand how to fix it. Can anyone help me to solve this error. Here's the code: import SwiftUI struct Something: View {     let rows = 17     let columns = 17     let size: CGFloat = 10     let blocks: [[Color]] = [[.purple, .purple]]     var body: some View {         VStack {             ForEach((0...self.rows - 1), id: \.self) { row in                  HStack (spacing: 0) {                     ForEach((0...self.columns - 1), id: \.self) { col in                         VStack (spacing: 0) {                             Rectangle()                                 .frame(width: self.size, height: self.size)                                 .foregroundColor(self.blocks[row][col]) // index out of range: the requested index was outside the bounds of the array.                         }                     }                 }             }         }     } }
Posted Last updated
.
Post not yet marked as solved
0 Replies
128 Views
Hi everyone, I was working on a playground project in Xcode, I was about to go AFK so I closed the application but it just started not responding and didn’t close. Is there a way I can fix this? When I try to force close Xcode I just lost the code I wrote even if I saved so I don’t know what to do. I also have another problem, sometimes when working on a code, I try to execute the playground but the program just stops working, I can’t click on the execute playground button also if I try with the shortcut. Thank you for the help
Posted
by _Face_.
Last updated
.
Post not yet marked as solved
1 Replies
139 Views
Hello everybody! I installed Swift Playgrounds a few months ago and I like it. Very good for a beginner like me. I started Learn to code I and was working fine. Few days ago I updated to version 4.1 and is not working anymore. No animation and of course can not running the code either. I have this message: "There was a problem running this page. Check your code for problems. If you are stuck, start over to delete all of your changes on this page and try again." I tried to reinstall Swift Playgrounds but doesn't resolve the problem . Does anybody know how to resolve this issue? My current MacOs Monterey 12.4
Posted
by DesmondCD.
Last updated
.
Post not yet marked as solved
2 Replies
230 Views
Howdy from Germany. I try to add a local httpServer with swift-http-server (https://github.com/bjtj/swift-http-server). import SwiftUI import SwiftHttpServer @main struct MyApp: App {   init() {     print("init")     let server = HttpServer(port: 9090)           class GetHandler: HttpRequestHandler {               var dumpBody: Bool = true               func onHeaderCompleted(header: HttpHeader, request: HttpRequest, response: HttpResponse) throws {       }               func onBodyCompleted(body: Data?, request: HttpRequest, response: HttpResponse) throws {         response.status = .ok         response.data = "Hello".data(using: .utf8)       }     }           do {       try server.route(pattern: "/", handler: GetHandler())     } catch let serverError {       print(serverError)     }           let queue = DispatchQueue.global(qos: .default)     queue.async {       do {         try server.run()       } catch let error {         print(error)       }     }   }   var body: some Scene {     WindowGroup {       ContentView() // Standard ContentView     }   } } When I try to add the local network rights on macOS (Montery 12.4 21F79) in the Playgrounds.app (Version 4.1 (1676.15)) it crashes everytime I hit the (+) button. On iPad (Air 4th gen) with latest ipadOS it works fine. I can add the network rights and can serve the little hello page, by entering :9090/ As mentioned on macOS not possible. Of couse if I reload the Playgrounds project on the Mac, the local network rights are now included, but still on the mac the "Hello" page is not served. Any hints/tips how to solve this problem? Need I to add more rights? Do I have to report the crash somewhere? I have the crashlog. Thanks in advance. Alex PS: Background: I try to write a Playgrounds App which generates some output on a webpage, which then is served via the in-app webserver, which then is integrated on Obs via web page plugin...
Posted
by alexrjs.
Last updated
.
Post not yet marked as solved
4 Replies
427 Views
IPad Playgrounds has started to routinely crash on me. It started crashing while I was trying to work with protocol meta types, so I thought something about that feature was doing it, but I’ve moved onto other projects, or even brand new ones and Playgrounds keeps crashing. It seems to have something to do with comments. If I’m just editing code it’ll keep going and the preview will update, but if I start to edit a comment I’ll see the compiler begin to spin and it crashes. I can post a video later if needed. I’ve rebooted the device and it’s still crashing. What do I do? If I delete and reinstall Playgrounds will all my projects stay there or do I need to back them up somehow?
Posted Last updated
.
Post not yet marked as solved
1 Replies
231 Views
Hello people, I'm new in Swift and Xcode, I'm learning and until now everything is okay and fantastic, however I have an issue with the next exercise. Lab - Enumerations.playground-App Exercise - Swimming Workouts In the last part of the exercise is written the next: Inside save() write a switch statement that switches on the instance's stroke property, and appends self to the proper array. I don't understand what means "appends self to the proper array", I tried everything and can not found the solution, someone can explain in more detail what this mean? Thank you all in advance. Ernesto.
Posted
by ereides_1.
Last updated
.
Post marked as solved
2 Replies
218 Views
Hi there, Before I post this in here, I have quick googled it but didn't found accurate answer. I'm in Chapters 2 the end of Parameters, I just want 3 rows's Character to solve the puzzle at same time, but I really don't know what way can do it, if I line the func one by one, they just complete one by one. Could you have any ideas, thanks a lot~
Posted
by Hark.
Last updated
.
Post not yet marked as solved
4 Replies
380 Views
I am a beginner and i am working on the Scrumdinger tutorial . this message keeps popping up "Cannot infer contextual base in reference to member 'trailingIcon'" I do not know how to fix it. Here is my code import SwiftUI struct CardView: View {   let scrum: DailyScrum   var body: some View {     VStack(alignment: .leading){       Text(scrum.title)         .font(.headline)         .accessibilityAddTraits(.isHeader)               Spacer()       HStack {         Label("(scrum.attendees.count)", systemImage: "person.3")           .accessibilityLabel("(scrum.attendees.count) attendees")         Spacer()         Label("(scrum.lengthInMinutes)", systemImage: "clock")           .accessibilityLabel("(scrum.lengthInMinutes) minute meeting")           .labelStyle(.trailingIcon)       }       .font(.caption)     }     .padding()     .foregroundColor(scrum.theme.accentColor)   } } struct CardView_Previews: PreviewProvider {   static var scrum = DailyScrum.sampleData[0]   static var previews: some View {     CardView(scrum: scrum)       .background(scrum.theme.mainColor)       .previewLayout(.fixed(width: 400, height: 60))   } } Any ideas?
Posted
by Len5261.
Last updated
.
Post not yet marked as solved
1 Replies
201 Views
I follow along with the video in the WWDC2022 session, "Build your first app in Swift Playgrounds". I construct the Tea Time app as demonstrated in the video. When I come to the part about adding the microphone capability in the App Settings, Playground crashes and disappears. I click the "+" button for the capability, for a fraction of a second I see a modal about the microphone, and then it crashes. I don't know what to do. How do I fix this problem and add the capability?
Posted Last updated
.
Post not yet marked as solved
2 Replies
264 Views
Using Version 14.0 beta (14A5228q) on Monterey 12.4 I am trying to follow the 'Meet Swift regex' presentation dated 7 June but I have stumbled on the most basic of errors that I cannot get around. At 3:56 in the video, the following line of code is shown let transaction = "DEBIT 03/05/2022 Doug's Dugout Dogs $33.27" let fragments = transaction.split(separator: /\s{2,}|\t/) // ["DEBIT", "03/05/2022", "Doug's Dugout Dogs", "$33.27"]`` The '/' of the separator string generates the error: error: cannot convert value of type 'Regex' to expected argument type 'String.Element' (aka 'Character') I am embarrassed but also stuck on the simple issue and I cannot figure out what I am missing. TIA Chris
Posted
by CSantavy.
Last updated
.
Post marked as solved
2 Replies
209 Views
I've been trying to use Swift Playgrounds to play with some basic code and have been encountering an infuriating issue. When I switch into the Playgrounds window from another program (by swiping left or right after writing notes in another program), I can no longer type into the Playgrounds window. Clicking anywhere to put my cursor in the editor just won't let me type. In order to be allowed to type again I have to first click one of the code suggestion options from the bottom of the screen. After that I can change and overwrite freely. It is driving me crazy.
Posted
by Dario006.
Last updated
.
Post not yet marked as solved
2 Replies
204 Views
import SwiftUI import PlaygroundSupport struct ProgressView: View {     let gradientColors: [Color] = [Color( colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)), Color( colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0))]     let sliceSize = 0.35     let progress: Double          private let percentageFormatter: NumberFormatter = {         let formatter = NumberFormatter()         formatter.numberStyle = .percent         return formatter     } ()          init(_ progress: Double = 0.3) {         self.progress = progress     }          var body: some View {         GeometryReader { geometry in             ZStack {                 Group {                     Circle()                         .trim(from: 0, to: 1 - CGFloat(self.sliceSize))                         .stroke(self.strokeGradient, style: self.strokeStyle(with: geometry))                         .opacity(0.5)                     Circle()                       .trim(from: 0, to: 1 - CGFloat(self.sliceSize)) * CGFloat(self.progress))                     .stroke(self.strokeGradient, style: self.strokeStyle(with: geometry))                                                               } .rotationEffect(.degrees(90) + .degrees(360 * self.sliceSize / 2))             }         }     } }
Posted
by a27divine.
Last updated
.
Post not yet marked as solved
0 Replies
130 Views
Another solution for easy understanding, hope it helps. var gemCounter = 0 var switchCounter = 0 while gemCounter >= switchCounter {     moveForward()     if isBlocked && isBlockedLeft {         turnRight()     }     while isOnGem {         collectGem()         gemCounter += 1     }     while isOnClosedSwitch && switchCounter !=  gemCounter {         toggleSwitch()         switchCounter += 1         if switchCounter == gemCounter {                          switchCounter += 1         }              } }
Posted
by Chaucer.
Last updated
.