【问题标题】:Custom dictionary not setting value on iPhone 5自定义字典未在 iPhone 5 上设置值
【发布时间】:2016-12-30 17:14:34
【问题描述】:

以下代码在 iPhone 6 和 5S 模拟器上完美运行,但是当我尝试在 iPhone 5 模拟器上运行此代码时,这些代码行不起作用。

self.groupCollection[creditCadKey] = creditCard
self.groupCollection[homeKey] = home
self.groupCollection[boletoKey] = boleto

字典根本不会改变它的值,它仍然是空的。我不知道为什么会这样。完整代码如下:

import Foundation

public class Checkout_GroupedPaymentSystem : NSObject {
    public var creditCard: [Checkout_PaymentSystem]?
    public var home: [Checkout_PaymentSystem]?
    public var voucher: [Checkout_PaymentSystem]?
    public var boleto: [Checkout_PaymentSystem]?

    public let creditCadKey = "Cartão de Crédito"
    public let homeKey = "Pagamento em Casa"
    public let voucherKey = "Voucher"
    public let boletoKey = "Boleto Bancário"

    public var groupCollection: [String:[Checkout_PaymentSystem]] = [:]

    public init(fromDictionary dictionay: NSDictionary) {

        if let creditCardArray = dictionay[creditCadKey] as? [NSDictionary]{
            creditCard = [Checkout_PaymentSystem]()
            for dic in creditCardArray {
                let value = Checkout_PaymentSystem(fromDictionary: dic)
                creditCard?.append(value)
            }
            self.groupCollection[creditCadKey] = creditCard
        }


         if let homeArray = dictionay[homeKey] as? [NSDictionary]{
            home = [Checkout_PaymentSystem]()
            for dic in homeArray {
                let value = Checkout_PaymentSystem(fromDictionary: dic)
                home?.append(value)
            }

            self.groupCollection[homeKey] = home
        }

        if let voucherArray = dictionay[voucherKey] as? [NSDictionary]{
            voucher = [Checkout_PaymentSystem]()
            for dic in voucherArray {
                let value = Checkout_PaymentSystem(fromDictionary: dic)
                voucher?.append(value)
            }

            //self.groupCollection[voucherKey] = voucher
       }

        if let boletoArray = dictionay[boletoKey] as? [NSDictionary]{
            boleto = [Checkout_PaymentSystem]()
            for dic in boletoArray {
                let value = Checkout_PaymentSystem(fromDictionary: dic)
                boleto?.append(value)
            }

            self.groupCollection[boletoKey] = boleto
        }
    }
}

【问题讨论】:

  • 您确定问题不是不同版本的 iOS 吗?您在每个模拟器上使用什么 iOS 版本?尝试打印 init 的字典参数,以确保每次都有相同的条目。
  • 我在所有模拟器中都使用 iOS 9.3。另外,字典总是一样的。

标签: ios swift iphone-5


【解决方案1】:

有一个更...更简单的方法来做这一切:

import Foundation

public class Checkout_GroupedPaymentSystem : NSObject {
    public static let creditCardKey = "Cartão de Crédito"
    public static let homeKey = "Pagamento em Casa"
    public static let voucherKey = "Voucher"
    public static let boletoKey = "Boleto Bancário"

    public var creditCards: [Checkout_PaymentSystem]?
    public var homes: [Checkout_PaymentSystem]?
    public var vouchers: [Checkout_PaymentSystem]?
    public var boletos: [Checkout_PaymentSystem]?

    public var groupCollection: [String:[Checkout_PaymentSystem]] = [:]

    public init(fromDictionary dictionary: NSDictionary) {
        guard let creditCardArray = dictionary[creditCardKey] as? [NSDictionary],
            let homeArray = dictionary[homeKey] as? [NSDictionary],
            let voucherArray = dictionary[voucherKey] as? [NSDictionary],
            let boletoArray = dictionary[boletoKey] as? [NSDictionary]else {
            fatalError("One of these is nil or is the wrong type.")
        }

        self.creditCards = creditCardArray.map{ Checkout_PaymentSystem(fromDictionary: $0) }
        self.homes = homeArray.map{ Checkout_PaymentSystem(fromDictionary: $0) }
        self.vouchers = voucherArray.map{ Checkout_PaymentSystem(fromDictionary: $0) }
        self.botelo = boletoArray.map{ Checkout_PaymentSystem(fromDictionary: $0) }

        self.groupCollection = [
            creditCardKey: self.creditCards,
            homeKey: self.creditCards,
            voucherKey: self.creditCards,
            boletoKey: self.creditCards,
        ]
    }
}

【讨论】:

  • 这个解决方案我仍然面临同样的问题,而且初始化时的字典参数可能是 nil 或有一些 nil 键。
  • 您能详细说明问题吗?
猜你喜欢
  • 1970-01-01
  • 2016-12-30
  • 2018-12-21
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
  • 2018-05-08
相关资源
最近更新 更多