【问题标题】:Swift code in Object-C: No visible @interface for 'ClassName' declares the selector 'alloc'Object-C 中的 Swift 代码:“ClassName”没有可见的@interface 声明选择器“alloc”
【发布时间】:2015-10-26 22:23:09
【问题描述】:

我正在尝试在 Objective-C 代码中使用 swift 类。

 ThisClass *thisClass = [[ThisClass alloc] init];

但是,在尝试分配和初始化该类时,我收到此错误:

“ThisClass”没有可见的@interface 声明选择器“alloc”

在 Swift 文件中这样声明类:

@objc class ThisClass : NSObject {

类继承自NSObject,所以不知道是什么问题。

这是我的 ProjectName-swift.h 中的课程:

SWIFT_CLASS("_TtC21ProductName16ClassName")
@interface ThisClass : NSObject
@property (nonatomic, copy) NSArray<Presentation *> * __nonnull presentations;
@property (nonatomic, copy) NSArray<Presentation *> * __nonnull userPresentations;
@property (nonatomic, strong) NSCache * __nonnull imageCache;
- (UIImage * __nullable)imageForPresentationSlideWithPresentation:(Presentation * __nonnull)presentation slideNumber:(NSInteger)slideNumber;
- (UIImage * __nullable)thumbnailForPresentationSlideWithPresentation:(Presentation * __nonnull)presentation slideNumber:(NSInteger)slideNumber;
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

【问题讨论】:

  • 谢谢,我改了 - 试图让真正的源代码保密
  • 有道理,但我在顶部有这个:#import "ProjectName-swift.h"
  • 成功了。看我的回答。

标签: swift


【解决方案1】:

问题在于,在您的真实代码中(与您在此处显示的相反)您将变量PresentationData的名称大写。但这已经是类的名称。这类事情会使编译器发疯(而且对您的代码的读者也不利 - 变量名应始终以小写字母开头)。

【讨论】:

  • 这是世界上最愚蠢的事情!非常感谢 :) 你是个天才,马特!
  • 我知道你会喜欢的! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-23
  • 2020-07-27
相关资源
最近更新 更多