There is no type initializer in Swift----One answer is to use static, it is the same as class final.

There is no type initializer in Swift----One answer is to use static, it is the same as class final.,第1张

概述“Unlike stored instance properties, you must always give stored type properties a default value. This is because the type itself does not have an initializer that can assign a value to a stored type p

“Unlike stored instance propertIEs,you must always give stored type propertIEs a default value. This is because the type itself does not have an initializer that can assign a value to a stored type property at initialization time.”

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. 

You Could use a type property which default value is a closure. So the code in the closure would be executed when the type property (or class variable) is set.

 

But class stored propertIEs not yet supported (tested in Xcode 8).

One answer is to use static,it is the same as class final.

Good link for that is

Setting a Default Property Value with a Closure or Function

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. 

Code example:
 

Prints

start

setting default property value with a closure

So it is lazy evaluated.

 

https://stackoverflow.com/questions/24137212/initialize-class-method-for-classes-in-swift

总结

以上是内存溢出为你收集整理的There is no type initializer in Swift----One answer is to use static, it is the same as class final.全部内容,希望文章能够帮你解决There is no type initializer in Swift----One answer is to use static, it is the same as class final.所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/999925.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-21
下一篇2022-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存