swiftui

What is the difference between @EnvironmentObject and @ObservedObject?

扶醉桌前 提交于 2021-02-08 15:12:37
问题 I have been reading about the property wrappers in SwiftUI and I see that they do a great job, but one thing which I really don't get is the difference between @EnvironmentObject and @ObservedObject . From what I learned so far, I see that @EnvironmentObject is used when we have an object that is needed in various places in our app but we don't need to pass it through all of them. For example if we have hierarchy A -> B -> C -> D and the object is created at A, it is saved in the environment

What is the difference between @EnvironmentObject and @ObservedObject?

丶灬走出姿态 提交于 2021-02-08 15:06:00
问题 I have been reading about the property wrappers in SwiftUI and I see that they do a great job, but one thing which I really don't get is the difference between @EnvironmentObject and @ObservedObject . From what I learned so far, I see that @EnvironmentObject is used when we have an object that is needed in various places in our app but we don't need to pass it through all of them. For example if we have hierarchy A -> B -> C -> D and the object is created at A, it is saved in the environment

Xcode 12 & SwiftUI: Cannot preview in this file — Failed to update preview

徘徊边缘 提交于 2021-02-08 13:38:38
问题 Seems like previews stopped working on Xcode 12! Trying to preview the SwiftUI file template and getting the error below. Any ideas how to fix this? Tried cleaning the build folder, deleting derived data and restarting Xcode to no avail! On Catalina 10.15.6. RemoteHumanReadableError: Failed to update preview. The preview process appears to have crashed. Error encountered when sending 'prepare' message to agent. ================================== | RemoteHumanReadableError: The operation

Performance Issue with SwiftUI List

时光总嘲笑我的痴心妄想 提交于 2021-02-08 11:34:32
问题 I have a List with displays the result of a dynamic FetchRequest. The code works fine, but having a bigger result set (e.g. 3000), the list is built quite slowly when the dynamic predicate changed. struct ItemList: View { @State var startsWith: String = "A" var body: some View { NavigationView { VStack { TextField("Startswith", text:$startsWith) FilterRequestList(filter: startsWith) } .navigationBarTitle("Tasks CD") } } } struct FilterRequestList: View { var fetchRequest: FetchRequest<Item>

Update State-variable Whenever CoreData is Updated in SwiftUI

淺唱寂寞╮ 提交于 2021-02-08 11:18:19
问题 After updating some data from CoreData, I also want to update a State -variable to the number of returned results. When CoreData is changed, the Stepper should always be set to the number of returned results. However, onAppear fires also when I use the Stepper . How can I check in onAppear whether CoreData was changed or the Stepper was used? Is that even possible? import SwiftUI struct ContentView: View { @State var numberOfResults = 0 @FetchRequest(entity: YourModel.entity(),

Adding headers to OutlineGroup / DisclosureGroup

跟風遠走 提交于 2021-02-08 10:47:39
问题 I'm using SwiftUI 2.0, and I need a way to add a header to an OutlineGroup or DisclosureGroup , similar to headers in List sections. Is there a way to do this? 回答1: You can use separate Section s to create the headers (and, optionally, footers). import SwiftUI struct Node : Identifiable { let id = UUID() let name: String let children: [Node]? } struct SectionedOutlineView: View { var body: some View { List(selection: $selection) { Section(header: Label("Nodes 1", systemImage: "sparkle")) {

SwiftUI + Combine, using Models and ViewModels together

我的未来我决定 提交于 2021-02-08 10:18:17
问题 I'm very new to Swift and I am currently trying to learn by building a rent splitting app with SwiftUI + Combine. I want to follow the MVVM pattern and am trying to implement this. At the moment I have the following Model, ViewModel and View files: Model: import Foundation import Combine struct InputAmounts { var myMonthlyIncome : Double var housemateMonthlyIncome : Double var totalRent : Double } ViewModel (where I have attempted to use the data from the Model to conform to the MVVM pattern,

How to automatically collapse DatePicker in a form when other field is being edited?

我的未来我决定 提交于 2021-02-08 09:33:18
问题 I got a simple SwiftUI view: import SwiftUI struct AddItemView: View { @State private var title = "" @State private var date = Date() var body: some View { Form { Section { TextField("Title", text: $title) DatePicker( selection: $date, in: Date()..., displayedComponents: .date, label: { Text("Date") } ) } } } } struct AddItemView_Previews: PreviewProvider { static var previews: some View { AddItemView() } } I am trying to achieve the following: If DatePicker is expanded (user tapped date

How do I disable the Show Tab Bar menu option in SwiftUI

南笙酒味 提交于 2021-02-08 09:24:07
问题 I have created a very simple app on MacOS created with SwiftUI. Default there is a menu item show tab bar . How do I remove this? I doesn't make sense to have tabs in this app. I've found the following answering the same question, but for older versions of Swift, not for SwiftUI: How do I disable the Show Tab Bar menu option in Sierra apps? 回答1: I was looking for an answer for this as well and found out the following: by default - as you already mentioned - the Show/Hide Tab is active: There

SwiftUI: PreferenceKey is not called when parent view is embedded in NavigationView

江枫思渺然 提交于 2021-02-08 09:15:52
问题 DynamicScalingView is a child view with two buttons designed to have equal width & height using preferencekey Issue: When DynamicScalingView is embedded in NavigationView it not longer adapts to intrinsic and increases its frame size. Current implementation without Navigation works fine but would like to understand how to fix this issue when embedded in NavigationView Uncomment NavigationView in sample view to reproduce the issue DynamicScalingView should adapt to Dynamic font size and