Post not yet marked as solved
I have this sample code:
struct Item: Identifiable {
var id = UUID()
var name: String?
}
struct ItemTable: View {
let items: [Item]
var body: some View {
Table(items) {
TableColumn("Name", value: \.name)
}
}
}
I get the following error:
Key path value type 'String?' cannot be converted to contextual type 'String'
I can solve using \.name!, but I'd like to give a default value instead (something like \.name ?? "default").
How can I achieve this?
Post not yet marked as solved
I'm trying to make SignInWithAppleButton.Style black when colorScheme is light and white when colorScheme is dark, but .signInWithAppleButtonStyle modifier doesn't update the view when colorScheme change.
struct LogInView: View {
@Environment(\.colorScheme) private var colorScheme
var body: some View {
SignInWithAppleButton { _ in } onCompletion: { _ in }
.signInWithAppleButtonStyle(colorScheme == .light ? .black : .white)
.frame(height: 50)
.padding()
}
}
This can be seen in build and in preview:
struct LogInView_Previews: PreviewProvider {
static var previews: some View {
Group {
LogInView()
LogInView()
.preferredColorScheme(.dark)
}
}
}
Post not yet marked as solved
I have a view that is updated asynchronously when it appears. It is updated via a publisher that gets some data and displays them
I put this view inside a ForEach wrapped in a List (but is the same as in a ScrollView).
The problem is as long as I scroll, the child views that appear are not updated until I release the finger from the screen.
I found a solution (following this question - https://stackoverflow.com/questions/4109898/my-custom-ui-elements-are-not-being-updated-while-uiscrollview-is-scrolled) putting inside the ObservableObject this queue:
private let queue = DispatchQueue(label: "datafetching.queue", attributes: .concurrent)
and adding .receive(on: queue) to the publisher.
It seems to work but when I do this SwiftUI complies saying:
Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates. As expected, some child views started updating wrongly.
Post not yet marked as solved
It is now possible to create scrolling pages of content using tabViewStyle() in SwiftUI.
By default, pages are presented horizontally.
How can I show them vertically?