SwiftData delete isn't working, when I attempt to delete a model, my app crashes and I get the following error:
SwiftData/PersistentModel.swift:359: Fatal error: Cannot remove My_App.Model2 from relationship Relationship - name: model2, options: [], valueType: Model2, destination: Model2, inverseName: models3, inverseKeypath: Optional(\Model2.models3) on My_App.Model3 because an appropriate default value is not configured.
I get that it's saying I don't have a default value, but why do I need one? Isn't @Relationship .cascade automatically deleting the associated models?
And onto of that, why is the error occurring within the do block, shouldn't it be caught by the catch, and printed?
I have put together a sample project below.
import SwiftUI
import SwiftData
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.modelContainer(for: Model3.self)
}
}
}
@Model
class Model1 {
var name: String
@Relationship(deleteRule: .cascade, inverse: \Model2.model1) var models2: [Model2] = []
init(name: String) {
self.name = name
}
}
@Model
class Model2 {
var name: String
var model1: Model1
@Relationship(deleteRule: .cascade, inverse: \Model3.model2) var models3: [Model3] = []
init(name: String, model1: Model1) {
self.name = name
self.model1 = model1
}
}
@Model
class Model3 {
var name: String
var model2: Model2
init(name: String, model2: Model2) {
self.name = name
self.model2 = model2
}
}
struct ContentView: View {
@Query var models1: [Model1]
@Environment(\.modelContext) var modelContext
var body: some View {
NavigationStack {
List(models1) { model1 in
Text(model1.name)
.swipeActions {
Button("Delete", systemImage: "trash", role: .destructive) {
modelContext.delete(model1)
do {
try modelContext.save()
//SwiftData/PersistentModel.swift:359: Fatal error: Cannot remove My_App.Model2 from relationship Relationship - name: model2, options: [], valueType: Model2, destination: Model2, inverseName: models3, inverseKeypath: Optional(\Model2.models3) on My_App.Model3 because an appropriate default value is not configured.
} catch {
print(error.localizedDescription)
}
}
}
}
.toolbar {
Button("Insert", systemImage: "plus") {
modelContext.insert(Model3(name: "model3", model2: Model2(name: "model2", model1: Model1(name: "model1"))))
}
}
}
}
}
Swift Student Challenge
RSS for tagAsk questions and connect with other challenge applicants.
Posts under Swift Student Challenge tag
103 Posts
Sort by:
Post
Replies
Boosts
Views
Activity
Good day, I want to make an application to use which will need a second person. he plays an important role and affects the operation of the application
Will such an app be eligible for Swift Student Challenge. Am I correct in understanding that if yes, I need to mention it in the application?
Last year's submission form says that if you select (Run on Xcode) the app will be in the simulator not on an iPad.
Has that changed this year, as I don't see it in the terms?
(My app has imported local package dependencies for Reality Composer Pro that I am not certain would work on playgrounds for iOS. And it needs the iPad camera for AR functionality.)
Is a student from a postgraduate course eligible to participate in the Apple Swift Student Challenge?
Hi, are we allowed to push the default support in Package.swift up to iOS 18 to allow for the latest APIs?
And with the terms of the competition, can we use stock 3D USDZ assets?
Thank you!
Trying to add a Reality Composer Pro project into my swift playground application. Can't figure out what name to call for the package.
Hey everyone,
I am wondering whether for the swift student challenge I should use mock data (e.g. achievements, tracking data etc.) or real data considering that one of the requirements states that I'll submit a playground with an interactive scene that can be experienced within 3 minutes.
Thanks in advance!
For the swift student challenge I was hoping to use swift data, I found that since it's a playground app, in package.swift the defaults is set to iOS 16 which means you can't use swift data. I changed it to iOS 17 and everything works but I want to know if that goes against the rules in anyway, changing th bios version in package.swift? This was the code I changed in package.swift.
let package = Package(
name: "ProStepper",
platforms: [
.iOS("17.0")
],
I am building an app playground for SSC'25 where I want to use Multipeer Connectivity framework that would allow me to send and receive data to and from stranger devices. I also want to use some other open-source packages for some of the features. I just wanted to know if we are allowed to use or not?
Hello,
I'm exploring the Secure Enclave APIs, and I'm wondering if it's possible to "cryptographically" determine if a block of data was signed on the Secure Enclave.
When I sign a block of data using the Secure Enclave (which implies using a key pair automatically generated by the enclave) and distribute the public key to others, is there any way to verify if the message was encrypted on it / its private key was generated by it? In other words, what I'm trying to achieve is to make sure that the public key hasn't been tampered with until it reaches its destination (including on-device threats, since otherwise I could've used a normal keychain item, perhaps?).
For the purpose of this example, I'm not necessarily interested in figuring out if the key was signed on a certain device's enclave, but rather on any Secure Enclave. So, using something derived from the enclave's GID Key (described in the Apple Platform Security guide) would work for this.
Pretty much what the title says, I received the email from Apple today regarding the three sessions that are being conducted for participants. When I open the link, I'm shown the button to register, but when I click register, I'm asked to log in, following which I'm redirected back to the same page except the register button is now missing. I'm unsure if it has registered me, and I haven't received any email confirming the same.
Iām working in the app playground and want to add my usdz file but when i drag drop the file to my main folder i cannot add target to it which leads to a resource not found error while I build my app. It was working on a normal xcode project but while transitioning to app playground it is not working. How can I fix this issue?
Hi everyone,
Iām working on my submission for the Swift Student Challenge and need some advice. I already have an existing app thatās live on the App Store, and Iād like to use the same app for my submission.
However, while creating the App Playground, I noticed that Iām unable to create additional targets. This is problematic because my app relies heavily on specific targets like ShieldAction, ShieldConfiguration, and DeviceActivityMonitor. Without these targets, the appās core functionality wonāt work as intended.
Is there a known limitation or workaround for this? How can I submit my appās functionality within the constraints of the App Playground?
Thanks in advance for your help!
Iām working on an Xcode App Playground project (.swiftpm) and trying to add a .mlmodel file (e.g., Exercises.mlmodel) to it. However, when I add the .mlmodel file to my project, I encounter the following error:
Exercises.mlmodel: No predominant language detected. Set COREML_CODEGEN_LANGUAGE to preferred language.
The .mlmodel file works perfectly fine when used in a regular Xcode project (.xcodeproj), but this issue occurs as soon as I add the file in an App Playground project (.swiftpm).
Steps Iāve Tried:
Ensuring the .mlmodel file is correctly added to the project folder.
Checking the generated Core ML Swift code in a .xcodeproj environmentāworks as expected.
Searching for a way to explicitly set COREML_CODEGEN_LANGUAGE in a .swiftpm project, but it seems that Xcode does not provide this option.
Any advice or solutions would be greatly appreciated!
I have tried the solutions in these articles, however none of them worked:
https://medium.com/@sofiadinizms/how-to-use-coreml-in-swift-playgrounds-8d5f001c5d15
https://developer.apple.com/forums/thread/743942?answerId=776359022#776359022
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! :)
I really want to participate in the upcoming Swift Student Challenge, but I'm using a MacBook that cannot update past MacOS Big Sur. Would submissions compiled from it still be eligible for SSC?
Thank you! :D
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.
I am currently developing my submission for the SSC and I noticed that the swift playgrounds app for iOS does not have support for the new swift ui features in iOS 18. Are we allowed to submit apps that use these iOS 18 only features?
As predictive code completion is natively included in Xcode, are we allowed to use it for the Swift Student Challenge?
I can't seem to access preview for any of my views in any of my swift projects at all (using Mac to code and using iPhone 16 pro as preview and simulation, simulation can work), even after restarting Mac and Xcode itself.
The preview section has a popup saying "Cannot preview in this file, unexpected error occured" (for every file), please tell me what is wrong and help me solve it to see the preview, thank you!
details