Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about Swift Array Declarations

Tags:

arrays

swift

Is there any difference between the following?

  1. var array1_OfStrings = [String]()

  2. var array2_OfStrings: [String] = []

  3. var array3_OfStrings: [String]

Testing in Playground shows that 1 and 2 are the same but 3 behaves differently. Can someone explain me the difference please? And also what will be the preferred way to declare an empty array of String?

like image 609
Vakas Avatar asked Nov 12 '15 10:11

Vakas


People also ask

What is an array in Swift?

An array can hold multiple elements of a given type. We can use them to store numbers, strings, classes, but in general elements can be anything. With the Any type you can actually express this and you can put anything into this random access collection. There are quite many ways to create an array in Swift.

What is the use of declaration in Swift?

You can also use a declaration to extend the behavior of an existing named type and to import symbols into your program that are declared elsewhere. In Swift, most declarations are also definitions in the sense that they’re implemented or initialized at the same time they’re declared.

What is the difference between top-level declarations and statements in Swift?

Top-level declarations consist of only declarations, and are allowed in all Swift source files. Executable top-level code contains statements and expressions, not just declarations, and is allowed only as the top-level entry point for the program.

Why can’t I access the original value in Swift?

Accessing the original is a simultaneous access of the value, which violates Swift’s memory exclusivity guarantee. For the same reason, you can’t pass the same value to multiple in-out parameters.


2 Answers

First two have the same effect.

  1. declare a variable array1_OfStrings, let it choose the type itself. When it sees [String](), it smartly knows that's type array of string.

  2. You set the variable array2_OfStrings as type array of string, then you say it's empty by []

  3. This is different because you just tell you want array3_OfStrings to be type array of string, but not given it an initial value.

I think the first one is recommended as The Swift Programming Language uses it more often.

like image 76
zc246 Avatar answered Oct 14 '22 14:10

zc246


While I might be late to the party, there is one thing that needs to be said.

First option set array1_OfStrings to array of Strings

The other option tells that array1_OfStrings is array of Strings and then set it empty.

While this might be a really small difference, you will notice it while compiling. For the first option compiler will automatically try to find out what is the type of array1_OfStrings. Second option won't do that, you will let compiler know that this actually is array of Strings and done deal.

Why is this important? Take a look at the following link: https://thatthinginswift.com/debug-long-compile-times-swift/

As you can see, if you don't declare type of your variable that might impact build performance A LOT.

like image 34
Miknash Avatar answered Oct 14 '22 15:10

Miknash