Skip to content

Instantly share code, notes, and snippets.

View indyfromoz's full-sized avatar
🏠
Working from home

Indrajit Chakrabarty indyfromoz

🏠
Working from home
View GitHub Profile

Thoughts on Swift 2 Errors

When Swift was first announced, I was gratified to see that one of the (few) philosophies that it shared with Objective-C was that exceptions should not be used for control flow, only for highlighting fatal programming errors at development time.

So it came as a surprise to me when Swift 2 brought (What appeared to be) traditional exception handling to the language.

Similarly surprised were the functional Swift programmers, who had put their faith in the Haskell-style approach to error handling, where every function returns an enum (or monad, if you like) containing either a valid result or an error. This seemed like a natural fit for Swift, so why did Apple instead opt for a solution originally designed for clumsy imperative languages?

I'm going to cover three things in this post:

@indyfromoz
indyfromoz / gist:3802974
Created September 29, 2012 02:41 — forked from jeffwilcox/gist:2432351
WinRT IsInstanceOfType, IsAssignableFrom
/// <summary>
/// Determines whether the specified object is an instance of the current Type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="o">The object to compare with the current type.</param>
/// <returns>true if the current Type is in the inheritance hierarchy of the
/// object represented by o, or if the current Type is an interface that o
/// supports. false if neither of these conditions is the case, or if o is
/// null, or if the current Type is an open generic type (that is,
/// ContainsGenericParameters returns true).</returns>
BOOL PSPDFIsUIKitFlatMode(void) {
static BOOL isUIKitFlatMode = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_7_0) {
PSPDFAssertIfNotMainThread();
// If your app is running in legacy mode, tintColor will be nil - else it must be set to some color.
if (UIApplication.sharedApplication.keyWindow) {
isUIKitFlatMode = [UIApplication.sharedApplication.delegate.window performSelector:@selector(tintColor)] != nil;
import UIKit
protocol ImageDownloadProtocol {
func downloadImage(from url: URL, completion: @escaping (UIImage?) -> Void)
}
extension ImageDownloadProtocol {
func downloadImage(from url: URL, completion: @escaping (UIImage?) -> Void) {
let session = URLSession(configuration: .default)
DispatchQueue.global(qos: .background).async {
//
import SwiftUI
extension Notification.Name {
static let popEverything = Notification.Name("pop_everything")
}
struct MoreTabView: View {
@State var triggerNext: Bool = false
// Run any SwiftUI view as a Mac app.
import Cocoa
import SwiftUI
public func run<V: View>(view: V) {
let delegate = AppDelegate(view)
let app = NSApplication.shared
NSApp.setActivationPolicy(.regular)
app.mainMenu = app.customMenu
@indyfromoz
indyfromoz / breathe.swift
Created May 15, 2020 19:30 — forked from navsing/breathe.swift
let’s recreate the breathe app in less than 3 minutes using SwiftUI and Swift Playgrounds on iPad
struct Breathe: View {
@State var scale = false
@State var rotate = false
var body: some View {
ZStack {
Group {
ZStack {
Circle().frame(width: 80, height: 80).foregroundColor(Color(UIColor.systemBlue)).offset(y: -42)
Circle().frame(width: 80, height: 80).foregroundColor(Color(UIColor.systemBlue)).offset(y: 42)
@indyfromoz
indyfromoz / AppStoreHome.swift
Last active May 15, 2020 19:31 — forked from navsing/AppStoreHome.swift
Let’s recreate the iOS app store home screen in less than 5 minutes using SwiftUI and Swift Playgrounds on iPad
import SwiftUI
import PlaygroundSupport
struct Screen: View {
var body: some View {
ScrollView {
HStack {
VStack (alignment: .leading) {
Text("TUESDAY, MAY 12").foregroundColor(.secondary).bold().font(.footnote)
@indyfromoz
indyfromoz / Twilio Asterisk Secure Trunking HOWTO.md
Created June 12, 2020 02:15 — forked from ianthetechie/Twilio Asterisk Secure Trunking HOWTO.md
A short guide on how to set up an encrypted VoIP system using Twilio and Asterisk.

Twilio Asterisk Secure Trunking HOWTO

This is a short guide on how to set up an encrypted VoIP system using Twilio and Asterisk. I was a little annoyed that just about everything these days still uses unencrypted RTP for media (though just about everyone supports SIP over TLS). So I spent a weekend looking at options, and settled on a totally overkill solution involving Twilio's secure trunking to an Asterisk PBX. While all bets are off once it hits the PSTN, at least you won't be blasting your conversations over the internet in clear text.

// I needed to fetch all the pages from a paged endpoint.
// In this specific case, the JSON results contained a `pagingStatus` section that provided extra information which I could use:
// Hiding that behind a protocol:
import Foundation
protocol PagedReturning {
var pagingStatus: PagingStatus { get }
}