在 Swift 中使用 var 来解开一个可选项? [英] Using var to unwrap an optional in Swift?

查看:39
本文介绍了在 Swift 中使用 var 来解开一个可选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只见过有人使用 let 来解开可选项.
我想知道这是否是一种好的做法,我也可以使用 var,或者还有其他用途吗?

I've only seen people using let to unwrap optionals.
I was wondering if it is a good practice and I could just as well use var, or is there something more to it?

使用 letvar 的优缺点是什么?

What are the pros and cons of using let versus var?

推荐答案

如前所述,你应该尽可能使用 let,因为它允许编译器做一些花哨的事情,从而使你的程序跑得更快.

As mentioned before, you should always use let when you can, as it allows compiler do some fancy stuff and thus makes your program run faster.

不过,技术上使用 var 没有任何问题.

There is technically nothing wrong with using var, though.

如需进一步阅读,请查看这篇文章:
http://www.touch-code-magazine.com/swift-optionals-use-let/

For further reading, please check out this article:
http://www.touch-code-magazine.com/swift-optionals-use-let/

这篇关于在 Swift 中使用 var 来解开一个可选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆