2016-12-06 11 views
-2

私はSwiftと私は現時点でちょっと立ち往生しています... "for"(***と記されています)の部分でエラーが発生し続けています。なぜそれが見えませんか?ここで(キー:X、値:X)は '(_、_、..........)'に変換できません。タプルの要素数は異なります

は、全体のコードです:

private func startPreload() { 

    if UserDefaults.standard.bool(forKey: ISPRELOADED) { 
     return 
    } 

    let loans = readPreload() 

    // *** Error happens here: 
    for (name, amount, note, created, due, done, image, contactInfo, contactTypeMail, chargeMode, chargeAmount) in loans { 
     let _ = LoanResources.insertLoan(withName: name, andAmount: amount, andNote: note, andCreated: created, andDue: due, andDone: done, andImage: image, andContactInfo: contactInfo, andContactTypeMail: contactTypeMail, andChargeMode: chargeMode, andChargeAmount: chargeAmount) 
    } 

    UserDefaults.standard.set(true, forKey: ISPRELOADED) 
} 

private func readPreload() -> [String: String] { 

    var preparedLoans = [String: String]() 

    let preloadFileUrl = Bundle.main.url(forResource: "preload", withExtension: "strings") 

    do { 
     guard let fileLoan = preloadFileUrl else { 
      return preparedLoans 
     } 

     let data = try Data(contentsOf: fileLoan) 
     let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! NSDictionary 

     let loanArray = json["Loans"] as! NSArray 
     for loan in loanArray { 
      let loanDict = loan as! NSDictionary 

      let name = loanDict["name"] as! String 
      let amount = loanDict["amount"] as! String 
      let note = loanDict["note"] as! String 
      let created = loanDict["created"] as! String 
      let due = loanDict["due"] as! String 
      let done = loanDict["done"] as! String 
      let image = loanDict["image"] as! String 
      let contactInfo = loanDict["contactInfo"] as! String 
      let contactTypeMail = loanDict["contactTypeMail"] as! String 
      let chargeMode = loanDict["chargeMode"] as! String 
      let chargeAmount = loanDict["chargeAmount"] as! String 

      preparedLoans[name] = name 
      preparedLoans[amount] = amount 
      preparedLoans[note] = note 
      preparedLoans[created] = created 
      preparedLoans[due] = due 
      preparedLoans[done] = done 
      preparedLoans[image] = image 
      preparedLoans[contactInfo] = contactInfo 
      preparedLoans[contactTypeMail] = contactTypeMail 
      preparedLoans[chargeMode] = chargeMode 
      preparedLoans[chargeAmount] = chargeAmount 
     } 

    } catch { 
     print(error.localizedDescription) 
    } 

    return preparedLoans 
} 

たぶん私はさらにステップと借入金のうち、キーと値を取得する必要がありますか?

答えて

1

このエラーメッセージ:

(キー:X、値:X)はタプルが異なる数の要素を持っている、 '(_、_、...)' への転換ではない

forループに要素が多すぎることを示します。あなたの場合は1つだけ必要です

for loan in loans { 
     let _ = LoanResources.insertLoan(
       withName: loan.name, 
       andAmount: loan.amount, 
       andNote: loan.note, 
       andCreated: loan.created, 
       andDue: loan.due, 
       andDone: loan.done, 
       andImage: loan.image, 
       andContactInfo: loan.contactInfo, 
       andContactTypeMail: loan.contactTypeMail, 
       andChargeMode: loan.chargeMode, 
       andChargeAmount: loan.chargeAmount) 
} 
関連する問題