Faruk
Faruk

Reputation: 2449

Missing return in a function

It's a simple question. As you can see in the code, there is return in every case. But , somehow, xcode does not recognizes the return in the refresh()'s completionHandler block.

Any suggestions?

    func accessTokenWithBearer() -> String {
            if !didTokenExpire() {
                return "Bearer \(accessToken!)"
            }else{
                Token.refresh({
                    return "Bearer \(self.accessToken!)"
                })
            }
    }

    class func refresh(completion: ()->()){
        completion()
    }

Upvotes: 0

Views: 259

Answers (1)

Code
Code

Reputation: 6251

As the comment mentioned, you should add a completion handler to the accessTokenWithBearer function.

func accessTokenWithBearer(completion: String -> ()) {
    if !didTokenExpire() {
        completion("Bearer \(accessToken!)")
    }else{
        Token.refresh({
            completion("Bearer \(self.accessToken!)")
        })
    }
}

You could also have both a completion handler and a return value.

func accessTokenWithBearer(completion: String -> ()) -> String? {
    if !didTokenExpire() {
        return "Bearer \(accessToken!)"
    }else{
        Token.refresh({
            completion("Bearer \(self.accessToken!)")
        })
        return nil
    }
}

Upvotes: 3

Related Questions