[英]Why people add .0 on CGFloat?
https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementingACustomControl.html#//apple_ref/doc/uid/TP40015214-CH19-SW1 In this tutorial, https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementingACustomControl.html#//apple_ref/doc/uid/TP40015214-CH19-SW1在本教程中,
button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true
constant is typed "44.0" not "44".常量键入“44.0”而不是“44”。
Is there any difference between them?它们之间有什么区别吗?
I measured time of the methods.我测量了方法的时间。
func evaluateProblem(problemNumber: Int, problemBlock: () -> Void)
{
print("Evaluating problem \(problemNumber)")
let start = DispatchTime.now() // <<<<<<<<<< Start time
let end = DispatchTime.now() // <<<<<<<<<< end time
let nanoTime = end.uptimeNanoseconds - start.uptimeNanoseconds // <<<<< Difference in nano seconds (UInt64)
print("Time to evaluate problem \(problemNumber): \(nanoTime)")
}
evaluateProblem(problemNumber: 2) {
let b: CGFloat = 44
print(b)
}
evaluateProblem(problemNumber: 1) {
let a: CGFloat = 44.0
print(a)
}
But the faster one is not fixed.但更快的不是固定的。
You can initialize Double
, Float
, CGFloat
, Int
, etc. with integer literals because all of the above conform to the ExpressibleByIntegerLiteral protocol.您可以使用 integer 文字初始化Double
、 Float
、 CGFloat
、 Int
等,因为上述所有内容都符合ExpressibleByIntegerLiteral协议。 Behind the scenes initialization with a literal simply calls the init(integerLiteral:)
method of the conforming type.在幕后使用字面量初始化只需调用符合类型的init(integerLiteral:)
方法。
Likewise, there is a ExpressibleByFloatLiteral protocol that handles initialization with floating point literals, and that protocol has an initializer that must also be implemented by conforming types.同样,有一个ExpressibleByFloatLiteral协议处理浮点字面量的初始化,并且该协议有一个初始化器,它也必须由符合类型实现。
As far as which to use, it's a matter of personal preference and style.至于使用哪个,这是个人喜好和风格的问题。 Both ways of initialization are valid and unless you're doing thousands of initializations the performance difference would be negligible.两种初始化方式都是有效的,除非您进行数千次初始化,否则性能差异可以忽略不计。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.