// ARRAY
// 1.
var arr1 : [Int] = [10,20,30]
var val1 = arr1[0]
print("First value : ", val1)
print("Second value : ", arr1[1])
print("Thirdvalue : ", arr1[2])
// 2. Iterator
for item in arr1{
print(item)
}
// 3. append
print("*********APPEND************")
var arr2 = [Int]() ;
arr2.append(5)
arr2.append(55)
arr2.append(555)
for item in arr2{
print(item)
}
// 4. assigning new value to old
print("***********assigning value on above record**************")
arr2[1] = 999
for item in arr2{
print(item)
}
// 5. adding two array
print("**********ADDING TWO ARRAY************")
var arr3 : [Int] = arr1 + arr2
for item in arr3{
print(item)
}
//6. count
print("*************COUNT PROPERTY*******************")
print("Array size (arr3.count) : \(arr3.count)")
//7. isEmpty
print("*************isEmpty Property***************")
var arr4 = [Int]()
print("arr3 isEmpty : \(arr3.isEmpty)")
print("arr4 isEmpty : \(arr4.isEmpty)")
****************************************************
8. Pass one function array to secind using global array.
CODE </ >
var marksGlobalArray : Array<Int> = []func first(){
let marksArray: [Int] = [10, 20, 30, 40, 50]
for mark in marksArray{
marksGlobalArray.append(mark)
}
}
func second(){
print(first())
for a1 in marksGlobalArray{
print(a1)
}
}
// CALL
second()
***********************************************************
9. Sort
var array : [String] = ["Test", "John", "Mike", "Divakar", "Anuj", "Somya"]
or var array : [Int] = [23, 98, 56, 72, 49]
array.sort(by: <) // will sort the array in ascending order array.sort(by: >) // will sort the array in descending order // you can also use the sorted method to create a sorted copy let sortedArray = array.sorted(by: <)
10. Filter
A.
let arr = ["Ram", "Mohan", "Shyam", "John"]
let filtered = arr.filter { $0.contains("am") }
print(filtered)
Output : ["Ram", "Shyam"]
//
B.
let terms = ["Hello","Bye","Halo"]
var filterdTerms = [String]()
func filterContentForSearchText(searchText: String) {
filterdTerms = terms.filter { term in
return term.lowercased().contains(searchText.lowercased())
}
}
filterContentForSearchText(searchText: "Lo")
print(filterdTerms)
Output : ["Hello", "Halo"]
No comments:
Post a Comment