Post

Replies

Boosts

Views

Activity

Swift Cpp/c++ interop: swift functions with c++ enums arguments not available in c++
I'm trying to develop a mix-language (c++ and Swift) framework where calls are mainly from c++ -> Swift. However, I'm having problems with get Swift functions available in c++ when they take a c++ enum value as parameter. Assume the following c++ header: #pragma once enum class MyCppEnumClass { A, B, C }; enum class MyCppEnum { D, E, F }; and following Swift source file: public func swiftFunctionWithCppEnumClass(_ value: MyCppEnumClass) { print("Swift function with C++ enum: \(value)") } public func swiftFunctionWithCppEnum(_ value: MyCppEnum) { print("Swift function with C++ enum: \(value)") } The project compiles correctly this way, so the bridging of the enum types does work, making both the enum and enum class available in Swift. However, when inspecting the generated Swift header, I'm seeing this: namespace CxxInteropExperiments SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("CxxInteropExperiments") { // Unavailable in C++: Swift global function 'swiftFunctionWithCppEnum(_:)'. // Unavailable in C++: Swift global function 'swiftFunctionWithCppEnumClass(_:)'. } // namespace CxxInteropExperiments I can't find any information on swift.org (https://www.swift.org/documentation/cxx-interop/) that this would be unsupported. Currently the only solution I can find is to 'mirror' the enum with native Swift enum and implement a convert function in c++ like so: public enum MySwiftEnum { case A case B case C } public func swiftFunctionWithSwiftEnum(_ value: MySwiftEnum) { print("Swift function with Swift enum: \(value)") } #include <CxxInteropExperiments/CxxInteropExperiments-Swift.h> CxxInteropExperiments::MySwiftEnum convert(MyCppEnumClass e) { switch(e) { case MyCppEnumClass::A: return CxxInteropExperiments::MySwiftEnum::A(); case MyCppEnumClass::B: return CxxInteropExperiments::MySwiftEnum::B(); case MyCppEnumClass::C: return CxxInteropExperiments::MySwiftEnum::C(); } } void callSwiftFunctionWithEnum(MyCppEnumClass e) { CxxInteropExperiments::swiftFunctionWithSwiftEnum(convert(e)); } and not use c++ enum or enum classes in Swift function signatures that I want to be able to use in c++. Am I missing something obvious, or is passing c++ enum values directly to Swift functions just not possible? Any help is appreciated.
3
0
761
Mar ’24
SwiftUI #Preview generate localizable strings in the Xcode String Catalog
Since Xcode 15.1, when compiling SwiftUI previews (using #Preview macro), strings that are unique to #Preview declarations are getting extracted and processed to the application's localizable strings catalog. In Xcode 15.0.1, this still works as expected and only UI strings from non-preview UI declarations are extracted and processed. Afaik, there is no setting for this in Xcode 15.1 and up. Anyone knows any other solution to avoid our catalog gets muddled with preview strings? I've already been reporting this via the Feedback hub since 15.1, but have had 0 responses from Apple on this.
0
1
573
Mar ’24
BAURLDownload failed on inaccurate fileSize when non-essential
Hi everyone, I'm integrating BackgroundAssets into my app and run into the following issue. According to the docs fileSize only needs to be accurate for essential downloads /// @abstract Constructs a download object to represent the download of a asset located inside of the provided @c request. /// @param identifier A unique identifier that is used to track the download across the app and extension. /// @param request The request used to perform the download. The URL provided inside of the request must be a https scheme. /// @param essential Whether the download is essential. See @c BADownload.isEssential. Default is false. /// @param fileSize The size of the file to download. For Essential downloads, this field must be accurate in order to show the user /// accurate progress during app installation. If the size does not match the file being downloaded, then the download will fail. It is recommended to /// report an accurate @c fileSize for both Essential and Non-Essential downloads. /// @param applicationGroupIdentifier The identifier of the application group that should used to store the finished download. /// @param priority A priority between @c BADownloaderPriorityMin - @c BADownloaderPriorityMax which is used to order the downloads for this process. /// It is recommended to use @c BADownloaderPriorityDefault if download priority does not matter. @available(iOS 16.4, *) public init(identifier: String, request: URLRequest, essential: Bool, fileSize: Int, applicationGroupIdentifier: String, priority: BADownload.Priority) However, when I'm queueing up non-essential downloads with inaccurate fileSize, my BADownloadManagerDelegate gets a failedWithError callback, with error The download failed because the file size provided does not match what was downloaded. This happens for me when the app needs to download a new version of the manifest, which contains the information about all the other background assets that needs to be processed. Is the documentation wrong and fileSize must always be accurate, even for non-essential downloads? All help is appreciated to resolve this.
2
0
715
Nov ’23
SFSpeechRecognizer.isAvailable returns wrong values
As of iOS 17 SFSpeechRecognizer.isAvailable returns true, even when recognition tasks cannot be fulfilled and immediately fail with error “Siri and Dictation are disabled”. The same speech recognition code works as expected in iOS 16. In iOS 16, neither Siri or Dictation needed to be enabled to have SpeechRecognition to be available and it works as expected. In the past, once permissions given, only an active network connection is required to have functional SpeechRecognition. There seems to be 2 issues in play: In iOS 17, SFSpeechRecognizer.isAvailable incorrectly returns true, when it can’t fulfil requests. In iOS 17 dictation or Siri being enabled is required to handle SpeechRecognition tasks, while in iOS 17 this isn’t the case. If issue 2. Is expected behaviour (I surely hope not), there is no way to actually query if Siri or dictation is enabled to properly handle those cases in code and inform the user why speech recognition doesn’t work. Expected behaviour: Speech recognition is available when Siri and dictation is disabled SFSpeechRecognizer.isAvailable returns correctly false when no SpeechRecognition requests can be handled. iOS Version 17.0 (21A329) Xcode Version 15.0 (15A240d) Anyone else experiencing the same issues or have a solution? Reported this to Apple as well -> FB13235751
1
0
1.1k
Oct ’23
Xcode "Connect via network" is stuck, controls disabled
Hi, since a while now, I've noticed that in Xcode 15 (beta 8), my iOS device running iOS 17 (latest beta), I can't seem to disable the setting "Connect via Network" in the Devices and Simulators window. The controls are disabled, and stuck to 'on'. I often also have issues with the 'Installing to device' step while development my apps, where a reboot of the iPad is required. My guess is that it gets stuck/confused how it is supposed to deploy and my network setup is a bit complicated due to VPN's, tight WiFi security etc. Unpairing the device doesn't help with resetting this setting. After unpairing the top-right header (with the Take Screenshot controls etc...) even still shows the details of the unpaired device. Anyone else has experienced this or know a solution.
8
14
4.5k
Sep ’23
Iphone 13 and 14 have unusably inaccurate depth data. iPhone 12 and iPad Pro is fine
The depth data captured by iPhone 13 and later is significantly of lower quality than depth data captured with iPhone 12, iPad Pro 2020, 2021 and 2022. All these devices more-or-less support the same depth resolution and capture frame rates, and none of the configurations on AVCaptureDevice is able to give more accurate results. I’ve attached screencaptures in which the difference in accuracy and noise between an iPhone 14 and an iPad Pro 2021 (11”) can be clearly seen. Captures have been made with a slightly adjusted version of Apple's TrueDepthStreamer app (https://developer.apple.com/documentation/avfoundation/additional_data_capture/streaming_depth_data_from_the_truedepth_camera). Is this expected bahavior? iPhone14 depth stream: https://www.icloud.com/iclouddrive/07bYXtTCFqddpv2XaJQdwttWQ#depthStream-iPhone14 iPad Pro 2021 (11") depth stream: https://www.icloud.com/iclouddrive/0294BDECZziOBhS4wWTvtZAZw#depthStream-iPadPro2021-11inch Enabling depth filtering, alleviates the issue to a small degree, but has as side affect that movement of the subject in the image created a ‘trail’ in the depth data. In our app, this makes the depth data unusable. I've also created a Issue with Apple: FB12151444
1
0
1.1k
Apr ’23
Touch layout not matching UI layout on iPhone after waking device when .sheet is open
In a SwiftUI app on iPhone when the device is put to sleep by pressing the power button, while the app is presenting a .sheet, waking the device will result in the touch layout to not match the UI layout. See attached project to reproduce the issue. This happens with Xcode 14.2, targeting iOS 16.0. Steps to reproduce (assuming the app has been built an running on a real device, xcode project linked at the bottom): Tap the “Hello World” button. This opens a plain sheet with a fully green color content Push the device’s power button to put it to sleep Push the device’s power button again to wake the device Optionally unlock the device if needed Dismiss the opened green sheet (the app should still be open and displaying the sheet after waking the device) Try to tap the “Hello World” button again. Observed behavior: the area required to tap to activate the button no longer matches the UI and you have to tap below the button to activate it. Desired behavior: after waking the device and dismissing the sheet, the touch and ui layout should still be correct. Demonstration XCode project
9
3
3.1k
Feb ’23
Bizar leaking ViewModel in SwiftUI
Hi all, Recently I stumbled upon some, for me at least, bizar memory leak. I have a viewmodel, which provides some sharing functionality. To do this it exposes 2 variables: class ViewModelOne: ObservableObject {   let tracker = DeinitTracker("ViewModelOne") //this is to easily track lifetime of instances   @Published var shareReady = false   var sharedUrls = [URL]()       func share() {     sharedUrls = [URL(string: "www.google.com")!]     shareReady = true   } } Next I have a mainview, which provides 2 subviews, with some controls to switch between the 2 subviews: enum ViewMode {   case one   case two } struct MainView: View {   @State var viewMode: ViewMode = .one   var body: some View {     VStack {       switch viewMode {       case .one:         ViewOne()       case .two:         ViewTwo()       }       HStack {         Spacer()         Button(action: {           viewMode = .one         }, label: { Image(systemName: "1.circle").resizable().frame(width: 80) })         Spacer()         Button(action: {           viewMode = .two         }, label: { Image(systemName: "2.circle").resizable().frame(width: 80) })         Spacer()       }.frame(height: 80)     }   } } struct ViewOne: View {   let tracker = DeinitTracker("ViewOne")   @StateObject var viewModel = ViewModelOne()   var body: some View {     VStack {       Button(action: {viewModel.share()},           label: { Image(systemName: "square.and.arrow.up").resizable() })       .frame(width: 40, height: 50)       Image(systemName: "1.circle")         .resizable()     }     .sheet(isPresented: $viewModel.shareReady) {       ActivityViewController(activityItems: viewModel.sharedUrls)     }   } } struct ViewTwo: View {   var body: some View {     Image(systemName: "2.circle")       .resizable()   } } ViewOne contains a button that will trigger its viewmodel to setup the urls to share and a published property to indicate that the urls are ready. That published property is then used to trigger the presence of a sheet. This sheet then shows the ActivityViewController wrapper for SwiftUI: struct ActivityViewController: UIViewControllerRepresentable {   let activityItems: [Any]   let applicationActivities: [UIActivity]? = nil   @Environment(\.presentationMode) var presentationMode       func makeUIViewController(context: UIViewControllerRepresentableContext<ActivityViewController>) -> UIActivityViewController {     let controller = UIActivityViewController(activityItems: activityItems, applicationActivities: applicationActivities)     controller.completionWithItemsHandler = { _, _, _, _ in       self.presentationMode.wrappedValue.dismiss()     }     return controller   }       func updateUIViewController(_ uiViewController: UIActivityViewController, context: UIViewControllerRepresentableContext<ActivityViewController>) {   } } And now for the bizar part. As long as the sheet hasn't been shown, all is well and switching between subview 1 and 2 behaves as expected, where ViewOne's ViewModel is deinitialized when the ViewOne instance is destroyed. However to once the sheet with ActivityViewController has been presented in ViewOne, and then switching to ViewTwo, ViewOne is still destroyed, but ViewOne's viewmodel isn't. To track this, I have helper struct DeinitTracker that prints when it gets initialized and deinitialized: public class DeinitTracker {   static var counter: [String: Int] = [:]   public init(_ deinitText: String) {     DeinitTracker.counter[deinitText] = (DeinitTracker.counter[deinitText] ?? -1) + 1     self.deinitText = deinitText     self.count = DeinitTracker.counter[deinitText] ?? -1     print("DeinitTracker-lifetime: \(deinitText).init-\(count)")   }       let deinitText: String   let count: Int       deinit {     print("DeinitTracker-lifetime: \(deinitText).deinit-\(count)")   } } I can't figure out who is holding a reference to the ViewModel that prevents it from being deinitialized. I know it's a rather complicated explanation, but I'm hoping the scenario is clear. I've prepared a Playgrounds app to demonstrate the problem -> https://www.icloud.com/iclouddrive/0629ZP6MXMrj7GJIWHpGum6Dw#LeakingViewModel I'm hoping someone can explain what's going on. Is this a bug in SwiftUI? Or am I using it wrong by binding a viewmodel's published property to a sheet's isPresented property. If you have any questions, don't hesitate to ask.
1
0
1.5k
Jul ’22