0

I want to set up the Linea Pro to charge the phone if the phone's battery gets low and I'm having a tough time mainly because all the examples are shown in objective-C still and not in Swift.

The manual says:

@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 */

and the code provided is the following:

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

So in Swift I first tried this:

self.scanner.setCharging = true

but that gives me the following error:

Cannot assign to property: 'setCharging' is a method

So I tried this:

self.scanner.setCharging(true)

which gives me this error:

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

Interesting because apparently I have to build it in a function called "setCharging" I think, but I have no idea what and how it wants me to set up the try and catch to, and quite frankly where am I opposed to get this information from? I think it should be along these lines or something, but I'm not clear on the specifics :

 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")
 }
 }
4

1 回答 1

1

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

以下是代码:

do{
      try self.scanner.setCharging(true)
   }catch let error as NSError{
        NSLog("Operation \(error as Error)")
   }
于 2017-03-01T03:54:45.880 回答