Objective C 代表 Swift

Objective C Delegate in Swift

我刚刚在我所有的 swift 应用程序中实施了 Chartboost。

我奖励了插页式广告。所以我需要使用 Chartboost 给我的 objective C 函数设置一个委托。我不知道该怎么做。这就是我通过 Swift.

执行 Admob 的方式
   NSNotificationCenter.defaultCenter().addObserver(self, selector: "presentInterstitial:", name: "AdMobCall", object: nil) 

   func presentInterstitial(sender:UIButton!) {

    if let isReady = interstitial?.isReady {   // _ isReady
        interstitial?.presentFromRootViewController(self)

    }
}

 NSNotificationCenter.defaultCenter().postNotificationName("AdMobCall", object: nil)

但是我得到的 Objective C Chartboost 函数是:

而且我不明白如何使用此函数创建与 Admob 相同的委托。这似乎不可能。我有 Chartboost 奖励插页式广告。但是我没有办法设置代表看他们是否看完了视频。

如果你想实现 objective-c 委托给 swift。您需要执行以下步骤。

  1. 需要创建桥接文件来导入您的 objective-c 类

    //
    //  Use this file to import your target's public headers that you would like to expose to Swift.
    //
    
    #import "UVIViewController.h"
    
  2. 您需要在 swift viewcontroller

    中扩展协议名称
    import UIKit
    
    class ViewController: UVIViewController, UVIDataRefreshProtocol {   // 2. Extend Protocal
    
        var secondController: DelegateViewController?
    
        @IBOutlet var delegateRefresh: UILabel!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
            self.registerForBasicDataRefreshNotification();             //  3. Register the protocol notification
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        // Register the protocol notification with same name. Example : DataRefreshNotification
        override func registerForBasicDataRefreshNotification() {
    
            self.validateBasicDataRefreshNotification();
            NSNotificationCenter.defaultCenter().addObserver(        // 4. Register the protocol notification with same name.
                self,
                selector: "basicDataDidRefresh",
                name: DataRefreshNotification,
                object: nil)
    
        }
    
        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            delegateRefresh.text = "";
            if segue.identifier == "view_to_delegate" {
                secondController = segue.destinationViewController as? DelegateViewController
            }
        }
    
        // Basic Data Did Refresh Notification
        func basicDataDidRefresh() {                            //  4. Event will be triggered while calling the protocol post notification. called with same name
                NSLog("\n Basic data is refreshed")
                delegateRefresh.text = "Basic data is refreshed";
        }
    
    }
    

    5. NSNotificationCenter.defaultCenter().postNotificationName(DataRefreshNotification, object: nil)  // Call the notification.

示例项目:https://github.com/vigneshuvi/Objective-C-Delegate-Swift