Post

Replies

Boosts

Views

Activity

how can I discern which SwiftData object trigger the .NSManagedObjectContextDidSave notification ?
Presently, I am encountering an issue with SwiftData. For instance, I have a SwiftData class Ledger that encompasses an array of SingleTransaction, which is also a SwiftData class. Here is the question: when I save a Ledger, how can I discern that the .NSManagedObjectContextDidSave notification was triggered by saving the Ledger and not by saving a SingleTransaction? This distinction is crucial to circumvent unnecessary updates. I attempted the following syntax, but Xcode indicates that Cast from NSManagedObject to unrelated type Ledger always fails. List {...} .onReceive( NotificationCenter .default .publisher(for: .NSManagedObjectContextDidSave) .receive(on: DispatchQueue.main), perform: { notification in if let userInfo = notification.userInfo, let updatedObjects = userInfo[NSUpdatedObjectsKey] as? Set<NSManagedObject> { if updatedObjects.contains(where: { $0 is Ledger }) { fetchLedgers() } } } ) What can I do?
3
0
1k
Oct ’24
Does SwiftData currently supports data sharing among multiple users through iCloud?
Currently, I am planning to add a new feature to my app that allows multiple users to collaboratively manage a single legder. Initially, I chose SwiftData with iCloud for development, so I wanted to inquire whether SwiftData currently supports data sharing among multiple users through iCloud. If it does not, should I transition entirely to Core Data, or is it feasible to allow Core Data and SwiftData to work together?
1
0
1.2k
Oct ’24
Does `TipKit` not available right now?
I want to try the new TipKit features but when I import it from project, Xcode told me that No such module 'TipKit' and I also try the following code to create a tip directly but it doesn't work and Xcode shows Cannot find type 'Tip'. What can I do to use the new feature? // MARK: Tips define here. struct bookmarkTip: Tip { }
2
0
2.1k
Aug ’23
What does the console output mean when using EventKitUI on Xcode 15.0 beta
I create a simple app to enable user add an agenda to their calendar. I create an EventEditViewController which can be used in my SwiftUI app. When I tap the save button appeared on the sheet, the agenda can be saved to calendar but comes with some Xcode console output: redactedMimicSaveEvent: could not find corresponding remoteUI user committed permanent objectID for hostTempID[x-apple-eventkit:///Alarm/t6]. initialTempID[x-apple-eventkit:///Alarm/t5] What does that mean?
0
0
613
Jun ’23
Why does the .png file I export from View(SwiftUI) not contain the ImageView?
This is a macOS app, while using Image("my file url") in my CoverImageView, I can export a .png file that contains the Image("my file url") view successfully. However, this doesn't happen when I use Image(nsImage: NSImage(data: imageData.imagedata)), the imageData.imagedata is a Data type that will be gotten when I tap a button and get it from a selected picture (I will show it later in my code). When I select an image, it can be seen in my app's View. However, when I save this View(CoverImageView) to a .png file it only contain the blue view, the Mac view is gone!!!! Here is the CoverImageView from which I want to create a .png file struct CoverImageView: View { @EnvironmentObject var imageData: ImageData var body: some View { ZStack { Rectangle() .frame(width: 512, height: 600) .foregroundColor(.blue) Image(nsImage: (NSImage(data: imageData.imagedata) ?? NSImage(byReferencing: URL(fileURLWithPath: "")))) .resizable() .aspectRatio(contentMode: .fit) .frame(width: 512, height: 512) } } } This is the main view PhotoTestView struct PhotoTestView: View { @State private var imageUrl: URL = URL(fileURLWithPath: "") @EnvironmentObject var imageData: ImageData var body: some View { VStack { CoverImageView() Divider() .frame(width: 1024) HStack { Button(action: { if let openURL = ImageProcess().showOpenPanel() { imageUrl = openURL if let codedImages = try? Data(contentsOf: openURL) { imageData.imagedata = codedImages } } }, label: { Image(systemName: "doc.badge.plus") }) Button(action: { ImageProcess().saveImage() }, label: { Image(systemName: "square.and.arrow.down") }) }.padding() } } } The View Extension which will create a png file from a view extension View { func imageRepresentation(rect: CGRect) -&gt; NSBitmapImageRep? { let hosting = NSHostingView(rootView: self) hosting.setFrameSize(rect.size) hosting.setBoundsSize(rect.size) hosting.layout() hosting.layerContentsRedrawPolicy = .onSetNeedsDisplay hosting.setNeedsDisplay(rect) if let imageRepresentation = hosting.bitmapImageRepForCachingDisplay(in: rect) { hosting.cacheDisplay(in: rect, to: imageRepresentation) return imageRepresentation } return nil } func asImage(rect: CGRect) -&gt; NSImage? { if let cgImage = imageRepresentation(rect: rect)?.cgImage { return NSImage(cgImage: cgImage, size: rect.size) } return nil } func asPngData(rect: CGRect) -&gt; Data? { return imageRepresentation(rect: rect)?.representation(using: .png, properties: [:]) } } png File Reader and Save struct ImageProcess { func showOpenPanel() -&gt; URL? { let openPanel = NSOpenPanel() openPanel.allowedContentTypes = [.image] openPanel.allowsMultipleSelection = false openPanel.canChooseDirectories = false openPanel.canChooseFiles = true let response = openPanel.runModal() return response == .OK ? openPanel.url : nil } func saveURL() -&gt; URL? { let savePanel = NSSavePanel() savePanel.allowedContentTypes = [.png] savePanel.canCreateDirectories = true savePanel.isExtensionHidden = false savePanel.allowsOtherFileTypes = false savePanel.title = "Save your image" savePanel.message = "Choose a folder and a name to store your image." savePanel.nameFieldLabel = "File name:" let response = savePanel.runModal() return response == .OK ? savePanel.url : nil } func saveImage() { let view = CoverImageView().environmentObject(ImageData()) let imageData = view.asPngData(rect: CGRect.init(x: 0, y: 0, width: 1024, height: 768)) if let url = saveURL() { try? imageData!.write(to: url) } // print(imageData) } } Could you help me?
0
0
1.3k
Feb ’22
how can I discern which SwiftData object trigger the .NSManagedObjectContextDidSave notification ?
Presently, I am encountering an issue with SwiftData. For instance, I have a SwiftData class Ledger that encompasses an array of SingleTransaction, which is also a SwiftData class. Here is the question: when I save a Ledger, how can I discern that the .NSManagedObjectContextDidSave notification was triggered by saving the Ledger and not by saving a SingleTransaction? This distinction is crucial to circumvent unnecessary updates. I attempted the following syntax, but Xcode indicates that Cast from NSManagedObject to unrelated type Ledger always fails. List {...} .onReceive( NotificationCenter .default .publisher(for: .NSManagedObjectContextDidSave) .receive(on: DispatchQueue.main), perform: { notification in if let userInfo = notification.userInfo, let updatedObjects = userInfo[NSUpdatedObjectsKey] as? Set<NSManagedObject> { if updatedObjects.contains(where: { $0 is Ledger }) { fetchLedgers() } } } ) What can I do?
Replies
3
Boosts
0
Views
1k
Activity
Oct ’24
Does SwiftData currently supports data sharing among multiple users through iCloud?
Currently, I am planning to add a new feature to my app that allows multiple users to collaboratively manage a single legder. Initially, I chose SwiftData with iCloud for development, so I wanted to inquire whether SwiftData currently supports data sharing among multiple users through iCloud. If it does not, should I transition entirely to Core Data, or is it feasible to allow Core Data and SwiftData to work together?
Replies
1
Boosts
0
Views
1.2k
Activity
Oct ’24
Does `TipKit` not available right now?
I want to try the new TipKit features but when I import it from project, Xcode told me that No such module 'TipKit' and I also try the following code to create a tip directly but it doesn't work and Xcode shows Cannot find type 'Tip'. What can I do to use the new feature? // MARK: Tips define here. struct bookmarkTip: Tip { }
Replies
2
Boosts
0
Views
2.1k
Activity
Aug ’23
What does the console output mean when using EventKitUI on Xcode 15.0 beta
I create a simple app to enable user add an agenda to their calendar. I create an EventEditViewController which can be used in my SwiftUI app. When I tap the save button appeared on the sheet, the agenda can be saved to calendar but comes with some Xcode console output: redactedMimicSaveEvent: could not find corresponding remoteUI user committed permanent objectID for hostTempID[x-apple-eventkit:///Alarm/t6]. initialTempID[x-apple-eventkit:///Alarm/t5] What does that mean?
Replies
0
Boosts
0
Views
613
Activity
Jun ’23
How to check the API availability for stored property in Xcode 14?
The following grammar is no longer available in Xcode 14, but the Store property is only available in macOS 12.0 +, my app also supports macOS 11.0, and now the compile is error, how can I solve this problem? struct PayButton: View {     @available(macOS 12.0, *)     @EnvironmentObject var store: Store ... }
Replies
0
Boosts
0
Views
897
Activity
Sep ’22
How can I use Live Text API in SwiftUI
Could anyone here tell me how I can use Live Text API in SwiftUI ? The code provide uses UIKit and not complete, the code is for existing project, but I want to develop a new SwiftUI project, so I have no idea how to implement it.
Replies
2
Boosts
0
Views
2.5k
Activity
Jun ’22
Why does the .png file I export from View(SwiftUI) not contain the ImageView?
This is a macOS app, while using Image("my file url") in my CoverImageView, I can export a .png file that contains the Image("my file url") view successfully. However, this doesn't happen when I use Image(nsImage: NSImage(data: imageData.imagedata)), the imageData.imagedata is a Data type that will be gotten when I tap a button and get it from a selected picture (I will show it later in my code). When I select an image, it can be seen in my app's View. However, when I save this View(CoverImageView) to a .png file it only contain the blue view, the Mac view is gone!!!! Here is the CoverImageView from which I want to create a .png file struct CoverImageView: View { @EnvironmentObject var imageData: ImageData var body: some View { ZStack { Rectangle() .frame(width: 512, height: 600) .foregroundColor(.blue) Image(nsImage: (NSImage(data: imageData.imagedata) ?? NSImage(byReferencing: URL(fileURLWithPath: "")))) .resizable() .aspectRatio(contentMode: .fit) .frame(width: 512, height: 512) } } } This is the main view PhotoTestView struct PhotoTestView: View { @State private var imageUrl: URL = URL(fileURLWithPath: "") @EnvironmentObject var imageData: ImageData var body: some View { VStack { CoverImageView() Divider() .frame(width: 1024) HStack { Button(action: { if let openURL = ImageProcess().showOpenPanel() { imageUrl = openURL if let codedImages = try? Data(contentsOf: openURL) { imageData.imagedata = codedImages } } }, label: { Image(systemName: "doc.badge.plus") }) Button(action: { ImageProcess().saveImage() }, label: { Image(systemName: "square.and.arrow.down") }) }.padding() } } } The View Extension which will create a png file from a view extension View { func imageRepresentation(rect: CGRect) -&gt; NSBitmapImageRep? { let hosting = NSHostingView(rootView: self) hosting.setFrameSize(rect.size) hosting.setBoundsSize(rect.size) hosting.layout() hosting.layerContentsRedrawPolicy = .onSetNeedsDisplay hosting.setNeedsDisplay(rect) if let imageRepresentation = hosting.bitmapImageRepForCachingDisplay(in: rect) { hosting.cacheDisplay(in: rect, to: imageRepresentation) return imageRepresentation } return nil } func asImage(rect: CGRect) -&gt; NSImage? { if let cgImage = imageRepresentation(rect: rect)?.cgImage { return NSImage(cgImage: cgImage, size: rect.size) } return nil } func asPngData(rect: CGRect) -&gt; Data? { return imageRepresentation(rect: rect)?.representation(using: .png, properties: [:]) } } png File Reader and Save struct ImageProcess { func showOpenPanel() -&gt; URL? { let openPanel = NSOpenPanel() openPanel.allowedContentTypes = [.image] openPanel.allowsMultipleSelection = false openPanel.canChooseDirectories = false openPanel.canChooseFiles = true let response = openPanel.runModal() return response == .OK ? openPanel.url : nil } func saveURL() -&gt; URL? { let savePanel = NSSavePanel() savePanel.allowedContentTypes = [.png] savePanel.canCreateDirectories = true savePanel.isExtensionHidden = false savePanel.allowsOtherFileTypes = false savePanel.title = "Save your image" savePanel.message = "Choose a folder and a name to store your image." savePanel.nameFieldLabel = "File name:" let response = savePanel.runModal() return response == .OK ? savePanel.url : nil } func saveImage() { let view = CoverImageView().environmentObject(ImageData()) let imageData = view.asPngData(rect: CGRect.init(x: 0, y: 0, width: 1024, height: 768)) if let url = saveURL() { try? imageData!.write(to: url) } // print(imageData) } } Could you help me?
Replies
0
Boosts
0
Views
1.3k
Activity
Feb ’22