本文介绍了快速的相同数据类型多变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

in objective-c we can declare variable like-NSString *a,*b,*c;

快速找到一种声明相同数据类型的多变量变量的方法,而不是像下面这样

in swift there a way to declare same datatype multiple variable variable rather than doing like below

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

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

So, is it possible to declare all a,b,c variable into one line likevar (a,b,c): a:NSstring=("","","") or something?

推荐答案

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

注意

如果代码中的存储值不会更改,请始终声明 使用let关键字将其作为常量.仅将变量用于存储 需要更改的值.

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.

类型注释:

var red, green, blue: Double

注意

在实践中很少需要编写类型注释.如果你 在以下点为常数或变量提供初始值: 根据定义,Swift几乎总是可以推断出要用于的类型 该常数或变量,如类型安全性和类型"中所述 推论.

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.

文档此处.

这篇关于快速的相同数据类型多变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 18:44
查看更多