【问题标题】:(Key: X, value: X) is not convertible to '(_, _, ..........)', tuples have a different number of elements(Key: X, value: X) 不能转换为 '(_, _, ..........)',元组有不同数量的元素
【发布时间】:2016-12-06 15:00:16
【问题描述】:

我对 Swift 很陌生,现在我有点卡住了...我在“for”(标有 ***)部分不断收到错误...我不能不明白为什么?!

这是完整的代码:

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
}

也许我必须进一步从贷款中获取关键和价值?

【问题讨论】:

    标签: arrays json swift dictionary tuples


    【解决方案1】:

    这个错误信息:

    (Key: X, value: X) 不能转换为 '(_, _, ...)',元组有不同数量的元素

    表示for 循环的元素过多。在您的情况下,您只需要一个

    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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      • 2014-04-23
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多