无法从objective-c访问swift 4类的属性

时间:2022-09-24 23:16:28

I've seen a lot of questions and answers on this, but none of them seem to cover or solve my situation.

我已经看到了很多问题和答案,但它们似乎都没有涵盖或解决我的情况。

I have a project originally written in Objc, and now, one by one, I'm replacing all classes with Swift 4 syntax.

我有一个最初用Objc编写的项目,现在,一个接一个,我用Swift 4语法替换所有类。

My problem is, that I cannot access swift properties in my Objective C files.

我的问题是,我无法访问Objective C文件中的swift属性。

This is what I have so far:

这是我到目前为止:

I have this one swift file, called MessagesViewController.swift that starts as follows:

我有一个名为MessagesViewController.swift的swift文件,其启动方式如下:

@objc class MessagesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

    @objc var mailboxNumber:NSNumber = 0
        ...

Then, in there is the objc class "MessagesBoxController" that needs to access mailboxNumber in the prepareForSegue method:

然后,有一个objc类“MessagesBoxController”需要访问prepareForSegue方法中的mailboxNumber:

So, in MessagesBoxController.h I import the generated interface header:

因此,在MessagesBoxController.h中我导入生成的接口头:

#import "myProduct-Swift.h"

and in MessagesBoxController.m I have this boilerplate method:

在MessagesBoxController.m中我有这个样板方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    MessagesViewController *destViewController = segue.destinationViewController;
    NSIndexPath *selectedIndexPath = [_tv indexPathForSelectedRow];
    destViewController.mailboxNumber =selectedIndexPath.row+1;
}

So, the Swift class "MessagesViewController" is recognised fine; no complaints by the compiler whatsoever. Its properties though are not exposed:

所以,Swift类“MessagesViewController”被认可很好;没有编译器的任何投诉。它的属性虽然没有暴露:

auto complete doesn't suggest mailboxNumber and upon building the project, I get the error

自动完成不建议mailboxNumber,并在构建项目时,我收到错误

"Property 'mailboxNumber' not found on object of type 'MessagesViewController *'"

“在'MessagesViewController *'类型的对象上找不到”属性'mailboxNumber'“

What part is missing to make this work?

缺少什么部分使这项工作?

1 个解决方案

#1


1  

OK, So, the basic setup is correct. I ended up cleaning the project. I then got the message that "myProduct-Swift.h" could not be found. Then I quit XCode and deleted the derived data. That helped.

好的,所以,基本设置是正确的。我最终清理了这个项目。然后我收到消息“myProduct-Swift.h”找不到。然后我退出XCode并删除了派生数据。这有帮助。

#1


1  

OK, So, the basic setup is correct. I ended up cleaning the project. I then got the message that "myProduct-Swift.h" could not be found. Then I quit XCode and deleted the derived data. That helped.

好的,所以,基本设置是正确的。我最终清理了这个项目。然后我收到消息“myProduct-Swift.h”找不到。然后我退出XCode并删除了派生数据。这有帮助。