Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensembles; Handling errors #267

Open
MickStupp opened this issue Apr 23, 2018 · 4 comments
Open

Ensembles; Handling errors #267

MickStupp opened this issue Apr 23, 2018 · 4 comments

Comments

@MickStupp
Copy link

I'd very much like to handle the error 'CDEErrorCode.cloudIdentityChange' as a run-time alert, but cannot get any line to work. Logic would suggest the following in the completion block:

  func sync(_ completion: (() -> Void)?) {
    if !ensemble.isLeeched {
        ensemble.leechPersistentStore {
            error in
            completion?()
        }        
    } else {
        //ensemble.merge
        ensemble.merge(completion: {
            error in
            if error == CDEErrorCode.cloudIdentityChanged {
                iCloudAlert()
            }
            completion?()  
        })
    }        
}

But the 'if error == ...' line returns the message, " Binary operator '==' cannot be applied to operands of type 'Error?' and 'CDEErrorCode' "

Have tried many variants & type casts to no avail. Suggestions would be hugely appreciated.
PS This is Swift 3, with Ensembles v1.7.1. Everything works wonderfully, just can't catch this error specifically.

@drewmccormack
Copy link
Owner

drewmccormack commented Apr 23, 2018

Would think you just cast to CDEErrorCode.

if let cdeError = error as? CDEErrorCode, cdeError == .cloudIdentityChanged {
    iCloudAlert()
}

@MickStupp
Copy link
Author

You make it sound so simple. Many thanks.
I revisited various casts I tried and still can't see why they didn't work, so setting time aside today to get this figured out and properly understood.

@MickStupp
Copy link
Author

For the benefit of anyone missing the point like I did:
Comma separated conditions in a 'if' statement are not the same as && (which is where I went wrong in one of my many 'nearly there' attempts). In this example, cdeError will not be of type CDEErrorCode if the two conditions are anything but two separate conditions, comma separated! Thanks again to Drew for this idiots guide to Swift.

@drewmccormack
Copy link
Owner

drewmccormack commented Apr 25, 2018

There are similarities to &&, but indeed not the same. It evaluates each condition in turn, and will not proceed unless they are all true. That much is the same. But they are separate statements, and evaluated separately.

Effectively, you are doing this:

if let cdeError = error as? CDEErrorCode {
    if cdeError == .cloudIdentityChanges {
        ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants