使用常量初始化swift数组

时间:2014-10-06 21:42:47

标签: arrays swift constants

我有一个二维数组,我试图用它来将一组值从拾取器映射到一组特定的值。我想让它可读,所以我尝试使用C样式并将我的一组值定义为常量,然后在初始化中使用它们,但是我收到一个错误,告诉我我的类没有名为常量的成员。我在操场上进行了测试,一切都很有效,因为操场是解释性的。任何人有任何想法如何最好地解决这个问题。我知道如何从技术角度解决问题,但我对保持可读性非常着迷。下面是代码吸管

// definitions for FORCE objects

let LIVELY              = 0
let LIGHT               = 1
let ROUNDED             = 2
let POWERFUL            = 3
let CHAMPAGNE           = 4
let SUCREBLANC          = 5
let SUCREROUGEOUBLANC   = 6
let SUCREROUGE          = 7
let CHAMPAGNEROSE       = 8
let ROSE                = 9

//definitions for SAVEUR objects

let FLORAL              = 0
let FRUITY              = 1
let OAKY                = 2
let SPICY               = 3

let redForceNamePointerArray = [
    /*           1     2        3       4      5        6            7             8         9       10      11         12    */
    /* 1 */   [LIGHT,ROUNDED,POWERFUL,NOIMAGE,LIGHT,CHAMPAGNE,SUCREROUGEOUBLANC,CHAMPAGNE,ROUNDED,ROUNDED,SUCREBLANC,POWERFUL],
    /* 1 */   [LIGHT,ROUNDED,POWERFUL,NOIMAGE,LIGHT,CHAMPAGNE,SUCREROUGEOUBLANC,CHAMPAGNE,ROUNDED,ROUNDED,SUCREBLANC,POWERFUL],
    /* 1 */   [LIGHT,ROUNDED,POWERFUL,NOIMAGE,LIGHT,CHAMPAGNE,SUCREROUGEOUBLANC,CHAMPAGNE,ROUNDED,ROUNDED,SUCREBLANC,POWERFUL],
    /* 1 */   [LIGHT,ROUNDED,POWERFUL,NOIMAGE,LIGHT,CHAMPAGNE,SUCREROUGEOUBLANC,CHAMPAGNE,ROUNDED,ROUNDED,SUCREBLANC,POWERFUL],
    /* 1 */   [LIGHT,ROUNDED,POWERFUL,NOIMAGE,LIGHT,CHAMPAGNE,SUCREROUGEOUBLANC,CHAMPAGNE,ROUNDED,ROUNDED,SUCREBLANC,POWERFUL],
    /* 1 */   [LIGHT,ROUNDED,POWERFUL,NOIMAGE,LIGHT,CHAMPAGNE,SUCREROUGEOUBLANC,CHAMPAGNE,ROUNDED,ROUNDED,SUCREBLANC,POWERFUL]];

l

1 个答案:

答案 0 :(得分:2)

常量初始值设定项不能引用类中的其他常量。这是语言或编译器的限制。这是另一种选择:

class C {
    enum Image: Int {
        case NOIMAGE = -1
        case LIVELY
        case LIGHT
        case ROUNDED
        case POWERFUL
        case CHAMPAGNE
        case SUCREBLANC
        case SUCREROUGEOUBLANC
        case SUCREROUGE
        case CHAMPAGNEROSE
        case ROSE
    }

    enum Flavor: Int {
        case FLORAL = 0
        case FRUITY
        case OAKY
        case SPICY
    }

    let redForceNamePointerArray: [[Image]] = {
        typealias I = Image
        return [
            /*           1     2        3       4      5        6            7             8         9       10      11         12    */
            /* 1 */   [I.LIGHT,I.ROUNDED,I.POWERFUL,I.NOIMAGE,I.LIGHT,I.CHAMPAGNE,I.SUCREROUGEOUBLANC,I.CHAMPAGNE,I.ROUNDED,I.ROUNDED,I.SUCREBLANC,I.POWERFUL],
            /* 1 */   [I.LIGHT,I.ROUNDED,I.POWERFUL,I.NOIMAGE,I.LIGHT,I.CHAMPAGNE,I.SUCREROUGEOUBLANC,I.CHAMPAGNE,I.ROUNDED,I.ROUNDED,I.SUCREBLANC,I.POWERFUL]
        ]
    }()
}

但现在redForceNamePointerArray的元素是C.Image而不是Int。如果你真的希望它们是Int

    let redForceNamesInt: [[Int]] = {
        typealias I = Image
        return [
            /*           1     2        3       4      5        6            7             8         9       10      11         12    */
            /* 1 */   [I.LIGHT,I.ROUNDED,I.POWERFUL,I.NOIMAGE,I.LIGHT,I.CHAMPAGNE,I.SUCREROUGEOUBLANC,I.CHAMPAGNE,I.ROUNDED,I.ROUNDED,I.SUCREBLANC,I.POWERFUL],
            /* 1 */   [I.LIGHT,I.ROUNDED,I.POWERFUL,I.NOIMAGE,I.LIGHT,I.CHAMPAGNE,I.SUCREROUGEOUBLANC,I.CHAMPAGNE,I.ROUNDED,I.ROUNDED,I.SUCREBLANC,I.POWERFUL]
        ].map { $0.map { $0.rawValue } }
    }()