Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

ios - Same datatype multiple variable declaration in swift

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 = ""

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

question from:https://stackoverflow.com/questions/32390934/same-datatype-multiple-variable-declaration-in-swift

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Type Annotations:

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.

Documentation HERE.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...