Swift Playgrounds

RSS for tag

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

Posts under Swift Playgrounds tag

83 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

View Update Failure
Hello, The appearance of one of my buttons is not updating after being tapped. I have had no trouble doing the exact same thing in my other views, but here it simply doesn't work. The "heart" button will keep its original appearance (in this case an unfilled heart) no matter what, despite my print statements indicating that the value has changed. The other actions performed when tapping the heart work perfectly. I've been at it for hours. Any help would be appreciated. Thanks! import SwiftUI struct SearchView: View { @State private var searchText = "" @StateObject var save = SaveWords() @State var heart: String? @State var disappear = false @State var done = true var body: some View { NavigationView { VStack { if !disappear { SearchBarView(text: $searchText) Spacer() } if searchText != "" { List(.constant(Array(FetchWord.getWordFromStart(start: searchText)).prefix(10).map {Word(word: $0.1)})) { suggestion in NavigationLink(destination: suggestion.IPA.wrappedValue == "error" ? AnyView(EmptyView()) : AnyView(PracticeView(wordSheet: suggestion) .onAppear { disappear = true if done { SaveWords.file = "Favorites" DispatchQueue.main.async { Task { try? await save.load() heart = save.words.contains(suggestion.wrappedValue) ? ".fill" : "" } } } } .onDisappear { disappear = false Task { SaveWords.file = "History" try? await save.load() if save.words.first != suggestion.wrappedValue { save.words.insert(suggestion.wrappedValue, at: 0) try? await save.save() } } } .toolbar { ToolbarItem(placement: .navigationBarTrailing) { Button(action: { SaveWords.file = "Favorites" done = false if heart == "" { heart = ".fill" DispatchQueue.main.async { Task { try? await save.load() if !save.words.contains(suggestion.wrappedValue) { save.words.insert(suggestion.wrappedValue, at: 0) try? await save.save() } } } } else { heart = "" DispatchQueue.main.async { Task { try? await save.load() try? await save.delete(wordToDelete: suggestion.wrappedValue) try? await save.save() } } } done = true }, label: { Image(systemName: "heart" + (heart ?? "")) }) } }) ) { if suggestion.IPA.wrappedValue != "error" { CardView(wordSheet: suggestion) } } } } } Spacer() } } } #Preview { SearchView() }
3
0
785
Feb ’24
iPad and Apple Pencil Playground Submission
Your app playground must be built with and run on Swift Playgrounds 4.4 or later (requires iPadOS 16 or macOS 13.5, or later) or Xcode 15 on macOS 13.5, or later. You may incorporate the use of Apple Pencil. If I want to develop a playground for the iPad and Apple Pencil, am I limited to using Playgrounds on iPadOS to make it? Or could I also use Playgrounds on macOS and Xcode? If I use Xcode to make my iPad playground, should I select "Xcode" for "Which software should we use to run your app playground?" I assume so, as I used Xcode to make my app — but then it will be run in a simulator by the judges, which means they cannot use the Apple Pencil. I'm a little lost here. Any guidance is appreciated.
1
0
636
Feb ’24
Seeking Advice: Best Apple Laptop for Coding?
Hey Developers, I'm on the hunt for a new Apple laptop geared towards coding, and I'd love to tap into your collective wisdom. If you have recommendations or personal experiences with a specific model that excels in the coding realm, please share your insights. Looking for optimal performance and a seamless coding experience. Your input is gold – thanks a bunch!
2
1
2.7k
Feb ’24
Cycles in picker view
Hi everyone, I just finished my first app and I have a doubt about whether picker view automatically makes a series of loops in its func. I put some prints inside the funcs and this is the result only when starting the view with the picker: Every time I select it the cycles continue. It's normal? Is there a way to avoid or control it? Thanks in advance for any response.
0
0
490
Feb ’24
Document-based SwiftData App Playgrounds
Is it possible to create SwiftData Document-based apps in App Playgrounds (or Swift Packages, since App Playgrounds are a special type of packages)? As explained in wwdc2023-10154, to create a document-based app you are required to provide an UTType that conforms to com.apple.package. This requires your file type to be declared and exported in the Info.plist file of an Xcode project, but App Playgrounds don't have them. Providing .package as a UTType seems to partially work: import SwiftUI import SwiftData @main struct SwiftDataFlashCardSample: App { var body: some Scene { #if os(iOS) || os(macOS) DocumentGroup(editing: Card.self, contentType: .package) { ContentView() } #else WindowGroup { ContentView() } .modelContainer(for: Card.self) #endif } } This way I am able to run the app, create and edit new document, and save it on disk. However, opening it again does not work (you cannot select it since it is a folder without a defined package type). Is there any workaround to this? I guess I could drop support for multiple documents entirely if this is not possible (I don't think this would affect my submission), but I would just like to check if there is any way to do this first.
0
1
740
Feb ’24
Preview Autogenerated Code Error
Hello, Very recently, the following code has automatically appeared at the bottom of three of my SwiftUI View files: @available(iOS 17.0, macOS 14.0, tvOS 17.0, visionOS 1.0, watchOS 10.0, *) struct $s10Accent_Ace33_0BADA584A03144EFDAB57154E6FD3FBALl7PreviewfMf_15PreviewRegistryfMu_: DeveloperToolsSupport.PreviewRegistry { static let fileID: String = "Accent_Ace/HistoryView.swift" static let line: Int = 47 static let column: Int = 1 static func makePreview() throws -> DeveloperToolsSupport.Preview { DeveloperToolsSupport.Preview { let randomWord1 = FetchWord.getRandomWord() let randomWord2 = FetchWord.getRandomWord() @State var randomWords = [Word(word: randomWord1.0, IPA: randomWord1.1, lineNumber: randomWord1.2), Word(word: randomWord2.0, IPA: randomWord2.1, lineNumber: randomWord2.2)] HistoryView(words: $randomWords) } } } This is from one of my files but it's very similar in the other two. It seems to have something to do with my previews. The problem is that this code generates an error: Ambiguous use of 'init(_:traits:body:)'. My previews worked just fine before the auto-generated code appeared, so I tried deleting it. However, it automatically comes back no matter how many times I get rid of it. It's preventing me from building my App Playground, so I can't run the app or even see the previews. Does anyone know how to get rid of it or fix the error? Thanks for the help!
4
0
1.1k
Feb ’24
Swift Student Challenge Vision
Hi Developers, I want to create a Vision app on Swift Playgrounds on iPad. However, Vision does not properly function on Swift Playgrounds on iPad or Xcode Playgrounds. The Vision code only works on a normal Xcode Project. SO can I submit my Swift Student Challenge 2024 Application as a normal Xcode Project rather than Xcode Playgrounds or Swift Playgrounds File. Thanks :)
7
0
1.3k
Feb ’24
Thanks Fellow Forum Members
Thanks for zero replies. I can't believe that 28 to 51 people looked at my post and no one responded back. Anyway, I figured it out on my own after taking a break. I was just asking in so many words, where do I put the bold highlighted code. WHY NOT TRY app completed………. // // ContentView.swift // WhyNotTry // // Created by Geraldine Jones on 1/26/24. // import SwiftUI struct ContentView: View { var activities = ["Archery", "Baseball", "Basketball", "Bowling", "Boxing", "Cricket", "Curling", "Fencing", "Golf", "Hiking", "Lacrosse", "Rugby", "Squash"] var colors: [Color] = [.blue, .cyan, .gray, .green, .indigo, .mint, .orange, .pink, .purple, .red] ******** @State private var selected = "Baseball"******** @State private var id = 1 var body: some View { VStack { Text("Why not try...") .font(.largeTitle.bold()) VStack { Circle() .fill(colors.randomElement() ?? .blue) .padding() .overlay( Image(systemName: "figure.\(selected.lowercased())") .font(.system(size: 144)) .foregroundColor(.white) ******** .transition(.slide)******** ******** .id(id)******** ) Text("\(selected)!") .font(.title) } Spacer() Button("Try again") { ****** withAnimation(.easeInOut(duration: 1))****** { selected = activities.randomElement() ?? "Archery" ********id += 1******** } } .buttonStyle(.borderedProminent) } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
2
0
440
Jan ’24
The WhyNotTry tutorial
https://www.swift.org/getting-started/swiftui/ Is there a better tutorial than this one. Can't seem to find a live youtube video that teaches it. I went to git hub and downloaded the source code but all it shows is a big round blue circle. I can't seem to get pass the below code without it giving me an error. I need to see this tutorial live. Any suggestions? struct ContentView: View { var activities = ["Archery", "Baseball", "Basketball", "Bowling", "Boxing", "Cricket", "Curling", "Fencing", "Golf", "Hiking", "Lacrosse", "Rugby", "Squash"] var selected = "Archery" var body: some View { // ... } }
0
1
502
Jan ’24
Does the app will be revised on a iPad or a MacOS?
I am developing my app for the Swift Student Challenge 2024. I have made significant progress on it and have been building it on a M1 Mac, where it works perfectly. However, when testing it on some iPads (specifically, the iPad Pro 2018 and iPad Air 5 M1), it did not perform well on these devices. I am wondering if I can specify whether the app should be reviewed on a Mac (M1 or later) or if it will be reviewed on an iPad.
1
0
611
Jan ’24
Swift Playground: second episode uses unreferenced command
I am using Swift Playground to teach "coding" to my nephew. One of the strict requirements I am trying to explain him is: when you are required to solve a problem, you need to use only the instructions/harward/etc you have available. In the first episode of "Get started with code" -> "Commands" -> "Issuing commands", you are presented with two commands moveForward() and collectGem(). In the second episode "Toggle a Switch" you are introduced to a new command "toggleSwitch()". Up to this point, the learner was introduced to three commands: moveForward(), collectGem(), toggleSwitch(). However, in order to solve the puzzle in the second episode, you need also to use another command: turnLeft(), that has not been referenced. It is clear that looking for unknown commands is part of learning to code, but this occurs too early in the playground. Please consider also that some non-English young learners have no idea on how to write "turn left", so they cannot even type the first letters and hope in the auto-completion of the commands. This has not been well thought by the designers of the playground.
1
0
606
Jan ’24
App Playground deployment target?
Hi! I'm preparing to apply for the Swift Student Challenge and had a question. In the terms and conditions, it says "Your app playground must be built with and run on Swift Playgrounds 4.4 or later (requires iPadOS 16 or macOS 13.5) or Xcode 15 on macOS 13.5 or later. You may incorporate the use of Apple Pencil." Does this mean that my app must run on iPadOS 16 or macOS 13.5, or can it run on iPadOS 17 or macOS 14 or later as long as it runs on Playgrounds 4.4? In other words, what should I set my Deployment Target to? Thanks for your help in advance 😜
1
0
769
Jan ’24