There is no type initializer in Swift----One answer is to use static, it is the same as class final.
“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
相关文章
- [systemd]How To Use Systemctl to Manage Systemd Services and Units
- Attempt to fetch logical page (...) in database 2 failed. It belongs to allocation unit xxxx not to xxx
- [AWS] Lab - Create a SNS and subscribe it
- postman测试重定向接口,We're sorry but system-cn doesn't work properly without JavaScript enabled. Please enable it to continue
- 有一些专业的it提供商,为什么企业还是不用他们?比如尘封做crm的,但是不能和我们的业务不能紧密结合,
- [Rust] Pass a JavaScript Function to WebAssembly and Invoke it from Rust
- 【22】报错ERROR:./depthai_sdk is not a valid editable requirement. It should either be a path to a local
- 【学亮IT手记】PL/SQL编程-存储过程
- transfer = C only read dynamically - why it fails to work
- 【Codeforces 231C】To Add or Not to Add
- Can‘t bind to formControl since it isn‘t a known property of input错误消息的处理
- 在vue里运行报错:Failed to resolve loader: sass-loader You may need to install it.提示需要安装sass模块
- 已解决Could not find file with URI “/opt/data-integrationugins“ because it is a relative path
- Unable to update the EntitySet 'T_JsAPI' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
- adb devices报错:* daemon not running. starting it now on port 5037(端口未被占用)解决(十九)
- 阿里程序员回老家被哥们吐槽,破IT就破IT,还阿里巴巴
- IT周盘点:快手申请“666”商标;数字人民币开通申请