Swift is a powerful and intuitive programming language for Apple platforms and beyond.

Posts under Swift tag

200 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

Conforming @MainActor class to Codable
How does one add Codable conformance to a class that needs to be isolated to the MainActor? For example, the following code gives compiler errors: @MainActor final class MyClass: Codable { var value: Int enum CodingKeys: String, CodingKey { case value } init(from decoder: Decoder) throws { // <-- Compiler error: Initializer 'init(from:)' isolated to global actor 'MainActor' can not satisfy corresponding requirement from protocol 'Decodable' let data = try decoder.container(keyedBy: CodingKeys.self) self.value = try data.decode(Int.self, forKey: .value) } func encode(to encoder: Encoder) throws { // <-- Compiler error: Instance method 'encode(to:)' isolated to global actor 'MainActor' can not satisfy corresponding requirement from protocol 'Encodable' var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(value, forKey: .value) } } I'm definitely struggling to get my head around actors and @MainActor at the moment!
1
1
2.6k
Jan ’24
Rotating a page in a pdf file - and get a mirrored image
I try to rotate a page 180° in a pdf file. I nearly get it, but the page is also mirrored horizontally. Some images to illustrate: Initial page: Result after rotation (see code): it is rotated 180° BUT mirrored horizontally as well: The expected result It is just as if it was rotated 180°, around the x axis of the page. And I would need to rotate 180° around z axis (perpendicular to the page). It is probably the result of writeContext!.scaleBy(x: 1, y: -1) I have tried a lot of changes for transform, translate, scale parameters, including removing calls to some of them, to no avail. @IBAction func createNewPDF(_ sender: UIButton) { var originalPdfDocument: CGPDFDocument! let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) let documentsDirectory = urls[0] // read some pdf from bundle for test if let path = Bundle.main.path(forResource: "Test", ofType: "pdf"), let pdf = CGPDFDocument(URL(fileURLWithPath: path) as CFURL) { originalPdfDocument = pdf } else { return } // create new pdf let modifiedPdfURL = documentsDirectory.appendingPathComponent("Modified.pdf") guard let page = originalPdfDocument.page(at: 1) else { return } // Starts at page 1 var mediaBox: CGRect = page.getBoxRect(CGPDFBox.mediaBox) // mediabox which will set the height and width of page let writeContext = CGContext(modifiedPdfURL as CFURL, mediaBox: &mediaBox, nil) // get the context var pageRect: CGRect = page.getBoxRect(CGPDFBox.mediaBox) // get the page rect writeContext!.beginPage(mediaBox: &pageRect) let m = page.getDrawingTransform(.mediaBox, rect: mediaBox, rotate: 0, preserveAspectRatio: true) // Because of rotate 0, no effect ; changed rotate to 180, then get an empty page writeContext!.translateBy(x: 0, y: pageRect.size.height) writeContext!.scaleBy(x: 1, y: -1) writeContext!.concatenate(m) writeContext!.clip(to: pageRect) writeContext!.drawPDFPage(page) // draw content in page writeContext!.endPage() // end the current page writeContext!.closePDF() } Note: This is a follow up of a previous thread, https://developer.apple.com/forums/thread/688436
2
0
1.5k
Nov ’23
@Published properties and the main thread
I am working on a library, a Swift package. We have quite a few properties on various classes that can change and we think the @Published property wrapper is a good way to annotate these properties as it offers a built-in way to work with SwiftUI and also Combine. Many of our properties can change on background threads and we've noticed that we get a purple runtime issue when setting the value from a background thread. This is a bit problematic for us because the state did change on a background thread and we need to update it at that time. If we dispatch it to the main queue and update it on the next iteration, then our property state doesn't match what the user expects. Say they "load" or "start" something asynchronously, and that finishes, the status should report "loaded" or "started", but that's not the case if we dispatch it to the main queue because that property doesn't update until the next iteration of the run loop. There also isn't any information in the documentation for @Published that suggests that you must update it on the main thread. I understand why SwiftUI wants it on the main thread, but this property wrapper is in the Combine framework. Also it seems like SwiftUI internally could ask to receive the published updates on the main queue and @Published shouldn't enforce a specific thread. One thing we are thinking about doing is writing our own property wrapper, but that doesn't seem to be ideal for SwiftUI integration and it's one more property wrapper that users of our package would need to be educated about. Any thoughts on direction? Is there anyway to break @Published from the main thread?
3
1
4.3k
Jan ’24
Type 'VNRecognizedPointKey' has no member 'thumbTip'
With the release of Xcode 13, a large section of my vision framework processing code became errors and cannot compile. All of these have became deprecated. This is my original code:  do {       // Perform VNDetectHumanHandPoseRequest       try handler.perform([handPoseRequest])       // Continue only when a hand was detected in the frame.       // Since we set the maximumHandCount property of the request to 1, there will be at most one observation.       guard let observation = handPoseRequest.results?.first else {         self.state = "no hand"         return       }       // Get points for thumb and index finger.       let thumbPoints = try observation.recognizedPoints(forGroupKey: .handLandmarkRegionKeyThumb)       let indexFingerPoints = try observation.recognizedPoints(forGroupKey: .handLandmarkRegionKeyIndexFinger)       let middleFingerPoints = try observation.recognizedPoints(forGroupKey: .handLandmarkRegionKeyMiddleFinger)       let ringFingerPoints = try observation.recognizedPoints(forGroupKey: .handLandmarkRegionKeyRingFinger)       let littleFingerPoints = try observation.recognizedPoints(forGroupKey: .handLandmarkRegionKeyLittleFinger)       let wristPoints = try observation.recognizedPoints(forGroupKey: .all)               // Look for tip points.       guard let thumbTipPoint = thumbPoints[.handLandmarkKeyThumbTIP],          let thumbIpPoint = thumbPoints[.handLandmarkKeyThumbIP],          let thumbMpPoint = thumbPoints[.handLandmarkKeyThumbMP],          let thumbCMCPoint = thumbPoints[.handLandmarkKeyThumbCMC] else {         self.state = "no tip"         return       }               guard let indexTipPoint = indexFingerPoints[.handLandmarkKeyIndexTIP],          let indexDipPoint = indexFingerPoints[.handLandmarkKeyIndexDIP],          let indexPipPoint = indexFingerPoints[.handLandmarkKeyIndexPIP],          let indexMcpPoint = indexFingerPoints[.handLandmarkKeyIndexMCP] else {         self.state = "no index"         return       }               guard let middleTipPoint = middleFingerPoints[.handLandmarkKeyMiddleTIP],          let middleDipPoint = middleFingerPoints[.handLandmarkKeyMiddleDIP],          let middlePipPoint = middleFingerPoints[.handLandmarkKeyMiddlePIP],          let middleMcpPoint = middleFingerPoints[.handLandmarkKeyMiddleMCP] else {         self.state = "no middle"         return       }               guard let ringTipPoint = ringFingerPoints[.handLandmarkKeyRingTIP],          let ringDipPoint = ringFingerPoints[.handLandmarkKeyRingDIP],          let ringPipPoint = ringFingerPoints[.handLandmarkKeyRingPIP],          let ringMcpPoint = ringFingerPoints[.handLandmarkKeyRingMCP] else {         self.state = "no ring"         return       }               guard let littleTipPoint = littleFingerPoints[.handLandmarkKeyLittleTIP],          let littleDipPoint = littleFingerPoints[.handLandmarkKeyLittleDIP],          let littlePipPoint = littleFingerPoints[.handLandmarkKeyLittlePIP],          let littleMcpPoint = littleFingerPoints[.handLandmarkKeyLittleMCP] else {         self.state = "no little"         return       }               guard let wristPoint = wristPoints[.handLandmarkKeyWrist] else {         self.state = "no wrist"         return       } ... } Now every line from thumbPoints onwards results in error, I have fixed the first part (not sure if it is correct or not as it cannot compile) to :         let thumbPoints = try observation.recognizedPoints(forGroupKey: VNHumanHandPoseObservation.JointsGroupName.thumb.rawValue)        let indexFingerPoints = try observation.recognizedPoints(forGroupKey: VNHumanHandPoseObservation.JointsGroupName.indexFinger.rawValue)        let middleFingerPoints = try observation.recognizedPoints(forGroupKey: VNHumanHandPoseObservation.JointsGroupName.middleFinger.rawValue)        let ringFingerPoints = try observation.recognizedPoints(forGroupKey: VNHumanHandPoseObservation.JointsGroupName.ringFinger.rawValue)        let littleFingerPoints = try observation.recognizedPoints(forGroupKey: VNHumanHandPoseObservation.JointsGroupName.littleFinger.rawValue)        let wristPoints = try observation.recognizedPoints(forGroupKey: VNHumanHandPoseObservation.JointsGroupName.littleFinger.rawValue) I tried many different things but just could not get the retrieving individual points to work. Can anyone help on fixing this?
2
0
1.6k
Aug ’23
How do you apply a diffable data source UI snapshot only after awaiting (with async/await) data fetched from the network?
I'm new to async/await, and am currently migrating my completion handler code to Swift 5.5's concurrency features. After generating an sync alternative in Xcode to my function func fetchMatchRecords(completion: @escaping ([Match]) -> Void), it becomes func fetchMatchRecords() async -> [Match]. I'm not sure how it would be used in the context of UIKit and diffable data sources. In a viewDidLoad, previously it would be MatchHistoryController.shared.fetchMatchRecords() { matches in DispatchQueue.main.async { self.dataSource.apply(self.initialSnapshot(), animatingDifferences: false) } } But I'm not sure how it would be used now Task { await MatchHistoryController.shared.fetchMatchRecords() } self.dataSource.apply(self.initialSnapshot(), animatingDifferences: false) How would I make sure that the snapshot is applied only after awaiting a successful fetch result? Here's the definition of initialSnapshot() that I used: func initialSnapshot() -> NSDiffableDataSourceSnapshot<Section, Match> { var snapshot = NSDiffableDataSourceSnapshot<Section, Match>() snapshot.appendSections([.main]) snapshot.appendItems(MatchHistoryController.shared.matches) return snapshot }
1
0
1.6k
Sep ’23
Xcode 13 typing is delayed / really slow
I just updated Xcode to the newest Version (13.0 13A233). And what I immediately recognized was that when I was typing in a large class (1000 Lines or more) the typing was delayed like 1 or 2 seconds. This makes it really hard to type or even do anything with my project. It works perfectly fine in smaller projects with only 100 Code Lines per File, but it is really hard to type in bigger projects with over 30 files and 3000 Code Lines per File. This always happens when I use Xcode, whether I run the app on a real device or a simulator. Things that could cause the issue in my case: I removed the contents of ~/Library/Developer/Xcode/iOS DeviceSupport, because it took the device previously too long to launch, but I don´t think that this should have an effect on my Xcode typing performance. I would really appreciate any help, because this issue makes my work impossible and really concerns me a lot.
52
3
19k
3w
SwiftUI NavigationView pops back when updating observableObject
Background: I have been stuck on this annoying problem for days, trying different solution and searching apple developer forum, stackoverflow etc for answers; some have had similar problems but no suggested solution had the desired effect. The problem is that when updating an observableObject or environmentObject down the navigation hierarchy view stack, the views get popped back to root. Viewing data from observableObject is fine, but not editing. Scenario is: I navigate to: root -> view1 -> view2. I update the environmentObject in View2 but then I get pushed back to: root -> view1 I have simplified my app in order to make it more understandable. See below: ObservableObject: class DataStore: ObservableObject { static let shared = dataStore() @Published var name : Int = "" } RootView: struct ContentView: View { @StateObject var dataStore = DataStore.shared @State private var isShowingView1 = false var body: some View { NavigationView{ VStack{ Text(dataStore.name) NavigationLink(destination: View1(), isActive: $isShowingView1) { } Button(action: { isShowingView1 = true }) } } } } View1: struct View1: View { @EnvironmentObject var dataStore: dataStore @State private var isShowingView2 = false var body: some View { ScrollView{ VStack(alignment: .center) { Text(dataStore.name) NavigationLink(destination: View2(), isActive: $isShowingView2) { } Button(action: { isShowingView2 = true }){ Text("Go to View2") } } } } } View2: struct View2: View { @EnvironmentObject var dataStore: dataStore var body: some View { ScrollView{ VStack(alignment: .center) { Text(dataStore.name) Button(action: { dataStore.name = "updated value" }){ Text("Update data") } // When updating this environmentObject the viewstack will be pushed back to View1. If view2 had been navigated to view3 and the view3 had been updating the environmentObject, then it would also be pushed back to View1. } } } } Solution: I spent many hours searching for solutions and trying different approaches, but nothing I tried worked. There seemed to be a few other people that had the same problem as I experienced, but the suggested solutions didn't cut it. But then I stumbled on a solution for this problem when trying to implement another feature. So to be frank I am writing here now, not to ask this great community for help, but instead to give back to the community by providing the this solution to others that might need to see this. The solution is really simple implement but was not so easy to come across. If you experience a problem similar to me then you will only need to update your rootView accordingly: RootView Updated: struct ContentView: View { @StateObject var dataStore = DataStore.shared @State private var isShowingView1 = false var body: some View { NavigationView{ VStack{ Text(dataStore.name) NavigationLink(destination: View1(), isActive: $isShowingView1) { } Button(action: { isShowingView1 = true }) } } .navigationViewStyle(.stack) //ADD THIS LINE ABOVE } } This one line .navigationViewStyle(.stack) fixed the problem of popping the viewstack for me. Unfortunately I can't provide you with the logic explanation for this behaviour, but it works and I am satisfied with that. Perhaps you are too, or perhaps you have insight on why this solution actually achieves the desired effect of allowing views down the hierarchy update observableObjects without being popped. Happy coding :)
15
12
15k
Feb ’24
ImportFromDevicesCommands - The operation couldn’t be completed. (Cocoa error 66563.)
I am trying to use import from iPhone option as shown in WWDC session. I added code  WindowGroup {             ContentView()                 .environment(\.managedObjectContext, persistenceController.container.viewContext)                      }         .commands {             ImportFromDevicesCommands()         } ContentView.swift is  List {                 ForEach(items) { item in                     NavigationLink {                         Text("Item at \(item.timestamp!, formatter: itemFormatter)")                     } label: {                         Text(item.timestamp!, formatter: itemFormatter)                     }                 }                 .onDelete(perform: deleteItems)             }             .importsItemProviders([.image,.png,.jpeg,.rawImage], onImport: { providers in                 print("checking reachability")                 return true             }) The importsItemProviders block itself is not executed and not printing anything. In addition I am getting alert The operation couldn’t be completed. (Cocoa error 66563.) Is there anything to add for making this functionality work ?
3
0
1.2k
Oct ’23
XPC, Swift, ObjC, and arrays
I create a protocol that had, among other things: @objc func setList(_: [MyType], withReply: @escaping (Error?) -> Void) The daemon part is in Swift, while the calling part is in Objective-C. Because why not? (Actually, because the calling part has to deal with C++ code, so that's ObjC++; however, I wanted the stronger typing and runtime checking for the daemon part, so I wrote it in Swift.) The ObjC part uses NSArray<MyType*>. I set up an NSXPCConnection link, and create a (synchronous) proxy with the right protocol name. But when I try to do the XPC setList call, I get an error. I assume that's because it doesn't like the signature. (Surely this is logged somewhere? I couldn't find it, if so. 😩) But... if I have a signature of @objc func addItem(_: MyType, withReply: @escaping (Error?) -> Void), then it works. So I assume it's the array. (Oh, I've also tried it without the @objc; the protocol itself is defined as @objc.) I've tried changing to protocol signature to using NSArray, but same thing.
7
0
1.1k
3w
In-App Purchase consumable can be purchased just once
I am making an app in SwiftUI using an In-app purchase. In this app, the user should be able to buy points as many times as he wants, so I have used consumable products (in the app store connect). But when I've tried to buy them once again I got the information "This In-App purchase has already been bought. It will be restored for free". I've already searched for a way how to do it but none of the ideas worked for me. Here is my StoreManager class: import Foundation import StoreKit import SwiftUI class StoreManager : NSObject, ObservableObject, SKProductsRequestDelegate { @EnvironmentObject var authViewModel: AuthViewModel @Published var transactionState: SKPaymentTransactionState? @Published var myProducts = [SKProduct]() var request: SKProductsRequest! func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) { print("Did receive response") if !response.products.isEmpty { for fetchedProduct in response.products { DispatchQueue.main.async { self.myProducts.append(fetchedProduct) } } for invalidIdentifier in response.invalidProductIdentifiers { print("Invalid identifiers found: \(invalidIdentifier)") } }else{ print("it's empty") } } func getProducts(productIDs: [String]) { let request = SKProductsRequest(productIdentifiers: Set(productIDs)) request.delegate = self request.start() } func request(_ request: SKRequest, didFailWithError error: Error) { print("Request did fail: \(error)") } func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { for transaction in transactions { switch transaction.transactionState { case .purchasing: transactionState = .purchasing break case .purchased: print("purchased") queue.finishTransaction(transaction) transactionState = .purchased break case .restored: print("restored") transactionState = .restored queue.finishTransaction(transaction) break case .failed, .deferred: queue.finishTransaction(transaction) transactionState = .failed break default: queue.finishTransaction(transaction) break } } } func purchaseProduct(product: SKProduct) { if SKPaymentQueue.canMakePayments() { let payment = SKPayment(product: product) SKPaymentQueue.default().add(payment) } else { print("User can't make payment.") } } func restoreProducts() { SKPaymentQueue.default().restoreCompletedTransactions() } } And I am simply using getProducts with onAppear, and purchase product on button's action. Please help me or if an answer to a similar question already exists send me a link to that thread.
3
0
1.8k
Oct ’23
objc[6125]: Class _PathPoint is implemented in both ...
I'm getting the following message every time I run the app: objc[6125]: Class _PathPoint is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore (0x11ef42e30) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/TextInputUI.framework/TextInputUI (0x16310a8b8). One of the two will be used. Which one is undefined. objc[6481]: Class RSABSSATokenBlinder is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/CryptoKitCBridging.framework/CryptoKitCBridging (0x1314c7470) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/CryptoKitPrivate.framework/CryptoKitPrivate (0x14f4c2368). One of the two will be used. Which one is undefined. I have no idea what that TextInputUI is or how it got there or why it's being repeated. How can I fix this without breaking its functionality?
15
4
6.6k
Aug ’23
Calling an environment variable from a .xcconfig file in swift
I'm trying to setup a GitHub repository for a project that will involve the use of an API Key. In order to keep that key a secret, I have decided to make a .xcconfig file with the API Key and add the .xcconfig file to my .gitignore. So far I have linked the file with my build settings for the app target. But I'm not actually sure how to go about calling the variable in my swift code. I know that there are environment variables within the Xcode build schemes, but I'm not sure how you would call them from there either. Is there some kind of framework that I need to call? Nothing online seems to be pointing to linking to the actual file itself.
3
1
5.9k
Aug ’23
Popover from NSToolbarItem in Mac Catalyst app
Hi, Inside a Mac Catalyst app, I need to display a popover starting from an NSToolbarItem contained inside the app toolbar (like the Apple Maps Mac app does, see below image). In order to do that, when I press the button I need to find the toolbar item view and use it as popover anchor. How can I find the view or frame of an NSToolbarItem on Mac Catalyst? A property that could help me is the NSToolbarItem "view" property (NSView), but that property has been marked has unavailable in Mac Catalyst. Any idea? Thank you
3
3
1.3k
Feb ’24
Can not install TestFlight version: "This app cannot be installed because its integrity could not be verified."
Hi, I created an application for a client. The client put me as a developer in his team on AppStore Connect. For that I use Xcode 13.2.1. It's a UIKit application in Swift with Storyboards. I built the app => no problem I tested it on my iPhone => no problem From Xcode I made an archive and uploaded it to TestFlight => no problem The archive has been received by Apple. It was then processed by Apple => no problem And finally I received a notification that it was available on TestFlight => no problem TestFlight offers me to install it but when I try it does not install it. If I try to click on the icon of the application it shows me the message "This app cannot be installed because its integrity could not be verified." I have tried installing other apps from TestFlight and have no issues. I checked on my mac for the various certificates for this client and they are all good and valid. I don't understand why for this application I have this behavior. Someone would have any idea ? Regards Tof
249
15
70k
1w
WKWebView evaluateJavascript method crash with async/await when javascript call doesn't have return value
I wrote a code like the example below to execute javascript code that has no return value. let webView: WKWebView // after load complete let result = await webView.evaluateJavascript("someFunction()") // :0: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value but when i use method with completion handler manner, it doesn't be crashed. but Xcode enforces me to use 'await' keyword and warning is bordering me await webView.evaluateJavaScript("someFunction()", completionHandler: nil) // warning: Consider using asynchronous alternative function The differnce I found is the different signature. Completion handler version has Optional result type, but async/await version has just Any result type func evaluateJavaScript(_ javaScriptString: String, completionHandler: ((Any?, Error?) -> Void)? = nil) func evaluateJavaScript(_ javaScriptString: String) async throws -> Any my Xcode version is 13.2.1. Hope to fix it soon.
6
9
3.3k
Oct ’23
How to get the page URL shared via the Share button on macOS?
I have a macOS Share Extension invoked when users tap the Share button in Safari. I'm trying to get the page's URL from the -[NSExtensionItem attachments] attribute, but it comes as an NSSecureCoding object, and I'm unable to read the URL from it. In the loadView method, I'm filtering and loading the attachments of type public.url: override func loadView() { super.loadView() guard let inputItem = extensionContext?.inputItems.first as? NSExtensionItem else { print("Didn't received input item from action.") return } var itemProvider: NSItemProvider? itemProvider = inputItem.attachments?.filter({ $0.registeredTypeIdentifiers.contains("public.url") }).first ?? inputItem.attachments?.filter({ $0.registeredTypeIdentifiers.contains("public.plain-text") }).first guard let itemProvider = itemProvider else { print("Didn't received attachments from input item.") return } if itemProvider.canLoadObject(ofClass: URL.self) { itemProvider.loadItem(forTypeIdentifier: "public.url", completionHandler: onLoadVideoURL) } else if itemProvider.canLoadObject(ofClass: String.self) { itemProvider.loadItem(forTypeIdentifier: "public.plain-text", completionHandler: onLoadVideoURL) } else { print("This action only supports URL and String.") } } The itemProvider.loadItem method runs for the type identifier public.url, calling the completion handler bellow: @objc private func onLoadVideoURL(dict: NSSecureCoding?, error: Error?) { print("URL: \(dict.debugDescription)") // ... } But the content that it prints to the console is: URL: Optional(<68747470 733a2f2f 73746163 6b6f7665 72666c6f 772e636f 6d2f7175 65737469 6f6e732f 35323231 39373030 2f686f77 2d746f2d 63617374 2d6e7373 65637572 65636f64 696e672d 746f2d6d 6b6d6170 6974656d 2d696e2d 61637469 6f6e2d65 7874656e 73696f6e>) The same code works as expected on iOS, printing the shared URL to the console. Do I have to somehow convert this NSSecureCoding to URL or another object? Or should I do this in a completely different way on macOS? The goal is to access the page's URL from the Share Extension activated when the user selects it in the Share Menu.
2
0
2.1k
Oct ’23
Why does a MainActor class / function not run on Main Thread?
When marking the ViewController and the function with @MainActor, the assertion to check that the UI is updated on main thread fails. How do I guarantee that a function is run on Main Thread when using @MainActor? Example code: import UIKit @MainActor class ViewController: UIViewController {     let updateObject = UpdateObject()     override func viewDidLoad() {         super.viewDidLoad()                  updateObject.fetchSomeData { [weak self] _ in             self?.updateSomeUI()         }     }     @MainActor     func updateSomeUI() {         assert(Thread.isMainThread) // Assertion failed!     } } class UpdateObject {     func fetchSomeData(completion: @escaping (_ success: Bool) -> Void) {         DispatchQueue.global().async {             completion(true)         }     } } Even changing DispatchQueue.global().async to Task.detached does not work. Tested with Xcode 13.2.1 and Xcode 13.3 RC
5
0
2.2k
Jan ’24
Problem decoding AttributedString containing emoji
I am trying to encode an AttributedString to JSON and then decode it back to an AttributedString. But when the AttributedString both (1) contains emoji, and (2) has any attributes assigned, the decoding seems to fail, producing a truncated AttributedString. By dump-ing the decoded value, I can see that the full string is still in there (in the guts property) but it is missing in normal uses of the AttributedString. Below is an example that reproduces the problem. import Foundation // An arbitrary AttributedString with emoji var attrString = AttributedString("12345💕☺️💕☺️💕☺️12345") // Set an attribute (doesn't seem to matter which one) attrString.imageURL = URL(string: "http://www.dummy.com/dummy.jpg")! // Encode the AttributedString var encoder = JSONEncoder() encoder.outputFormatting = .prettyPrinted let data = try! encoder.encode(attrString) // Print the encoded JSON print("encoded JSON for AttributedString:") print(String(data: data, encoding: .utf8)!) // Output from above omitted, but it looks correct with the full string represented // Decode the AttributedString and print it let decoder = JSONDecoder() let decodedAttrString = try! decoder.decode(AttributedString.self, from: data) print("decoded AttributedString:") print(decodedAttrString) // Output from above is a truncated AttributedString: // // 12345💕☺️ { // NSImageURL = http://www.dummy.com/dummy.jpg // } print("dump of AttributedString:") dump(decodedAttrString) // Interestingly, `dump` shows that the full string is still in there: // // ▿ 12345💕☺️ { // NSImageURL = http://www.dummy.com/dummy.jpg // } // ▿ _guts: Foundation.AttributedString.Guts #0 // - string: "12345💕☺️💕☺️💕☺️12345" // ▿ runs: 1 element // ... //
9
0
1.2k
2w