【发布时间】:2018-05-25 12:09:00
【问题描述】:
我正在尝试将 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];
});
Swift 文件
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放在var productKey或class ProductViewController前面? -
@dfd 嘿,在 swift 文件的 var 前面添加“@objc”确实可以消除目标 c 错误。但是,我现在不确定如何创建 var empty 以供 segue 设置。
-
@dfd 我得到以下错误:'属性不能被标记为 objc,因为它的类型不能在 Objective-C 中表示'
-
我最赞成的答案可能会对您有所帮助 - 基本上是添加一个默认值。见这里:stackoverflow.com/questions/46024160/…
标签: ios objective-c swift segue objc-bridging-header