gpt4 book ai didi

ios - swift 中的相同数据类型多变量声明

转载 作者:IT王子 更新时间:2023-10-29 05:04:26 25 4
gpt4 key购买 nike

在 objective-c 中我们可以像这样声明变量-NSString *a,*b,*c;

在 swift 中有一种方法可以声明相同数据类型的多变量变量而不是像下面那样做

var a: NSString = ""
var b: NSString = ""
var c: NSString = ""

那么,是否可以将所有 a、b、c 变量声明为一行,例如var (a,b,c): a:NSstring=("","","") 还是什么?

最佳答案

You can declare multiple constants or multiple variables on a single line, separated by commas:

var a = "", b = "", c = ""

NOTE

If a stored value in your code is not going to change, always declare it as a constant with the let keyword. Use variables only for storing values that need to be able to change.

类型注解:

You can define multiple related variables of the same type on a single line, separated by commas, with a single type annotation after the final variable name:

var red, green, blue: Double

NOTE

It is rare that you need to write type annotations in practice. If you provide an initial value for a constant or variable at the point that it is defined, Swift can almost always infer the type to be used for that constant or variable, as described in Type Safety and Type Inference.

文档 HERE .

关于ios - swift 中的相同数据类型多变量声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32390934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com