设置 self 时无法正确更新浮动的 UILabel 文本的函数(仅适用于 super)
function to set UILabel text from float not correctly updating when setting self (only works with super)
我在 UILabel 的子类上有一个类似这样的函数:
func setTextFromFloat(amount: Float) {
super.text = formatCurrency(fromFloat: amount)
}
还有一个:
internal func formatCurrency(fromFloat amount: Float) -> String {
let currencyFormatter: NSNumberFormatter = NSNumberFormatter()
currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
currencyFormatter.locale = NSLocale.currentLocale()
print(currencyFormatter.stringFromNumber(NSNumber(float: amount))!)
return currencyFormatter.stringFromNumber(NSNumber(float: amount))!
}
和覆盖初始化:
override internal var text: String? {
didSet {
super.text = formatCurrency(fromString: self.text)
}
}
然后我可以打电话给:
myLabel.setTextFromFloat(2.5)
而且有效。
它最初并没有工作,因为首先我尝试在 setTextFromFloat()
方法中设置 self.text
而不是 super.text
。为什么我必须将其更改为设置 super
?为什么 self
不起作用?
This looks as an interview question ;-)
问题基本上是您覆盖了 var text
存储的变量并添加了一个观察者(通过 didSet
)基本上每次都重写相同的值在无限循环中(如果您在该行中调用 self
而不是 super
)
它适用于 super
,因为您依赖 不 让观察者设置数据的实现。
一个快速的解决方案是删除该观察者,无论哪种方式,您都在调用 setTextFromFloat(amount: Float)
来完成工作。
A quick snippet solving the problem follows:
class ExtendedLabel : UILabel {
func setTextFromFloat(amount: Float) {
self.text = formatCurrency(fromFloat: amount)
}
internal func formatCurrency(fromFloat amount: Float) -> String {
let currencyFormatter: NSNumberFormatter = NSNumberFormatter()
currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
currencyFormatter.locale = NSLocale.currentLocale()
let formattedFloat = currencyFormatter.stringFromNumber(NSNumber(float: amount))!
print(formattedFloat)
return formattedFloat
}
}
class ViewController: UIViewController {
@IBOutlet var lab : ExtendedLabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
lab.setTextFromFloat(2.5)
}
}
无论哪种方式,您都可能希望将此视为可能的 扩展 ,并重构代码。
我在 UILabel 的子类上有一个类似这样的函数:
func setTextFromFloat(amount: Float) {
super.text = formatCurrency(fromFloat: amount)
}
还有一个:
internal func formatCurrency(fromFloat amount: Float) -> String {
let currencyFormatter: NSNumberFormatter = NSNumberFormatter()
currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
currencyFormatter.locale = NSLocale.currentLocale()
print(currencyFormatter.stringFromNumber(NSNumber(float: amount))!)
return currencyFormatter.stringFromNumber(NSNumber(float: amount))!
}
和覆盖初始化:
override internal var text: String? {
didSet {
super.text = formatCurrency(fromString: self.text)
}
}
然后我可以打电话给:
myLabel.setTextFromFloat(2.5)
而且有效。
它最初并没有工作,因为首先我尝试在 setTextFromFloat()
方法中设置 self.text
而不是 super.text
。为什么我必须将其更改为设置 super
?为什么 self
不起作用?
This looks as an interview question ;-)
问题基本上是您覆盖了 var text
存储的变量并添加了一个观察者(通过 didSet
)基本上每次都重写相同的值在无限循环中(如果您在该行中调用 self
而不是 super
)
它适用于 super
,因为您依赖 不 让观察者设置数据的实现。
一个快速的解决方案是删除该观察者,无论哪种方式,您都在调用 setTextFromFloat(amount: Float)
来完成工作。
A quick snippet solving the problem follows:
class ExtendedLabel : UILabel {
func setTextFromFloat(amount: Float) {
self.text = formatCurrency(fromFloat: amount)
}
internal func formatCurrency(fromFloat amount: Float) -> String {
let currencyFormatter: NSNumberFormatter = NSNumberFormatter()
currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
currencyFormatter.locale = NSLocale.currentLocale()
let formattedFloat = currencyFormatter.stringFromNumber(NSNumber(float: amount))!
print(formattedFloat)
return formattedFloat
}
}
class ViewController: UIViewController {
@IBOutlet var lab : ExtendedLabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
lab.setTextFromFloat(2.5)
}
}
无论哪种方式,您都可能希望将此视为可能的 扩展 ,并重构代码。