定义多维对象的属性 [英] Define object with multidimensional properties

查看:172
本文介绍了定义多维对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:
我正在寻找的是,所以我想我需要在表格中显示此信息:

PROBLEM: What I'm looking for is to display this info in a table so I think I need:

MyHouses1 [节] [行] .getAddressDetails()

MyHouses1[section][row].getAddressDetails()

我如何正确地定义MyHouses1?

How do I properly define MyHouses1?

    import UIKit

class House {
    var address = "Address"
    var street = "Street"
    var city = "City"
    var state = "State"
    var zip = "Zip"

    func getAddressDetails() -> String {
        return "\(address) \(street) \(city) \(state) \(zip)"
        //return "\(address) \(street)"
    }
    func getCityState() -> String {
        return "\(city) - \(state)"
    }
}

class newhouse: House {
    var lighting = "crappy"
    var plumbing = "sucks"
    var heating = "heats good"
    var cooling = "cools good"
    var repairs = "needs alot of repairs"

    func getFixtureDetails() -> String {
        return "\(lighting) \(plumbing) \(heating) \(cooling)"
    }
}

// THIS WORKS
var MyHouses: [newhouse] = [] 
MyHouses.append(newhouse())  
MyHouses[0].address = "test" 
MyHouses[0].getAddressDetails() 


// THIS DOESN'T WORK 

var MyHouses1: [[newhouse]] = [] // No Error Yet
MyHouses1.append // Getting an error here, not sure how to append

详细信息:为了发布网站要我增加更多的细节.....所以我在这里加入了更多细节笑

Details: in order to post the website wants me to add more details.....so here I am adding more details lol

推荐答案

您可以做到这一点。

var MyHouses1: [[newhouse]] = []// No Error Yet
var houses = [newhouse]() //Initialize your `newhouse` array here

for i in 0...4 {
    houses += [newhouse()]
}

MyHouses1 += [houses] 

print("\(MyHouses1[0][0].city)") // will print "City"

这篇关于定义多维对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆