Arrays in Swift Programming Language
Array In Swift
- Arrays are fundamental building blocks of apps.
In computer programming, arrays are numbered lists (or “collections”) of items with the same type. A variable contains one value, and an array can hold many values. Let’s get started with the syntax to create an array.
Swift arrays are applied for storing multiple values in ordered lists of any specific type. Swift set up strict checking mechanism which does not allow programmers to enter or insert any wrong type in an array even by mistake, i.e., arrays in swift programming are specific about the kinds of values they can store. The same value can appear in an array multiple times at different positions.
Shorthand syntax of arrays:
The swift array can also be written in this format as - Array<SomeType>, where 'SomeType' is the type that the array is going to store. Programmers can also write the type of an array in shorthand form as SomeType[]. Although the two mentioned forms are functionally identical, the shorthand form is preferred more than the other one.
Ex:
Starting value of array is 6
Second element of array is 6
Third element of array is 6
Fourth element of array is 6
Array’s have fixed types, just like any other variable in Swift. That means that every element in an array has the same type, that cannot be changed once set. All array elements must have the same type, too. In the above examples, every item has a fixed type: String, Int and Double.
In computer programming, arrays are numbered lists (or “collections”) of items with the same type. A variable contains one value, and an array can hold many values. Let’s get started with the syntax to create an array.
few examples of literals:-
In the above code, we’re creating 3 constants with 3 literal values: an integer, a double and a string. You’re looking at the literal values of these types; that’s why they’re called literals. You’re literally writing integers, doubles and strings in your code.
Array’s have fixed types, just like any other variable in Swift. That means that every element in an array has the same type, that cannot be changed once set. All array elements must have the same type, too.
In the above examples, every item has a fixed type: String, Int and Double. Therefore, the types of the months, scores and fraction arrays are [String], [Int] and [Double] respectively.
Getting and Changing Array Items:
Creating arrays, and their types
Appending items to arrays
Removing items from arrays
Inserting items into arrays
Every one of these items involves array indices, and it’s worth it to discuss that. Indices are the numeric “labels” that every item gets, starting at 0.
Array indices are consecutive, so you can’t insert an item at index 99 for an array that has only 10 items. Likewise, when you remove an item from an array, the items on the right of the removed item will shift one position to the left.
It’s easiest to imagine array items on a line, with the left side starting at 0. It then makes sense to think of “left” and “right”. Spending some time to get to know how array indices work will definitely help you learn iOS development better, and it’ll save you from some crazy bugs in the future.
// Output: Trillian
That names[2] part is called subscript syntax. You can access any array element this way, i.e. with array[index]. We can also use it to modify an array. Like this:
Looping Over an Array
Arrays and loops are like Simon and Garfunkel – they’re great on their own, but also go really well together. Before we start looping over arrays though, let’s check out some useful array properties:
array.count contains the number of elements in an array
array.first contains the first item of the array
array.last contains the last item of the array
array.isEmpty is true when the array is empty
You can use array.randomElement() to get a random element from the array (Swift 4.2+). Arrays have a few other useful functions, like first(where:) and contains(_:). You can also manipulate arrays with higher-order functions like map(_:) and filter(_:).
Let’s say you want to calculate the average score for an exam. Like this:
Here’s how that code works:
First, we’re creating an array scores of type [Int] with a bunch of exam scores between 1 and 10. We’re also initializing a variable sum and set it to zero.
Then, we loop over every item in the scores array. Inside the loop, the current collection item is assigned to constant score. The score is added to sum with the += operator.
On the last line, we print out the result of sum / scores.count. We’re dividing the sum of scores by the number of scores, which gives us the average score. (We’re converting to Double() here so the result is a decimal-point number.)
Comments
Post a Comment
If you have any douts, Please let me know