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

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

时间: 2024-10-31 16:46:36

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

debian The type initializer for 'System.Drawing.KnownColors' threw an exception

Change the "System.Drawing" reference of "CoreCompat.System.Drawing"if you throw The type initializer for 'System.Drawing.KnownColors' threw an exception to do:apt-get install libgdiplus debian The type initializer for 'System.Drawing.

.net 调用 Matlab生成dll出现的问题(The type initializer for 'MathWorks?.MATLAB.NE?T.Utility.?MWMCR' threw an exception.)

https://cn.mathworks.com/matlabcentral/answers/278399-i-get-an-error-saying-the-type-initializer-for-mathworks-matlab-net-utility-mwmcr-threw-an-except 这篇文章帮忙解决了问题 记录一下备忘: Hi Nug, Ensure that you have the MCR version corresponding to MATLAB 2012b ins

The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.

当使用EF时,配置文件中会多出下面的配置代码: <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.C

System.Security.SecurityException The type initializer for &#39;System.Data.Entity.Internal.AppConfig&#39; threw an exception

[15/08/19 00:03:10] [DataManager-7292-ERROR] System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for 'System.Data.Entity.Internal.AppConf

使用Spire组件抛出异常The type initializer for &#39;spr857&#39; threw an exception

使用Spire组件抛出异常The type initializer for 'spr857' threw an exception 我使用免费的Spire.Xls组件尝试去转换Excel文档到PDF文档,在执行下面第二行代码,也就是做加载文档这步时,抛出了异常"The type initializer for 'spr857' threw an exception" Workbook workbook = new Workbook(); workbook.LoadFromFile(&q

C#中异常:“The type initializer to throw an exception(类型初始值设定项引发异常)”的简单分析与解决方法

对于C#中异常:“The type initializer to throw an exception(类型初始值设定项引发异常)”的简单分析,目前本人分析两种情况,如下: 情况一: 借鉴麒麟.NET的类型初始值设定项引发异常文章!!!写的很详细,大家可以看一看! 其实麒麟.NET的这篇文章主要讲解分析了静态成员的方面,总的说就是:类型初始化或访问类型的静态成员时,都会对类中的其他静态成员进行初始化,如果有静态构造函数的话,一并执行静态构造函数.在这过程中所引发的异常我就直接借鉴麒麟.NET的例

随手记Swift基础和Optional Type(问号?和感叹号!)

距离Apple推出Swift已经有几天了,网上也时不时出现"急招Swift程序员,要求有一天工作经验"的帖子. 看到Swift,除了苹果放的另外一门语言的链接(http://swift-lang.org/),还可能联想到Taylor Swift,或者铃木雨燕. 这几天网上关于Swift的话题和材料应该迅速膨胀,比如StackOverflow的相关标签,还有知乎上的一些相关讨论. 关于Swift的推出,我个人觉得对现有的使用Objective-C的iOS开发者不算是很大的冲击,毕竟写代码

swift中文文档- 类型转换

未翻译完 待续(英语烂,求斧正) Type Casting 类型转换 Type casting is a way to check the type of an instance, and/or to treat that instance as if it is a different superclass or subclass from somewhere else in its own class hierarchy. 类型转换是检测实例所属类型的一种方法,和/或 去对待实例好像它是一个

&amp;quot;Swift&amp;quot;编程语言

来自英文文档.百度翻译以及自己没过4级的渣渣英语功底,为了自己以后看起来方便 About Swift 关于"海燕" IMPORTANT 重要 This is a preliminary document for an API or technology in development. Apple is supplying this information to help you plan for the adoption of the technologies and programm