在Swift 1.2中声明不能同时是“final”和“dynamic”错误 [英] A declaration cannot be both 'final' and 'dynamic' error in Swift 1.2

查看:166
本文介绍了在Swift 1.2中声明不能同时是“final”和“dynamic”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下的声明

import Foundation

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

会导致以下编译错误

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


$

我使用的是Swift 1.2(Xcode 6.3.1 6D1002中发布的版本)

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

推荐答案

出现这个问题是因为Swift试图为Obj-C兼容性的静态属性生成一个动态访问器,继承自 NSObject

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.

如果您的项目只使用Swift,而不是使用 var 存取器,您可以通过Swift 2.0中的 @nonobjc 属性避免此问题:

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
}

这篇关于在Swift 1.2中声明不能同时是“final”和“dynamic”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆