【问题标题】:Unable to use class(swift) into objective c无法在目标 c 中使用类(swift)
【发布时间】:2015-11-23 05:55:41
【问题描述】:

我正在执行苹果文档中编写的相同步骤。但我无法访问目标 c 文件中的 swift 类。

代码:

Swift 文件

import Foundation
import UIKit
@objc class checkk : UIViewController {
    func alertView ()
    {
        let al = UIAlertView(title: "HeLLo", message: "Working", delegate: nil, cancelButtonTitle: "OK", otherButtonTitles: "ww", "sdd")

        al.show()

    }
}

Objective C 文件

#import "ViewController.h"

#import "SwiftIntoObjectiveC-Bridging-Header.h"  // automatically generated by Xcode. Nothing is written in this.

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

     checkk *ck =[checkk new];  // error : use of undeclared identifier check.

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.
}

- (IBAction)Click:(id)sender {

}
@end

错误: 使用未声明的标识符检查。

【问题讨论】:

  • 尝试使用 checkk *ck = [[checkk alloc] init];
  • 你的错误意味着你是#import任何地方的checkk类
  • 你忘记在 ViewController 中实现 #import "checkk-Swift.h"

标签: ios objective-c swift


【解决方案1】:

在 Objective-C 中,用于访问 Swift 类的导入如下所示:

#import "YOUR_APP_NAME-Swift.h"

YOUR_APP_NAME 实际上是目标的模块名称,但通常与您的应用相同。

该文件包含 Objective-C 访问 Swift 中定义的符号所需的 Swift 原型。

在您的情况下,您的模块名称可能是 SwiftIntoObjectiveC 并且您可以使用

#import "SwiftIntoObjectiveC-Swift.h"

桥接头永远不会导入到 Objective-C 中,因为它不被 Objective-C 端使用,并且只用于在 Swift 端访问 Objective-C 符号。

【讨论】:

  • 不能通过更改桥接头名称来工作。 @丹尼尔。
  • 不要更改桥接头名称。将导入更改为#import "YOUR_APPS_MODULE_NAME-Swift.h"
【解决方案2】:

另外,请确保编译器可以找到您的桥接头,在大多数情况下,您的桥接头路径可能是问题所在。

这是文档的link

【讨论】:

    【解决方案3】:
    • func alertView()之前添加@objc
    • 添加这一行 #import "YourAppName-Swift.h" 而不是 #import "SwiftIntoObjectiveC-Bridging-Header.h"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      相关资源
      最近更新 更多