ios - 从 Obj-c 到 swift 的 Segue 找不到属性

标签 ios objective-c swift segue objc-bridging-header

我正在尝试将 int 从 objective-c 文件传递​​到 swift。

我所采取的步骤确实是 segue,但遗憾的是,由于以下错误,它没有传递 int:Property 'productKey' not found on object of type 'ProductViewController *'

准备转场

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([segue.identifier isEqualToString:@"viewProduct"]) {
    ProductViewController *destVC = segue.destinationViewController;
    destVC.productKey = brandID;
  }
}

执行转场

dispatch_async(dispatch_get_main_queue(), ^(){
   [self performSegueWithIdentifier:@"viewProduct" sender:self];
});

快速文件

class ProductViewController: UIViewController {

    var productKey: Int!

    override func viewDidLoad() {
        super.viewDidLoad()
           print(productKey)
    }
}

头桥

 //
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "ARSceneViewController.h"

最佳答案

如果您在 objc 类中使用 Swift 文件,桥接 header 在这里无济于事。

Xcode 将创建您需要导入的头文件

首先转到build设置并搜索 Defines Modules 并将其设置为 true。

现在在.m文件中添加如下头文件

#import "YourTargetName-Swift.h" 

并构建项目

并且在你的属性之前添加@objc

希望对你有帮助

关于ios - 从 Obj-c 到 swift 的 Segue 找不到属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50511740/

相关文章:

ios - Facebook 登录后无法推送到 PFQueryTableViewController

ios - 代码 : reasons for not using Core Data?

ios - VIPER 中的 UINotificationFeedbackGenerator?

objective-c - 如何从其父类(super class)的实例分配 NSObject 子类实例?

ios - 如何在iOS中获取路径扩展

iOS Swift - 推送通知 : Save all Remote-Notifications in a Database (also while App is terminated)

ios - 使用 Swift 2.0 播放 AVPlayer

objective-c - 在 NSTableCellView 中滚动时跟踪鼠标事件的问题

json - 键嵌套数组和/或字典的 Swift JSON 对象

ios - Swift 4 - 抛出函数的无效转换