Post not yet marked as solved
Hi! While working on my Swift Student Challenge submission it seems that I found a race condition (TOCTOU) bug in SwiftUI when using sheets, and I'm not sure if this is expected behaviour or not.
Here's an example code:
import SwiftUI
struct ContentView: View {
@State var myVar: Int?
@State private var presentSheet: Bool = false
var body: some View {
VStack {
// Uncommenting the following Text() view will "fix" the bug (kind of, see a better workaround below).
// Text("The value is \(myVar == nil ? "nil" : "not nil")")
Button {
myVar = nil
} label: {
Text("Set value to nil.")
}
Button {
myVar = 1
presentSheet.toggle()
} label: {
Text("Set value to 1 and open sheet.")
}
}
.sheet(isPresented: $presentSheet, content: {
if myVar == nil {
Text("The value is nil")
.onAppear {
print(myVar) // prints Optional(1)
}
} else {
Text("The value is not nil")
}
})
}
}
When opening the app and pressing the open sheet button, the sheet shows "The value is nil", even though the button sets myVar to 1 before the presentSheet Bool is toggled.
Thankfully, as a workaround to this bug, I found out you can change the sheet's view to this:
.sheet(isPresented: $presentSheet, content: {
if myVar == nil {
Text("The value is nil")
.onAppear {
if myVar != nil {
print("Resetting View (TOCTOU found)")
let mySwap = myVar
myVar = nil
myVar = mySwap
}
}
} else {
Text("The value is not nil")
}
})
This triggers a view refresh by setting the variable to nil and then to its non-nil value again if the TOCTOU is found.
Do you think this is expected behaivor? Should I report a bug for this? This bug also affects .fullScreenCover() and .popover().
Post not yet marked as solved
Hi,
I have the following predicate used in a SwiftData Query:
#Predicate<Item> {
searchText.isEmpty || $0.myarray.contains(where: {
$0.text.localizedStandardContains(searchText)
})
})
Even though this compiles, I get the unsupportedKeyPath error in the logs:
Query encountered an error: SwiftData.SwiftDataError(_error: SwiftData.SwiftDataError._Error.unsupportedKeyPath)
I thought I could use .contains(where:) inside predicates, but this doesn't work. What could be the problem?
Item.myarray is an array of a Codable struct type, and searchText is a String.
Hi! I just read the updated terms and conditions, and I have a question regarding the submission requirements:
Your app playground must be fully functioning, and be built with and run on Swift Playgrounds 4.4 on iPadOS 16 or macOS 13.5 or Xcode 15 on macOS 13.5 or later. You may incorporate the use of Apple Pencil.
This means that the app can be built on iPadOS 17 / macOS 14 as well (and perhaps add some features specific to iPadOS 17 / macOS 14 or later using features such as SwiftData?) but does the app need to be fully compatible with earlier versions as well?
I think this means it should be compatible with all versions from iPadOS 16 / macOS 13.5 onwards?
Post not yet marked as solved
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.
Post not yet marked as solved
I noticed updating a @Transient attribute's value does not refresh SwiftUI views when using a SwiftData model with @Query or @Bindable.
Here is a sample code (clicking the button changes the Transient attribute):
import SwiftUI
import SwiftData
// SwiftUI App struct declaration
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(for: MyModel.self)
}
}
// My SwiftData test model
@Model
class MyModel {
var myFirstArray: [Int]
@Transient var mySecondElement: Bool = false
init(myFirstArray: [Int] = [Int](), mySecondElement: Bool = false) {
self.myFirstArray = myFirstArray
self.mySecondElement = mySecondElement
}
}
// The SwiftUI View
struct ContentView: View {
@Query var myModels: [MyModel]
@Environment(\.modelContext) var modelContext
var body: some View {
VStack {
if myModels.count != 0 {
Text("Model:")
// I expected this to update when the button is clicked
Text("mySecondElement's value: \(myModels[0].mySecondElement ? "True" : "False" )")
Button {
// button that changes the transient attribute
myModels[0].mySecondElement.toggle()
print(myModels[0].mySecondElement) // this prints the actual value
} label: {
Text("Add mySecondElement data")
}
}
}
.onAppear {
try? modelContext.delete(model: MyModel.self)
if myModels.count == 0 {
let model = MyModel(myFirstArray: [0, 1, 3])
modelContext.insert(model)
}
}
}
}
I expected ContentView() to be refreshed when I clicked on the button. However, this seems to not be the case - it seems that attributes marked with @Transient don't publish updates to Views.
Is this expected behaviour? Should I file a bug / feedback for this?
Hello, is the new Journaling Suggestions API available in App Playgrounds (made using Xcode)?
I know I can add capabilities in Xcode, but it seems that the Journaling Suggestions capability isn't available. I assume this API is currently unsupported in App Playgrounds?
Post not yet marked as solved
Hi,
In Xcode 14 I was able to train linear regression models with Create ML using large CSV files (I tested on about 30000 items and 5 features):
However, in Xcode 15 (I tested on 15.0.1 and 15.1), the training continuously stays in the "Processing" state:
When using a dataset with 900 items, everything works fine.
I filed a feedback for this issue: FB13516799.
Does anybody else have this issue / can reproduce it?
Hello! I am wondering on which target will the Swift Student Challenge submissions run on, when choosing the option to run on Xcode 14 on macOS 13.
In Xcode, you can pick multiple simulators and devices as a target for an App Playground. If the submission will run on one of the Mac options, will it run as Designed for iPad or as a Mac Catalyst app?