在Swift中为Linea Pro设备设置setCharging

时间:2017-02-26 20:47:45

标签: swift3 try-catch linea-pro

我想设置Linea Pro为手机充电,如果手机的电池电量不足而且我很难过,主要是因为所有的例子都显示在Objective-C中而不是迅速的。

手册说:

@param enabled TRUE to enable charging, FALSE to disable/stop it @param error pointer to NSError object, where error information is stored in case function fails. You can pass nil if you don't want that information @return TRUE if function succeeded, FALSE otherwise */

,提供的代码如下:

-(BOOL)setCharging:(BOOL)enabled error:(NSError **)error;

所以在Swift中我首先尝试了这个:

self.scanner.setCharging = true

但是这给了我以下错误:

Cannot assign to property: 'setCharging' is a method

所以我尝试了这个:

self.scanner.setCharging(true)

这给了我这个错误:

Call can throw, but it is not marked with 'try' and the error is not handled

有趣,因为显然我必须在一个名为" setCharging"的函数中构建它。我想,但我不知道它是什么以及它希望我如何设置尝试并抓住,坦率地说,我反对从哪里获取这些信息? 我认为它应该是这些或类似的东西,但我不清楚具体细节:

 func setCharging(_ enabled: Bool) throws -> Bool {
 do {
 try
//something goes here I'm not sure what
 } catch {
//and then maybe something here on that to do with error 
 print("some error")
 }
 }

1 个答案:

答案 0 :(得分:0)

答案由制造商提供给我。无需创建与API同名的函数,可以在代码中的任何位置调用API,但处理错误除外。所以在这种情况下,我只是在我的代码中直接使用它而不是函数,它只是有效。 (因为我在viewWillAppear块中有我的scanner.connect代码,开始充电的代码太早,无法在那里调用,所以我将它放在viewDidAppear块中。)

以下是代码:

do{
      try self.scanner.setCharging(true)
   }catch let error as NSError{
        NSLog("Operation \(error as Error)")
   }