Collections and tuples Types of collections: Arrays, dictionaries, and sets. All items stored in collections have to be of the same type. Collections can be constant or variable. (Use let or var) Arrays Each item in an array is an element. We can initialise arrays like this: let firstArray = [1, 2, 3] We can also declare them like this (these arrays are empty at first): var secondArray = [Int]() var thirdArray: [Int] = [] We can create arrays to store String, Double, SelfCreatedObjects We can create arrays to store more than one type: var fourthArray: [Any] = [1, "Three"] We can also create arrays that can store objects of any class type: var fifthArray:[AnyObject] = [] We can initialise an array to have 5 elements, each having the number 1: var sixthArray = [Int](repeating: 1, count: 5) We can create 2 dimensional arrays: var twoD1 =[[1,2], [3,4],[5,6],[7,8]] var twoD2 = [[Int]]() To access the elements of an array: It starts with 0: print(firstArray[0]) //will print 1 To ac...
Comments
Post a Comment