Swift: Creating an Array Literal

Creating an Array Literal in Swift:

There is another way to make an array than the one we learned in Swift. A list of values separated by commas and surrounded by a pair of square brackets can also be used to declare and initialize an array. The following is the syntax for creating an array literal in this manner:

var name = [list of values];

Let’s say we want to make an array of the O/A level marks of a student.

var marks = [235 , 567.5];

The type of the array is inferred thanks to Swift’s built-in type inference. Still to be specific we can declare data type as follows too:

var marks: [Double] = [235 , 567.5];

Helpful Tools:

Here are some useful tools to help you along your journey!

Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.

Scroll to Top
%d bloggers like this: