12

I'm getting the following error

Cannot assign value of type 'StarButton' to type 'CAAnimationDelegate?'

on the last line of this CABasicAnimation block:

 let fillCircle = CABasicAnimation(keyPath: "opacity")
 fillCircle.toValue = 0
 fillCircle.duration = 0.3
 fillCircle.setValue(notFavoriteKey, forKey: starKey)
 fillCircle.delegate = self // this is where the error is thrown

self is a custom UIButton class. This wasn't an issue in previous versions of Swift... any suggestions on a solution?

UPDATE

Here is a downloadable link to the source file for the StarButton class for best reference:

https://www.dropbox.com/s/gvc2sky05f4p3au/StarButton.swift?dl=0

TenaciousJay
  • 6,750
  • 3
  • 45
  • 48
Jared Garcia
  • 751
  • 1
  • 8
  • 17
  • Are you sure it conforms to the delegate protocol? – Fogmeister Jul 28 '16 at 22:56
  • `UIButton` from the Apple Docs (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/) doesn't conform to `CAAnimationDelegate`. Did you explicitly say that your subclass was a delegate? See http://stackoverflow.com/questions/24024466/how-do-i-make-a-class-conform-to-a-delegate-in-swift – Doc Jul 28 '16 at 22:58
  • @Fogmeister well this is part of an open source component I grabbed & after examining the file it doesn't seem to have any protocol's.. however this worked perfectly fine in swift 2.xx – Jared Garcia Jul 28 '16 at 23:07
  • @Doc well this is part of an open source component I grabbed & after examining the file it doesn't seem to have any protocol's.. however this worked perfectly fine in swift 2.xx – Jared Garcia Jul 28 '16 at 23:08
  • 1
    Maybe Swift 3 made protocol casts strict? Anyways you should still add the protocol; if the class supports being a delegate it should say so in the declaration – Doc Jul 28 '16 at 23:11
  • how should I go about adding the protocol? @Doc – Jared Garcia Jul 28 '16 at 23:16
  • can you post the StarButton class? – Christian Abella Jul 28 '16 at 23:29
  • 1
    Adding a protocol is like adding another superclass; if you have something like `class StarButton: UIButton`, replace with `class StarButton: UIButton, CABasicAnimationDelegate` – Doc Jul 28 '16 at 23:39
  • check updated question @ChristianAbella – Jared Garcia Jul 28 '16 at 23:40
  • check updated question @Doc – Jared Garcia Jul 28 '16 at 23:41
  • 3
    Just replace the line `class StarButton: UIButton {` with `class StarButton: UIButton, CAAnimationDelegate {`. You might need to import CoreAnimation also – Doc Jul 28 '16 at 23:46
  • that did it! haha, wow something that seemed so trivial @Doc – Jared Garcia Jul 28 '16 at 23:54

1 Answers1

10

As @Doc mentioned in the comments above, the solution is to replace

class StarButton: UIButton { 

with

class StarButton: UIButton, CAAnimationDelegate {

It would seem that the CAAnimationDelegate protocol is no longer implied and must be specified explicitly.

TenaciousJay
  • 6,750
  • 3
  • 45
  • 48