【问题标题】:How do you access the attributes of a class instance from inside an array? (Swift)如何从数组内部访问类实例的属性? (迅速)
【发布时间】:2014-12-29 19:28:55
【问题描述】:

我不知道怎么做。这是我的代码,我希望从存储它们的数组外部访问tastePfrence 和temperaturePreference 属性(客户类的属性)。

// Create Customers
    var arrayOfCustomers:[Customer] = []
    var index = 0
    for index; index < 10; index++ {
        var customer = Customer()
        customer.tastePreference = Int(arc4random_uniform(UInt32(10)))
        customer.temperaturePreference = Int(arc4random_uniform(UInt32(10)))
        arrayOfCustomers += [customer]
    }

【问题讨论】:

    标签: arrays class swift


    【解决方案1】:

    您可以通过下标访问数组的成员,然后像往常一样访问属性。例如:

    let temperaturePreference = arrayOfCustomers[0].temperaturePreference
    

    对于数组中的第一个客户。

    【讨论】:

      猜你喜欢
      • 2015-06-09
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 1970-01-01
      相关资源
      最近更新 更多