2015-02-23 53 views
0

我正在构建一个机器人手臂并使用iOS应用程序控制手臂。我无法将位置发送到arduino蓝牙4.0屏蔽。无法使用类型为'(UInt64)'的参数列表调用

我使用滑块来控制手臂的位置。

有两个错误。

  1. “不能调用 'writePosition' 类型的 '(UINT8)' 参数列表”
  2. “不能调用 'sendPosition' 类型的 '(UINT64)' 参数列表”

    func sendPosition(position: UInt8)   
    if !self.allowTX { 
        return 
    } 
    
    // Validate value 
    if UInt64(position) == lastPosition { 
        return 
    } 
    else if ((position < 0) || (position > 180)) { 
        return 
    } 
    
    // Send position to BLE Shield (if service exists and is connected) 
    if let bleService = btDiscoverySharedInstance.bleService { 
        bleService.writePosition(position) ***1)ERROR OCCURS ON THIS LINE*** 
        lastPosition = position 
    
        // Start delay timer 
        self.allowTX = false 
        if timerTXDelay == nil { 
         timerTXDelay = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("timerTXDelayElapsed"), userInfo: nil, repeats: false) 
        } 
        } 
    } 
    
    func timerTXDelayElapsed() { 
    self.allowTX = true 
    self.stopTimerTXDelay() 
    
    // Send current slider position 
    self.sendPosition(UInt64(self.currentClawValue.value)) **2)ERROR OCCURS ON THIS LINE** 
    

    }

这里是我的 “writePosition” 功能。

func writePosition(position: Int8) { 
    // See if characteristic has been discovered before writing to it 
    if self.positionCharacteristic == nil { 
     return 
    } 

    // Need a mutable var to pass to writeValue function 
    var positionValue = position 
    let data = NSData(bytes: &positionValue, length: sizeof(UInt8)) 
    self.peripheral?.writeValue(data, forCharacteristic: self.positionCharacteristic, type: CBCharacteristicWriteType.WithResponse) 
} 

我不知道我是否要留下一些东西或完全失去一些东西。 我已经尝试过UInt8和UInt64之间的简单转换,但这些都没有奏效。

回答

2

你的问题是你正在使用的不同的int类型。

首先让我们来检查writePosition方法。您使用Int8作为参数。因此,您需要确保您还以Int8作为参数调用该方法。为了确保您使用的是Int8你可以将它转换:

bleService.writePosition(Int8(position)) 

正如你看到这里,你需要转换positionInt8

现在检查你的sendPosition方法。你有类似的问题。你想要一个UInt8作为参数,但你用UInt64参数来调用它。这是你不能做的事情。您需要使用相同的整数类型:

self.sendPosition(UInt8(self.currentClawValue.value)) 

这里是一样的。使用UInt8而不是UInt64来使铸件工作。

2

也许我失去了一些东西,但错误表明你打电话“writePosition”类型的参数列表“(UINT8)”

然而,writePosition的参数列表指定的INT8。将writePosition的参数类型更改为UInt8,或将调用参数更改(或强制转换)为Int8。

同样,与sendPosition,它想要一个UInt8,但你发送一个UInt64。

Swift更加烦恼,因为它抱怨隐式类型转换。

您应该使用最适合您的数据的整数大小,或者API要求您使用。

相关问题