2

在 Xcode 12.5 上,当我尝试编译我的测试目标时,我得到了这个:

/Users/user/Development/project-ios/My ProjectTests/My ProjectTests-Bridging-Header.h:7:9: note: in file included from /Users/user/Development/project-ios/My ProjectTests/My ProjectTests-Bridging-Header.h:7:
#import "SpecHelper.h"
        ^
/Users/user/Development/project-ios/My ProjectTests/SpecHelper.h:36:9: note: in file included from /Users/user/Development/project-ios/My ProjectTests/SpecHelper.h:36:
#import "My_Project-Swift.h"
        ^
/Users/user/Library/Developer/Xcode/DerivedData/My._Project-eybfcywypdtewfbihzpxuwyltrxw/Build/Intermediates.noindex/My Project.build/Debug-iphonesimulator/My Project.build/DerivedSources/My_Project-Swift.h:98:10: error: 'SWIFT_NORETURN' macro redefined
# define SWIFT_NORETURN __attribute__((noreturn))
         ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.5.sdk/usr/lib/swift/shims/Visibility.h:86:9: note: previous definition is here
#define SWIFT_NORETURN __attribute__((__noreturn__))
        ^
<unknown>:0: error: failed to import bridging header '/Users/user/Development/project-ios/My ProjectTests/My ProjectTests-Bridging-Header.h'
Command MergeSwiftModule failed with a nonzero exit code

项目-Swift.h

#if __has_attribute(noreturn)
# define SWIFT_NORETURN __attribute__((noreturn))
#else
# define SWIFT_NORETURN
#endif

和可见性.h

#if __has_attribute(noreturn)
#define SWIFT_NORETURN __attribute__((__noreturn__))
#else
#define SWIFT_NORETURN
#endif

注意:这适用于 Xcode 12.4。

知道如何解决这个问题吗?

更新(5 月 27 日):如果我删除派生数据,运行测试套件失败并出现上述错误。尝试第二次运行它似乎有效。我不知道为什么会这样。

更新 2:(6 月 3 日):问题似乎是我在 .h 文件中定义了一个类,该类符合 swift 文件中定义的协议,不同的目标。解释了为什么我不能使用前向声明,所以我不必导入“MyProject-Swift.h”文件。

4

2 回答 2

1

尝试检查您的头文件是否包含 line #import "Project-Swift.h"。理想情况下,只有*.m文件应包含此生成的标头。

https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c

于 2021-06-02T11:41:37.623 回答
1

我不清楚为什么要在不同的地方声明宏,但必须有循环导入或最终重新定义的东西。

选项 1 是重构代码,以便您只在一处声明该宏,然后从必要的位置导入它。

选项 2 是包装每个声明,使其不会被重新定义:

#ifndef SWIFT_NORETURN

#if __has_attribute(noreturn)
#define SWIFT_NORETURN __attribute__((__noreturn__))
#else
#define SWIFT_NORETURN
#endif

#endif

或者:

#if __has_attribute(noreturn) && !defined(SWIFT_NORETURN)
#define SWIFT_NORETURN __attribute__((__noreturn__))
#else
#define SWIFT_NORETURN
#endif
于 2021-05-17T17:03:54.197 回答