Ios NumberFormatter只能在闭包中写入
在《大书呆子牧场指南》之后,我在其中一章中遇到了一段话,要求您创建一个Ios NumberFormatter只能在闭包中写入,ios,swift,closures,number-formatting,Ios,Swift,Closures,Number Formatting,在《大书呆子牧场指南》之后,我在其中一章中遇到了一段话,要求您创建一个NumberFormatter的实例。一切正常,但我注意到格式化程序是使用闭包创建的,如下所示: class ConversionViewController: UIViewController { let numberFormatter: NumberFormatter = { let nf = NumberFormatter() nf.numberStyle = .decimal
NumberFormatter
的实例。一切正常,但我注意到格式化程序是使用闭包创建的,如下所示:
class ConversionViewController: UIViewController {
let numberFormatter: NumberFormatter = {
let nf = NumberFormatter()
nf.numberStyle = .decimal
nf.minimumFractionDigits = 0
nf.maximumFractionDigits = 1
return nf
}()
func updateCelsiusLabel() {
if let celsiusValue = celsiusValue {
celsiusLabel.text = numberFormatter.string(from: NSNumber(value: celsiusValue.value))
} else {
celsiusLabel.text = "???"
}
}
}
出于好奇,我尝试在闭包之外创建此格式化程序,如下所示:
let nf = NumberFormatter()
nf.numberStyle = .decimal
nf.minimumFractionDigits = 0
nf.maximumFractionDigits = 1
但是有个错误是说
预期申报
我的问题是:
为什么在这种情况下不能在闭包之外创建NumberFormatters
案子
括号()
在结尾代表什么
关闭?我的猜测是,它是自我调用的,但为什么它需要呢李>
到目前为止,我从未见过以这种方式编写闭包。Apple文档中有什么可以解释这一点吗?NumberFormatter
以及闭包实例化在这里是一个麻烦:问题是您试图直接在类型声明的范围内更改实例属性(nf
)(尽管您未能向我们展示您的所有代码确实都包含在类型定义的范围内),但不在实例函数或初始值设定项的范围内
与之相比:
struct Foo {
var a = 1
a = 2 // Error: expected declaration
}
struct Foo {
let a: Int = {
var a = 1
a = 2
return a
}() // instantiate 'a' of Foo by _once-only
// invoking a specified closure_.
}
例如:
struct Foo {
var a = 1
mutating func mutateMe() {
a = 2 // OK
}
}
至于您的问题2):parantises()
用于执行闭包的一次性调用,其中闭包的返回用于实例化nf
。如果您没有调用它,那么nf
将是()->NumberFormatter
类型的闭包,而不是NumberFormatter
的实际实例。与之相比:
struct Foo {
var a = 1
a = 2 // Error: expected declaration
}
struct Foo {
let a: Int = {
var a = 1
a = 2
return a
}() // instantiate 'a' of Foo by _once-only
// invoking a specified closure_.
}
与同一概念进行比较,但不在类型声明/定义的范围内:
// this is a closure
let aClosure: () -> Int = { _ in return 42 }
// this is an invokation of a closure
// (discarding the result)
_ = aClosure()
// this is also an invokation of a closure
let num = { _ in return 42 }() // 'num' inferred to Int
NumberFormatter
以及闭包实例化在这里是一个麻烦事:问题是您试图直接在类型声明的范围内更改实例属性(nf
)(尽管您没有向我们展示您的所有代码确实都包含在类型定义的范围内),但不在实例函数或初始值设定项的范围内
与之相比:
struct Foo {
var a = 1
a = 2 // Error: expected declaration
}
struct Foo {
let a: Int = {
var a = 1
a = 2
return a
}() // instantiate 'a' of Foo by _once-only
// invoking a specified closure_.
}
例如:
struct Foo {
var a = 1
mutating func mutateMe() {
a = 2 // OK
}
}
至于您的问题2):parantises()
用于执行闭包的一次性调用,其中闭包的返回用于实例化nf
。如果您没有调用它,那么nf
将是()->NumberFormatter
类型的闭包,而不是NumberFormatter
的实际实例。与之相比:
struct Foo {
var a = 1
a = 2 // Error: expected declaration
}
struct Foo {
let a: Int = {
var a = 1
a = 2
return a
}() // instantiate 'a' of Foo by _once-only
// invoking a specified closure_.
}
与同一概念进行比较,但不在类型声明/定义的范围内:
// this is a closure
let aClosure: () -> Int = { _ in return 42 }
// this is an invokation of a closure
// (discarding the result)
_ = aClosure()
// this is also an invokation of a closure
let num = { _ in return 42 }() // 'num' inferred to Int
nf
在本例中是实例属性。它本身就是一个拥有自己属性的类。当你申报时
let nf = NumberFormatter()
nf为您提供,但具有默认属性。并且不能在声明中设置其属性。您将得到这个错误
nf
在本例中是实例属性。它本身就是一个拥有自己属性的类。当你申报时
let nf = NumberFormatter()
nf为您提供,但具有默认属性。并且不能在声明中设置其属性。您将得到这个错误
第一个答案:我在操场上测试了您的代码片段,它没有显示任何错误。我想你可能做错了与
NumberFormatter
无关的事情
let nf = NumberFormatter()
nf.numberStyle = .decimal
nf.minimumFractionDigits = 0
nf.maximumFractionDigits = 1
第二个答案:闭包的末端花括号告诉Swift立即执行闭包。如果省略这些括号,则试图将闭包本身指定给属性,而不是闭包的返回值 第一个答案:我在操场上测试了您的代码片段,它没有显示任何错误。我想你可能做错了与
NumberFormatter
无关的事情
let nf = NumberFormatter()
nf.numberStyle = .decimal
nf.minimumFractionDigits = 0
nf.maximumFractionDigits = 1
第二个答案:闭包的末端花括号告诉Swift立即执行闭包。如果省略这些括号,则试图将闭包本身指定给属性,而不是闭包的返回值 你能在创建格式化程序的地方分享更多代码吗?可能是@AbhishekJain I的重复更新了我的问题。让我知道这是否足够。在闭包之外创建NumberFormatter不会出错。共享出现错误的代码?我再次更新了我的问题,以表明我正在
UIViewController
实例中尝试此操作。您可以在创建格式化程序的地方共享更多代码吗?可能重复@AbhishekJain我更新了我的问题。让我知道这是否足够。在闭包之外创建NumberFormatter不会出错。共享出现错误的代码?我再次更新了我的问题,以表明我正在UIViewController
实例中尝试此操作。显然OP使用了一个类。关于问题1,班级和操场的行为完全不同。显然OP使用班级。关于问题#1,类和游乐场的行为是完全不同的。我想我现在唯一的问题是在这种情况下类型注释的概念。Swift中的变量是否足够聪明,可以确定闭包中的返回值将是它的值?我想目前唯一的问题是在这种情况下类型注释的概念。Swift中的变量是否足够智能,以确定闭包中的返回值将是其值?