Dive into the world of programming languages used for app development.

All subtopics

Post

Replies

Boosts

Views

Activity

Objective-C++ confuses two private classes with the same name
I think I found a bug in the Objective-C++ compiler, linker, or runtime. Here’s the scenario: We have a macOS app written in Swift. To control hardware from a 3rd party manufacturer, we use a couple SDKs provided by the manufacturer. The SDKs use dynamically loaded libraries and the interface is defined in C++ headers. To bridge between our Swift code and the C++ APIs we have a private Cocoapod that wraps the 3rd party interface with Objective-C++ classes. The two SDKs each provide an interface for discovering attached devices using a callback class that the programmer provides. By accident we named both callback implementations DiscoveryCallback, but this was not a compiler error because neither class was publicly declared, and each was defined in the .mm file where it was used. However, the problem we’re seeing is this: We want to discover Videohub devices, so we register a new instance of DiscoveryCallback (defined in the same .mm file as this code) with the Videohub SDK. A Videohub device is connected and the SDK calls a method on our callback. Surprise! The callback we registered in step 1 was actually the one intended for Decklink devices, defined in a completely different .mm file. This violates all sorts of assumptions and our app quickly crashes. The funny thing is, the two implementations of DiscoveryCallback have completely different method names. The Videohub SDK is supposed to be calling NewVideohubDevice, yet somehow it successfully calls DeckLinkDeviceArrived on an instance of a class it shouldn’t even know about. So the compiler has checked that our intended DiscoveryCallback matches the protocol that the SDK expects, but at runtime the compiled code instantiates a completely different implementation of DiscoveryCallback and somehow doesn’t immediately fail; we still call a method on it that doesn’t even share a name with the intended target. I imagine at this point the method names are long forgotten and are just pointers in a table. I don’t know if this is a bug in the compiler, the Objective-C++ runtime, or if this is just “working as designed” undefined behavior that I should have avoided by not giving two private classes the same name. I know it’s possible to use a private API simply by redeclaring it in my own code, and this seems related to that, but I feel like the compiler or linker should have warned me that I had two implementations of the same class, or if that is not an error, then the runtime should have instantiated the class that was privately defined in the same source file where it was used. Obviously I can’t share our entire project; I’d like to provide some sample code that replicates the issue, but I don’t have time to do that right now. I’m posting this to see if other developers have had a similar experience.
2
1
454
Jul ’24
How to use network sockets with async/await?
I have an application that communicates with custom external hardware on the network (using UDP). I have a thread that receives and process the UDP data and then signals a waiting thread by releasing a semaphore when data is available. A have a asyncSendAndReceive and asyncReceive function that just begs to use async/await. But I cannot simply switch because of the use of the semaphore. Various forums and discussions said that semaphores should no longer be used for signalling. If not semaphores, then what else? Note that my two async functions may not always block. If data was received before they were called, then it is queued (and the semaphore is signalled).
9
0
2.8k
Nov ’22
Model Container Sendable Throwing Error in Swift 6
Hi all, I just wanted to ask how people were using ModelActor with the Swift 6 language mode enabled. My current implementation involves passing the ModelContainer to my ModelActor, which worked in Sonoma and previous betas of Sequoia, however in the current Beta 3, I get this error: "Sending 'self.modelContext.container' risks causing data races" I am a bit confused by this, as from what I understand, ModelContainer conforms to Sendable, so ideally this error should not be thrown. Is this a bug in Beta 3? Thanks in advance.
3
1
721
Jul ’24
gfortran error
I am getting an following error while compiling the Fortran file with "gfortran TEST_1_fortran_only_fixed.f" ld: unsupported tapi file type '!tapi-tbd' in YAML file '/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk/usr/lib/libSystem.tbd' for architecture x86_64 collect2: error: ld returned 1 exit status Please help me to solve this issue
1
0
304
Jul ’24
Contents of Swift dictionaries and arrays being lost
Just when I think I am finally starting to understand Swift I come across a gotcha like the following: I have two object, a swiftui display and one of data to be displayed. Ok, sounds easy. The data is read out of a JSON file so I have a set of arrays and dictionaries. The data is valid when read, it is definitely there, but when I go to display it, its gone. Just vanished. Wasted about a day on this so far, and I’ve seen it before, the inability to pass out of an object an array or dictionary with contents intact. If I create an array var, and not let the system do it, contents are preserved. So, in the data object I’ll have something like this: struct DataObject{ var item: [String:Any] item=JSONData serialized out of memory, and may have say, 12 fields } In my SwiftUI module I have: var item=dataObject.item dataObject.item now has 0 fields. I can allocate and initialize a dictionary in DataObject and those elements come through fine. So it seems like the stuff being serialized from JSON is being deleted out from under me.
3
0
379
Jul ’24
Swift app is failing to build with Xcode 15.4 on Sonoma Silicon
HI, I have a app with Swift Programming language. It is built successfully on my Monterey Intel by using Xcode 14. I am trying to built same app on Sonoma Silicon arm64 by using Xcode 15.4. But app is failing to build with below errors. Can anyone suggest reason for this? Copy /Users/testuser/git/agent/out/Darwin/Release/TESTFileProvider.appex/Contents/Resources/swift-nio__NIOFileSystem.bundle /Users/testuser/git/agent/out/Darwin/Release/swift-nio__NIOFileSystem.bundle (in target 'TESTFileProvider' from project 'TEST') cd /Users/testuser/git/agent/dgagent/agent/macosx/dgc/TEST builtin-copy -exclude .DS_Store -exclude CVS -exclude .svn -exclude .git -exclude .hg -resolve-src-symlinks /Users/testuser/git/agent/out/Darwin/Release/swift-nio__NIOFileSystem.bundle /Users/testuser/git/agent/out/Darwin/Release/TESTFileProvider.appex/Contents/Resources error: /Users/testuser/git/agent/out/Darwin/Release/swift-nio__NIOFileSystem.bundle: No such file or directory (in target 'TESTFileProvider' from project 'TEST') Copy /Users/testuser/git/agent/out/Darwin/Release/TESTFileProvider.appex/Contents/Resources/swift-nio_NIOPosix.bundle /Users/testuser/git/agent/out/Darwin/Release/swift-nio_NIOPosix.bundle (in target 'TESTFileProvider' from project 'TEST') cd /Users/testuser/git/agent/dgagent/agent/macosx/dgc/TEST builtin-copy -exclude .DS_Store -exclude CVS -exclude .svn -exclude .git -exclude .hg -resolve-src-symlinks /Users/testuser/git/agent/out/Darwin/Release/swift-nio_NIOPosix.bundle /Users/testuser/git/agent/out/Darwin/Release/TESTFileProvider.appex/Contents/Resources error: /Users/testuser/git/agent/out/Darwin/Release/swift-nio_NIOPosix.bundle: No such file or directory (in target 'TESTFileProvider' from project 'TEST')
3
0
410
Jul ’24
Why use async/await vs completion handlers?
I'm pretty sure I'm missing something completely obvious, but I can't see what. I watched WWDC session, read the Swift evolution blog, and they all make sense, but still it doesn't click for me. Please help me out here :) . I'm diving into adopting the 'new' async/await style of coding (I know, it's old news at this point, but I could only get to it now), and so I'm all pumped to get my code to go eleven and therefore I wrote a small data-downloader class. It has one method, well two: one oldskool function with a completionHandler, and one new style async/await one. When using the oldskool one, it works as everyone would expect: print(1) dataFetcher.fetchSomeData { print(2) let data = $0 // process data ... print(3) } print(4) The output is, unsurprisingly: 1 4 2 3 Now, when I use my new style function: let data = await dataFetcher.fetchSomeData() // process data ... Xcode gives me an error: 'async' call in a function that does not support concurrency That makes sense, I am calling this in the viewDidLoad() method of a UIViewController subclass. Can't mark viewDidLoad() as await, as super's implementation is not async. No problem, let me wrap it in Task: print(1) Task { print(2) let data = await dataFetcher.fetchSomeData() // process data ... print(3) } print(4) No errors, and this code works exactly as expected, the output is: 1 4 2 3 So now I am wondering: why take the effort of changing/adding code for async/await style function that ultimately end up requiring exactly the same amount of code, and is exactly as non-linear as completionHandlers? Note that the dataFetcher only has one property, an instance ofURLSession, so I am also not even managing my own queues or threads in the oldskool method vs the new one. They just wrap URLSession's functions: func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask and func download(for request: URLRequest, delegate: URLSessionTaskDelegate? = nil) async throws -> (URL, URLResponse) Is async/await useful only with SwiftUI maybe? What am I missing here? Please help me see the light.
8
0
6.4k
Aug ’22
Need Objective-C translation of DispatchSource.makeFileSystemObjectSource
I came across a useful repo on GitHub: https://github.com/GianniCarlo/DirectoryWatcher/blob/master/Sources/DirectoryWatcher/DirectoryWatcher.swift self.queue = DispatchQueue.global() self.source = DispatchSource.makeFileSystemObjectSource(fileDescriptor: descriptor, eventMask: .write, queue: self.queue) self.source?.setEventHandler { [weak self] in self?.directoryDidChange() } self.source?.setCancelHandler() { close(descriptor) } self.source?.resume() How do I translate this to OC version? I have an app that was written in OC and I plan to incorporate this directory watcher into the project.
2
0
458
Jul ’24
Create code at runtime on iOS: possible any more?
I'm the developer of 8th (https://8th-dev.com), which compiles the program at run-time, on the device. There used to be an iOS version which worked, but it seems things have changed since then. I'm trying to distribute an iOS version of an app written in 8th, and am encountering "SIGKILL - CODESIGNING" when trying to execute freshly compiled code. I am doing the 'sys_icache_invalidate' thing after writing into a mmap'ed bit of memory (rwx). I'm not writing into memory that was codesigned, so I don't know why the error is that. Anyway, the question is: is it possible any more to do what I used to be able to do?
5
1
597
Jul ’24
What does ".island" suffix in symbol name mean?
During my analysis of the binary size changes after compiling Swift source code, I discovered symbols with the ".island" suffix. I couldn't find meaningful information about this suffix through my search, so I decided to reach out for assistance. While comparing the changes in binary size after modifying specific code, I noticed a significant increase (from 33MB to 520MB). Upon analyzing the symbols of the enlarged binary using the nm command, I found the following pattern: t _$s12{SomeSymbol}WOb t _$s12{SomeSymbol}WOb.island t _$s12{SomeSymbol}WOb.island2 t _$s12{SomeSymbol}WOb.island3 When I output the symbols of the binary using nm, I noticed many symbols with the same name but different ".island", ".island2", ".island3" suffixes. Disassembling the binary showed that functions with these suffixes simply delegate calls sequentially: x.island3 -> x.island2 -> x.island1 -> x. It appears that these symbols serve as delegates for function calls, but I would like to understand why such duplicated functions with these suffixes are generated. Could someone help me to provide some insights on this matter?
1
0
373
Jul ’24
Bug Report: 'consume' Applied to Unsupported Value
'consume' applied to value that the compiler does not support. This is a compiler bug. Please file a bug with a small example of the bug. public func requestTopicsAndSubTopics() async throws -> (topics: [Topic], subTopics: [String: [SubTopic]]) { var subTopics = [String: [SubTopic]]() let topics = try await getTopics().sorted { $0.index < $1.index } try await withThrowingTaskGroup(of: ([SubTopic], String).self) { [weak self] group in guard let self else { return } for topic in topics { guard let topicId = topic.id else { throw Error.missingId } group.addTask { let subTopics = try await self.getSubtopics(topicId: topicId).sorted { $0.name < $1.name } return (consume subTopics, topicId) } } for try await (resultedSubTopics, topicId) in group { subTopics.updateValue(resultedSubTopics, forKey: topicId) } } return (consume topics, consume subTopics) }
1
0
364
Jul ’24
Link to a Precompiled Static C Library in a Swift Library Package
I want to build a Swift library package that uses modified build of OpenSSL and Curl. I have already statically compiled both and verified I can use them in an Objective-C framework on my target platform (iOS & iOS Simulator). I'm using XCFramework files that contain the static library binaries and headers: openssl.xcframework/ ios-arm64/ openssl.framework/ Headers/ [...] openssl ios-arm64_x86_64-simulator/ openssl.framework/ Headers/ [...] openssl Info.plist I'm not sure how I'm supposed to set up my Swift package to import these libraries. I can use .systemLibrary but that seems to use the embedded copies of libssl and libcurl on my system, and I can't figure out how to use the path: parameter to that. I also tried using a .binaryTarget pointing to the XCFramework files, but that didn't seem to work as there is no module generated and I'm not sure how to make one myself. At a basic high level, this is what I'm trying to accomplish: where libcrypto & libssl come from the provided openssl.xcframework file, and libcurl from curl.xcframework
6
0
634
Jul ’24
Multiple async lets crash the app
Usage of multiple async lets crashes the app in a nondeterministic fashion. We are experiencing this crash in production, but it is rare. 0 libswift_Concurrency.dylib 0x20a8b89b4 swift_task_create_commonImpl(unsigned long, swift::TaskOptionRecord*, swift::TargetMetadata<swift::InProcess> const*, void (swift::AsyncContext* swift_async_context) swiftasynccall*, void*, unsigned long) + 384 1 libswift_Concurrency.dylib 0x20a8b6970 swift_asyncLet_begin + 36 We managed to isolate the issue, and we submitted a technical incident (Case-ID: 8007727). However, we were completely ignored, and referred to the developer forums. To reproduce the bug you need to run the code on a physical device and under instruments (we used swift concurrency). This bug is present on iOS 17 and 18, Xcode 15.1, 15.4 and 16 beta, swift 5 and 6, including strict concurrency. Here's the code for Swift 6 / Xcode 16 / strict concurrency: (I wanted to attach the project but for some reason I am unable to) typealias VoidHandler = () -> Void enum Fetching { case inProgress, idle } protocol PersonProviding: Sendable { func getPerson() async throws -> Person } actor PersonProvider: PersonProviding { func getPerson() async throws -> Person { async let first = getFirstName() async let last = getLastName() async let age = getAge() async let role = getRole() return try await Person(firstName: first, lastName: last, age: age, familyMemberRole: role) } private func getFirstName() async throws -> String { try await Task.sleep(nanoseconds: 1_000_000_000) return ["John", "Kate", "Alex"].randomElement()! } private func getLastName() async throws -> String { try await Task.sleep(nanoseconds: 1_400_000_000) return ["Kowalski", "McMurphy", "Grimm"].randomElement()! } private func getAge() async throws -> Int { try await Task.sleep(nanoseconds: 2_100_000_000) return [56, 24, 11].randomElement()! } private func getRole() async throws -> Person.Role { try await Task.sleep(nanoseconds: 500_000_000) return Person.Role.allCases.randomElement()! } } @MainActor final class ViewModel { private let provider: PersonProviding = PersonProvider() private var fetchingTask: Task<Void, Never>? let onFetchingChanged: (Fetching) -> Void let onPersonFetched: (Person) -> Void init(onFetchingChanged: @escaping (Fetching) -> Void, onPersonFetched: @escaping (Person) -> Void) { self.onFetchingChanged = onFetchingChanged self.onPersonFetched = onPersonFetched } func fetchData() { fetchingTask?.cancel() fetchingTask = Task { do { onFetchingChanged(.inProgress) let person = try await provider.getPerson() guard !Task.isCancelled else { return } onPersonFetched(person) onFetchingChanged(.idle) } catch { print(error) } } } } struct Person { enum Role: String, CaseIterable { case mum, dad, brother, sister } let firstName: String let lastName: String let age: Int let familyMemberRole: Role init(firstName: String, lastName: String, age: Int, familyMemberRole: Person.Role) { self.firstName = firstName self.lastName = lastName self.age = age self.familyMemberRole = familyMemberRole } } import UIKit class ViewController: UIViewController { @IBOutlet private var first: UILabel! @IBOutlet private var last: UILabel! @IBOutlet private var age: UILabel! @IBOutlet private var role: UILabel! @IBOutlet private var spinner: UIActivityIndicatorView! private lazy var viewModel = ViewModel(onFetchingChanged: { [weak self] state in switch state { case .idle: self?.spinner.stopAnimating() case .inProgress: self?.spinner.startAnimating() } }, onPersonFetched: { [weak self] person in guard let self else { return } first.text = person.firstName last.text = person.lastName age.text = "\(person.age)" role.text = person.familyMemberRole.rawValue }) @IBAction private func onTap() { viewModel.fetchData() } }
1
0
465
Jul ’24
Decoding JSON with a reserved property name
I need to decode JSON into a class. The JSON has a field called "Type", and I cannot declare a property with that name in my class since Type is a reserved word. I tried declaring CodingKeys, but that doesn't work unless I declare EVERY property in the CodingKeys. This class has about a hundred properties and I have others like it, I do not want to do this. Is there a better solution?
2
0
311
Jul ’24