我想在這裏做兩件不同的事情,並且兩者都有錯誤。我試圖將結構添加到字典,然後我寫了一個函數,隨機抽取一個字典條目。將結構添加到字典中,然後隨機調用這些結構
這裏的結構:
struct Customer
{
var name: String
var email: String
}
和字典:
var customerDatabase: [String: String] = [Customer(name: "Lionel Messi", email:
"[email protected]"), Customer(name: "Cristiano Ronaldo", email: "[email protected]"),
Customer(name: "Wayne Rooney", email: "[email protected]")]
這裏的錯誤消息我得到的詞典:
Playground execution failed: :45:42: error: type '[String : String]' does not conform to protocol 'ArrayLiteralConvertible' var customerDatabase: [String: String] = [Customer(name: "Lionel Messi", email: "[email protected]"), Customer(name: "Cristiano Ronaldo", email: "[email protected]"), Customer(name: "Wayne Rooney", email: "[email protected]")]
最後,我的功能,將從我的字典中隨機抽取一個Customer結構體。
func randomCustomer() ->()
{
var customer = arc4random_uniform(customerDatabase)
return customer
}
我的功能的錯誤消息。
<EXPR>:48:39: error: '[String : String]' is not convertible to 'UInt32'
var customer = arc4random_uniform(customerDatabase)
^
Darwin._:1:5: note: in initialization of parameter '_'
let _: UInt32
對於問這樣一個簡單的問題,我感覺自己像個小菜鳥。非常感謝您的幫助!
太棒了,這有助於很多。你介意分享你在這個功能中做了什麼,以使它發揮作用嗎?假裝你正在向一個5歲的孩子解釋(我是Swift的新手,每個細節都有幫助)。 – giwook 2014-08-31 00:43:44
另外,我看到你在說什麼關於使用數組而不是字典。如果我想使用字典,以便將客戶的名稱用作關鍵字,那麼我將如何實現? – giwook 2014-08-31 00:49:15