swift属性的未知类型名称 [英] Unknown type name for swift property

查看:71
本文介绍了swift属性的未知类型名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Swift编写的CustomViewController类和一个用Objective C编写的CustomNavigationController类。我正在尝试将CustomNavigationController作为属性添加到我的CustomViewController中。我已将 #importCustomNavigationController.h添加到我的桥接标题中。

I have a CustomViewController class written in swift and a CustomNavigationController class written in Objective C. I'm trying to add my CustomNavigationController as a property to my CustomViewController. I've added #import "CustomNavigationController.h" to my bridging header.

在我的CustomViewController中我有:

In my CustomViewController I have:

class CustomViewController: UIViewController {

    var navController: CustomNavigationController?
...
//init methods


...


 override func viewDidLoad() {
        super.viewDidLoad()

        //Set up Navigation Controller
        navController = self.storyboard.instantiateViewControllerWithIdentifier("CustomNavigationController") as CustomNavigationController!
}

在我尝试构建和运行之前没有错误...我得到了未知类型名称'CustomNavigationController';你的意思是'UINavigationController'?

There are no errors until I try to build and run...I get "unknown type name 'CustomNavigationController'; did you mean 'UINavigationController'?"

有谁知道为什么它不能识别这种类型?

Does anyone know why it doesn't recognize the type?

推荐答案

在Objective-C代码中,您可以导入自动生成的 -Swift.h 标题。在相同的代码中,之前 #import 行,插入 #importCustomNavigationController.h。这两个 #import 语句的顺序至关重要!

In your Objective-C code, you are somewhere importing the automatically generated -Swift.h header. In that same code, before that #import line, insert #import "CustomNavigationController.h". The order of these two #import statements is crucial!

这将解决问题,确保CustomNavigationController是在自动生成 -Swift.h 标题之前的命名空间中,后者会知道前者并且一切都会好的。

This will solve the problem by making sure that CustomNavigationController is in the namespace before the automatically generated -Swift.h header, and so the latter will know about the former and all will be well.

如果Objective-C类不需要了解CustomNavigationController,这是一件令人讨厌的事情,但它解决了前面的问题,并允许你继续使用你的混合项目。

This is something of an annoyance if that Objective-C class had no need to know about CustomNavigationController, but it solves the problem going forward and allows you to continue to work with your hybrid project.

这篇关于swift属性的未知类型名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆