Giter Club home page Giter Club logo

Comments (4)

rcedwards avatar rcedwards commented on July 24, 2024

@lyricsboy You should be able to do the following

do {
    moc.saveContextAndWait()
} catch let error {
    let freeCast = error as NSError
    // do stuff with expected NSError
}

I believe this is what I ran into and @zwaldowski showed me this.

from coredatastack.

lyricsboy avatar lyricsboy commented on July 24, 2024

I have no idea why that works, but it does compile. I see this quote in the Swift guide:

β€œThe cases of a switch statement use the forced version of the type cast operator (as, not as?) to check and cast to a specific type. This check is always safe within the context of a switch case statement.”

Excerpt From: Apple Inc. β€œThe Swift Programming Language (Swift 2 Prerelease).” iBooks. https://itun.es/us/k5SW7.l

Is that what's going on? What does it mean to be "safe" in this context?

from coredatastack.

zwaldowski avatar zwaldowski commented on July 24, 2024

That's in general how switch statements work, it's the syntax within a switch statement. The let binding in switch cases uses as, which sounds pretty close to what you're quoting from the book, and doesn't use as? or as unless it's in a where clause. as compiles down to to the appropriate optimized as? when used within pattern matching.

ErrorType also comes at this/complicates this with a fair amount of magic. All types conforming to ErrorTypes are implicitly convertible to NSError to cross the Objective-C bridge, and therefore "safe". This allows you to throw from Swift and catch/handle it in Objective-C.

from coredatastack.

lyricsboy avatar lyricsboy commented on July 24, 2024

Ah, great explanation. Thanks guys. I think the ErrorType<->NSError magic is what I was missing.

from coredatastack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.