数组值不会附加到列表中

时间:2018-01-06 09:18:14

标签: ios arrays swift4

class Model1:NSObject {

    var name1:String = ""
    var size1:String = ""
    var colour1:String = ""

init (name:String, size:String, colour:String) {
    self.name1 = name
    self.size1 = size
    self.colour1 = colour
  }
}

class ViewController1: UIViewController {

        var list2 = [Model1]()

        override func viewDidLoad() {
        super.viewDidLoad()
            var array1 = ["1","2","3","4"]
            var array2 = ["one","two","three","four"]
            var array3 = ["one111","two222","three333","four444"]
            // var list11 = [array1[0], array2[0], array3[0]]

            let elm11 = Model1(name: array1[0], size: array2[0], colour: array3[0])  //Error
            list2.append(elm11)

            print("result)\(Model1(name: array1[0], size: array2[0], colour: array3[0]))") ////Error (result<searchTableview.Model1: 0x604000288610>)

            list2.append(Model1(name: array1[0], size: array2[0], colour: array3[0]))
            print("list2.append(Model1(name: array1[0], size: array1[0], colour: array1[0]))\(list2.append(Model1(name: array1[0], size: array1[0], colour: array1[0])))")
        }
    }

以上代码数组值不会附加到列表中。如果我使用我的代码我
我得到了的错误                searchTableview.Model1:0x604000288610

我想结果是

   Model1(name: "1", size: "one", colour: "one111")
   Model1(name: "2", size: "two", colour: "two222")
   Model1(name: "3", size: "three", colour: "three333")

如何获得我的输出请帮助我!

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码使用您的班级数组:

class Model1:NSObject {

    var name1:String = ""
    var size1:String = ""
    var colour1:String = ""

init (name:String, size:String, colour:String) {
    self.name1 = name
    self.size1 = size
    self.colour1 = colour
  }
}

var arrItems = [Model1]()

arrItems.append(Model1(name: "Test 1", size: "1", colour: "Black"))
arrItems.append(Model1(name: "Test 2", size: "2", colour: "Blue"))

print("Name is \(arrItems[0].name1)")
  

输出:

     

名称是测试1