0

我无法从Objective C中看到要设置的方法/变量,myVar即使true我已经添加了@objcandpublic修饰符并且方法签名Bool中没有。setMyVarTrue()

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

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

如何Bool从 Objective C 设置 Swift?

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];
4

2 回答 2

0

你应该做

  1. 将@objc 添加到您的班级声明中

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

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

你的快速课程

于 2018-09-11T10:54:29.253 回答
0

解决方案是清理并重建项目。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
于 2018-09-11T11:37:39.500 回答