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

90 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

Comparing colors of two ModelEntities
I want to compare the colors of two model entities (spheres). How can i do it? The method i'm currently trying to apply is as follows case let .color(controlColor) = controlMaterial.baseColor, controlColor == .green { // Flip target sphere colour if let targetMaterial = targetsphere.model?.materials.first as? SimpleMaterial, case let .color(targetColor) = targetMaterial.baseColor, targetColor == .blue { targetsphere.model?.materials = [SimpleMaterial(color: .green, isMetallic: false)] // Change to |1⟩ } else { targetsphere.model?.materials = [SimpleMaterial(color: .blue, isMetallic: false)] // Change to |0⟩ } } This method (baseColor) was deprecated in swift 15.0 changes to 'color' but i cannot compare the value color to each other.👾
1
0
143
2d
Swift Playgrounds Will Not Upload to App Store Connect
When I go to the App Settings menu in Swift Playgrounds and attempt to upload to App Store Connect, I run into the below error screen: I am registered with the Apple Developer Program. Swift Playgrounds is updated to its latest version. This is occurring on both macOS and iPadOS. All the information I put in is valid. Any reason why this would be happening?
2
1
119
2d
Swift/ Swift UI errors.
Hi everyone, I am a new developer. I am writing some code in swift playgrounds and came across some odd errors. I have looked for solutions and have found nothing. If any more experienced developers could point me in the right direction, that would be great.
4
0
195
1w
Can not use or do anything on the Playgrounds app on iPad Air M2 running iPadOS 18.3 beta
Hi, I have recently downloaded the app to start learning how to code in swift and so on. On my first try I have gotten the error stating that ”.swiftpm files cannot be opened”. I have followed some forum posts. I restarted my iPad several times and redown the app several times. I tried to disable the iCloud sync but I think I deleted the iCloud directory entirely that just results in the app closing on any action taken. (such as pressing the “New App” option or choosing the quick actions to make apps). Is there a way to fix this? Any help is appreciate, please ask me to provide any extra needed information.
1
0
110
2d
Using Core ML in a .swiftpm file
Hi everyone, I've been struggling for a few weeks to integrate my Core ML Image Classifier model into my .swiftpm project, and I’m hoping someone can help. Here’s what I’ve done so far: I converted my .mlmodel file to .mlmodelc manually via the terminal. In my Package.swift file, I tried both "copy" and "process" options for the resource. The issues I’m facing: When using "process", Xcode gives me the error: "multiple resources named 'coremldata.bin' in target 'AppModule'." When using "copy", the app runs, but the model doesn’t work, and the terminal shows: "A valid manifest does not exist at path: .../Manifest.json." I even tried creating a Manifest.json manually to test, but this led to more errors, such as: "File format version must be in the form of major.minor.patch." "Failed to look up root model." To check if the problem was specific to my model, I tested other Core ML models in the same setup, but none of them worked either. I feel stuck and unsure of how to resolve these issues. Any guidance or suggestions would be greatly appreciated. Thanks in advance! :)
2
0
325
4d
Vision Framework Causes EXC_BREAKPOINT Error in Xcode App Playground (.swiftpm) File
I’m trying to use the Vision framework in a Swift Playground to perform face detection on an image. The following code works perfectly when I run it in a regular Xcode project, but in an App Playground, I get the error: Thread 12: EXC_BREAKPOINT (code=1, subcode=0x10321c2a8) Here's the code: import SwiftUI import Vision struct ContentView: View { var body: some View { VStack { Text("Face Detection") .font(.largeTitle) .padding() Image("me") .resizable() .aspectRatio(contentMode: .fit) .onAppear { detectFace() } } } func detectFace() { guard let cgImage = UIImage(named: "me")?.cgImage else { return } let request = VNDetectFaceRectanglesRequest { request, error in if let results = request.results as? [VNFaceObservation] { print("Detected \(results.count) face(s).") for face in results { print("Bounding Box: \(face.boundingBox)") } } else { print("No faces detected.") } } let handler = VNImageRequestHandler(cgImage: cgImage, options: [:]) do { try handler.perform([request]) // This line causes the error. } catch { print("Failed to perform Vision request: \(error)") } } } The error occurs on this line: try handler.perform([request]) Details: This code runs fine in a normal Xcode project (.xcodeproj). I'm using an App Playground instead (.swiftpm). The image is being included in the .xcassets folder. Is there any way I can mitigate this issue? Please do not recommend switching to .xcodeproj, as I am making a submission for Apple's Swift Student Challenge, and they require that I use .swiftpm.
1
0
181
2w
Playground created on iPad not opening on Mac
I've entered some code on Version 4.5.1 of Playgrounds on my iPad mini running iOS 18.1.1. The file is saved on iCloud and so is available on my Mac running OS 15.2. However, the file will not open in either Playgrounds v 4.5.1 on the Mac (error "Unable to get file wrapper for contents.xcplayground.") or Xcode 16.1 (error "Couldn't load settings from contents.xcplayground"). The file's type is .playgorund. Any thoughts?
1
0
203
4d
Swift Playgrounds Incompatibility with Xcode 16 Files and Swift Versions
I'm facing an issue with Swift Playgrounds and files created in Xcode 16. It seems that Swift Playgrounds does not support Swift 6, but even when I create files specifically with Swift 5, Swift Playgrounds still reports that the files are unsupported. This creates a significant problem because macOS Sequoia does not allow me to revert to Xcode 15, which might have offered better compatibility. As it stands, I can't find a solution to work seamlessly between Xcode and Swift Playgrounds. Has anyone else encountered this issue? Are there any workarounds or updates planned to address this compatibility gap? Any advice would be greatly appreciated!
3
0
269
4d
Why am I unable to render .dae file in Playground?
This is my code in ContentView: import SwiftUI import SceneKit import PlaygroundSupport struct ContentView: View { var body: some View { VStack { Text("SceneKit with SwiftUI") .font(.headline) .padding() SceneView( scene: loadScene(), options: [.autoenablesDefaultLighting, .allowsCameraControl] ) .frame(width: 400, height: 400) .border(Color.gray, width: 1) } } } func loadScene() -> SCNScene? { if let fileURL = Bundle.main.url(forResource: "a", withExtension: "dae") { do { let scene = try SCNScene(url: fileURL, options: [ SCNSceneSource.LoadingOption.checkConsistency: true ]) print("Scene loaded successfully.") return scene } catch { print("Error loading scene: \(error.localizedDescription)") } } else { print("Error: Unable to locate a.dae in Resources.") } return nil } a.dae file exists in the Resources section of macOS Playground app. And a.dae can be viewed in Xcode. Console shows: Error loading scene: The operation couldn’t be completed. (Foundation._GenericObjCError error 0.) Any input is appreciated.
1
0
184
3w
Application "help" menu does not open main help book page
Following the official documentation, I'm trying to create a set of three localised Help Books. The Help Books should be available in Spanish, English and Polish. Presently, I'm trying to complete English version. App Structure This is the plugin application consisting of main app and the plugin. The main app structure would looks as follows: Files . <XcodeProject Top> ├── Localizable.xcstrings ├── MyAppExtension │   ├── MyAppExtension.swift │   └── <other swift files>.swift ├──MyApp │ ├── Info.plist │   ├── +Array.swift │   ├── +ButtonStyle.swift │   ├── <other app swift files>.swift ├── Resources     └── MyApp.help └── MyApp.help └── Contents ├── Info.plist └── Resources ├── English.lproj │   ├── ExactMatch.plist │   ├── InfoPlist.strings │   ├── MyApp.helpindex │   ├── MyApp.html │   └── pgs └── shrd MyApp / MyApp.help / Info.plist file Consists the following values: Bundle name: MyApp HPDBookAccessPath: MyApp.html HPDBookTitle: My App Help Default localization: en_gb MyApp / Info.plist file Contains the following entries: Help Book directory name: MyApp.help Help Book Identifier: MyApp Help Build phase The Copy Bundle Resources copies MyApp.help in MyApp/Resources. Questions Is the provided folder structure valid for creating a localised help books Is there anything that is missing from across Info.plist files or is in the wrong places? Why the MyApp -> Help opens the main help menu, not the app help
3
0
257
3w
(Swift Playgrounds for Mac) How do I build for iPhone?
I am trying to challenge myself by building an iPhone app in Swift Playgrounds. I just got a Mac Mini M4, so I am trying Swift Playgrounds for Mac. I can submit a build to TestFlight, but it's for Mac and won't let me even test on my iPhone. How can I build my app for iPhone? The least "hacky" answer is appreciated, but I'll entertain anything. Examples in order of hackiness might be: I missed an option in Playgrounds for Mac. Open my playground in Xcode, change something, open back in Playgrounds for Mac. Same as above, but maybe I have to add/copy a file (e.g., manifest.plist). Open my playground in Playgrounds for iPad (I have an iPad 9), do something, copy back to Playgrounds for Mac. Submit via Playgrounds for iPad. Submit via Xcode. BTW, I'm challenging myself not just for its own sake, but because I'm curious how far a student/newbie/etc can take Swift Playgrounds. I was hoping to set a good example for others looking for a lighter-resource, more user-friendly experience than Xcode (which is why I assume Playgrounds for Mac even exists). But right now it feels like Playgrounds for Mac is a … third-class citizen? Maybe the iPad version is second-class? I haven't really tried it. P.S. Why does this text editor seem so lame? Making a numbered list and adding to it is a nightmare. Should be: start a numbered list, hit return, new number appears below. I'm on Safari. This should be the best dev experience on the planet, not the worst!
1
0
280
Dec ’24
Missing EnvironmentObject from TableColumn on macOS
I encountered a strange behavior that reminded me of when .sheet() modifiers didn't inherit environment objects. Unless I'm missing something very obvious, it seems to me that TableColumn may expose the same issue. At least on macOS, because the very same code does not crash on iOS. I'm posting this here before reporting a SwiftUI bug. Below is a gist for a playground: https://gist.github.com/keeshux/4a963cdebb1b577b87b08660ce9d3364 I also observe inconsistent behavior when building with Xcode 16.1 or 15.4, specifically: https://github.com/passepartoutvpn/passepartout/issues/872#issuecomment-2477687967 The workaround I resorted to is re-propagating the environment from the parent: https://github.com/passepartoutvpn/passepartout/pull/873/files#diff-c662c4607f2adfd0d4e2c2a225e0351ba9c21dbdd5fc68f23bc1ce28a20bce4dR45
1
0
297
Nov ’24