无法将类型[()]的值转换为预期的参数类型()

时间:2016-11-17 23:30:52

标签: swift tuples

我正在尝试从函数中打印一个对象的数组元组,但Xcode会出现错误:

  

无法将[()]类型的值转换为预期的参数类型()

问题是我们如何绕过元组的数组方面在函数中使用它?

这是我们的代码:

import UIKit

class ViewController: UIViewController {
    var products = [("Kayak","A boat for one person","Watersports",275.0,10),
                    ("Lifejacket","Protective and fashionable","Watersports",48.95,14),
                    ("Soccer Ball","FIFA-approved size and weight","Soccer",19.5,32),
                    ("Corner Flags","Give your playing field a professional touch","Soccer",34.95,1),
                    ("Stadium","Flat-packed 35,000-seat stadium","Soccer",79500.0,4),
                    ("Thinking Cap","Improve your brain efficiency by 75%","Chess",16.0,8),
                    ("Unsteady Chair","Secretly give your opponent a disadvantage","Chess",29.95,3),
                    ("Human Chess Board","A fun game for the family","Chess",75.0,2),
                    ("Bling-Bling King","Gold-plated, diamond-studded King","Chess",1200.0,4)]

    func writeProductDetails(product:(String,String,String,Double,Int)){
        print("Name: \(product.0)")
        print("Description: \(product.1)")
        print("Category: \(product.2)")
        let formattedPrice = NSString(format: "$%.2lf",product.3)
        print("Price: \(formattedPrice)")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        writeProductDetails(product: products)
    }
}

4 个答案:

答案 0 :(得分:2)

您声明了writeProductDetails方法接受单个元组参数。但是在viewDidLoad中,您将一个元组数组作为参数传递给参数。因此,编译器无法将元组数组转换为元组的错误。

您需要将writeProductDetails方法签名更改为:

func writeProductDetails(product:[(String,String,String,Double,Int))] {

然后更新其实现以循环遍历元组。或者您需要将writeProductDetails中的viewDidLoad调用更改为仅传递products数组中的一个元组。

答案 1 :(得分:1)

您应该使用Struct来表示您的数据。一切都变得简单了:

struct Product: CustomDebugStringConvertible {
    let name: String
    let description: String
    let category: String
    let price: Double
    let something: Int //TODO: NAME ME

    var debugDescription: String {
        let formattedPrice = NSString(format: "$%.2lf", price)

        return "Product(\n" +
        "\tName: \(name)\n" +
        "\tDescription: \(description)\n" +
        "\tCategory: \(category)\n" +
        "\tPrice: \(formattedPrice)\n" +
        ")"
    }
}

class ViewController: UIViewController {
    var products = [
        Product(
            name: "Kayak",
            description: "A boat for one person",
            category: "Watersports",
            price: 275.0,
            something: 10
        ),
        Product(
            name: "Lifejacket",
            description: "Protective and fashionable",
            category: "Watersports",
            price: 48.95,
            something: 14
        ),
        Product(
            name: "Soccer Ball",
            description: "FIFA-approved size and weight",
            category: "Soccer",
            price: 19.5,
            something: 32
        ),
        Product(
            name: "Corner Flags",
            description: "Give your playing field a professional touch",
            category: "Soccer",
            price: 34.95,
            something: 1
        ),
        Product(
            name: "Stadium",
            description: "Flat-packed 35,000-seat stadium",
            category: "Soccer",
            price: 79500.0,
            something: 4
        ),
        Product(
            name: "Thinking Cap",
            description: "Improve your brain efficiency by 75%",
            category: "Chess",
            price: 16.0,
            something: 8
        ),
        Product(
            name: "Unsteady Chair",
            description: "Secretly give your opponent a disadvantage",
            category: "Chess",
            price: 29.95,
            something: 3
        ),
        Product(
            name: "Human Chess Board",
            description: "A fun game for the family",
            category: "Chess",
            price: 75.0,
            something: 2
        ),
        Product(
            name: "Bling-Bling King",
            description: "Gold-plated, diamond-studded King",
            category: "Chess",
            price: 1200.0,
            something: 4
        )
    ]

    func printProductDetails(products: [Product]){
        for product in products {
            print(product)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        printProductDetails(products: products)
    }
}

答案 2 :(得分:1)

您可以尝试在初始数组声明中声明元组部分的名称。

var products = [(name:String,description:String, category:String, price:Double, something:Int)]

然后循环它

for product in products {
    print(product.name)
    print(product.description)
    print(product.category)
    print(product.price)
    print(product.something)
}

然后您只需要添加您拥有的产品。

答案 3 :(得分:1)

首先声明tupple,然后使用tupple作为Any变量,如下所示:

let ProductTupple = (String,String,String,Double,Int)

import UIKit

class ViewController: UIViewController {
    var products[ProductTupple] = [("Kayak","A boat for one person","Watersports",275.0,10),
                    ("Lifejacket","Protective and fashionable","Watersports",48.95,14),
                    ("Soccer Ball","FIFA-approved size and weight","Soccer",19.5,32),
                    ("Corner Flags","Give your playing field a professional touch","Soccer",34.95,1),
                    ("Stadium","Flat-packed 35,000-seat stadium","Soccer",79500.0,4),
                    ("Thinking Cap","Improve your brain efficiency by 75%","Chess",16.0,8),
                    ("Unsteady Chair","Secretly give your opponent a disadvantage","Chess",29.95,3),
                    ("Human Chess Board","A fun game for the family","Chess",75.0,2),
                    ("Bling-Bling King","Gold-plated, diamond-studded King","Chess",1200.0,4)]

    func writeProductDetails(product:ProductTupple){
        print("Name: \(product.0)")
        print("Description: \(product.1)")
        print("Category: \(product.2)")
        let formattedPrice = NSString(format: "$%.2lf",product.3)
        print("Price: \(formattedPrice)")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // if passing tupple
        writeProductDetails(product: products(0))
        // if passing tupple array, in this case, you need productDetails with for loop.
        writeProductDetails(product: products)
    }
}
相关问题