From 59e8e8b6f3b90420a7a10c27696e620c7e024427 Mon Sep 17 00:00:00 2001 From: ivansaul Date: Tue, 12 Mar 2024 08:50:04 +0000 Subject: [PATCH] doc(swift): Update Arrays and Collections section - Correct redundant Array title - Add initializing arrays with default values - Update array traverse title - Add array .isEmpty property --- source/_posts/swift.md | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/source/_posts/swift.md b/source/_posts/swift.md index b97ada7..2e5f9a5 100644 --- a/source/_posts/swift.md +++ b/source/_posts/swift.md @@ -719,7 +719,7 @@ Arrays and collections -### Array array +### Array ```swift var scores = [Int]() @@ -765,6 +765,15 @@ var snowfall = [2.4, 3.6, 3.4, 1.8, 0.0] var temp: [Int] = [33, 31, 30, 38, 44] ``` +### Initialize with default value + +```swift +var teams = [Int](repeating: 0, count: 3) +print(teams) // prints: [0, 0, 0] +// or with Array type +var sizes = Array(repeating: 0, count: 3) +print(sizes) // prints: [0, 0, 0] +``` ### .append() method and += operator @@ -792,7 +801,7 @@ moon.remove(at: 4) -### Traverse the array +### Iterate over an array ```swift var employees = ["Peter", "Denial", "Jame"] @@ -879,6 +888,16 @@ if names.contains("Waldo"){ // print: There's Waldo! ``` +### .isEmpty property + +```swift +var emptyList = [String]() +print(emptyList.isEmpty) // print: true + +var populatedList: [Int] = [1, 2, 3] +print(populatedList.isEmpty) // print: false +``` + ### Iterate over a collection ```swift