'Class.Type' 没有名为 'variable' 的成员错误只是缺乏类变量支持? [英] 'Class.Type' does not have a member named 'variable' error just a lack of class variable support?

查看:20
本文介绍了'Class.Type' 没有名为 'variable' 的成员错误只是缺乏类变量支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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.

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

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

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
    }
}

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

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.

@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];
}

推荐答案

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

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

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

self refers 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 作为默认属性值的事实,很明显这个错误消息是 Swift 寻找名为 someConstant 的类变量的结果.从 Beta 4 开始,仍然不支持类变量.

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 self for 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.

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

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
}

参考:我对 The Swift Programming Language 的Properties"和Initializers"章节的回忆.

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

这篇关于'Class.Type' 没有名为 'variable' 的成员错误只是缺乏类变量支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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