我試圖從功能打印對象的數組元組,但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)
}
}
你應該看看結構。元組是** reallllly **不會像這樣使用。 – Alexander