Array in Swift.
Swift providing three type of Collection known as Array , Set and Dictionary (Dictionary in Swift) to storing the value. Array store the value in the order. Set store the value unordered but unique. and Dictionary store the value in unordered with key-value.
Array storing the value of same datatype in order list. And all value are store in indexing. Same value can also store different index. one value at one unique index.
This example provide to definition and its useful function of Array.
Create an empty Array :
1. Append item + operator :
Append Array to Array : Appending with + operator.
Check Array contain item :
Join Array element with separator - :
Get element from Array for-in loop :
Reverse an Array :
Change and Set element of Array index :
Sort Array in Ascending Order:
Sort Array in Descending Order:
Swap two element in Array :
Remove item Array by Value:
Flatten function in Array: Array inside Array flatten in single Array.
flatMap function in Array: Array inside Array flatten in single Array.
Here I'm try to explain of Swift Array and Its function , Please comment for more useful function which is missing in this tutorials.
Another Collection type of Swift Dictionary, See the tutorial of Swift Dictionary. Dictionary in Swift.
Thanks.
Array storing the value of same datatype in order list. And all value are store in indexing. Same value can also store different index. one value at one unique index.
This example provide to definition and its useful function of Array.
Create an empty Array :
var myIntArray = [Int]() // Integer Empty Array
var myStringArray = [String]() // String Empty Array
Creating Array with default value :var myFruitList: [String] = ["Banana", "Apple" , "Orange"] //Default String in Array
Number of item in Array : return 3print(myFruitList.count) // Number of item
Check Array is empty :if myFruitList.isEmpty {
print("The shopping list is empty.")
} else {
print("The shopping list is not empty.")
}
Append item to array : Two way to appending item to array1. Append item + operator :
myFruitList += ["PineApple"]
2. Append item using append function :myFruitList.append("Graphs")
var myFruitArray = ["Banana", "Apple", "Orange", "PineApple", "Graphs"] // Fruit
var myVegList = ["Patato", "Onion"] // Vegetable
myFruitList += myFruitArray // Append
Check Array contain item :
// Initialize the Array
var myFruitAllArray = ["Banana", "Apple", "Orange", "PineApple", "Graphs"]
// Check if it contains the Banana.
if myFruitAllArray.contains("Banana") {
print("Yes, it does contain Banana")
}
else {
print("No, it doesn't")
}
Concatenate Array :// Initialize the Array
var Alpha = ["A" , "B" , "C"]
let newAplha = Alpha + ["D", "E","F"]
print(newAplha)
Filter an Array using filter function:// Initialize the Array
var a = ["Kirit" , "Kirti" , "Kotak" , "Modi"]
// Get only array which starting Character is K.
a = a.filter { $0.characters.first == "K"}
print(a)
Finding the index of item : // Initialize the Array
let fruits = ["Apple", "Mango", "Graphs"]
// Finding Index
var index = fruits.indexOf("Mango")
print(index!)
Finding item At index :// Initialize the Array
let names = ["kirit", "Modi", "Hitesh" , "Ghanchi"]
// Finding item at Index
print(names[2])
First item of Array :// Initialize the Array
let namesFirst = ["kirit", "Modi", "Hitesh" , "Ghanchi"]
// First item
print(namesFirst.first)
Last item of Array :// Initialize the Array
let namesLast = ["kirit", "Modi", "Hitesh" , "Ghanchi"]
// Last item
print(namesLast.last)
Insert item At array Index :// Initialize the Array
var myNickNames = ["kirit", "Modi", "Hitesh" , "Ghanchi"]
// Insert New Nick Name Kit At index 1.
myNickNames.insert("Kit", atIndex: 1)
print(myNickNames)
Join Array element with separator - :
// Initialize the Array
var myJoinArray = ["A", "B", "C" ,"D" , "E" , "F" , "G"]
// Join array elements using '-' as separator
var str = myJoinArray.joinWithSeparator("-")
print(str)
Get element from Array for-in loop :
// Initialize the Array
var elementsArray = ["Apple","Mango","Banana"]
// Iterate through the array
for element in elementsArray {
print(element)
}
Get element and Index from Array for-in loop :// Initialize the Array
var elementsIndexArray = ["Apple","Mango","Banana"]
// Iterate through the array
// keeping track of the elements' index
for (index,element) in elementsIndexArray.enumerate() {
print("\(index) = \(element)")
}
Map an Array of element : // Initialize the Array
var myArrayName = ["Kirit","Hitesh"]
// Map values to their -Modi etc
var b = myArrayName.map { $0 + " Modi" }
print(b)
Reverse an Array :
// Initialize the Array
var ReverseArray = ["Kirit","Modi"]
// Reverse all elements
ReverseArray = ReverseArray.reverse()
print(ReverseArray)
Change and Set element of Array index :
// Initialize the Array
var setArrayElement = ["Apple","Mango","Banana"]
// Set Array element At index 1.
setArrayElement[1] = "Graphs"
print(setArrayElement)
Sort Array in Ascending Order:
// Initialize the Array
var asendingArray = ["Apple","Mango","Banana" ,"PineApple"]
// Sort ascending elements.
asendingArray = asendingArray.sort({$0 < $1})
print(asendingArray)
Sort Array in Descending Order:
// Initialize the Array
var decendingArray = ["Apple","Mango","Banana" ,"PineApple"]
// Sort decending element.
decendingArray = decendingArray.sort({$0 > $1})
print(decendingArray)
// Initialize the Array
var arrNames = ["Hitesh" , "Kirit" , "Chetan" , "Harpal"]
// Swap elements at index: 1 and 2
swap(&arrNames[1], &arrNames[2])
print(arrNames)
Remove item Array by Value:
// Initialize the Array
var removeArrayValue = ["Apple", "Banana", "Graphs"]
// Remove item with value 'Apple'
removeArrayValue = removeArrayValue.filter { $0 != "Apple" }
print(removeArrayValue)
Remove item At Array index:// Initialize the Array
var removeIndexE = ["Apple", "Banana", "Graphs"]
// Remove item "Banana "at index 1.
removeIndexE.removeAtIndex(1)
print(removeIndexE)
Remove duplicate item from Array:// Initialize the Array
var removeDuplicate = ["Apple", "Banana", "Graphs" , "Apple" , "Banana" , "Orange", "PineApple", "Banana"]
// first convert to a set then Array
removeDuplicate = Array(Set(removeDuplicate))
print(removeDuplicate)
Remove All item from Array:// Initialize the Array
var removeArray = ["Chetan" , "Kirit"]
// Remove all elements
removeArray.removeAll()
print(removeArray)
Flatten function in Array: Array inside Array flatten in single Array.
// Initialize the Array
var indideArray = [["Chetan" , "Kirit"] , ["hitesh" , "Ganesh"] , ["Paresh" , "Ronak"]]
// Flatten function of Array
let newArray = Array(indideArray.flatten())
print(newArray)
flatMap function in Array: Array inside Array flatten in single Array.
// Initialize the Array
var arrayInsideName = [["Chetan" , "Kirit"] , ["hitesh" , "Ganesh"] , ["Paresh" , "Ronak"]]
// flatMap function in Array
let newSepArray = arrayInsideName.flatMap({$0})
print(newSepArray)
Here I'm try to explain of Swift Array and Its function , Please comment for more useful function which is missing in this tutorials.
Another Collection type of Swift Dictionary, See the tutorial of Swift Dictionary. Dictionary in Swift.
Thanks.
Array in Swift.
Reviewed by KIRIT MODI
on
04:47:00
Rating:
No comments: