I filed a bug report. Let's hope this one gets fixed fast 🤞
Post
Replies
Boosts
Views
Activity
This issue continues to exist with Xcode 15.0 Beta 2 and iOS17 Beta 2.
This issue is still present in Xcode 14.3.1.
I can confirm this issue for Xcode Version 15.0 beta 4 (15A5195m).
For me this issue also occurs for arrays of enums in a model.
@Model
final class Entity {
var values: [SomeValue]
init(values: [SomeValues]) {
self.values = values // Fatal error: Illegal attempt to use a Optional(Swift.Mirror.DisplayStyle.enum) as a Property
}
}
enum SomeValue {
case value1
case value2
}
...
let object = Entity(values: [.value1, .value2])
I have submitted a bug report: FB12674261
In case anyone is wondering how to fix this issue I describe what is happening and how I worked around it.
When a user is submitting their input from a searchable view the searchable view will display an empty input field. However, the value bound to the view still has the value, the user submitted, it is just not displayed. This seems to be a bug in SwiftUI. I worked around it by first setting the bound value to an empty String, waiting a short period of time and the setting the value to the previous user input again.
import SwiftUI
struct SomeView: View {
@State private var searchExpression = ""
@State private var temporarySearchExpression = "" // a new state to temporarily hold the search expression
@State private var isPresented = false
init() {
}
var body: some View {
NavigationStack {
VStack {
Text("some view")
.searchable(text: $searchExpression, isPresented: $isPresented)
.onSubmit(of: .search) {
isPresented = false
temporarySearchExpression = searchExpression
searchExpression = ""
// Waiting for a short period of time is necessary.
// It seems SwiftUI needs some time to process the state changes.
DispatchQueue.main.asyncAfter(deadline: .now().advanced(by: DispatchTimeInterval.milliseconds(1))) {
searchExpression = temporarySearchExpression
}
}
}
}
}
}
#Preview {
SomeView()
}
This issue was resolved for me with Xcode 15 beta 7. However, I now have another error when trying to build Previews. See this issue https://developer.apple.com/forums/thread/736152#736152021
I found a workaround in an old thread (https://developer.apple.com/forums/thread/697772) that works for me: To successfully build a preview for a View in a Swift Package, switch to the scheme of the package the view is located in. Thanks to user @derekc00 for pointing it out.
Are you seeing this behavior with views located in your app or located in packages?
I haven’t had this issue since the later Xcode 15 betas. My solution (as described above) is to always switch to a scheme for the app or package the view is located in.
I am having the same iss, which I have reported here https://developer.apple.com/forums//thread/740120.
I figured out a working solution, tested on iOS 17.3 on device. I will copy/paste code snippets from around this forum to compile a short how to.
First, create a model actor, that is used to access swift data:
@ModelActor
actor MyModelActor {
init(container: ModelContainer) {
modelContainer = container
let context = ModelContext(container)
modelExecutor = DefaultSerialModelExecutor(modelContext: context)
}
// example usage
func create() {
let item = Item()
modelContext.insert(item)
try! modelContext.save()
}
}
Snippet copied from https://developer.apple.com/forums/thread/736154 and modified for better readability.
Second, initialize the model actor in a detachted task:
Task.detached {
let actor = MyModelActor()
await actor.create()
}
Information from https://forums.developer.apple.com/forums/thread/736226
As of iOS 17.3 creating the model actor in a detached task is imperative because otherwise it will always execute on the main thread. This seems to be a bug in SwiftData that is covered extensively in the linked forum post. (this one was killing me)
Thanks to @Fat Xu and @rkhamilton for pointing me into the right directions.
I have reported this issue using the feedback assistant at the time I created this thread. I am having trouble creating a minimal example project that reproduces this issue. Igor me, this issue only occurs in my production app. f anyone can provide me an example project, I will attach it to my feedback report For Apple to inspect.