Post

Replies

Boosts

Views

Activity

Reply to CoreData works iOS not macOS
Since you haven't included the compilation error, I pasted your code snippet into a new CoreData mac project. First issue seems to be the way you have declared the FetchedResults, but perhaps that is just a typo or forum display issue. This is what you want@FetchRequest(entity: Account.entity(), sortDescriptors: []) var account: FetchedResults<Account>Or you can drop the entity parameter if you want, since it can be automatically inferred for you@FetchRequest(sortDescriptors: []) var account: FetchedResults<Account>If you are autogenerating the Swift classes for your model entities, sometimes Xcode will be confused and report "Use of undeclared type" but the issue normally resolves with a clean build, ensuring that your classes are regenerated. If not, try switching to manually creating and adding the NSManagedObject subclasses for your entities.Finally there is one runtime gotcha that might be the actual problem you are referring to since you mention that your project works on iOS. If you launch the application and you see this type of log message[error] error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'Account' so +entity is confused. Have you loaded your NSManagedObjectModel yet ?This is a problem in the Xcode SwiftUI CoreData project template for mac, where the SwiftUI view gets created before the model has been loaded. This is due to the persistentContainer, which will load the model, being a lazy property of the app delegate meaning that it's not created until first accessed. Easiest fix if you want to keep it lazy is to split up this part// Create the SwiftUI view and set the context as the value for the managedObjectContext environment keyPath. // Add `@Environment(\.managedObjectContext)` in the views that will need the context. let contentView = ContentView().environment(\.managedObjectContext, persistentContainer.viewContext)into// Create the SwiftUI view and set the context as the value for the managedObjectContext environment keyPath. // Add `@Environment(\.managedObjectContext)` in the views that will need the context. let context = persistentContainer.viewContext let contentView = ContentView().environment(\.managedObjectContext, context)which will ensure that the model is loaded before the view is created, and is similar to what the iOS project template looks like which might explain the different experience
Feb ’20
Reply to Update View every second does not work
The article you reference is slightly outdated and the publisher in the ObservableObject protocol has been renamed from willChange to objectWillChange so you'll have to start by making that change to your TimerWrapper class for SwiftUI to properly react to changesIt also looks like the timer that you pass to your view is never actually startedMultiplayerBar() .shadow(radius: 5) .environmentObject(TimerWrapper()) // start(withTimeInterval:) not calledIf the MultiplayerBar is a subview of your rootView then you could pass the timer created and started in your SceneDelegate as an environmentObject to the rootView and it will be made available to all interested subviews. Or you need to start the timer you pass to the MultiplayerBar above.If the timer is only going to be used to update this particular view, another alternative would be to skip the wrapper althogether and just create a timer locally and make the code a bit more explicit, for exampleimport SwiftUI struct MultiplayerBar: View { @State var player1Progress = 0.0 let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() var body: some View { GeometryReader { geometryReader in Rectangle() .fill(Color.green) Rectangle() .fill(Color.red) .frame(width: player1Progress * geometryReader.size.width)) .animation(.easeIn) } .frame(height: 200) .cornerRadius(15) .padding() .onReceive(timer) { _ in self.player1Progress = getPlayer1Progress() } } } struct MultiplayerBar_Previews: PreviewProvider { static var previews: some View { MultiplayerBar() } }
Feb ’20
Reply to TextField not updating
Instead of introducing the additional stringValue state property and trying to keep that in sync with the doubleValue binding you can utilize the doubleValue binding directly in the textfield with a formatter to do the double to string conversionTextField("0.00", value: $doubleValue, formatter: NumberFormatter())
Mar ’20
Reply to @EnvironmentObject not updating Home View when @Published var is changed by a function
It looks like your button that starts the motion capture does so on a new separate instance of UserData,rather than on the EnvironmentObject that you have bound your UI elements to.Button(action:{ UserData().manageMotionCapture() })So you might want to try changing that action to the following, and hopefully that will get your UI updating correctly.Button(action:{ self.userData.manageMotionCapture() })
Apr ’20