Skip to content

Instantly share code, notes, and snippets.

View stinger's full-sized avatar

Ilian Konchev stinger

View GitHub Profile
@stinger
stinger / CombineFetcherAndDecoder.swift
Last active February 17, 2024 02:07
Combine - fetching and decoding JSON data
import Foundation
import Combine
enum APIError: Error, LocalizedError {
case unknown, apiError(reason: String), parserError(reason: String)
var errorDescription: String? {
switch self {
case .unknown:
return "Unknown error"
@stinger
stinger / Swift3JSONStructs.swift
Last active May 26, 2023 10:58
Swift 3: JSON-serializable structs using protocols
//: # Swift 3: JSON-serializable structs using protocols
//: Most of the implementation is based on the code in [this blog post](http://codelle.com/blog/2016/5/an-easy-way-to-convert-swift-structs-to-json/)
import Foundation
//: ### Defining the protocols
protocol JSONRepresentable {
var JSONRepresentation: Any { get }
}
protocol JSONSerializable: JSONRepresentable {}
@stinger
stinger / Swift3DataTaskDelegate.swift
Last active March 25, 2023 22:12
Swift 3: URLSessionDelegate
//: # Swift 3: URLSessionDelegate
//: The following example shows how to use `OperationQueue` to queue the network requests. This is useful in many ways (for delaying queued requests when the networking goes down for example)
import Foundation
import PlaygroundSupport
class Requester:NSObject {
let opQueue = OperationQueue()
var response:URLResponse?
@stinger
stinger / CombineFetcher.swift
Last active January 28, 2023 18:07
Combine - fetching data using URLSession publishers
import Foundation
import Combine
enum APIError: Error, LocalizedError {
case unknown, apiError(reason: String)
var errorDescription: String? {
switch self {
case .unknown:
return "Unknown error"
@stinger
stinger / DateFormattingPlayground.swift
Created November 14, 2018 15:35
Functional date formatting
import Foundation
extension Date {
typealias FormattedString = (DateFormatter) -> String
func formatted(by format: String) -> FormattedString {
return { formatter in
formatter.dateFormat = format
return formatter.string(from: self)
}
@stinger
stinger / ConcurrencyFetcher.swift
Created October 28, 2021 13:18
Swift Concurrency fetcher
enum APIError: Error, LocalizedError {
case unknown, apiError(reason: String), parserError(reason: String)
var errorDescription: String? {
switch self {
case .unknown:
return "Unknown error"
case .apiError(let reason), .parserError(let reason):
return reason
}
@stinger
stinger / SplashScrollView.swift
Created March 26, 2021 13:10 — forked from valvoline/SplashScrollView.swift
A simple Parallax Effect based paged ScrollView
//
// ContentView.swift
// Shared
//
// Created by Costantino Pistagna on 25/03/21.
//
import SwiftUI
struct ContentView: View {
@stinger
stinger / AVPhotoCaptureSample.swift
Last active December 4, 2021 15:21
An example of using the new AVPhotoCaptureOutput class in iOS 10
// Add the following to the Info.plist
// <key>NSCameraUsageDescription</key>
// <string>Allow our app to take photos</string>
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: {[weak self] status in
if status {
let session = AVCaptureSession()
for device in AVCaptureDevice.devices() {
if let device = device as? AVCaptureDevice, device.position == AVCaptureDevicePosition.back {
self.device = device
@stinger
stinger / Swift3Base64.swift
Created June 17, 2016 14:58
Swift 3: Base64 encoding and decoding strings
//: # Swift 3: Base64 encoding and decoding
import Foundation
extension String {
//: ### Base64 encoding a string
func base64Encoded() -> String? {
if let data = self.data(using: .utf8) {
return data.base64EncodedString()
}
return nil
@stinger
stinger / CombineMockClient.swift
Created July 3, 2019 12:26
Mock data loader using Combine
import Foundation
import Combine
enum APIError: Error, LocalizedError {
case unknownError
case unknownURL
case unknownData
case parserError(reason: String)
case apiError(reason: String)