Objective c 如何选择将使用桥接文件移植到ObjC的Swift文件?
我正在使用.h桥头文件在我的ObjC项目中使用Swift类。 但是没有任何Swift文件/头被编译到其中,只有来自环境的默认类 查看正在生成的标题中的代码:Objective c 如何选择将使用桥接文件移植到ObjC的Swift文件?,objective-c,swift,Objective C,Swift,我正在使用.h桥头文件在我的ObjC项目中使用Swift类。 但是没有任何Swift文件/头被编译到其中,只有来自环境的默认类 查看正在生成的标题中的代码: // Generated by Apple Swift version 1.2 (swiftlang-602.0.51.1 clang-602.0.51) #pragma clang diagnostic push #if defined(__has_include) && __has_include(<swift/
// Generated by Apple Swift version 1.2 (swiftlang-602.0.51.1 clang-602.0.51)
#pragma clang diagnostic push
#if defined(__has_include) && __has_include(<swift/objc-prologue.h>)
# include <swift/objc-prologue.h>
#endif
#pragma clang diagnostic ignored "-Wauto-import"
#include <objc/NSObject.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#if defined(__has_include) && __has_include(<uchar.h>)
# include <uchar.h>
#elif !defined(__cplusplus) || __cplusplus < 201103L
typedef uint_least16_t char16_t;
typedef uint_least32_t char32_t;
#endif
typedef struct _NSZone NSZone;
#if !defined(SWIFT_PASTE)
# define SWIFT_PASTE_HELPER(x, y) x##y
# define SWIFT_PASTE(x, y) SWIFT_PASTE_HELPER(x, y)
#endif
#if !defined(SWIFT_METATYPE)
# define SWIFT_METATYPE(X) Class
#endif
#if defined(__has_attribute) && __has_attribute(objc_runtime_name)
# define SWIFT_RUNTIME_NAME(X) __attribute__((objc_runtime_name(X)))
#else
# define SWIFT_RUNTIME_NAME(X)
#endif
#if !defined(SWIFT_CLASS_EXTRA)
# define SWIFT_CLASS_EXTRA
#endif
#if !defined(SWIFT_PROTOCOL_EXTRA)
# define SWIFT_PROTOCOL_EXTRA
#endif
#if !defined(SWIFT_ENUM_EXTRA)
# define SWIFT_ENUM_EXTRA
#endif
#if !defined(SWIFT_CLASS)
# if defined(__has_attribute) && __has_attribute(objc_subclassing_restricted)
# define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) __attribute__((objc_subclassing_restricted)) SWIFT_CLASS_EXTRA
# else
# define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA
# endif
#endif
#if !defined(SWIFT_PROTOCOL)
# define SWIFT_PROTOCOL(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) SWIFT_PROTOCOL_EXTRA
#endif
#if !defined(SWIFT_EXTENSION)
# define SWIFT_EXTENSION(M) SWIFT_PASTE(M##_Swift_, __LINE__)
#endif
#if !defined(OBJC_DESIGNATED_INITIALIZER)
# if defined(__has_attribute) && __has_attribute(objc_designated_initializer)
# define OBJC_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer))
# else
# define OBJC_DESIGNATED_INITIALIZER
# endif
#endif
#if !defined(SWIFT_ENUM)
# define SWIFT_ENUM(_type, _name) enum _name : _type _name; enum SWIFT_ENUM_EXTRA _name : _type
#endif
#if __has_feature(nullability)
# define SWIFT_NULLABILITY(X) X
#else
# if !defined(__nonnull)
# define __nonnull
# endif
# if !defined(__nullable)
# define __nullable
# endif
# if !defined(__null_unspecified)
# define __null_unspecified
# endif
# define SWIFT_NULLABILITY(X)
#endif
#if defined(__has_feature) && __has_feature(modules)
#endif
#pragma clang diagnostic ignored "-Wproperty-attribute-mismatch"
#pragma clang diagnostic ignored "-Wduplicate-method-arg"
#pragma clang diagnostic pop
那么,我如何选择要编译到这个头文件中的类/文件,以便在我的Obj-C项目中使用它们呢?
谢谢
编辑
所有Swift类都有@objc前缀,并从NSObject继承。桥接头不用于将Swift文件包含在目标C文件中,相反。将您的Objective C文件导入桥接头,然后您就可以在swift文件中使用这些类 我的标题叫做
SmartFusion-Bridging-Header.h
在里面,我引进了这样的东西
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "ViewController.h"
#import <AFNetworking.h>
要将Swift导入到Objective C中,让这些桥自动生成,您不需要做任何事情
相反,请在希望包含Swift类的每个Objective C文件的顶部导入此语句
#import <YourAppName-Swift.h>
也许Swift类与Objective-C不兼容?它们是从NSObject继承的还是用@objc标记的?是的,它们在类上都有@objc前缀,并且是从NSObject继承的。另一个秘密:Swift类的“目标成员资格”复选框被打开了?是的,所有Swift文件都在目标成员资格中。除非我完全弄错了,问题中显示了的内容,所以OP可能知道该机制。我想我弄错了名称。我指的是生成的接口头。但无论如何,我已经在使用import MYAPPNAME Swift.h导入的每个文件中。@MartinR除非在OP的问题中明确说明,否则我在陈述显而易见的问题时没有问题。很多问题都是这样回答的。