2016-08-23 34 views
0

以下代碼在iPhone 6和5S模擬器上完美工作,但是當我嘗試在iPhone 5模擬器上運行此代碼時,這些代碼行不起作用。自定義字典不設置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 
     } 
    } 
} 
+0

你確定這個問題是不是不同版本的iOS?你在每個模擬器上使用了哪些iOS版本?嘗試打印init的字典參數以確保每次都有相同的條目。 – deadbeef

+0

我在所有模擬器中使用iOS 9.3。另外,字典總是一樣的。 –

回答

0

有做這一切的多...更簡單的方法:

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, 
     ] 
    } 
} 
+0

給這個男人一個該死的複選標記! – ospahiu

+0

我仍然面臨與此解決方案相同的問題,並且init的字典參數可能爲零或有一些零鍵。 –

+0

您能詳細解答一下問題嗎? – Alexander