TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to SWIFT

Swift Combine Arrays: append, contentsOf

Combine arrays with the plus operator and the append contentsOf method.
Combine arrays. Programming languages have many syntaxes for combining 2 arrays. In Swift we use the plus operator. We can also append many elements (in an array) at once.Array
And with append contentsOf, we can add arrays to arrays with a more verbose syntax. This can make some programs clearer and may be preferred.
First example. Here we merge 2 arrays with an addition expression. We can use array variable names (like "numbers") or place the array expression directly in the addition statement.

Combined: The combined array has 6 elements. This is equal to the length of the two arrays "numbers" and "numbers2."

Swift program that combines two arrays let numbers = [10, 20, 30] let numbers2 = [1, 2, 3] // Combine the two arrays into a third array. // ... The first 2 arrays are not changed. let combined = numbers + numbers2 print(combined) Output [10, 20, 30, 1, 2, 3]
Add many elements. Sometimes we want to append two or more elements to an array at once. We can use an addition expression for this purpose.
Swift program that appends multiple elements // This must be a var. var colors = ["red"] // Add two elements to the array. // ... The arrays are combined. colors += ["orange", "blue"] // Add more elements. colors += ["silver", "gold"] print(colors) Output ["red", "orange", "blue", "silver", "gold"]
Append, contentsOf. With this method, we add the elements of one array to another. This has the same effect as the plus operator expressions. But it uses a normal method call.
Swift program that uses append, contentsOf var animals = ["cat"] var animals2 = ["bird", "fish", "dog"] // Add the second array to the first. // ... This is like using the "add" operator. animals.append(contentsOf: animals2) print(animals) Output ["cat", "bird", "fish", "dog"]
A summary. In Swift we can use a plus to combine two arrays. But the append() method is also worth using as it may be clearer to understand in some programs.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf