首页 > 解决方案 > 如何在 app 和 Bundle 中使用相同的类?

问题描述

在我的应用程序中,我将一个对象从我的应用程序传递到我的包。问题是 Swift 认为它们是不同的类型。因此,当我进行检查时,它会失败,说这MyApp.RestaurantMyBundle.Restaurant.

func exportRestaurant(_ restaurant: Restaurant) {
    if let restaurant: Restaurant = restaurant as? Restaurant {
        // This will never be executed.
    }
    let restaurant: Restaurant = restaurant as! Restaurant
    // This will crash the app. Could not cast value of type 'MyApp.Restaurant' (0x100ba6460) to 'MyAppSupportingBundle.Restaurant' (0x104eaaec0).
}

所以,我不能将它转换为类型。有什么办法可以克服这个吗?

标签: iosswift

解决方案


推荐阅读