Core Data custom accessor crash when saving context with nil required attribute
<p>After reading <a href="http://martiancraft.com/blog/2015/12/nsmanaged/" rel="nofollow">this excellent article</a> about custom accessors in Swift, I refactored from <code>NSDecimalNumber</code> to use the new <code>Decimal</code> type. I have a fairly complex model and things have been working fine for several days, but I now see an <code>EXC_BAD_ACCESS</code> crash if I save the managed object context while a required (non-optional) property is nil, <code>baseAmount</code> in my case. When using <code>NSDecimalNumber</code> directly, before the refactor, saving the object would fail, but it wouldn't crash.</p>

<p>Not sure if this is important, but I'm saving the context using Magical Record's function: <code>NSManagedObjectContext.mr_default().mr_saveToPersistentStoreAndWait()</code></p>

<p>Any ideas on how I can modify the <code>baseAmount</code> accessors to achieve the same failed-save behavior that Core Data provides so that it doesn't crash?</p>

<p>Here is the relevant part of my NSManagedObject subclass, followed by the stack trace:</p>

<pre><code>extension DFTransaction {

    @NSManaged private var primitiveBaseAmount: NSDecimalNumber

    var baseAmount: Decimal {
        get {
            willAccessValue(forKey: "baseAmount")
            defer { didAccessValue(forKey: "baseAmount") }
            return primitiveBaseAmount.decimalValue
        }
        set {
            willChangeValue(forKey: "baseAmount")
            defer { didChangeValue(forKey: "baseAmount") }
            primitiveBaseAmount = NSDecimalNumber(decimal: newValue)
        }
    }
}


[appname]`@objc DFTransaction.baseAmount.getter:
0x100343cd0 &lt;+0&gt;:   stp    x29, x30, [sp, #-16]!
0x100343cd4 &lt;+4&gt;:   mov    x29, sp
0x100343cd8 &lt;+8&gt;:   sub    sp, sp, #96               ; =96 
0x100343cdc &lt;+12&gt;:  stur   x0, [x29, #-32]
0x100343ce0 &lt;+16&gt;:  stur   x8, [x29, #-40]
0x100343ce4 &lt;+20&gt;:  bl     0x10051ca64               ; symbol stub for: objc_retain
0x100343ce8 &lt;+24&gt;:  sub    x8, x29, #24              ; =24 
0x100343cec &lt;+28&gt;:  ldur   x30, [x29, #-32]
0x100343cf0 &lt;+32&gt;:  str    x0, [sp, #48]
0x100343cf4 &lt;+36&gt;:  mov    x0, x30
0x100343cf8 &lt;+40&gt;:  bl     0x100343da4               ; [appname].DFTransaction.baseAmount.getter : __C.Decimal at DFTransaction.swift:64
0x100343cfc &lt;+44&gt;:  ldur   w9, [x29, #-24]
0x100343d00 &lt;+48&gt;:  ldurh  w10, [x29, #-20]
0x100343d04 &lt;+52&gt;:  ldurh  w11, [x29, #-18]
0x100343d08 &lt;+56&gt;:  ldurh  w12, [x29, #-16]
0x100343d0c &lt;+60&gt;:  ldurh  w13, [x29, #-14]
0x100343d10 &lt;+64&gt;:  ldurh  w14, [x29, #-12]
0x100343d14 &lt;+68&gt;:  ldurh  w15, [x29, #-10]
0x100343d18 &lt;+72&gt;:  ldurh  w16, [x29, #-8]
0x100343d1c &lt;+76&gt;:  ldurh  w17, [x29, #-6]
0x100343d20 &lt;+80&gt;:  ldur   x0, [x29, #-32]
0x100343d24 &lt;+84&gt;:  str    w9, [sp, #44]
0x100343d28 &lt;+88&gt;:  str    w10, [sp, #40]
0x100343d2c &lt;+92&gt;:  str    w11, [sp, #36]
0x100343d30 &lt;+96&gt;:  str    w12, [sp, #32]
0x100343d34 &lt;+100&gt;: str    w13, [sp, #28]
0x100343d38 &lt;+104&gt;: str    w14, [sp, #24]
0x100343d3c &lt;+108&gt;: str    w15, [sp, #20]
0x100343d40 &lt;+112&gt;: str    w16, [sp, #16]
0x100343d44 &lt;+116&gt;: str    w17, [sp, #12]
0x100343d48 &lt;+120&gt;: bl     0x10051ca58               ; symbol stub for: objc_release
0x100343d4c &lt;+124&gt;: ldr    w9, [sp, #44]
0x100343d50 &lt;+128&gt;: ldur   x8, [x29, #-40]
-&gt;  0x100343d54 &lt;+132&gt;: str    w9, [x8]    //EXC_BAD_ACCESS
0x100343d58 &lt;+136&gt;: ldr    w10, [sp, #40]
0x100343d5c &lt;+140&gt;: strh   w10, [x8, #4]
0x100343d60 &lt;+144&gt;: ldr    w11, [sp, #36]
0x100343d64 &lt;+148&gt;: strh   w11, [x8, #6]
0x100343d68 &lt;+152&gt;: ldr    w12, [sp, #32]
0x100343d6c &lt;+156&gt;: strh   w12, [x8, #8]
0x100343d70 &lt;+160&gt;: ldr    w13, [sp, #28]
0x100343d74 &lt;+164&gt;: strh   w13, [x8, #10]
0x100343d78 &lt;+168&gt;: ldr    w14, [sp, #24]
0x100343d7c &lt;+172&gt;: strh   w14, [x8, #12]
0x100343d80 &lt;+176&gt;: ldr    w15, [sp, #20]
0x100343d84 &lt;+180&gt;: strh   w15, [x8, #14]
0x100343d88 &lt;+184&gt;: ldr    w16, [sp, #16]
0x100343d8c &lt;+188&gt;: strh   w16, [x8, #16]
0x100343d90 &lt;+192&gt;: ldr    w17, [sp, #12]
0x100343d94 &lt;+196&gt;: strh   w17, [x8, #18]
0x100343d98 &lt;+200&gt;: mov    sp, x29
0x100343d9c &lt;+204&gt;: ldp    x29, x30, [sp], #16
0x100343da0 &lt;+208&gt;: ret    
</code></pre>