xcode 'Class.Type' 没有名为 'variable' 的成员错误只是缺乏类变量支持?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24876985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:14:41  来源:igfitidea点击:

'Class.Type' does not have a member named 'variable' error just a lack of class variable support?

xcodeswift

提问by kasplat

I have been trying to use constants and variables in a class that refer by name to other constants and variables in the same class at the class level. AFAIK, as of Xcode 6 beta 4, Swift still doesn't have class variable support. What I'm wondering is whether the errors I see when trying to refer to other constants (let) or variables (var) are simply due to lack of class variable support?

我一直在尝试在类中使用常量和变量,这些常量和变量在类级别按名称引用同一类中的其他常量和变量。AFAIK,从 Xcode 6 beta 4 开始,Swift 仍然没有类变量支持。我想知道的是,我在尝试引用其他常量 (let) 或变量 (var) 时看到的错误是否仅仅是由于缺乏类变量支持?

You can refer to the constants and variables within a method or outside the class, you just don't seem to be able to reference by name at the class level. The following class shows several variations along with the errors you will see displayed in Xcode 6 beta 4.

您可以在方法内或类外引用常量和变量,但您似乎无法在类级别按名称引用。以下类显示了几种变体以及您将在 Xcode 6 beta 4 中看到的错误。

This can be tested in a playground or regular .swift file.

这可以在操场或常规 .swift 文件中测试。

class Simple {

    let someConstant = 0.50

    var someVariable = 1

    // uncomment let and var lines to see the errors

    // error: 'Simple.Type' does not have a member named 'someConstant'
    // let referringConstant = someConstant
    // error: Use of unresolved identifier 'self'
    // let referringConstant = self.someConstant

    // error: 'Simple.Type' does not have a member named 'someVariable'
    // var referringVar = someVariable
    // error: Use of unresolved identifier 'self'
    // var referringVar = self.someVariable

    // can't do class constants or variables yet either
    // let referringConstant = Simple.someConstant
    // var referringVar = Simple.someVariable

    func simpleMethod() {
        // both of these forms are valid as long as it is unambiguous
        let referToConstant = someConstant
        let referToVariable = someVariable

        var anotherConstant = self.someConstant
        var anotherVariable = self.someVariable
    }
}

For reference, the original Objective-C code that led to this problem in Swift was from the CS193P SuperCard app where a C #define is used for a constant and then that constant is used to set a variable.

作为参考,导致 Swift 中出现此问题的原始 Objective-C 代码来自 CS193P SuperCard 应用程序,其中 C #define 用于常量,然后该常量用于设置变量。

@interface PlayingCardView()
@property (nonatomic) CGFloat faceCardScaleFactor;
@end

@implementation PlayingCardView

#pragma mark - Properties

@synthesize faceCardScaleFactor = _faceCardScaleFactor;

#define DEFAULT_FACE_CARD_SCALE_FACTOR 0.90

- (CGFloat)faceCardScaleFactor
{
    if (!_faceCardScaleFactor) _faceCardScaleFactor = DEFAULT_FACE_CARD_SCALE_FACTOR;
        return _faceCardScaleFactor;
}

- (void)setFaceCardScaleFactor:(CGFloat)faceCardScaleFactor
{
    _faceCardScaleFactor = faceCardScaleFactor;
    [self setNeedsDisplay];
}

回答by Andrew

The two errors messages you're getting are actually a result of two different, but related, reasons.

您收到的两条错误消息实际上是由两个不同但相关的原因造成的。

error: Use of unresolved identifier 'self'

错误:使用未解析的标识符“self”

selfrefers to an instance of a type and does not exist until that type is considered fully initialized, and therefore cannot be used for default property values.

self指的是一个类型的实例,在该类型被认为完全初始化之前不存在,因此不能用于默认属性值。

error: 'Simple.Type' does not have a member named 'someConstant'

错误:“Simple.Type”没有名为“someConstant”的成员

Class variables and static variables are considered "type variables" of classes and structs, respectively. What this means is that they're defined as variables on the type itself, as opposed to an instance of that type. By this definition and the fact that you can't use selffor default property values, it's clear that this error message is a result of Swift looking for a class variable named someConstant. Class variables are still not supported as of Beta 4.

类变量和静态变量分别被认为是类和结构的“类型变量”。这意味着它们被定义为类型本身的变量,而不是该类型的实例。根据这个定义以及不能self用于默认属性值的事实,很明显这个错误消息是 Swift 寻找名为someConstant. 从 Beta 4 开始,仍然不支持类变量。

The analogous code for structs, with type variables taken into account, compiles just fine:

结构体的类似代码,考虑了类型变量,编译得很好:

struct Simple {
    static let someConstant = 0.50
    static var someVariable = 1

    let referringConstant = someConstant
    var referringVar = someVariable

    let explicitTypeProperty = Simple.someConstant
}

Reference: my recollection of the "Properties" and "Initializers" chapters of The Swift Programming Language.

参考:我对The Swift Programming Language的“Properties”和“Initializers”章节的回忆。