Skip to content

Instantly share code, notes, and snippets.

View bestiosdeveloper's full-sized avatar

Pramod Kumar bestiosdeveloper

View GitHub Profile
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
@main
class NewAppDelegate: UIResponder, UIApplicationDelegate {
static func main() {
// Write some magical statement
}
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
}
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()
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()
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
}
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 {
class User {
var age: Int = 0 {
didSet { print("didSet for age") }
}
var name: String = "" {
didSet { print(oldValue) }
}
}
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
//
// CodableExtensionExample.swift
//
// Created by Pramod Kumar on 05/15/20.
// Copyright © 2020 Swift Commmunity. All rights reserved.
//
import UIKit
let json = """