繁体   English   中英

在Swift中将闭包设置为变量并尝试从Objective C类调用

[英]Setting a closure as a variable in Swift and trying to call from Objective C class

我在Swift Class中将变量定义为闭包。 从Swift类调用变量时,我的代码工作正常。 但是Objective C类无法访问变量。 我可以打电话的其他功能。

    @objc class IAPHelper: NSObject {
    static let shared = IAPHelper()
    var purchaseStatusBlock: ((IAPHelperAlertType) -> Void)?  
     }

    called from a swift class 
    IAPHelper.shared.purchaseStatusBlock = {[weak self] (type) in
                guard let strongSelf = self else{ return }
                if type == .purchased {
                   // show an alert
                }
            } 

    I tried to call variable purchaseStatusBlock from Objective C class  
    IAPHelper.shared.purchaseStatusBlock ,
    Compiler Error :Property 'purchaseStatusBlock' not found on object of type 'IAPHelper *'

这是我的Objective C代码:

#import "VMedu-Swift.h"
@interface FreeSubscriptionController ()
{ 
}
- (void)viewDidLoad {
    [super viewDidLoad];

UIButton *restoreBtn = [VMEduUtil roundBtnWithFrame:CGRectMake(hmBtn.frame.origin.x-20, hmBtn.frame.origin.y+10, 70, searchImg.size.height) title:@"Restore" cornerRadius:3 backgroundColor:[UIColor clearColor] target:IAPHelper.shared action:@selector(restorePurchase)];
    restoreBtn.tintColor = [UIColor whiteColor];
    [topBarView addSubview:restoreBtn];

  IAPHelper.shared.purchaseStatusBlock  ..... shows me error
}

@end

使用相同闭包变量purchaseStatusBlock的另一个Swift类PurchaseSubscriptionController效果很好。 这是我的代码

  override func viewDidLoad() {
        super.viewDidLoad()

      IAPHelper.shared.fetchAvailableProducts()
        IAPHelper.shared.purchaseStatusBlock = {[weak self] (type) in
            guard let strongSelf = self else{ return }
            if type == .purchased {
                let alertView = UIAlertController(title: "", message: type.message(), preferredStyle: .alert)
                let action = UIAlertAction(title: "OK", style: .default, handler: { (alert) in

                    print("product purchased")
                })
                alertView.addAction(action)
                strongSelf.present(alertView, animated: true, completion: nil)
            }
        }
    }

您使用的是错误的Objective-C代码

在你的枚举声明中添加一个@objc

@objc
enum IAPHelperAlertType : Int{
    case aaaa
    case bbbb
}

替换它

IAPHelper.shared.purchaseStatusBlock  ..... shows me error

有了这个

[[IAPHelper shared] setPurchaseStatusBlock:^(NSInteger inte) {
        NSLog(@"test");
    }];

似乎你只需要使用@objc显式注释一些属性和类型:

@objc public enum IAPHelperAlertType: Int {
    case disabled
    case restored
    case purchased
    func message() -> String{
        switch self {
        case .disabled:
            return "Purchases are disabled in your device!"
        case .restored:
            return "You've successfully restored your purchase!"
        case .purchased:
            return "You've successfully bought this purchase!"

        }
    }
}

(添加:Int Xcode建议的:Int 。)

class IAPHelper: NSObject {
    @objc static let shared = IAPHelper()
    @objc var purchaseStatusBlock: (@convention(block) (IAPHelperAlertType) -> Void)?
}

(添加了两个@objc@convention(block)@convention(block)使闭包类型与Objective-C块兼容。)

通过上面的这些更改,您可以在Objective-C代码中编写类似的内容:

    IAPHelper.shared.purchaseStatusBlock(IAPHelperAlertTypeDisabled)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM