Skip to content

Instantly share code, notes, and snippets.

@nunogoncalves
nunogoncalves / BetterDecodingError.swift
Last active January 26, 2024 08:37
Better Decoding Error Messages
import Foundation
enum BetterDecodingError: CustomStringConvertible {
case dataCorrupted(_ message: String)
case keyNotFound(_ message: String)
case typeMismatch(_ message: String)
case valueNotFound(_ message: String)
case any(_ error: Error)
@muizidn
muizidn / FontLoader.swift
Last active December 21, 2023 09:59
Using custom font in iOS without register to Info.plist
public class FontLoader {
private enum Error: Swift.Error {
case error(String)
}
/// Register fonts
///
/// - Parameter fonts: Font names
static func registerFonts(fonts: [String]) throws {
let bundle = Bundle(for: FontLoader.self)
@vickyturtle
vickyturtle / Converter.kt
Last active November 14, 2023 20:18
Custom Retrofit converter for wrapped responses
class CustomConverterFactory(gson: Gson) : Converter.Factory() {
private val gsonConverterFactory: GsonConverterFactory = GsonConverterFactory.create(gson)
override fun responseBodyConverter(type: Type, annotations: Array<Annotation>, retrofit: Retrofit): Converter<ResponseBody, *>? {
val wrappedType = object : ParameterizedType {
override fun getActualTypeArguments(): Array<Type> = arrayOf(type)
override fun getOwnerType(): Type? = null
override fun getRawType(): Type = Data::class.java
}
@fxm90
fxm90 / WebViewExampleViewController.swift
Last active June 17, 2023 01:18
Show progress of WKWebView in UIProgressBar that is attached to an UINavigationBar
//
// WebViewExampleViewController.swift
//
// Created by Felix Mau on 06.01.18.
// Copyright © 2018 Felix Mau. All rights reserved.
//
import UIKit
import WebKit
@Ravi61
Ravi61 / DecodingError.swift
Created July 13, 2017 15:30
Decoding Error Catch
//...
do {
let model = try jsonDecoder.decode(BattleShip.self, from: jsonData!)
print(model)
} catch DecodingError.dataCorrupted(let context) {
print(context.debugDescription)
} catch DecodingError.keyNotFound(let key, let context) {
print("\(key.stringValue) was not found, \(context.debugDescription)")
} catch DecodingError.typeMismatch(let type, let context) {
print("\(type) was expected, \(context.debugDescription)")
@martinhoeller
martinhoeller / NSStackView+Animations.swift
Last active June 13, 2023 15:15
An NSStackView extension for animated hiding/showing of views
//
// NSStackView+Animations.swift
//
// Created by Martin Höller on 18.06.16.
// Copyright © 2016 blue banana software. All rights reserved.
//
// Based on http://prod.lists.apple.com/archives/cocoa-dev/2015/Jan/msg00314.html
import Cocoa
@phucnm
phucnm / gist:77a4ba13f30efff0c475b2c7e56c2d1a
Created April 14, 2016 08:14
Detect current page number in paging enabled Collection view
// Scrollview.tag will equal to your collection view's tag
// Use page to update page control or whatever
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
print("tag = \(scrollView.tag)")
let pageWidth = scrollView.frame.size.width
let page = Int(floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1)
print("page = \(page)")
}
@jadeatucker
jadeatucker / HOWTODMG.md
Last active July 10, 2024 23:35
How to create a "DMG Installer" for Mac OS X

Creating a "DMG installer" for OS X

A DMG Installer is convenient way to provide end-users a simple way to install an application bundle. They are basically a folder with a shortcut to the Applications directory but they can be customized with icons, backgrounds, and layout properties. A DMG file (.dmg) is a Mac OS X Disk Image file and it is used to package files or folders providing compression, encryption, and read-only to the package.

##Creating the DMG file #Disk Utility

@aral
aral / TextViewPinchToZoom
Created May 11, 2011 10:36
How to use add pinch-to-zoom to a TextView
// Create a pinch gesture recognizer instance.
self.pinchGestureRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)] autorelease];
// And add it to your text view.
[self.myTextView addGestureRecognizer:self.pinchGestureRecognizer];
// ...
- (void)pinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer
{