简体   繁体   English

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

[英]Swift - How to declare a private nested struct?

I want o make my code more readable, so I decided to make some repeated dictionary keys, soft coded... So I created a new .swift file, with 2 structs in it: 我想让我的代码更具可读性,所以我决定制作一些重复的字典键,软编码...所以我创建了一个新的.swift文件,里面有2个结构:

struct DatabaseTableNames {
    let Photo = PhotoTable()
}

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

So I have the initial struct I'm gonna use and the second one, which I don't want it to be visible outside of the file's scope... The thing is, it says that the Photo property of the DatabaseTableNames struct, needs to be declared as fileprivate since PhotoTable is private ... 所以我有我要使用的初始结构和第二个结构,我不希望它在文件范围之外可见......事情是,它说, DatabaseTableNames结构的Photo属性,需要要声明为fileprivate因为PhotoTableprivate ......

What am I doing wrong here? 我在这做错了什么?

The key was to nest PhotoTable and make its properties static. 关键是嵌套PhotoTable并使其属性保持静态。

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

Example Use: 示例使用:

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

swap around your private settings - and make sure this is defined in the same file as the UIViewController you want to use it 交换您的private设置 - 并确保在与您要使用它的UIViewController相同的文件中定义它

private struct DatabaseTableNames {
    let Photo = PhotoTable()
}

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

and then, access the struct 然后,访问结构

private var photo : DatabaseTableNames?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM