var vs让Swift [复制](var vs let in Swift [duplicate])

编程入门 行业动态 更新时间:2024-10-08 04:28:37
var vs让Swift [复制](var vs let in Swift [duplicate])

这个问题在这里已经有了答案:

swift中`let`和`var`有什么区别? 26个答案

我是Swift编程的新手,我遇到了var和let类型。 我知道let是一个常量,我知道这意味着什么,但是我从来没有使用常量,主要是因为我不需要。 那么为什么我应该使用var而不是let ,我应该在什么情况下使用它?

This question already has an answer here:

What is the difference between `let` and `var` in swift? 29 answers

I'm new to Swift programming, and I've met the var and let types. I know that let is a constant and I know what that means, but I never used a constant mainly because I didn't need to. So why should I use var instead of let, at what situation should I use it?

最满意答案

swift中的正确术语不是固定不变的 ,而是可变的

当你知道一旦你赋值给一个变量,它就不会改变 - 即它是不可变的。 如果你声明表视图单元格的id,它很有可能在它的生命周期中不会改变,所以通过声明它是不可变的,就不会有错误地改变它的风险 - 编译器会通知你。

典型用例:

常量(定时器的超时值,或固定大小的标签的宽度,最大登录尝试次数等)。 在这种情况下,常量是代码中字面值的替代(想想#define ) 用作另一个函数的输入的函数的返回值 表达式的中间结果,用作另一个表达式的输入 一个容器,用于在可选绑定中显示值 由REST API调用返回的数据,从JSON反序列化为结构,该结构必须存储在数据库中

还有更多。 每次我写var ,我都会问自己: 这个变量可以改变吗? 。 如果答案是否定的,我用let替换var 。 有时我也会采用一种更具保护性的方法:我将所有内容都声明为不可变的,然后当我尝试修改其中一个时,编译器会通知我,对于每种情况,我都可以相应地进行处理。

一些考虑

对于引用类型(类),不可变意味着一旦将实例分配给不可变变量,就不能将另一个实例分配给同一个变量。

对于值类型(数字,字符串,数组,字典,结构体,枚举),不可变意味着一旦分配了一个值,就不能更改其值。 对于简单的数据类型( Int , Float , String ),这意味着你不能指定另一个相同类型的值。 对于复合数据类型(结构体,数组,字典),这意味着您不能分配新值(例如结构的新实例), 并且不能更改其存储的任何属性。

对于开发者和阅读代码的人来说,不变变量也具有语义含义 - 它清楚地表明该变量不会改变。

最后,但从纯粹的开发角度来看可能不太重要,不可变对象可以通过编译器进行优化。

Rather than constant and variable, the correct terminology in swift is immutable and mutable.

You use let when you know that once you assign a value to a variable, it doesn't change - i.e. it is immutable. If you declare the id of a table view cell, most likely it won't change during its lifetime, so by declaring it as immutable there's no risk that you can mistakenly change it - the compiler will inform you about that.

Typical use cases:

A constant (the timeout of a timer, or the width of a fixed sized label, the max number of login attempts, etc.). In this scenario the constant is a replacement for the literal value spread over the code (think of #define) the return value of a function used as input for another function the intermediate result of an expression, to be used as input for another expression a container for an unwrapped value in optional binding the data returned by a REST API call, deserialized from JSON into a struct, which must be stored in a database

and a lot more. Every time I write var, I ask myself: can this variable change?. If the answer is no, I replace var with let. Sometimes I also use a more protective approach: I declare everything as immutable, then the compiler will let me know when I try to modify one of them, and for each case I can proceed accordingly.

Some considerations:

For reference types (classes), immutable means that once you assign an instance to the immutable variable, you cannot assign another instance to the same variable.

For value types (numbers, strings, arrays, dictionaries, structs, enums) immutable means that that once you assign a value, you cannot change the value itself. For simple data types (Int, Float, String) it means you cannot assign another value of the same type. For composite data types (structs, arrays, dictionaries) it means you cannot assign a new value (such as a new instance of a struct) and you cannot change any of their stored properties.

Also an immutable variable has a semantic meaning for the developer and whoever reading the code - it clearly states that the variable won't change.

Last, but maybe less important from a pure development point of view, immutables can be subject to optimizations by the compiler.

更多推荐

本文发布于:2023-08-07 11:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464142.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:var   Swift   duplicate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!