从 Objective-c 传递 CLLLocation 数组会导致崩溃

Passing a CLLLocation array from Objective-c produces a crash

最近,当我尝试将 CLLocation 数组从 Objective-c 模块传递到 Swift 模块时,我得到:

fatal error: NSArray element failed to match the Swift Array Element type

我是这样调用函数的:

routeLine = [PreloadedLine lineWithLoadedPath:preloadedPath 
    key:lineKey 
    andNotification:(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)];

这里是函数的 Swift 签名:

class func lineWithLoadedPath(path: [CLLocation]?, 
    key:String?, 
    andNotification notification:Bool)->Line?

这是它的 Objective-c 签名:

+ (Line * __nullable)lineWithLoadedPath:(NSArray<CLLocation *> * __nullable)path 
    key:(NSString * __nullable)key 
    andNotification:(BOOL)notification;

相信错误信息。不是 CLLocation 的东西正在进入 NSArray。

因此,在 Objective-C 方面,您是说:

 [PreloadedLine lineWithLoadedPath:preloadedPath ...

...但是 preloadedPath 是一个 NSArray,其中包含的内容不全是 CLLocation,因此在 Swift 方面,应用程序崩溃了。

我还将后者 class 连同其层次结构移植到 Swift,这个问题自行解决了。