“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:@H_404_37@
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。