2017-06-09 96 views
0

我的代碼:斯威夫特無法將類型CBPeripheral的價值預期參數類型

func didDiscoverBLE(_ peripheral: CBPeripheral!, address: String!, rssi: Int32) { 


DispatchQueue.main.async(execute: {() -> Void in 

        // Handle Discovery 
        self.arrayPeripehral.contains(where:peripheral) 
         return 
        }) 
      self.arrayPeripehral.append(peripheral) 
      let title: String = "\(peripheral.name) \(address) (RSSI:\(rssi))" 
      self.arrayPeripheralName.append(title) 

在這一行我有一個問題:

self.arrayPeripehral.contains(where:peripheral) 
    return 
}) 

有沒有人的想法?


下面是我從obective複製C將迅速而就死在這個錯誤

- (void)didDiscoverBLE:(CBPeripheral *)peripheral address:(NSString *)address rssi:(int)rssi 
    { 
     dispatch_async(dispatch_get_main_queue(), ^{ 

      // Handle Discovery 
      if([arrayPeripehral containsObject:peripheral]) 
       return; 

      [arrayPeripehral addObject:peripheral]; 

      NSString * title = [NSString stringWithFormat:@"%@ %@ (RSSI:%d)", peripheral.name, address, rssi]; 

      [arrayPeripheralName addObject:title]; 
+0

你可以顯示'arrayPeripehral'的聲明。 –

+0

var arrayPeripehral = [Any]()var arrayPeripheralName = [Any]() –

+0

我使用框架obj c和我有briddging文件,我宣佈外圍設備 var peripheral:CBPeripheral? var arrayPeripehral = [Any]() var arrayPeripheralName = [Any]() –

回答

0

變化arrayPeripehral[CBPeripheral][Any]型,這將使​​編譯器有更多的想法代碼的然後使用contains(where:)這樣來檢查數組是否包含對象。

var arrayPeripehral = [CBPeripheral]() 

現在使用contains(where:)這種方式來檢查數組是否包含對象。

if self.arrayPeripehral.contains(where: { $0.name == peripheral.name }) { 
    return 
} 

而且從[Any]爲你在它僅追加對象String改變arrayPeripheralName類型聲明[String]

var arrayPeripheralName = [String]() 
+0

這裏是我的代碼 ,但設備未找到 https://drive.google.com/open?id=0B0YSY8kyU68lVHo1aVU2a29aY0E –

+0

@AlexArshansky您是否仍然面臨任何錯誤? –

+0

是越來越多...我沒見過設備 –

相關問題