Skip to content

Instantly share code, notes, and snippets.

View kamwysoc's full-sized avatar

Kamil Wysocki kamwysoc

View GitHub Profile
@kamwysoc
kamwysoc / .swift
Created July 3, 2023 21:11
Swift decode array of different objects inside
struct Car: Decodable {
let manufacturer: String
let model: String
}
struct Person: Decodable {
let name: String
let lastName: String
}
let names = ["John", "Paul", "Peter", "Tim"]
let shuffledNames = names.shuffled() // returns an array of names in shuffled order.
let emptyCollection : [String] = []
emptyCollection.randomElement() // retuns nil
let names = ["John", "Paul", "Peter", "Tim"]
names.randomElement()!
let playerNumberToName : [Int: String] = [9: "Lewandowski", 7: "Ronaldo"]
playerNumberToName.randomElement()!
let randomIntFrom0To20 = Int.random(in: 0 ..< 20)
let randomFloat = Float.random(in: 0 ..< 1)
let randomIntFrom1to10 = 1 + (arc4random() % 10) // return random number is the 1...6
struct City: Hashable {
let name : String
let state : String
let population : String
}
extension City : Hashable {
func hash(into hasher: inout Hasher) {
name.hash(into: &hasher)
state.hash(into: &hasher)
}
var isTheWeatherNice : Bool = true
print(isTheWeatherNice) // prints true
//now it's starts to rain
isTheWeatherNice.toggle() // it will change the bool value.
print(isTheWeatherNice) // prints false
let s: Set<[Int?]> = [[1, nil, 2], [3, 4], [5, nil, nil]]
s.contains([1,nil,2]) // returns true
let arrayOfArrays = [[1,2],[3,4],[5,6]]
arrayOfArrays.contains([1,2]) // return false in Swift 4.1
arrayOfArrays.contains([1,2]) // now it returns True because of fact that the elements in the array conforms to Equatable protocol