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
3 Replies
280 Views
Was doing the tutorials from Swift Playgrounds new lesson "Keep going with Apps" and on the "Bindings" chapter was asked to put a ColorPicker view. To see the color options I should run the app instead of seeing it in the preview. I tried to do that but I don't get a response from Playgrounds... Has someone had the same problem while doing this lesson?
Posted Last updated
.
Post not yet marked as solved
1 Replies
55 Views
Hi everyone! I just started learning Swift Playgrounds and I have been enjoying it so much! But I have a problem that I haven't fixed and I would really appreciate it if you could help me. I tried to upload a picture but is showing me an error every time I try. It's a window with the next message: Error: Consecutive statements on a line must be separated by '.' / (Something like that) Thank you so much for your help.
Posted Last updated
.
Post not yet marked as solved
9 Replies
966 Views
Hello :) My iOS project does not work with SwiftUI Preview in Xcode 14 beta. The project builds and runs fine, but Preview never works. The error message is as follows. HumanReadableSwiftError SettingsError: noExecutablePath(<IDESwiftPackageStaticLibraryProductBuildable:ObjectIdentifier(0x000060002ac3e880):'AppCenterCrashes'>) I do use the AppCenterCrashes dependency in my project. But I'm not sure if this error message is accurate, because sometimes it reports other packages. After I executed Generate Report, I found the following error in SerializationErrors.txt. Error Domain=NSCocoaErrorDomain Code=516 "“LogCaffeineIntent.swift” couldn’t be linked to “Intermediates” because an item with the same name already exists." UserInfo={NSSourceFilePathErrorKey=/Users/gong/Library/Developer/Xcode/DerivedData/.../Build/Intermediates.noindex/HiCoffee.build/Debug-iphonesimulator/IntentsExtension.build/DerivedSources/IntentDefinitionGenerated/Intents/LogCaffeineIntent.swift, NSUserStringVariant=( Link ), NSDestinationFilePath=/var/folders/lv/58r5dhv52j779_qn41gnq4c80000gn/T/previews-diagnostics-20220608-144336/Intermediates/LogCaffeineIntent.swift, NSFilePath=/Users/gong/Library/Developer/Xcode/DerivedData/.../Build/Intermediates.noindex/HiCoffee.build/Debug-iphonesimulator/IntentsExtension.build/DerivedSources/IntentDefinitionGenerated/Intents/LogCaffeineIntent.swift, NSUnderlyingError=0x600033223810 {Error Domain=NSPOSIXErrorDomain Code=17 "File exists"}} The problem seems to be related to the Intent generation code. I don't know how to fix it yet.
Posted
by Gong.
Last updated
.
Post not yet marked as solved
9 Replies
1.8k Views
Can't seem to get CryptoKit to import. Is there some other configuration setting, etc. I need to fix? I though all you need to do in a playground is the import statement. My code looks like this: import UIKit import CryptoKit let string = "Hello world" let data = Data(string.utf8) let digest = SHA256.hash(data: data) let hash = digest.compactMap { String(format: "%02x", $0)}.joined() print("The hash is \(hash)") Basically, I can't use any method from CryptoKit although when I was typing the import statement, soon as I typed 'Cr' I was prompted with the fill of 'CryptoKit'. It also working in an IoS project in Swift. So I know it's there. Just won't work in the playground. I get the following error message: error: Couldn't lookup symbols:   static CryptoKit.HashFunction.hash<τ_0_0 where τ_1_0: Foundation.DataProtocol>(data: τ_1_0) -> τ_0_0.Digest   static CryptoKit.HashFunction.hash<τ_0_0 where τ_1_0: Foundation.DataProtocol>(data: τ_1_0) -> τ_0_0.Digest   static CryptoKit.HashFunction.hash<τ_0_0 where τ_1_0: Foundation.DataProtocol>(data: τ_1_0) -> τ_0_0.Digest   static CryptoKit.HashFunction.hash<τ_0_0 where τ_1_0: Foundation.DataProtocol>(data: τ_1_0) -> τ_0_0.Digest   static CryptoKit.HashFunction.hash<τ_0_0 where τ_1_0: Foundation.DataProtocol>(data: τ_1_0) -> τ_0_0.Digest Appreciate any insights. Thanks
Posted
by Khaja.
Last updated
.
Post not yet marked as solved
0 Replies
70 Views
The following code fails on Swift Playgrounds for Mac but not Swift Playgrounds for iPad: import SwiftUI struct Response: Codable {   var results: [Result] } struct Result: Codable {   var trackId: Int   var trackName: String   var collectionName: String } struct ContentView: View {   @State private var results = [Result]()       var body: some View {     List(results, id: \.trackId) { item in       VStack(alignment: .leading) {         Text(item.trackName)           .font(.headline)         Text(item.collectionName)       }     }     .task {       results = await loadData()     }   } } func loadData() async -> [Result]{   var results = [Result(trackId: -1, trackName: "No Tracks Available", collectionName: "No Collection Available")]       let url = URL(string: "https://itunes.apple.com/search?term=taylor+swift&entity=song")       do {     let (data, _) = try await URLSession.shared.data(from: url!)           if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {       results = decodedResponse.results     }   } catch {     print("Invalid data")   }   return results } Repost of https://developer.apple.com/forums/thread/708856 which I think was incorrectly accepted as answered. Example from https://www.hackingwithswift.com/books/ios-swiftui/sending-and-receiving-codable-data-with-urlsession-and-swiftui
Posted
by jakee417.
Last updated
.
Post not yet marked as solved
0 Replies
70 Views
Not sure if you guys are taking feedback but is it would be cool to show us a sample of how you coded the solution to problem after we are successful with ours? I believe it will help us new learners understand the best routes to take when in coding better. Cheers and thanks for making this app for us!
Posted
by EckoNode.
Last updated
.
Post not yet marked as solved
1 Replies
137 Views
I have successfully made an iOS app with Swift Playgrounds 4.1 (without Xcode). Now I'm ready to publish it. However, publishing the app requires screenshots from 4 different devices. How do I obtain those screenshots without Xcode and without owning those 4 devices? Any suggestions?
Posted
by danatkiva.
Last updated
.
Post not yet marked as solved
0 Replies
107 Views
Hi guys, so I was hoping to understand more about how others code their apps to work and understand how/if we think differently in our approach. This was the first one I did that I wonder if anyone else would have done differently. Please share your thoughts.
Posted
by EckoNode.
Last updated
.
Post marked as solved
5 Replies
297 Views
I have an app using UrlSession in Swift Playgrounds for iPad, and everything worked great. I opened the same code in Swift Playgrounds for Mac, and the line: let (data, _) = try await URLSession.shared.data(from: url) silently fails everytime. I exported a small snippet to a playground in Mac, and found that adding: import PlaygroundSupport PlaygroundPage.current.needsIndefiniteExecution = true At the beginning of the script fixes this in the playground. I am wondering if there is a way to move this code to the app version and get this up and running on the Mac? Or maybe this code was never designed to run on a Macbook?
Posted
by jakee417.
Last updated
.
Post not yet marked as solved
1 Replies
160 Views
I created a swiftpm project with Xcode Version 13.4.1 (13F100). I changed the target from the default created by Xcode to this:     targets: [         .executableTarget(             name: "App",             path: "App"         )     ] I also changed the package version to 5.6 to match the AboutMe Package file. I also created an App directory and moved all my files (bar the Package.swift file) to it, and I also created a Guide folder and created a Guide.tutorial file based on the example in the WWDC video. I've turned on Author Debugging Mode (this is on Swift Playgrounds on the Mac, I'll try later on Swift Playgrounds on the iPad), but I get no messages other than the "Could not display guide. Try reopening the current project..." I also get a build error that just says "Building failed" What am I missing?
Posted
by mhanlon99.
Last updated
.
Post not yet marked as solved
3 Replies
401 Views
Hi, I am a computer science teacher at secondary schoool. Would it be possible to have the entire code of this app please? I am going to implement something similar for my students. Kind Regards, Federico
Posted Last updated
.
Post not yet marked as solved
1 Replies
197 Views
I want to make an app and become a real developer but I can’t because I only have $140 and I can’t find any iPads or MacBooks under $140. I could only find android tablets. Can anyone help me find an iPad or MacBook under $140 or a Swift Playgrounds alternative for iOS? Please I need help!
Posted Last updated
.
Post not yet marked as solved
2 Replies
245 Views
Hello! I am trying to learn to make apps with Swift Playgrounds and Swift UI. The devices I own include an iPad Air 4 running iPadOS 15, M1 Mac Mini running macOS Monterey, iPhone 13 running iOS 15, and an Apple Watch Series 4 running WatchOS 8. I plan to update all of these devices to the next-gen public betas as they release. Ive completed the “Getting Started with Apps” course on Swift Playgrounds on my iPad. does anyone have any tips on starting from scratch, or know how I can learn more and sharpen my skills? thank you!
Posted Last updated
.
Post not yet marked as solved
0 Replies
93 Views
I have a simple class to interval run some code when call it start ; // Test.swift public class Test { var timer Timer? public func start(){ // print("2“) // print timer = Timer.scheduledTimer(5,true,{ // code here print("3") // not print } // RunLoop.current.add(timer,forMode:.commonMode) // uncomment this line also not work } } // Class2.swift class Class2: ParentClass{ private let test = Test() @objc public func check(flag:String){ if flag == "Y" { print("1") test.start() } } } But the timer seems not work --- not print "3" But I try to create a timer at some other class, it work, just at Test class not run .... Anyone know why ?
Posted
by yuhj.
Last updated
.
Post not yet marked as solved
0 Replies
141 Views
A puzzling phenomenon occurs in obtaining the week number. Please see the attached source. In Step 0, the correct week number can be obtained according to ISO8601. In Step 1, by changing minimumDaysInFirstWeek, a new (correct) week number can be obtained. In Step 2, if I re-set the firstWeekday, for some reason the week number reverts back to the original, even though the value has not been changed. Shouldn't Step2 return 1 for both weekOfMonth and weekOfYear? I have verified this with XCode 13.4 playground. Thanks in advance. import Foundation var calendar = Calendar(identifier: .iso8601) let date = calendar.date(from: DateComponents(year: 2022, month: 1, day: 1)) print("*** Step 0 ***") print("firstWeekday : \(calendar.firstWeekday)") print("minimumDaysInFirstWeek : \(calendar.minimumDaysInFirstWeek)") print("weekOfMonth : \(calendar.component(.weekOfMonth, from: date!))") print("weekOfYear : \(calendar.component(.weekOfYear, from: date!))") print("\n*** Step 1 ***") calendar.minimumDaysInFirstWeek = 1 print("firstWeekday : \(calendar.firstWeekday)") print("minimumDaysInFirstWeek : \(calendar.minimumDaysInFirstWeek)") print("weekOfMonth : \(calendar.component(.weekOfMonth, from: date!))") print("weekOfYear : \(calendar.component(.weekOfYear, from: date!))") print("\n*** Step 2 ***") calendar.firstWeekday = 2 print("firstWeekday : \(calendar.firstWeekday)") print("minimumDaysInFirstWeek : \(calendar.minimumDaysInFirstWeek)") print("weekOfMonth : \(calendar.component(.weekOfMonth, from: date!))") print("weekOfYear : \(calendar.component(.weekOfYear, from: date!))") Result *** Step 0 *** firstWeekday : 2 minimumDaysInFirstWeek : 4 weekOfMonth : 0 weekOfYear : 52 *** Step 1 *** firstWeekday : 2 minimumDaysInFirstWeek : 1 weekOfMonth : 1 weekOfYear : 1 *** Step 2 *** firstWeekday : 2 minimumDaysInFirstWeek : 1 weekOfMonth : 0 weekOfYear : 52
Posted Last updated
.