This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var numbers = Array(1...15) | |
// Find the indices of all the even numbers | |
let indicesOfEvens = numbers.subranges(where: { $0.isMultiple(of: 2) }) | |
// Perform an operation with just the even numbers | |
let sumOfEvens = numbers[indicesOfEvens].reduce(0, +) | |
// sumOfEvens == 56 | |
// You can gather the even numbers at the beginning |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@main | |
class NewAppDelegate: UIResponder, UIApplicationDelegate { | |
static func main() { | |
// Write some magical statement | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | |
return true | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let locations = [(latitude: 63.3454, longitude: 12.4543), (latitude: 54.5434, longitude: 23.6434), (latitude: 63.2443, longitude: 23.7545)] | |
let origin = (latitude: 0.00, longitude: 0.00) | |
// error: Argument type '(latitude: Double, longitude: Double)' does not conform to expected type 'Equatable' | |
if locations.contains(origin) { | |
// do some serious calculations here | |
} | |
// error: Referencing instance method 'sorted()' on 'Sequence' requires that '(latitude: Double, longitude: Double)' conform to 'Comparable' | |
let sortedLocations = locations.sorted() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Ranking: Comparable { | |
case platinum(Int) // < | |
case gold // < | |
case silver // < | |
case bronze | |
} | |
let someRanks: = [Ranking.gold, Ranking.platinum(8), Ranking.bronze Ranking.platinum(10), Ranking.silver] | |
someRanks.sorted() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Coder { | |
var isCoding = false | |
func perform(_ work: @escaping () -> Void) { | |
work() | |
} | |
func startCoding() { | |
perform { [self] in // <- Fix-it: capture 'self' explicitly to enable implicit 'self' in this closure. Fix-it: insert '[self] in' | |
isCoding = true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum RocketError: Error { | |
case insufficientFuel | |
case insufficientAstronauts(needed: Int) | |
case noAstronauts(message: String) | |
case outOfFuel(message: String) | |
case unknownError(message: String) | |
} | |
func igniteRockets(fuel: Int, astronauts: Int) throws { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User { | |
var age: Int = 0 { | |
didSet { print("didSet for age") } | |
} | |
var name: String = "" { | |
didSet { print(oldValue) } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var firstProperty:[Model]? = nil | |
var secondProperty:[Model]? = [Model()] | |
firstProperty ??= [] // firstProperty is set since its value is .None | |
secondProperty ??= [] // secondProperty's value isn't changed since its value is .Some |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CodableExtensionExample.swift | |
// | |
// Created by Pramod Kumar on 05/15/20. | |
// Copyright © 2020 Swift Commmunity. All rights reserved. | |
// | |
import UIKit | |
let json = """ |
NewerOlder