xcode 获取类消息的错误接收器是一个前向声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41800808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:44:07  来源:igfitidea点击:

Getting Error Receiver for class message is a forward declaration

iosobjective-cswiftxcode

提问by Aleem

I'm working on creating my own framework, I added objective c files and its working fine after that I needed to add some swift file after adding swift file xcode is not giving me option of auto Creating Bridgingthen I followed Mix and Match approach. I followed everything which I thing I understand.

我正在创建我自己的框架,我添加了目标 c 文件并且它工作正常之后我需要在添加 swift 文件后添加一些 swift 文件 xcode 没有给我自动创建桥接的选项,然后我遵循了混合和匹配方法。我遵循了我理解的一切。

I want to access swift file into Objective c but I'm getting error of forward declaration so Here is sample code I attached, kindly guide me where I'm doing wrong.

我想将 swift 文件访问到 Objective c 中,但是我收到了前向声明的错误,所以这是我附上的示例代码,请指导我哪里做错了。

TestObjectCFile.h

测试对象CFile.h

#import <Foundation/Foundation.h>
@class TestSwiftFile;
@interface TestObjCFile : NSObject


@end

TestObjectCFile.m

测试对象CFile.m

#import "TestObjCFile.h"
#import <TestFrameworkTry/TestFrameworkTry-Swift.h>
#import "TestFrameworkTry.h"


@implementation TestObjCFile

- (void)TestMethodForImportingSwiftFile
{

    TestSwiftFile * testSwiftFile = [[TestSwiftFile alloc] init];
//    TestSwiftFile * testSwiftFile = [self returnSwiftClassInstance];
    NSLog(@"%@",testSwiftFile);
}
@end

TestSwiftFile.swift

TestSwiftFile.swift

import Foundation

And the error occur on TestObjectCFile.mfollowing line.

错误发生在TestObjectCFile.m以下行上。

TestSwiftFile * testSwiftFile = [[TestSwiftFile alloc] init];

as show attached picture. enter image description here

如图所示。 在此处输入图片说明

回答by Aleem

Finally I got solution. I did following changes. Here below is to do list in your Framework.

最后我得到了解决方案。我做了以下更改。以下是您的框架中的待办事项列表。

Swift classes usage in Objective C classes

Swift 类在 Objective C 类中的使用

  • Use Open Keyword before class name in Swift
  • Use @objc Keyword before Open Keyword of Swift class
  • Import all header of objective c classes in Umbrella file YourProject.h those are consuming Swift Classes.
  • Use #import <YourProjectName/YourProjectName-Swift.h>in Objective c
  • 在 Swift 中的类名之前使用 Open 关键字
  • 在 Swift 类的 Open 关键字之前使用 @objc 关键字
  • 在 Umbrella 文件 YourProject.h 中导入所有使用 Swift 类的目标 c 类的头文件。
  • #import <YourProjectName/YourProjectName-Swift.h>在目标 c 中使用

I followed Apple's Mix and Match approach.

我遵循了 Apple 的 Mix and Match 方法。

Note: sometimes the <YourProjectName/YourProjectName-Swift.h>file can not be imported into headers, but only into .mfiles. FYI, this file is created automatically by the compiler. If you search your project for it, you won't be able to find it, but if you ? click on it (as a file imported in your code) in a file that is in your target, Xcode will (should) open it and show you all the Objective-C class interfaces for your Swift classes. To see the name for the Swift -> Objective-C module header, go to your target's Build Settings and search for $(SWIFT_MODULE_NAME)-Swift.hor simply -Swift.

注意:有时<YourProjectName/YourProjectName-Swift.h>文件不能导入到标题中,而只能导入到.m文件中。仅供参考,这个文件是由编译器自动创建的。如果您在您的项目中搜索它,您将无法找到它,但是如果您 ? 在目标中的文件中单击它(作为导入到代码中的文件),Xcode 将(应该)打开它并向您显示 Swift 类的所有 Objective-C 类接口。要查看 Swift -> Objective-C 模块头文件的名称,请转到目标的构建设置并搜索$(SWIFT_MODULE_NAME)-Swift.h-Swift