如何访问结构中的字典

时间:2018-05-05 20:23:41

标签: swift dictionary struct

我在struct中有一个带字典的结构,我想访问struct中字典的内容。

这是我的结构:

struct TransactionsObject: Codable {
    let UserInitiatedTransactions: [UserEdTransaction]?
    let UserReceivedTransactions: [UserEdTransaction]?

    enum CodingKeys: String, CodingKey {
        case UserInitiatedTransactions = "UserInitiatedTransactions"
        case UserReceivedTransactions = "UserReceivedTransactions"
    }
}

struct UserEdTransaction: Codable {
    let transactionCode: Int?
    let transactionDate: String?
    let transactionType: String?
    let transactionTime: String?
    let transactionAmount: String?
    let transactionTo: String?
    let transactionFrom: String?

    enum CodingKeys: String, CodingKey {
        case transactionCode = "transactionCode"
        case transactionDate = "transactionDate"
        case transactionType = "transactionType"
        case transactionTime = "transactionTime"
        case transactionAmount = "transactionAmount"
        case transactionTo = "transactionTo"
        case transactionFrom = "transactionFrom"
    }
}

我想访问此 transactionCode 。我试过这个到TransactionsObject.UserInitiatedTransactions.transactionCode,我得到以下错误:

Value of type '[UserEdTransaction]?' has no member 'transactionCode'

3 个答案:

答案 0 :(得分:3)

首先问题应该是如何在结构中访问数组

仅当结构成员和键与例如 lowerCamelCase 变量名称不匹配时,才使用CodingKeys

第三,只声明结构的成员是可选的,其关键等价物可能缺失

struct TransactionsObject: Codable {
    let userInitiatedTransactions: [UserEdTransaction]
    let userReceivedTransactions: [UserEdTransaction]

    enum CodingKeys: String, CodingKey {
        case userInitiatedTransactions = "UserInitiatedTransactions"
        case userReceivedTransactions = "UserReceivedTransactions"
    }
}

struct UserEdTransaction: Codable { // no CodingKeys !!
    let transactionCode: Int
    let transactionDate: String
    let transactionType: String
    let transactionTime: String
    let transactionAmount: String
    let transactionTo: String
    let transactionFrom: String
}

发生错误是因为userInitiatedTransactions是一个数组。您需要索引订阅

print(transactionsObject.userInitiatedTransactions[0]) // can crash if the array is empty

或重复循环

for transaction in transactionsObject.userInitiatedTransactions {
    print(transaction.transactionCode)
}

答案 1 :(得分:2)

你没有字典。 UserInitiatedTransactions是一个数组。数组没有transactionCode,但是数组的元素有。{/ p>

现在问题变成了,您想要哪个交易的交易代码?

如果你想要第一个,你可以这样做:

someTransactionsObject.UserInitiatedTransactions.first?.transactionCode

如果你想要第四个,

someTransactionsObject.UserInitiatedTransactions[3].transactionCode

如果你想要第n个,

someTransactionsObject.UserInitiatedTransactions[n-1].transactionCode

或者您可以获得一系列交易代码:

someTransactionsObject.UserInitiatedTransactions.map { $0.transactionCode }

答案 2 :(得分:1)

您正在访问array作为UserEdTransaction对象,因此您需要从数组中获取对象,然后使用如下所示:

TransactionsObject.UserInitiatedTransactions.first.transactionCode

TransactionsObject.UserInitiatedTransactions[0].transactionCode

TransactionsObject.UserInitiatedTransactions.forEach { (object) in
            print(object.transactionCode)
        }