0

I'm a noob in the Swift-Universe, but I have to get the app running ;) It would be great if you help me to find a solution. Thanks a lot in advance.

The Problem occurs after upgrading to newer version of X-Code (Version 9.4.1) and Swift 4.

private var stoppedSuccessfully: (() -> Void)?

func stopRecording() -> Promise<Void> {

    return Promise { success, _ in

        self.stoppedSuccessfully = success // The error occors here: Cannot assign value of type '(Void) -> Void' to type '(() -> Void)?'


        if WCSession.default.isReachable {
            logger.info("Watch is reachable - Send Stop recording message.")
            let command = RecordingCommand.stop

            self.sendMessage(command, EvomoWatchConnectivityCore.WatchConnectivityCoreMethod.transferUserInfo,
                             nil, nil)

            // Create a Timeout
            let timeoutDelay: Double = 15

            DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + timeoutDelay) {

                if self.stoppedSuccessfully != nil {
                    self.logger.warning("### Stopped waiting for Apple Watch recording by Timeout!")
                    success(Void())
                    self.stoppedSuccessfully = nil
                }

            }

            return
        }

        success(Void())
        self.stoppedSuccessfully = nil

    }

}

// In a other part of the code:
self.stoppedSuccessfully?()
self.stoppedSuccessfully = nil
rmaddy
  • 314,917
  • 42
  • 532
  • 579
JayUU
  • 23
  • 1
  • 4

2 Answers2

3

First change the type of stoppedSuccessfully from (() -> Void)? to ((Void) -> Void)?:

private var stoppedSuccessfully: ((Void) -> Void)?

Because, when you use Promise<T>, the closure type passed to success is of type (T)->Void. In your code, you are using Promise<Void>, so the type of success is (Void)->Void, not ()->Void.

Thus, your stoppedSuccessfully should be declared as Optional<(Void)->Void>, which is equivalent to ((Void)->Void)?.


And to call the closure of type (Void)->Void, you need to pass one argument of type Void. There's a literal notation of type Void, it's (), an empty tuple.

So, you can replace all success(Void())s to simply success(()).

And you can invoke stoppedSuccessfully in the similar manner:

// In a other part of the code:
self.stoppedSuccessfully?(())
OOPer
  • 47,149
  • 6
  • 107
  • 142
  • Thanks a lot for your answer. That works fine! The answer of @Prashant also works but this solution is cleaner. – JayUU Jul 28 '18 at 10:54
0

Don't need to use Void use simply stoppedSuccessfully: ((Void) -> ())? and call it with

 let obj:Void = Void()
 stoppedSuccessfully?(obj) 

I don't understand why you have used stoppedSuccessfully it is un necessarily assigned from success which will never been nil so Don't think your condition if self.stoppedSuccessfully != nil will fail

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
  • Thanks for your answer! If I change the variable declaration as you mentioned. X-Code returns following error message on the "self.stoppedSuccessfully = success" line: Cannot assign value of type '(Void) -> Void' to type '(() -> ())?'. I don't wrote this code. So I don't know if the condition will fail. But I know that the code worked before in Swift 3. – JayUU Jul 27 '18 at 10:54
  • @JayUU check stoppedSuccessfully: ((Void) -> ())? – Prashant Tukadiya Jul 27 '18 at 11:04
  • This solve the error in "self.stoppedSuccessfully = success" line but create a new error in the "self.stoppedSuccessfully?()" line. -> Missing argument for parameter #1 in call – JayUU Jul 27 '18 at 11:27
  • @JayUU This is not right way but it will work `let obj:Void = Void() stoppedSuccessfully?(obj)` – Prashant Tukadiya Jul 27 '18 at 11:34
  • You are the man!!! It's working. Thanks a lot! If you change/extend your answer, I can mark it as solved ... – JayUU Jul 27 '18 at 12:19
  • @JayUU Glad that it worked for you now you can accept my answer – Prashant Tukadiya Jul 27 '18 at 13:19
  • Sorry, that I didn't accept your answer, but @OOPers solution is cleaner. Neverless thanks a lot for your effort. – JayUU Jul 28 '18 at 10:56