Swift - 如何声明私有嵌套结构?

时间:2017-02-01 21:36:19

标签: swift struct nested private

我想让我的代码更具可读性,所以我决定制作一些重复的字典键,软编码...所以我创建了一个新的.swift文件,里面有2个结构:

struct DatabaseTableNames {
    let Photo = PhotoTable()
}

private struct PhotoTable {
    let lowQuality = "lowQuality"
    let highQuality = "highQuality"
    let numberOfLikes = "numberOfLikes"
}

所以我有我要使用的初始结构和第二个结构,我不希望它在文件范围之外可见......事实是,它表示Photo DatabaseTableNames结构的属性需要声明为fileprivate,因为PhotoTableprivate ...

我在这里做错了什么?

2 个答案:

答案 0 :(得分:7)

关键是要嵌套PhotoTable并使其属性保持静态。

struct DatabaseTableNames {
    struct PhotoTable {
        static let lowQuality = "lowQuality"
        static let highQuality = "highQuality"
        static let numberOfLikes = "numberOfLikes"
    }
}

使用示例:

let test = DatabaseTableNames.PhotoTable.lowQuality
print(test)

答案 1 :(得分:1)

交换您的private设置 - 并确保在与您想要使用它的UIViewController相同的文件中定义

private struct DatabaseTableNames {
    let Photo = PhotoTable()
}

struct PhotoTable {
    let lowQuality = "lowQuality"
    let highQuality = "highQuality"
    let numberOfLikes = "numberOfLikes"
}

然后,访问struct

private var photo : DatabaseTableNames?