Yogesh Chauhan's Blog

How to create an empty array in Swift?

in Swift on March 7, 2020

We can create an empty array by specifying the Element type of your array in the declaration.

For example:

var numbers: [Double] = []

var names: [String] = []

// The full type name is also allowed
var emptyFloats: Array<Float> = Array()

Add value to the array:


Also, we can just append a variable. For example,

let myName = "Yogesh"

Add multiple elements at the same time by passing another array or a sequence of any kind to the append(contentsOf:) method.

names.append(contentsOf: ["Shakia", "William"])

You can add new elements in the middle of an array by using the insert(_:at:) method for single elements and by using insert(contentsOf:at:) to insert multiple elements from another collection or array literal. The elements at that index and later indices are shifted back to make room.

names.insert("Liam", at: 3)

To remove elements from an array, use the remove(at:), removeSubrange(_:), and removeLast() methods.

names.remove(at: 0)


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to remove special characters (dash, asterisk etc) from any string in PHP?PHPVariables scope and shadowing in SCSS (Sass)SCSSShort-Circuit Evaluation in JavaScriptJavaScriptHow to convert datetime to date format in JavaScript?JavaScriptDynamically generate names in SCSS (animation example)SCSSIs there a way to do a FULL OUTER JOIN in MySQL?SQL/MySQLLearn to make a responsive gallery using CSS Grid and without media queriesCSSHow to CREATE TABLE in SQL with and without using Another Table?SQL/MySQLHow to add Lifecycle Methods to a Class in React?ReactHow to create and store JSON objects in localStorage using JavaScript?JavaScriptWordPress 5.7 is migrating to latest jQuery version and people are freaking outjQueryWhat are Conditional Tags in WordPress?WordPress