2016-08-23 3 views
0

Le code suivant ci-dessous fonctionne parfaitement sur simulateur iPhone 6 et 5S, mais lorsque j'essaie d'exécuter ce code sur iPhone 5 simulateur ces lignes de code ne fonctionnent pas.Dictionnaire personnalisé ne définissant pas de valeur sur iPhone 5

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

Le dictionnaire ne change tout simplement pas sa valeur, il reste vide. Je n'ai aucune idée de pourquoi cela se passe. Voici le code complet:

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

Êtes-vous sûr que le problème n'est pas différentes versions d'iOS? Quelle version iOS utilisez-vous sur chaque simulateur? Essayez d'imprimer le paramètre dictionnaire de init pour vous assurer que vous avez la même entrée à chaque fois. – deadbeef

+0

J'utilise iOS 9.3 dans tous les simulateurs. De plus, le dictionnaire c'est toujours pareil. –

Répondre

0

Il y a beaucoup ... moyen beaucoup plus facile de faire tout cela:

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

Donnez une foutue coche à cet homme! – ospahiu

+0

Je suis toujours confronté au même problème avec cette solution, aussi, le paramètre du dictionnaire à l'init peut être nul ou avoir quelques clés nulles. –

+0

Pourriez-vous élaborer sur les problèmes? – Alexander