Pyroh
Pyroh

Reputation: 216

Swift : animator() causes EXC_BAD_ACCESS

I've noticed some strange behaviour with implicit animation under OS X 10.10 since beta 5. Calling animation proxy will sometimes cause app to crash. I set up a really simple custom view. Here's the full code :

import Cocoa
import QuartzCore

class AnimatedView: NSView {

    var firstColor: NSColor = NSColor.blackColor() {
        didSet {
           self.needsDisplay = true
        }
    }

    var secondColor: NSColor = NSColor.whiteColor() {
        didSet {
            self.needsDisplay = true
        }
    }

    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)

        let outerRect = self.bounds
        let halfW = outerRect.width / 4.0
        let halfH = outerRect.height / 4.0
        var innerRect = outerRect
        innerRect.inset(dx: halfW, dy: halfH)
        self.firstColor.set()
        NSRectFill(outerRect)
        self.secondColor.set()
        NSRectFill(innerRect)
    }

    override func animationForKey(key: String!) -> AnyObject! {
        switch key {
        case "firstColor", "secondColor" :
            return CABasicAnimation()
        default :
            return super.animationForKey(key)
        }
    }
}

If I try to animate NSView's ans ancestor's properties everything work like a charm. Such as :

animView.animator().frame = someFrame

If I try to animate AnimatedView's property such as firstColor or secondColor :

animView.animator().firstColor = someColor
animView.animator().secondColor = someOtherColor

It crashes with various error code (no change made in code between tries) :

No object is Nil (I've checked twice).

Sometimes it complains about __CFString, sometimes it just crashes without change in code but it never works...

I think it's some sort of bug but I have to be sure before reporting, perhaps I forgot something.

Upvotes: 3

Views: 555

Answers (1)

Pyroh
Pyroh

Reputation: 216

Re-read release notes, noticed dynamic keyword, tested it, works ! So if you want to animate custom properties you have to use dynamic keyword in property declaration :

dynamic var firstColor: NSColor = NSColor.blackColor() {
    didSet {
        self.needsDisplay = true
    }
}

dynamic var secondColor: NSColor = NSColor.whiteColor() {
    didSet {
        self.needsDisplay = true
    }
}

I don't know why it worked prior to beta 5 and I don't know if it is a temporary workaround or if it is the normal way to use it but it works which is nice.

Upvotes: 9

Related Questions