【问题标题】:Can't see Swift variable/method in Objective C在 Objective C 中看不到 Swift 变量/方法
【发布时间】:2018-09-11 10:32:50
【问题描述】:

我无法从 Objective C 中看到将myVar 设置为true 的方法/变量,即使很难,我已经添加了@objcpublic 修饰符,并且@987654326 中没有Bool @方法的签名。

这可能是由于 Swift 的 Bool 和 Objective C 的 BOOL 之间的差异造成的。

其他类方法/变量是可见的,只是这个特定的不可见。

如何在 Objective C 中设置 Swift 的 Bool

Swift 代码:

public class MyViewController : UIViewController {
    public var myVar:Bool = false

    @objc public func setMyVarTrue() {
        self.myVar = true
    }
}

目标 C 代码:

MyViewController* myViewController = [MyViewController new];
myViewController.myVar = true // Variable not found
myViewController.setMyVarTrue() // Method not found
[self presentViewController:myViewController animated:NO completion:nil];

【问题讨论】:

  • 您是否导入了 modulename-swift.h 文件?您的属性和方法在该文件中可见吗?
  • 在你的objective c项目中导入swift文件,然后只能从objective c访问它
  • objective-c 中将是[myViewController setMyVarTrue];。欢迎从swift回到objective-c

标签: ios objective-c swift bridging-header objective-c-swift-bridge


【解决方案1】:

你应该这样做

  1. 将@objc 添加到类的声明中

  2. 添加一个名为“yourName-swift.h”的文件,其中应包含@class yourClass;

  3. 在您要调用的 Objective C 文件中导入“yourName-swift.h”

你的快速课程

【讨论】:

    【解决方案2】:

    解决方案是清理并重建项目。 Xcode 自动生成 swift 桥接头,在我的情况下,它在重新生成桥接头之前停止了构建过程并出现(未找到变量/方法)错误。

    SWIFT_CLASS("_TtC11MyProject25MyViewController")
    @interface MyViewController : UIViewController
    @property (nonatomic, strong) MBProgressHUD * _Nonnull progressIndicator;
    - (nonnull instancetype)initWithNibName:(NSString * _Nullable)nibNameOrNil bundle:(NSBundle * _Nullable)nibBundleOrNil OBJC_DESIGNATED_INITIALIZER;
    - (nullable instancetype)initWithCoder:(NSCoder * _Nonnull)aDecoder OBJC_DESIGNATED_INITIALIZER;
    - (void)viewDidLoad;
    - (void)viewWillAppear:(BOOL)animated;
    - (void)submitProgressHandler:(NSNotification * _Nonnull)notification;
    - (void)submitSuccessHandler;
    - (void)submitFailureHandler:(NSNotification * _Nonnull)notification;
    - (void)subscribe;
    - (void)unsubscribe;
    @end
    

    现在可以正常使用了:

    SWIFT_CLASS("_TtC11MyProject25MyViewController")
    @interface MyViewController : UIViewController
    @property (nonatomic, strong) MBProgressHUD * _Nonnull progressIndicator;
    @property (nonatomic) BOOL closing;
    - (nonnull instancetype)initWithNibName:(NSString * _Nullable)nibNameOrNil bundle:(NSBundle * _Nullable)nibBundleOrNil OBJC_DESIGNATED_INITIALIZER;
    - (nullable instancetype)initWithCoder:(NSCoder * _Nonnull)aDecoder OBJC_DESIGNATED_INITIALIZER;
    - (void)viewDidLoad;
    - (void)viewWillAppear:(BOOL)animated;
    - (void)setClosingTrue;
    - (void)submitProgressHandler:(NSNotification * _Nonnull)notification;
    - (void)submitSuccessHandler;
    - (void)submitFailureHandler:(NSNotification * _Nonnull)notification;
    - (void)subscribe;
    - (void)unsubscribe;
    @end
    

    【讨论】:

      猜你喜欢
      • 2019-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多