0

I am trying to write some Swift code that calls an existing Objective C function. The Objective C function looks like:

+(void) getCurrentUserprofileWithCompletion:(RequestCallback)completion {...}

where RequestCallback is defined in a .h file as:

typedef void (^RequestCallback) (ResponseInfo *responseInfo);

I have tried a number of different things, but nothing seems to work. The code that looks the most logical to me is:

let callback: (responseInfo: ResponseInfo) -> Void = {(responseInfo: ResponseInfo) -> Void in
    if let organization: Organizations = Organizations.organizationWithId(orgId) {
        completionBlock(false, nil)
    } else {
        self.switchOrganization(user, organization: organization, completionBlock: completionBlock)
    }
}
Users.getCurrentUserprofileWithCompletion(callback)

but this is getting the error

cannot convert value of type '(responseInfo: ResponseInfo) -> Void' to expected argument type 'RequestCallback!'

Does anyone have any idea what I am doing wrong here? I have scoured the internet looking for help including the various Apple documentation, but either I am blind or misreading because nothing seems to be working.

Thanks in advance!

1 Answer 1

1

Just remove the type specification for responseInfo a use the type RequestCallback

let callback: RequestCallback = { responseInfo -> Void in
    if let organization: Organizations = Organizations.organizationWithId(orgId) {
        completionBlock(false, nil)
    } else {
        self.switchOrganization(user, organization: organization, completionBlock: completionBlock)
    }
}

Users.getCurrentUserprofileWithCompletion(callback)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much Aleš! I don't know why I am having a mental block with some of this syntax in Swift. I spent 2 years coding in Scala so none of these concepts are new to me, but I found myself just staring dumbfound at this. :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.