This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

Day 3: 100 Days of SwiftUI




Arrays, dictionaries, units, and enums

https://www.hackingwithswift.com/100/swiftui/3



Array

Swift has a method of storing ordered lists of values of the identical kind, often called Array. There are 3 ways of declaring an array.

var trainers = ["Ash", "Brock", "Misty"]
var numbers = [1, 29, 45]
var grades = [95.2, 86.8, 90.1]
Enter fullscreen mode

Exit fullscreen mode

var pokemonRegions = [String]()
Enter fullscreen mode

Exit fullscreen mode

var pokemonSpecies = Array<String>()
Enter fullscreen mode

Exit fullscreen mode

You entry knowledge from an array utilizing its index, which begins at 0.

print(trainers[0])
Enter fullscreen mode

Exit fullscreen mode

You possibly can add gadgets to an array utilizing append methodology.

var pokemonTypes = Array<String>()
pokemonTypes.append("Fireplace")
pokemonTypes.append("Water")
pokemonTypes.append("Grass")
print(pokemonTypes)
Enter fullscreen mode

Exit fullscreen mode

Arrays additionally has different helpful strategies.

print(trainers.rely)
trainers.take away(at: 2)
print(trainers.rely)
print(trainers)

print(pokemonTypes.comprises("Ice"))
print(pokemonTypes.sorted())
Enter fullscreen mode

Exit fullscreen mode



Dictionaries

Dictionaries retailer gadgets as key-value pair. The left half(earlier than semicolor) is the key and the appropriate half is the worth.

let pokemon = [
    "name": "Charizard",
    "type": "Fire",
    "ability": "blaze"
]
Enter fullscreen mode

Exit fullscreen mode

You entry the info utilizing its key, not the index. When studying from a dictionary, you may present a default worth to make use of if the important thing doesn’t exist.

print("Title: (pokemon["name", default: "Unknown"])")
print("Title: (pokemon["type", default: "Unknown"])")
print("Title: (pokemon["ability", default: "Unknown"])")
Enter fullscreen mode

Exit fullscreen mode

You can even create an empty dictionary and add gadgets after.

var regionGeneration = [String: Int]()
regionGeneration["Kanto"] = 1
regionGeneration["Johto"] = 2
regionGeneration["Hoenn"] = 3
Enter fullscreen mode

Exit fullscreen mode



Units

Units are like array however shops an unordered record of distinctive parts. Which means, the orders should not assured, and parts shouldn’t be duplicated.

let emails = Set(["test@email.com", "juan@email.com", "sample@email.com",])
print(emails)
Enter fullscreen mode

Exit fullscreen mode

You add gadgets to set utilizing insert methodology.

var pokemons = Set<String>()
pokemons.insert("Pikachu")
pokemons.insert("Charmander")
pokemons.insert("Dragonite")
print(pokemons)
Enter fullscreen mode

Exit fullscreen mode



Enum

An enumeration or enum defines a standard kind for a bunch of associated values. You declare enum in two methods:

enum Weekday {
    case monday
    case tuesday
    case wednesday
    case thursday
    case friday
}
Enter fullscreen mode

Exit fullscreen mode

enum Aspect {
    case fireplace, water, earth, wind
}
Enter fullscreen mode

Exit fullscreen mode

You employ enum this fashion:

var day = Weekday.friday
print(day)

var component = Aspect.fireplace
print(component)
Enter fullscreen mode

Exit fullscreen mode

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?