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")
 }
 }
Chaim Friedman
  • 720
  • 6
  • 24

1 Answers1

1

The answer was provided to me by the manufacturer. It is unnecessary to create a function with the same name as the API, APIs can be called anywhere in the code with the exception of handling error. So in this case I just have this directly in my code not in a function and it just works. (Since I have my scanner.connect code inside a viewWillAppear block, the code to start charging was too early to be called in there, so I placed it inside of a viewDidAppear block).

The following is the code:

do{
      try self.scanner.setCharging(true)
   }catch let error as NSError{
        NSLog("Operation \(error as Error)")
   }
Chaim Friedman
  • 720
  • 6
  • 24