Skip to content

Instantly share code, notes, and snippets.

@auramagi
auramagi / PTRBug.swift
Created Jan 12, 2022
UIRefreshControl stuck when changing tabs in UITabBarController
View PTRBug.swift
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
func makeRefreshControl() -> UIRefreshControl {
let refresh = UIRefreshControl()
@auramagi
auramagi / BracketScanner.swift
Created Nov 29, 2021
Parse Swift type into a tree where the child nodes are the generic types. Useful for making trees from SwiftUI View types.
View BracketScanner.swift
import Foundation
final class BracketScanner {
let text: String
init(text: String) {
self.text = text.trimmingCharacters(in: CharacterSet(charactersIn: "()"))
}
lazy var scanner: Scanner = {
@auramagi
auramagi / ToDoApp.swift
Last active Sep 2, 2021
ForEachでBindingを渡す書き方のサンプルコード
View ToDoApp.swift
import SwiftUI
@main
struct ToDoApp: App {
@StateObject var model: Model = .init()
var body: some Scene {
WindowGroup {
ScrollView {
VStack(alignment: .leading) {
@auramagi
auramagi / ConsoleOutput.txt
Last active Aug 5, 2021
SwiftUI _viewDebugData
View ConsoleOutput.txt
// Output of print(_viewDebugData), formatted for easier viewing
[SwiftUI._ViewDebug.Data(
data: [
SwiftUI._ViewDebug.Property.value: SwiftUI._SafeAreaInsetsModifier(elements: [], nextInsets: nil),
SwiftUI._ViewDebug.Property.displayList: (display-list
(item #:identity 2 #:version 6
(frame (143.66666666666666 354.0; 88.0 20.333333333333332))
(text "Hello World" #:size (88.0, 20.333333333333332))
)
@auramagi
auramagi / Cats.swift
Last active Dec 11, 2020
Swift cat images
View Cats.swift
enum Cats {
private struct CatResponse: Decodable {
let url: URL
}
@discardableResult
static func getRandomImageURL(completion: @escaping (URL?) -> Void) -> URLSessionDataTask {
let request = URLRequest(url: URL(string: "https://api.thecatapi.com/v1/images/search")!)
let task = URLSession.shared.dataTask(with: request) { data, _, _ in
let catURL: URL? = {
View NoReloadDataTableViewController.swift
// reloadDataを使わないようにしましょう
import UIKit
import DiffableDataSources // https://github.com/ra1028/DiffableDataSources
@available(iOS, introduced: 13.0)
typealias TableViewDiffableDataSource = UITableViewDiffableDataSource
@available(iOS, introduced: 13.0)
typealias DiffableDataSourceSnapshot = NSDiffableDataSourceSnapshot
@auramagi
auramagi / PickerLabel.swift
Created Feb 18, 2019
Present UIPicker on a UILabel to select from a few options. Supports a placeholder value. For this example the picker selects a weekday.
View PickerLabel.swift
import UIKit
enum Weekday: Int {
case monday = 1
case tuesday = 2
case wednesday = 3
case thursday = 4
case friday = 5
case saturday = 6
case sunday = 0
@auramagi
auramagi / RoundedButton.swift
Created Jan 14, 2019
Rounded opaque buttons for iOS
View RoundedButton.swift
// Rounded opaque buttons for iOS
// Drop-in replacement for UIButton with Interface Builder support
// UIButton.buttonType type should be set to .custom
// Color is chosen based on the tint color. Supports highlighted and selected states
// Assumes that tint color has high brightness and saturation (e.g. not white or black)
// Written for iOS 12 with Swift 4.2
import UIKit
let kRoundedButtonDefaultFont = UIFont.systemFont(ofSize: 14.0, weight: .medium)
@auramagi
auramagi / decompress_ecd.py
Last active Apr 26, 2018
3DS ECD archive decompression
View decompress_ecd.py
def bit_is_set(byteval,idx):
return ((byteval&(1<<idx))!=0)
def read_int_bbo(f, s, signed=False):
return int.from_bytes(f.read(s), byteorder='big', signed=signed)
def decompressECD(f, out_fname): #file handle at ECD start position, out file name
magic = f.read(3)
if not magic == b'ECD':
return