Difference between [String] and Array <String>

What is the best way to declare an Array and a Dictionary , I used both:

Array<String>

[String]

For me, [String] very fast in terms of coding, but actually, how do they differ from each other in terms of compiler and performance, and which one should we stick with?

+5
source share
3 answers

From the iOS developer library in Swift ...

The Swift array type is written in full because Array <Element>, where Element is the type of values โ€‹โ€‹that the array can be stored. You can also write the type of the array in abbreviated form as [Element]. Although the two forms are functionally identical , an abbreviated form is preferred and is used in this guide when it comes to array type.

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html

+12
source

Both are equivalent.

From Apple

Shorthand Array Type Syntax

The Swift array type is completely written as Array, where Element is the type of value that the array can be stored. You can also write the type of the array in abbreviated form as [Element]. Although the two forms are functionally identical, an abbreviated form is preferred and is used in this manual when referring to an array type.

+5
source

I would notice that one difference is that if you are trying to create an instance of an array of objects where you need to specify a module (due to name conflicts), then the shortened form seems to suffocate.

 let array1 = [MyModule.MyClass]() // Compile error: Invalid use of '()' to call a value of non-function type '[MyClass.Type]' let array2 = Array<MyModule.MyClass>() // Works as expected. 

Other situations, such as optional deployments or working with parameters, use shorthand notation. I only tried in Swift 2.3

0
source

Source: https://habr.com/ru/post/1237979/


All Articles