gpt4 book ai didi

swift - 具有默认值的自定义变量 setter

转载 作者:行者123 更新时间:2023-11-28 16:10:22 26 4
gpt4 key购买 nike

我无法为这个简单的类创建具有默认值的自定义 setter。我想通过时区检查实现自定义 tz setter。

怎么了?

public class City: NSObject {
public var lat = 0.0, lon = 0.0, name = ""
private static let def_tz = "GMT+00:00"
private static let timezones = NSTimeZone.knownTimeZoneNames()
public var tz: String = def_tz {
set { //// <<<<< There is the error
var i = City.timezones.indexOf(newValue ?? City.def_tz) ?? 0
self.tz = timezones[i].name
}
get {
return self.tz
}
}
// MARK:- Init

required public init(name: String, lat: Double, lon: Double, tz: String) {
super.init()
setup(name, lat: lat, lon: lon, tz: tz)
}

private func setup(name: String, lat: Double, lon: Double, tz: String) {
self.lat = lat
self.lon = lon

self.tz = tz == "" ? City.def_tz : tz
self.name = name
}

}

错误是

City.swift:16:9: Use of unresolved identifier 'set'

最佳答案

关于swift - 具有默认值的自定义变量 setter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39637568/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com