Post

Replies

Boosts

Views

Activity

onReceive(_:perform:) on Frontmost Window Only?
I have a simple document-based application for macOS. struct ContentView: View { @Binding var document: TextDocument var body: some View { .onReceive(NotificationCenter.default.publisher(for: .notificationTextWillAppendSomeTextt), perform: { _ in }) VStack { TextEditor(text: $document.text) } } } extension Notification.Name { static let notificationTextWillAppendSomeTextt = Notification.Name("TextWillAppendSomeText") } Suppose that my application currently has three tabs. If I call a menu command through post(name:object:) this menu command call will affect all three of them. This stackoverflow topic talks about it, too. So how could I tell which window should get a call and others don't? Thanks.
3
0
95
May ’25
Image Miniaturization Issue
I have three toolbar buttons with images from Assets.xcassets. Initially, I didn't use @1x, @2x, @3x sizes. I just put one size (72 x 72) for all of them. It was never a problem till a few days ago. The reviewer has reported numerous issues, which all seem to originate from miniaturized toolbar images. They have given me a screenshot from an iPad. Now, each of the three to the left has shrunken to 4 x 4, according to them. Some lines of code are the following. import SwiftUI struct ContentView: View { var body: some View { NavigationStack { ZStack { VStack { ... ... ... } .background(.brown) .navigationBarTitleDisplayMode(.inline) .navigationBarItems( leading: HStack(content: { Button { } label: { Image("ToolbarImage1") .resizable() .foregroundColor(.red) .aspectRatio(contentMode: .fit) .frame(width: 28) } Button { } label: { Image("ToolbarImage2") .resizable() .foregroundColor(.cyan) .aspectRatio(contentMode: .fit) .frame(width: 28) } Button { } label: { Image("ToolbarImage3") .resizable() .foregroundColor(.gray) .aspectRatio(contentMode: .fit) .frame(width: 28) } }), trailing: HStack(content: { Button { } label: { Text("X") .font(.body) .fontWeight(.semibold) .foregroundStyle(colorScheme == .light ? .white : .black) .frame(width: 28, height: 28) .background { Circle() .fill(!disableGroupMenu ? .green : .green.opacity(0.6)) } } Button { withAnimation(.easeInOut(duration: 0.2)) { showCopyMenu.toggle() manageMenu() } } label: { Text("Y") .font(.body) .fontWeight(.semibold) .foregroundStyle(colorScheme == .light ? .white : .black) .frame(width: 28, height: 28) .background { Circle() .fill(!disableCopyMenu ? .indigo: .indigo.opacity(0.6)) } } }) ) .toolbar { ToolbarItem(placement: .principal) { Text("App name") .bold() .foregroundColor(.white) } } } } } } I don't see this minituralization issue on any of my actual devices (iPhone XR, iPhone 14, iPad 9th gen.) on top of various simulator models including iPad A16 with iOS 26. This is my first iOS submission after iOS 26 was released. I don't know if it has something to do with iOS 26. The reviewer hasn't told me about their iPad model or the iOS version. I have the same app for macOS, which was submitted after macOS 26 was released. And they haven't reported the miniaturization issue after 4 or 5 software updates. If you have any idea as to what's causing it, please let me know. I have submitted a new binary with @3x as a resort. I doubt the issue has been resolved. Thanks. Initally, I've used Xcode 16.4 to built the app. I have tried building it with Xcode 26. And I don't see the minituralization issue on any of the simulator models (iPad mini, iPad A16...).
Topic: UI Frameworks SubTopic: SwiftUI
3
0
120
Oct ’25
List Single Selection
I have some lines of code below where I can make multiple selections. import SwiftUI struct ContentView: View { @State private var selectedUsers: Set<String> = [] @State var users = ["Susan", "Kate", "Natalie", "Kimberly", "Taylor", "Sarah", "Nancy", "Katherine", "Nicole", "Linda", "Jane", "Mary", "Olivia", "Barbara"] var body: some View { VStack { List(selection: $selectedUsers) { ForEach(users, id: \.self) { user in Text(user) } } .environment(\.editMode, .constant(.active)) } } } So the blue selection symbol appears as shown in the screenshot above. That's good. But that's not what I'm after. I just want to select one row at a time. import SwiftUI struct ContentView: View { @State var selectedUser: String? @State var users = ["Susan", "Kate", "Natalie", "Kimberly", "Taylor", "Sarah", "Nancy", "Katherine", "Nicole", "Linda", "Jane", "Mary", "Olivia", "Barbara"] var body: some View { VStack { List(selection: $selectedUser) { ForEach(users, id: \.self) { user in Text(user) } } .environment(\.editMode, .constant(.active)) } } } In the lines of code above, I only let myself select one row at a time. And I don't get the blue selection symbol. I wonder why? I find two or three websites where they have similar lines of code and where they select one row at a time. And they have the blue selection symbol. Why don't I get it? Mucho thankos for reading.
4
0
1.3k
Jul ’23
Heavy Duty Work With Async Await
I am perplexed as to how to use async await. In the following example, I don't use GCD or performSelector(inBackground:with:). The view controller is NSViewController, but it doesn't make any difference if it's NSViewController or UIViewController. import Cocoa class ViewController: NSViewController { func startWriteImages() { Task{ let bool = await startWriteImagesNext() if bool { print("I'm done!") } } } func startWriteImagesNext() async -&gt; Bool { // pictures is a path to a folder in the sandbox folder // appDelegate.defaultFileManager is a variable pointing to FileManager.default in AppDelegate let pictURL = URL(fileURLWithPath: pictures) if let filePaths = try? self.appDelegate.defaultFileManager.contentsOfDirectory(atPath: pictURL.path) { for file in filePaths { let fileURL = pictURL.appending(component: file) if self.appDelegate.defaultFileManager.fileExists(atPath: fileURL.path) { let newURL = self.folderURL.appending(component: file) do { try self.appDelegate.defaultFileManager.copyItem(at: fileURL, to: newURL) } catch { print("Ugghhh...") } } } return true } return false } func startWriteImagesNext2() async -&gt; Bool { let pictURL = URL(fileURLWithPath: pictures) if let filePaths = try? self.appDelegate.defaultFileManager.contentsOfDirectory(atPath: pictURL.path) { DispatchQueue.global().async() { for file in filePaths { let fileURL = pictURL.appending(component: file) if self.appDelegate.defaultFileManager.fileExists(atPath: fileURL.path) { let newURL = self.folderURL.appending(component: file) do { try self.appDelegate.defaultFileManager.copyItem(at: fileURL, to: newURL) } catch { print("Ugghhh...") } } } } return true } return false } } In the code above, I'm saving each file in the folder to user-selected folder (self.folderURL). And the application will execute the print guy only when work is done. Since it's heavy-duty work, I want to use CCD or performSelector(inBackground:with:). If I use the former (startWriteImagesNext2), the application will execute the print guy right at the beginning. I suppose I cannot use GCD with async. So how can I perform heavy-duty work? Muchos thankos.
4
0
1.3k
Feb ’24
The Mystery of an Array of Strings and Escaped Characters
I have a very simple set of lines of code. It doesn't matter whether you run it under UIKit or SwiftUI. In SwiftUI, I have the following. import SwiftUI struct ContentView: View { var body: some View { VStack { Button("Click on me") { let tabLine = "1\tAnthony James\t139.9" var item = "" let tabs = tabLine.components(separatedBy: "\t") for tab in tabs { item += "'\(tab)'" } print("\(item)") } } } } So I have tab-separated values. And I want to separate them and quote each value either with an apostrophe or a double quotation mark. In the case above, I get the following print. '1''Anthony James''139.9' That's exactly what I want. Now, I have an array of three of those guys like the following. import SwiftUI struct ContentView: View { var body: some View { VStack { Button("Click on me") { let tabLine0 = "1\tAnthony James\t139.9" let tabLine1 = "2\tKim Harbaugh\t181.4" let tabLine2 = "3\tAnthony James\t212.4" let tabTextLines = [tabLine0, tabLine1, tabLine2] var strings = [String]() for tabLine in tabTextLines { var item = "" let tabs = tabLine.components(separatedBy: "\t") for tab in tabs { item += "'\(tab)'" } strings.append(item) } print("\(strings)") } } .frame(width: 360, height: 240) } } And I get the following print. This is a nightmare situation. Each value is quoted with an escaped apostrophe. I can't even remove the escapees with replacingOccurrences(of:with:). How does that happen when you have an array of strings? If I try quoting the values with a unicode character, things are the same. Is there a workaround? Muchos thankos.
4
0
533
Aug ’24
Opening FileDocument with URL → should only be called in the main thread
Its document says openDocument can open a document at a specific URL. So I've saved a model as a JSON object with its URL and a bookmark as Data. With its security-scoped bookmark data resolved, I am able to open a document except that the app will crash right after opening a document. Console says should only be called in the main thread struct ContentView: View { @EnvironmentObject var bookmarkViewModel: BookmarkViewModel var body: some View { VStack { } .onAppear { loadBookmarks() } } extension ContentView { func loadBookmarks() { print("1 \(Thread.current)") // NSMainThread Task { for bookmarkItem in bookmarkViewModel.bookmarkItems { // resolving a security-scoped bookmark print("2 \(Thread.current)") // NSMainThread if let _ = resolveBookmark(bookmarkData: bookmarkItem.bookmarkData) { print("3 \(Thread.current)") // NSMainThread do { print("4 \(Thread.current)") // NSMainThread try await openDocument(at: bookmarkItem.bookmarkURL) print("5 \(Thread.current)") // NSMainThread } catch { print("\(error.localizedDescription)") } } } } } } Well, the application is on the main thread. I've checked every line before and after opening a document with its URL. Call what on the main thread? This is confusing. Thanks. class BookmarkViewModel: ObservableObject { @Published var bookmarkItems: [BookmarkItem] = [] var defaultFileManager: FileManager { return FileManager.default } var documentURL: URL? { ... } init() { fetchBookmarkItems() } func fetchBookmarkItems() { bookmarkItems.removeAll() if let documentURL { let bookmarkFolderURL = documentURL.appending(path: "MyApp").appending(path: "Bookmarks") do { let contents = try defaultFileManager.contentsOfDirectory(atPath: bookmarkFolderURL.path) for content in contents { ... let fileURL = bookmarkFolderURL.appending(path: content) let data = try Data(contentsOf: fileURL) let bookmarkItem = try JSONDecoder().decode(BookmarkItem.self, from: data) bookmarkItems.append(bookmarkItem) } } catch { print("Error fetching folder content: \(error.localizedDescription)") } } } } struct BookmarkItem: Codable, Hashable { let bookmarkURL: URL let date: Date let bookmarkData: Data let open: Bool }
4
0
116
May ’25
Projecting a Cube with a Number in ARKit
I'm a novice in RealityKit and ARKit. I'm using ARKit in SwiftUI to show a cube with a number as shown below. import SwiftUI import RealityKit import ARKit struct ContentView : View { var body: some View { return ARViewContainer() } } #Preview { ContentView() } struct ARViewContainer: UIViewRepresentable { typealias UIViewType = ARView func makeUIView(context: UIViewRepresentableContext<ARViewContainer>) -> ARView { let arView = ARView(frame: .zero, cameraMode: .ar, automaticallyConfigureSession: true) arView.enableTapGesture() return arView } func updateUIView(_ uiView: ARView, context: UIViewRepresentableContext<ARViewContainer>) { } } extension ARView { func enableTapGesture() { let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:))) self.addGestureRecognizer(tapGestureRecognizer) } @objc func handleTap(recognizer: UITapGestureRecognizer) { let tapLocation = recognizer.location(in: self) // print("Tap location: \(tapLocation)") guard let rayResult = self.ray(through: tapLocation) else { return } let results = self.raycast(from: tapLocation, allowing: .estimatedPlane, alignment: .any) if let firstResult = results.first { let position = simd_make_float3(firstResult.worldTransform.columns.3) placeObject(at: position) } } func placeObject(at position: SIMD3<Float>) { let mesh = MeshResource.generateBox(size: 0.3) let material = SimpleMaterial(color: UIColor.systemRed, roughness: 0.3, isMetallic: true) let modelEntity = ModelEntity(mesh: mesh, materials: [material]) var unlitMaterial = UnlitMaterial() if let textureResource = generateTextResource(text: "1", textColor: UIColor.white) { unlitMaterial.color = .init(tint: .white, texture: .init(textureResource)) modelEntity.model?.materials = [unlitMaterial] let id = UUID().uuidString modelEntity.name = id modelEntity.transform.scale = [0.3, 0.1, 0.3] modelEntity.generateCollisionShapes(recursive: true) let anchorEntity = AnchorEntity(world: position) anchorEntity.addChild(modelEntity) self.scene.addAnchor(anchorEntity) } } func generateTextResource(text: String, textColor: UIColor) -> TextureResource? { if let image = text.image(withAttributes: [NSAttributedString.Key.foregroundColor: textColor], size: CGSize(width: 18, height: 18)), let cgImage = image.cgImage { let textureResource = try? TextureResource(image: cgImage, options: TextureResource.CreateOptions.init(semantic: nil)) return textureResource } return nil } } I tap the floor and get a cube with '1' as shown below. The background color of the cube is black, I guess. Where does this color come from and how can I change it into, say, red? Thanks.
4
0
150
Jul ’25
Dealing with IAP Purchase Restore
Do you let your users restore their IAP purchases with or without history of purchases? I don't. And it had not been a problem in the past 10 years or so till two days ago when the reviewer rejected my new iOS software submission. He or she said that it was a bug since the Restore button is disabled. I saw the screenshot he or she gave me in which it was obvious that the reviewer had not made a purchase. For me, I cannot think of a reason why one with no history of purchases should be allowed to proceed and restore purchases. I don't even know what the reviewer is trying to restore. So far, the reviewer doesn't buy my explanation or seems to ignore me. What do you think? Do you let them go lucky by accident? By the way, this is a non-consumable IAP product.
4
0
89
Aug ’25
App Record Creation Error with Organizer
I've been trying to send an archive with Organizer to iTunes Connect. It's not my first time. I've been doing it for more than a decade. Anyway, when I try to send a package for my new macOS application, Organizer gives me two error messages that I have never seen before. App Record Creation Error App Record Creation failed due to an invalid attribute. The SKU you entered has already been used. App Record Creation Error App Record Creation failed due to request containing an attribute already in use. The app name you entered is already being used for another app in your account. If you would like to use the name for this app you will need to submit an update to your other app to change the name, or remove it from App Store Connect. An odd thing is that, as shown in the screenshot below, Organizer demands that I enter an application name and SKU manually. I've entered the exactly same ones from the App Store Connect page. I didn't see this step on Organizer last month. I'm using a new SKU for this submission. And I don't have an existing application at iTunes Connect with the same application name. I guess it's the same issue that has been reported here.. I have no pending contract issues. How do I send an archive without errors? My Xcode version is Version 16.4 (16F6). Thanks.
4
0
254
Sep ’25
Showing Multiple Instances of View and Showing Results from Them
I have the following lines of code to show multiple instances of View (MyTextView) import SwiftUI struct ContentView: View { @State var myTextViews = [MyTextView]() var body: some View { VStack { Button { } label: { Text("Show me your current text strings") }.padding(.vertical, 10.0) VStack { ForEach(0 ..< myTextViews.count, id: \.self) { _ in MyTextView() } } Button { myTextViews.append(MyTextView()) } label: { Text("Add me!") }.padding(.vertical, 10.0) } } } struct MyTextView: View { @State var text = "" var body: some View { ZStack { TextField("Enter some text", text: $text) }.padding(.horizontal, 50.0) } } According to the screenshot, I have three instances, each of which contains TextField. After I tap the top button (Show me your current...), I want to show the result from each TextField. How can I do that? Thanks.
5
0
1.2k
Apr ’22
Different Build Schemes -> Error: -Onone Swift optimization level to use previews
I have a sample SwiftUI iOS app. As shown in the screenshot below, my project has three configurations: Debug, MyDebug, Release. If I select the Debug or MyDebug scheme, I get a preview. But if I select the Release scheme, I get an error that says the following. ”***.app” needs -Onone Swift optimization level to use previews (current setting is -O) , where *** is the app name. It probably has nothing to do with the Preview error, but the Info.plist has a dictionary such that the key name is devMode, and the value is $(DEVMODE). And I have a user-defined setting as shown below. My ContentView has the following. import SwiftUI struct ContentView: View { @State private var state: String = "" var body: some View { VStack { Text("Hello, world!: \(state)") } .onAppear { if let devMode = Bundle.main.object(forInfoDictionaryKey: "devMode") as? String { print("Development mode: \(devMode)") state = devMode } if let path = Bundle.main.path(forResource: "Info", ofType: "plist") { if let dict = NSDictionary(contentsOfFile: path) { print("**** \(dict)") } } #if DEBUG print("Debug") #elseif MYDEBUG print("MyDebug") #else print("Que?") #endif } } } #Preview { ContentView() } So my question is how I get the preview for all three build schemes? Muchos thankos.
5
0
355
Apr ’25
Too Complex To Check Code in Reasonable Time?
Again, I have a vertical stack of horizontal stacks of buttons as follows. import SwiftUI struct ContentView: View { &#9;&#9;@State private var eventPresented = Bool() &#9;&#9;@State private var selectedEventIndex = Int() &#9;&#9;@State private var monthSelection = Int() &#9;&#9; &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;VStack { &#9;&#9;&#9;&#9;&#9;&#9;VStack(spacing: 0.0) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;HStack(spacing: 0.0) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ForEach((0...6), id: \.self) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;index in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Button(buttonTitles[index] ?? "") { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;eventPresented = true &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;selectedEventIndex = index + 1 - self.weekIndex &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.foregroundColor(titleColors[index]) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.overlay(Text(eventNumbers[index] ?? "").font(.footnote).foregroundColor(.blue).offset(x: -16, y: -16)) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.buttonStyle(BorderlessButtonStyle()) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.frame(width: 48, height: 48, alignment: .center) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.background(RoundedRectangle(cornerRadius: 2) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.fill(fillColors[index]) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.shadow(color: shadowColors[index], radius: 2, x: 0, y: 0) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.sheet(isPresented: $eventPresented) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;EventView(eventVisible: self.$eventPresented, dayFromParent: self.$selectedEventIndex, monthFromParent: self.$monthSelection) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;... &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;... &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;HStack(alignment: .top, spacing: 0.0) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ForEach((35...36), id: \.self) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;index in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Button(buttonTitles[index] ?? "") { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;eventPresented = true &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;selectedEventIndex = index + 1 - self.weekIndex &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.foregroundColor(titleColors[index]) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.overlay(Text(eventNumbers[index] ?? "").font(.footnote).foregroundColor(.blue).offset(x: -16, y: -16)) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.buttonStyle(BorderlessButtonStyle()) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.frame(width: 48, height: 48, alignment: .center) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.background(RoundedRectangle(cornerRadius: 2) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.fill(fillColors[index]) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.shadow(color: shadowColors[index], radius: 2, x: 0, y: 0) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.sheet(isPresented: $eventPresented) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;EventView(eventVisible: self.$eventPresented, dayFromParent: self.$selectedEventIndex, monthFromParent: self.$monthSelection) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.frame(width: 336.0, height: 48.0, alignment: .leading) &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;} } And I want to send a few variables to EventView when the user clicks on a button. struct EventView: View { &#9;&#9;@Binding var eventVisible: Bool &#9;&#9;@Binding var dayFromParent: Int &#9;&#9;@Binding var monthFromParent: Int &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;VStack { &#9;&#9;&#9;&#9;&#9;&#9;Text("Window sheet.") &#9;&#9;&#9;&#9;&#9;&#9;Button("OK") { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.eventVisible = false &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print("month from parent: \(monthFromParent)") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print("day from parent: \(dayFromParent)") &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;.frame(width: 240, height: 180) &#9;&#9;} } If I just want to send two variables to it, EventView(eventVisible: self.$eventPresented, dayFromParent: self.$selectedEventIndex) the compiler didn't complain. For the third variable, it says SwiftUI the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions I know what it means. Some say it could resolve the issue by making the data types of variables clearer. Others say you could use a function to return a variable for a somewhat complex algebra equation. But what can I do in my case? Does anybody have any suggestions? Thank you
6
0
807
Jan ’21
Using Combine-Future to Fetch Server Data
I could do it with completionHandler, but I'm trying to get server data with Combine. The following is what I have. // UIViewController // import UIKit import Combine class ViewController: UIViewController { // MARK: - Variables private var cancellableSet: Set<AnyCancellable> = [] // MARK: - Life cycle override func viewDidLoad() { super.viewDidLoad() let urlStr = "https://api.github.com/repos/ReactiveX/RxSwift/events" let viewModel = ViewModel(urlStr: urlStr, waitTime: 2.0) viewModel.fetchData(urlText: viewModel.urlStr, timeInterval: viewModel.waitTime) .sink { completion in print("complete") } receiveValue: { dataSet in print("count: \(dataSet)") } .store(in: &cancellableSet) print("Yeah...") } } struct DataModel: Hashable, Decodable { let id: String let type: String } // ViewModel // import UIKit import Combine class ViewModel: NSObject { var cancellables = [AnyCancellable]() var urlStr: String var waitTime: Double init(urlStr: String, waitTime: Double) { self.urlStr = urlStr self.waitTime = waitTime } func fetchData(urlText: String, timeInterval: Double) -> Future<[DataModel], Error> { return Future<[DataModel], Error> { [weak self] promise in guard let strongSelf = self else { return } if let url = URL(string: urlText) { var request = URLRequest(url: url) request.timeoutInterval = timeInterval let sessionConfiguration = URLSessionConfiguration.default let publisher = URLSession(configuration: sessionConfiguration).dataTaskPublisher(for: request) publisher.sink { completion in print("complete") } receiveValue: { (data: Data, response: URLResponse) in do { let dataModels = try JSONDecoder().decode([DataModel].self, from: data) promise(.success(dataModels)) } catch { print("Error while parsing: \(error)") promise(.failure("Failure" as! Error)) } } .store(in: &strongSelf.cancellables) } else { promise(.failure("Failure" as! Error)) } } } } If I run it, I don't get an error. The app doesn't crash, either. The view controller doesn't deliver anything. What am I doing wrong? Muchos thankos.
6
0
1.8k
Dec ’21