Skip to content

Instantly share code, notes, and snippets.

View ethanhuang13's full-sized avatar

Ethan Huang ethanhuang13

View GitHub Profile
extension Result {
func tryMap<T>(_ transform:(Success) throws -> T) -> Result<T, Swift.Error> {
switch self {
case let .success(success):
do {
return .success(try transform(success))
} catch {
return .failure(error)
}
case let .failure(failure):
@ytyubox
ytyubox / AutoUpdateColor.swift
Last active January 18, 2021 06:49
Dark mode color without xcassets
import UIKit
class AutoUpdateColor: UIColor {
convenience init(lightColor: UIColor, darkColor: UIColor) {
if #available(iOS 13.0, *) {
self.init {
traitCollection in
traitCollection.userInterfaceStyle == .dark
? darkColor
: lightColor
}
extension UIHostingController {
convenience public init(rootView: Content, ignoreSafeArea: Bool) {
self.init(rootView: rootView)
if ignoreSafeArea {
disableSafeArea()
}
}
func disableSafeArea() {
#import <Foundation/Foundation.h>
// clang -o main -framework Foundation main.m
int main(int argc, const char*argv[]) {
@autoreleasepool {
NSLog(@"%@", (@42).class); // __NSCFNumber
NSLog(@"%@", (@YES).class); // __NSCFBoolean
NSLog(@"%@", @"Hello".class); // __NSCFConstantString
NSLog(@"%@", [NSString stringWithFormat:@"%d", 42].class); // NSTaggedPointerString
////===--- EitherSequence.swift - A sequence type-erasing two sequences -----===//
////
//// This source file is part of the Swift.org open source project
////
//// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
//// Licensed under Apache License v2.0 with Runtime Library Exception
////
//// See https://swift.org/LICENSE.txt for license information
//// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
////
// Run any SwiftUI view as a Mac app.
import Cocoa
import SwiftUI
NSApplication.shared.run {
VStack {
Text("Hello, World")
.padding()
.background(Capsule().fill(Color.blue))
@yllan
yllan / main.swift
Created May 14, 2020 14:43
ShitWeb
//
// main.swift
// ShitWeb
//
// Created by Yung-Luen Lan on 2020/5/14.
// Copyright © 2020 Yung-Luen Lan. All rights reserved.
//
import Foundation
import Network
@yesleon
yesleon / UIWindow+AuthenticationSession.swift
Last active July 13, 2020 03:51
A wrapper around ASWebAuthenticationSession and SFAuthenticationSession.
//
// Usage:
//
// Facebook:
//
// 1. Follow this guide (WITHOUT setting up a URL scheme in Xcode):
// https://github.com/fullstackreact/react-native-oauth/issues/76#issuecomment-335902057
//
// 2. call startFacebookAuthenticationSession(appID:completionHandler:) on a window:
//
enum Equality<T: Equatable> {
case equal(T)
case notEqual
}
func == <T: Equatable>(lhs: T, rhs: T) -> Equality<T> {
if lhs == rhs {
return .equal(lhs)
} else {
return .notEqual
@jankais3r
jankais3r / iOS 13 Entitlements
Created May 1, 2020 12:46
iOS 13 Entitlements
//Sourced from http://newosxbook.com/ent.jl?ent=&osVer=iOS13
abs-client
absinthe-client
adi-client
allow-obliterate-device
allow-softwareupdated
appData
application-identifier
aps-connection-initiate
aps-environment