2016-01-02 56 views
-2

我想知道如何添加更多元素到我的數組'faq'。現在它只是圖像,但我想串起一個標籤和其他圖像。這是Xcode Swift。Swift:添加更多元素到我的追加

VAR問題:[FAQ] = FAQ

var selectQuest = 0; 

var doneQuest = Bool() 

override func viewDidLoad() { 
    super.viewDidLoad() 


    questions.append(faq(fImage: "1.png") here x) or X<-- I guess in here where ar you add more elements? 
    questions.append(faq(fImage: "2.png")) 
    questions.append(faq(fImage: "3.png")) 
    questions.append(faq(fImage: "4.png")) 
    questions.append(faq(fImage: "5.png")) 
    questions.append(faq(fImage: "6.png")) 
    questions.append(faq(fImage: "7.png")) 
    questions.append(faq(fImage: "8.png")) 



    dispendserIQGCollectionView.reloadData() 
    print(questions.count) 
+2

什麼是'faq'?爲該類/結構添加更多變量。 – luk2302

回答

0

如果我理解你的問題正確,你有FAQ陣列。 FAQ類有一個名爲fImage的成員。但是,您希望將更多成員添加到課程中。

我覺得你的類看起來是這樣的 -

class FAQ { 

    var fImage: UIImage 
    /*Rest of class*/ 
} 

要更多成員添加到類,你需要添加一個名爲fLabel到類其他成員,如下圖所示

class FAQ { 

    var fImage: UIImage 
    var fLabel: UILabel 

    /*Rest of class*/ 
} 
1

Swift Documentation: Collection Types

另外,追加的一個或多個兼容項目的數組與另外賦值運算符(+=):

shoppingList += ["Baking Powder"] 
// shoppingList now contains 4 items 
shoppingList += ["Chocolate Spread", "Cheese", "Butter"] 
// shoppingList now contains 7 items