如何声明新数组以附加元组数组?

时间:2016-10-03 08:10:39

标签: ios arrays swift

我有两个数组,内容是元组。我需要向它展示不同的表格。但是,我想将两个数组放在一个数组中。那么声明新数组追加devCourseswebCourses数组的最佳方法是什么。

  var devCourses = [
            ("iOS App","Simon Allardice"),
            ("iOS 9 with swift","Matt Neuburg")]

    var webCourses = [
            ("HTML","James"),
            ("Design","Ray") ]

enter image description here

我需要在viewdidload之外声明它 将其附加到viewdidload

2 个答案:

答案 0 :(得分:0)

As Array:

var courses:[[(String, String )]]!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    var devCourses = [
        ("iOS App","Simon Allardice"),
        ("iOS 9 with swift","Matt Neuburg")]

    var webCourses = [
        ("HTML","James"),
        ("Design","Ray") ]

    courses = [devCourses , webCourses]
    print(courses)
}

作为字典:如果要在表格节标题中显示节标题,这是更好的选择。

var courses:[String:[(String, String )]]! = [:]

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    let devCourses = [
        ("iOS App","Simon Allardice"),
        ("iOS 9 with swift","Matt Neuburg")]

    let webCourses = [
        ("HTML","James"),
        ("Design","Ray") ]

    courses["devCourses"] = devCourses
    courses["webCourses"] = webCourses
    print(courses)
}

答案 1 :(得分:0)

您可以使用append函数将新元素添加到数组中,但是这种情况我建议您使用类,如果您可以使用类,则更容易管理代码。