gpt4 book ai didi

ios - 我想在 swift 中使用 Objective C 代码。添加桥接 header 后,我开始收到所有原始数据类型的错误

转载 作者:行者123 更新时间:2023-11-30 12:48:08 24 4
gpt4 key购买 nike

我有一个模型

@interface Section : NSObject
@property (nonatomic, strong) NSString *sectionId;
@property (nonatomic, strong) NSString *title;

+(instancetype)createObject:(NSDictionary *)sectionInfo;
@end

我的桥接 header 如下所示

// MyProj-Bridging-Header.h    

#ifndef MyProj_Bridging_Header_h
#define MyProj_Bridging_Header_h

#import "Section.h"

#endif /* MyProj_Bridging_Header_h */

我的 Swift Controller 看起来像这样

@objc class SampleSwiftVC : UIViewController {
var supportedSectionInfo: Section?
}

我开始收到编译时错误,例如

/*PathToProject*/Section.h:17:39: error: expected a type
+(instancetype)createObject:(NSDictionary *)sectionInfo;
^
<unknown>:0: error: failed to import bridging header '/*PathToProject*/MyProj-Bridging-Header.h'

我转到build设置,发现头文件引用是为 Objective-C Bridging header 键设置的 enter image description here

最佳答案

我发现了这个问题。问题是 ObjectiveC 模型是从 NSObject.h 继承的。这是实际的问题,因为未导入 Foundation 类。添加

#import <Foundation/Foundation.h>

之前

@interface Section : NSObject

解决了问题

关于ios - 我想在 swift 中使用 Objective C 代码。添加桥接 header 后,我开始收到所有原始数据类型的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41359133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com