2017-03-08 271 views
-1
使用Objective-C代表團

我想實現一個objectve-C代表團到我迅速類,但我不斷收到斯威夫特

Type 'ViewController' does not conform to protocol 

這是我的Objective-C類

#import <Foundation/Foundation.h> 
#import <CoreBluetooth/CoreBluetooth.h> 


@protocol BT_BLE_LibDelegate; 
@interface BT_BLE_Lib : NSObject< CBCentralManagerDelegate> 
{ 
    NSObject<BT_BLE_LibDelegate> __weak *delegate; 
    NSError *BTError; 
    int BLEMode; 
} 
@property (nonatomic, weak) id<BT_BLE_LibDelegate> __weak delegate; 
- (void) UARTSend:(NSString*)message;   // UART 送到藍芽 
- (int) iBeaconStartScan;     // 找 iBeacon 
- (int) iBeaconStopScan;      // 停止 iBeacon 
- (int) ConnectBlueTooth;     // 連接藍芽 
- (int) BlueToothConnectStatus;    // 藍芽連接情況 
- (void) DigitalOuts:(NSString*)sender;  // 藍芽接腳數位輸出 
- (void) DigitalInput;      // 藍芽接腳數位輸入 
@end 

@protocol BT_BLE_LibDelegate 
- (void) BlueToothStatus:(NSString*)errorMsg; // 取的HW 藍牙的連接情況 

- (void) DidReceiveData:(NSString*)message; // 收到資料 

- (void) DidReadHardwareRevisionString:(NSString*)Msg; // 硬體相關的訊息 
- (void) DigitalInputPinsChanges:(NSString*)Msg; // 數位輸入的情況改變 
- (void) DidReceiveiBeacon:(NSString*)Name RSSI:(NSNumber *)RSSI Action:(NSString*)Action;       // iBeacon 的情況 
@end 

這是我的雨燕類

import UIKit 

class ViewController: UIViewController , BT_BLE_LibDelegate{ 

var myObj : BT_BLE_Lib? 
override func viewDidLoad() { 

    super.viewDidLoad() 
    myObj = BT_BLE_Lib() 
    myObj?.delegate = self 

} 

func DidReceiveData(message: NSString?) { 

} 

func BlueToothStatus(errorMsg: NSString?) { 

}// 取的HW 藍牙的連接情況 

func DidReadHardwareRevisionString(Msg: NSString?) { 

}// 硬體相關的訊息 
func DigitalInputPinsChanges(Msg: NSString?) { 

}// 數位輸入的情況改變 
func DidReceiveiBeacon(Name: NSString?, RSSI: Int, Action: NSString?) { 

} 

注:我試過的參數的名稱或添加之前添加_ - >功能後無效,但相同的結果

+2

請[編輯]你的問題,替換實際的複製圖像並粘貼代碼。 – rmaddy

+0

ok .. done ...... –

+0

你已經聲明你的Swift函數是接受一個可選的,但你沒有設置他在目標C端的可空性 – Paulw11

回答

-1

好吧,我找到了解決方案 我剛剛用String替換了NSString!

public func didReceiveiBeacon(_ Name: String!, rssi RSSI: NSNumber!, action Action: String!) 

現在我有沒有語法錯誤.. 但該方法不工作(我用調試器)

+0

你不想要'!'。 – rmaddy

0

您已命名的Objective-C的協議,您的委託方法不正確使用駝峯,但是Swift在導入協議時爲你解決了這個問題。因此,您的斯威夫特代表實施需要如下所示:

func didReceiveData(_ message: String?) { 

} 

func blueToothStatus(_ errorMsg: String?) { 

} 

func didReadHardwareRevisionString(_ Msg: String?) { 

} 

func digitalInputPinsChanges(_ Msg: String?) { 

} 

func didReceiveiBeacon(_ Name: String?, rssi RSSI: NSNumber, action Action: String?) { 

} 

請注意小寫的第一個字母。我也不確定這些參數是否可選;我建議你更新你的Objective-C協議以使用正確的命名方式,並指出參數的可空性或其他參數(_Nonnull_Nullable)。喜歡的東西:

@protocol BT_BLE_LibDelegate 
- (void) blueToothStatus:(NSString* _Nullable)errorMsg; // 取的HW 藍牙的連接情況 

- (void) didReceiveData:(NSString* _Nullable)message; // 收到資料 

- (void) didReadHardwareRevisionString: (NSString* _Nullable)Msg; // 硬體相關的訊息 
- (void) digitalInputPinsChanges:(NSString* _Nullable)Msg; // 數位輸入的情況改變 
- (void) didReceiveiBeacon:(NSString* _Nullable)Name RSSI:(NSNumber * _Nonnull)RSSI Action:(NSString* _Nullable)Action;       // iBeacon 的情況 
@end 

而且,在風格的主題,iOS的採用駝峯,不pythonesque下劃線,所以你的方案應該是這樣BtBLELibDelegate

+0

那麼..目標-C類是從我正在使用的框架..所以不能改變它 –

+0

好吧,然後只是改變你的Swift方法名稱有一個小寫的第一個字母,它會工作。請務必進行防禦性編碼並正確解開這些選項,因爲Objective-C簽名不會告訴你這些參數是否具有值 – Paulw11

+0

我認爲問題出在框架之內..因爲只有一個函數可以工作,它甚至不能正常工作雖然它在我的Objective-C項目中工作得很好。不過謝謝。 –