在swift类中找不到在ObjectiveC类中选择器的已知类方法

时间:2019-01-09 13:24:03

标签: ios objective-c swift swift4 xcode10.1

我在一个swift类中编写了一个类函数,然后尝试从一个ObjectiveC类访问相同的函数,但选择器'myMethodName'的类方法未知。我也做了#import "ProjectName-Swift.h"。 Xcode低于10.1可以正常工作。请尽早帮助我。提前致谢。

import UIKit

typealias LoginCompletionHandler = (_: Bool) -> Void

class SCLoginViewController: UIViewController {

    var myLoginCompletionBlock: LoginCompletionHandler? = nil
    // MARK: - View Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    //PRAGMA:- Class Methods
    class func showOnNavigation(navigationController:UINavigationController, completionBlock:@escaping LoginCompletionHandler) {
        let vc = SCLoginViewController.init(nibName: "SCLoginViewController", bundle: nil)
        vc.myLoginCompletionBlock = completionBlock
        navigationController.pushViewController(vc, animated: true)
    }

}


#import "LoginOptionVC.h"
#import "Project-Swift.h"

@interface LoginOptionVC () <>

@end

@implementation LoginOptionVC


- (instancetype)initWithBarButtonType:(BarButtonType)btnType showClubLogo:(BOOL)showLogo onCompletion:(CompletionHandler) completionBlock {

    self = [[ClubLoginOptionVC alloc]initWithNibName:@"ClubLoginOptionVC" bundle:nil];

    if (self) {
        self.myCompletionBlock = completionBlock;
    }

    return self;
}


#pragma mark - View LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
- (IBAction)clickLogin:(id)sender {
    [SCLoginViewController showOnNavigationWithNavigationController:self.navigationController completionBlock:^(BOOL isSuccess) {
        if (isSuccess) {
            NSLog(@"logged in ");
        }

        if (self.myCompletionBlock) {
            self.myCompletionBlock(isSuccess);
        }
    }];
}
}

2 个答案:

答案 0 :(得分:3)

在功能上添加@objc属性,以将Swift代码公开给Objective-C

@obj class func showOnNavigation(navigationController:UINavigationController, completionBlock:@escaping LoginCompletionHandler)

答案 1 :(得分:0)

除了Scriptable的答案。如果要在类中将所有函数公开给Objective-C。然后添加@objcMembers属性。

@objcMembers class MyController: UIViewController {
   class func showOnNavigation(navigationController:UINavigationController, completionBlock:@escaping LoginCompletionHandler) {

   }
}

通过在类中添加@objcMembers属性的方法,您无需在函数中添加@objc属性。