首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >'SWIFT_NORETURN‘宏已重新定义

'SWIFT_NORETURN‘宏已重新定义
EN

Stack Overflow用户
提问于 2021-05-04 20:46:04
回答 2查看 142关注 0票数 2

在Xcode12.5上,当我试图编译我的测试目标时会得到这样的结果:

代码语言:javascript
复制
/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

Project-Swift.h

代码语言:javascript
复制
#if __has_attribute(noreturn)
# define SWIFT_NORETURN __attribute__((noreturn))
#else
# define SWIFT_NORETURN
#endif

和Visibility.h

代码语言:javascript
复制
#if __has_attribute(noreturn)
#define SWIFT_NORETURN __attribute__((__noreturn__))
#else
#define SWIFT_NORETURN
#endif

注意:这适用于Xcode12.4。

你知道怎么解决这个问题吗?

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

更新2:(6月3日):问题似乎是我在一个.h文件中定义了一个类,它符合一个快速文件中定义的协议,不同的目标。This解释了为什么我不能使用转发声明,这样我就不必导入"MyProject-Swift.h“文件。

EN

回答 2

Stack Overflow用户

发布于 2021-05-18 01:03:54

我不清楚为什么你要在不同的地方声明宏,但是肯定有一个循环导入,或者它最终被重新定义的地方。

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

选项2是包装每个声明,这样它就不会被重新定义:

代码语言:javascript
复制
#ifndef SWIFT_NORETURN

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

#endif

或者:

代码语言:javascript
复制
#if __has_attribute(noreturn) && !defined(SWIFT_NORETURN)
#define SWIFT_NORETURN __attribute__((__noreturn__))
#else
#define SWIFT_NORETURN
#endif
票数 1
EN

Stack Overflow用户

发布于 2021-06-02 19:41:38

试着检查一下,你的头文件不包含行#import "Project-Swift.h"。理想情况下,只有*.m文件应该包含这个生成的头文件。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67385055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档