码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-07-22 15:06:01      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:static   strong   ini   ram   closure   book   exec   you   code   

“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.

标签:static   strong   ini   ram   closure   book   exec   you   code   

原文地址:https://www.cnblogs.com/feng9exe/p/11225790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!