123

The declaration of value below

import Foundation

class AAA: NSObject {
    func test2() {
        self.dynamicType
    }
}
extension AAA {
    static let value    =   111
}

causes the following compilation error

A declaration cannot be both 'final' and 'dynamic'

Why does this happen, and how can I deal with this?

I am using Swift 1.2 (the version shipped within Xcode 6.3.1 6D1002)

3
  • The func test2 declaration is not needed to trigger the error, as of Xcode 7.3.1.
    – rob mayoff
    Commented May 2, 2016 at 19:21
  • 1
    Swift bug SR-993
    – rob mayoff
    Commented May 2, 2016 at 19:33
  • Just put that static variable into another better naming struct
    – onmyway133
    Commented May 3, 2016 at 14:43

7 Answers 7

224

This issue arises because Swift is trying to generate a dynamic accessor for the static property for Obj-C compatibility, since the class inherits from NSObject.

If your project is in Swift only, rather than using a var accessor you can avoid the issue via the @nonobjc attribute in Swift 2.0:

import Foundation

class AAA: NSObject {}
extension AAA {
    @nonobjc static let value = 111
}
4
  • My project has some Objective-C files, but none of that code interacts with instances of this class (AAA here), so I guess I'm in the clear? Commented Oct 23, 2015 at 8:25
  • This should be the selected answer if using a pure Swift codebase.
    – idzski
    Commented Oct 29, 2015 at 9:57
  • I was trying to add static (class) vars to an NSManagedObject subclass. This fixed it! Commented Feb 23, 2016 at 2:24
  • Am I the only one who has found this fix to completely screw up SourceKitService for Xcode 7.3? Commented Apr 28, 2016 at 0:44
57

You will get this error if your class satisfies these conditions.

  • Subclassed from NSObject.
  • Has a static let field.
  • Accesses the field from an instance method via dynamicType.

I don't know why this happens, but you can try this workaround.

static var value: Int {
    get {
        return 111
    }
}

Or in shorter form.

static var value: Int {
    return 111
}

Use static var { get } instead of static let.


Though property getter and its calling cost is very likely to be eliminated by LLVM optimiser in above example, you might want to avoid it explicitly.

If you're concerned about such value calculation cost, you can create it once and cache like this.

static var value: Int {
    return cache
}
private let cache = getTheNumber()

Or like this if you want to hide the existence of cache completely.

static var value: Int {
    struct Local {
        static let cache = getTheNumber()
    }
    return Local.cache
}
4
  • 5
    This produces a computed property, which will be recomputed at every access. For this case it might not matter too much, but I think it's worth mentioning so that nobody uses this workaround for bigger objects. Commented Oct 25, 2015 at 20:57
  • @NickPodratz would this be a calculated property as well? private static let _value: Int = 111 static var value: Int { return _value } it doesn't have the get { but the compiler mentions something about computed property if I use var instead of let
    – hashier
    Commented Aug 17, 2016 at 13:04
  • 1
    @hashier it is. Inside the curly braces you create a closure, the get is in this case implicit. What you can do instead is assign the result of the closure to the variable so that the closure is called just once: let value: Int = { return 111 }(). The brackets at the end call the closure. But be aware that this is a stored property again and therefore not available in extensions. Commented Aug 17, 2016 at 17:02
  • Agree with @NickPodratz 's assessment. While this resolves the error the OP mentions and therefore makes this a legit answer, it doesn't provide any benefit if you desire your variable to actually be static (which seems like the point). Alex's answer is better in that case (assuming pure Swift)
    – Matt Long
    Commented Nov 1, 2016 at 18:21
18

I had this error too.

My issue was just a static var in a swift extension.

extension NotificationsViewController: UITableViewDataSource , UITableViewDelegate {

    static var timeIntervalFormatter = NSDateComponentsFormatter()

}

Moving it to the class implementation resolved the problem for me.

7

I just stumbled over the same issue with a different cause and would like to post it here for other people experiencing the same useless error message.

A final class which overrides a computed variable defined in an extension also causes this error. It works for functions though and thus looks like a compiler bug.

// at line 0: a declaration cannot be both 'final' and 'dynamic'

import UIKit

extension UIViewController {
    var test: Int { return 0 }
}

final class TestController: UIViewController {
    override var test: Int { return 1 }
}
7

I solved this issue by moving the static declaration into the new struct I defined in the extension.

So instead of this:

extension NSOperationQueue {
    static var parsingQueue : NSOperationQueue = {
        let queue = NSOperationQueue()
        queue.maxConcurrentOperationCount = 1
        return queue
        }()
}

I have this:

extension NSOperationQueue {        
    struct Shared {
        static var parsingQueue : NSOperationQueue = {
            let queue = NSOperationQueue()
            queue.maxConcurrentOperationCount = 1
            return queue                
            }()
    }
}
0

You can mark it as private to prevent this error. If you want to expose it, you can wrap it in a public function:

extension AAA {

    private static let value = 111

    public func getDatValue() -> Int {
        return AAA.value
    }    
}

In my case, I only referenced the property in the extension itself, so there was no need to expose it.

0

As a slight improvement over @Eonil's answer, the get not necessary:

static var value: Int { return  111 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.