快速启动栏_快速启动

快速启动栏

Init() is a method used to initialize values in swift. Swift will not allow you to compile a class without initializing the non-optional values of a class.

Init()是一种用于快速初始化值的方法。 Swift不允许您在不初始化类的非可选值的情况下编译类。

Image for post

To avoid this error you could initialize above variables with some default value when we create them, like below.

为了避免发生此错误,您可以在创建变量时使用一些默认值初始化上述变量,如下所示。

var myStringValue : String = “Hello”
var myIntValues : Int = 0

But what if you don’t want to assign values to the variables while creating them, you could always make them optional. But creating unwanted optionals are not the right approach, that's where we use Init() method. It can be used to initialize the values of class when creating the object of the class.

但是,如果您不想在创建变量时将值分配给变量,可以始终使它们可选。 但是创建不必要的可选方法不是正确的方法,这就是我们使用Init()方法的地方。 创建类的对象时,可用于初始化类的值。

class initExample{var myStringValue : Stringvar myIntValue : Intinit(myString:String, myInt:Int) {self.myStringValue = myStringself.myIntValue = myInt}}let initObject = initExample(myString: “Hello”, myInt: 10)

This is a basic feature of swift, that is used by developers all the time. Hope I helped.

这是swift的基本功能,开发人员一直在使用它。 希望我能帮上忙。

翻译自: https://medium.com/swlh/init-in-swift-b6c8012ee029

快速启动栏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值