2016-11-17 157 views
0

我試圖從功能打印對象的數組元組,但Xcode中介紹錯誤:無法將類型[()],以期望的參數類型()的價值

cannot convert value of type [()] to expected argument type()

的問題是怎麼做的我們繞過元組的數組方面在函數內使用它?

這裏是我們的代碼:

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

你應該看看結構。元組是** reallllly **不會像這樣使用。 – Alexander

回答

1

首先聲明元組,然後用元組的任何變量象下面這樣:

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

你宣佈你的writeProductDetails方法接受一個元組參數。但是在viewDidLoad中,您將一個元組數組作爲參數傳遞給參數。因此,編譯器無法將元組數組轉換爲元組的錯誤。

您可能需要您的writeProductDetails方法簽名更改爲:

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

,然後通過元組更新其實施循環。或者您需要將writeProductDetails的呼叫更改爲viewDidLoad,以便只傳遞products陣列中的一個元組。

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

雖然建議使用'struct'作爲一個很好的建議,但這個答案實際上並沒有試圖回答問題中描述的問題。 – rmaddy

+0

我沒有打擾,它只是你的答案的副本。 – Alexander

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

然後你只需要添加你必須有產品循環。

相關問題