I've found numerous post about the NSArray element failed to match the Swift Array Element type
error. However, I still can't get this to work. I suspect the problem is related to the objective C framework binding I made and is not a casting issue. Among others, one thing I do not understand is why the code work inside a lldb p
instruction while not as an unit test executed within host ios application (I can't test it otherwise as the code requires B-LE which is not available from the iphone simulator).
class onceFirstMuseConnected: IXNMuseListener {
let museManager: IXNMuseManager
let callback: (_ muse: IXNMuse) -> Void
init(museManager: IXNMuseManager, callback: @escaping (_ muse: IXNMuse) -> Void) {
self.museManager = museManager
self.callback = callback
museManager.setMuseListener(self)
museManager.startListening()
}
func museListChanged() {
let muses: [IXNMuse] = museManager.getMuses()
guard muses.count > 0 else {
return
}
let _ = muses.first!.getName() // <<<< `muses.first` fails as well as any other
// kind of array's item access with "Fatal
// error: NSArray element failed to match the
// Swift Array Element type"
}
}
What's weird is I am able to make the line work in lldb as shown on the image below.
edit: As I've got multiple downvote, I would appreciate an explanation on why the lldb line work in comment while the main execution crashes. I assume the lldb line wouldn't work if it was a type conversion issue. None of the related questions I found answer this. I admit I am a swift/objc newbie, there is likely something obvious I don't understand if it is not an issue related to the objc-swift framework mapping process.
The getMuses
function is wrapped by djinni using a module map I wrote to bind a proprietary objective C framework to swift. Here is the module map I wrote. I have had no issue with the other function bindings yet (see comments). Removing the [system]
attribute don't trigger any additional warning.
module Muse [system] {
header "Muse.framework/Headers/Muse.h"
export *
}
The djinni generated declaration:
public func getMuses() -> [IXNMuse]
The original objc declaration:
- (nonnull NSArray<IXNMuse *> *)getMuses;
The definition is unavailable as the framework is proprietary.
The returned pointer is unlikely to be null or to point out a wrong memory address as the lldb-made call to the getName method shown in the screenshot wouldn't give proper result if so.