如何根据 class 中的另一个 属性 创建变量 属性
How to create an variable property based on the other property in the class
class someClass
{
// This is a list of restaurant names
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "Thai Cafe"]
// This is an array to record the restaurants I have visited
var restaurantIsVisited:[Bool] = [Bool](count: 21, repeatedValue: false)
// This is a function to set the restaurants I have visited
func setTheVisitedRestaurants(somenumber:Int)
{
self.restaurantIsVisited[somenumber] = true
}
}
let thisVisit = someClass()
thisVisit.setTheVisitedRestaurants(1)
let somearray = thisVisit.restaurantIsVisited
以上代码在playground中没有错误。但是,如果我不知道餐厅总数是 21 家(可能有数百家,我不想数)怎么办?我尝试了以下代码来创建一个不可变的计算 属性。但它不会工作,因为稍后 属性 将被函数 setTheVisitedRestaurants
更改并且它将 return 出错。
// var restaurantIsVisited:[Bool]{
// return [Bool](count: restaurantNames.count, repeatedValue: false)
// }
一句话,问题就是如何根据class中的另一个属性创建一个变量属性。我是初学者,我真的尝试过。请帮忙!
使用 lazy
关键字声明 restaurantIsVisited
。这将确保它在第一次访问之前不会被创建,到那时你将能够向 restaurantNames
请求它的 count
:
class someClass
{
// This is a list of restaurant names
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "Thai Cafe"]
// This is an array to record the restaurants I have visited
lazy var restaurantIsVisited:[Bool] = [Bool](count: self.restaurantNames.count, repeatedValue: false)
// This is a function to set the restaurants I have visited
func setTheVisitedRestaurants(somenumber:Int)
{
self.restaurantIsVisited[somenumber] = true
}
}
let thisVisit = someClass()
thisVisit.setTheVisitedRestaurants(1)
let somearray = thisVisit.restaurantIsVisited
println(somearray.count) // "21"
为什么不用结构体来表示餐厅,例如:
struct Restaurant {
let name: String
var visited: Bool
init(name: String, visited: Bool = false) {
self.name = name
self.visited = visited
}
}
现在可以更轻松地跟踪去过哪些餐厅,哪些没有去过,因为您不必担心每家餐厅的信息会被分成两个数组。
struct SomeStruct {
var restaurants = [Restaurant(name: "Cafe Deadend"),
Restaurant(name: "Homei"),
Restaurant(name: "Cafe Loisl")]
}
用法:
var thisVisit = SomeStruct()
thisVisit.restaurants[0].visited = true
class someClass
{
// This is a list of restaurant names
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "Thai Cafe"]
// This is an array to record the restaurants I have visited
var restaurantIsVisited:[Bool] = [Bool](count: 21, repeatedValue: false)
// This is a function to set the restaurants I have visited
func setTheVisitedRestaurants(somenumber:Int)
{
self.restaurantIsVisited[somenumber] = true
}
}
let thisVisit = someClass()
thisVisit.setTheVisitedRestaurants(1)
let somearray = thisVisit.restaurantIsVisited
以上代码在playground中没有错误。但是,如果我不知道餐厅总数是 21 家(可能有数百家,我不想数)怎么办?我尝试了以下代码来创建一个不可变的计算 属性。但它不会工作,因为稍后 属性 将被函数 setTheVisitedRestaurants
更改并且它将 return 出错。
// var restaurantIsVisited:[Bool]{
// return [Bool](count: restaurantNames.count, repeatedValue: false)
// }
一句话,问题就是如何根据class中的另一个属性创建一个变量属性。我是初学者,我真的尝试过。请帮忙!
使用 lazy
关键字声明 restaurantIsVisited
。这将确保它在第一次访问之前不会被创建,到那时你将能够向 restaurantNames
请求它的 count
:
class someClass
{
// This is a list of restaurant names
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "Thai Cafe"]
// This is an array to record the restaurants I have visited
lazy var restaurantIsVisited:[Bool] = [Bool](count: self.restaurantNames.count, repeatedValue: false)
// This is a function to set the restaurants I have visited
func setTheVisitedRestaurants(somenumber:Int)
{
self.restaurantIsVisited[somenumber] = true
}
}
let thisVisit = someClass()
thisVisit.setTheVisitedRestaurants(1)
let somearray = thisVisit.restaurantIsVisited
println(somearray.count) // "21"
为什么不用结构体来表示餐厅,例如:
struct Restaurant {
let name: String
var visited: Bool
init(name: String, visited: Bool = false) {
self.name = name
self.visited = visited
}
}
现在可以更轻松地跟踪去过哪些餐厅,哪些没有去过,因为您不必担心每家餐厅的信息会被分成两个数组。
struct SomeStruct {
var restaurants = [Restaurant(name: "Cafe Deadend"),
Restaurant(name: "Homei"),
Restaurant(name: "Cafe Loisl")]
}
用法:
var thisVisit = SomeStruct()
thisVisit.restaurants[0].visited = true