Skip to content

Instantly share code, notes, and snippets.

@sindresorhus
sindresorhus / esm-package.md
Last active November 17, 2024 22:07
Pure ESM package

Pure ESM package

The package that linked you here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
@ole
ole / UIAlertController+TextField.swift
Last active September 13, 2022 14:20
A UIAlertController with a text field and the ability to perform validation on the text the user has entered while the alert is on screen. The OK button is only enabled when the entered text passes validation. More info: https://oleb.net/2018/uialertcontroller-textfield/
import UIKit
/// A validation rule for text input.
public enum TextValidationRule {
/// Any input is valid, including an empty string.
case noRestriction
/// The input must not be empty.
case nonEmpty
/// The enitre input must match a regular expression. A matching substring is not enough.
case regularExpression(NSRegularExpression)
@sharplet
sharplet / SimpleNetworkingExample.swift
Created September 14, 2017 21:37
Example Source Code for "A Simple Approach to Thread-Safe Networking in iOS Apps"
import Foundation
import PlaygroundSupport
enum URLResult {
case response(Data, URLResponse)
case error(Error, Data?, URLResponse?)
}
extension URLSession {
@discardableResult
@SheldonWangRJT
SheldonWangRJT / iOS Dynamic vs. Static Library & Framework.md
Last active November 2, 2024 06:08
iOS Dynamic vs. Static Library / Framework

iOS Dynamic vs. Static Library / Framework

Find me on Github, Facebook, Youtube, etc by #iOSBySheldon.

Concept

Library and Framework are not same but close. Frameworks are just libraries with linking (binding). In other words, frameworks are just libraries that are bundled into targets. Therefore, their file extensions are different.

File Format

  • Static Library - xxxx.a
  • Dynamic Library - xxxx.dylib
@saoudrizwan
saoudrizwan / TapGestureRecognizerWithoutSelector.swift
Last active March 4, 2024 06:45
Easily create tap gesture recognizers for any view using closures as actions instead of selectors.
import UIKit
extension UIView {
// In order to create computed properties for extensions, we need a key to
// store and access the stored property
fileprivate struct AssociatedObjectKeys {
static var tapGestureRecognizer = "MediaViewerAssociatedObjectKey_mediaViewer"
}
@robertpainsi
robertpainsi / commit-message-guidelines.md
Last active November 16, 2024 11:48
Commit message guidelines

Commit Message Guidelines

Short (72 chars or less) summary

More detailed explanatory text. Wrap it to 72 characters. The blank
line separating the summary from the body is critical (unless you omit
the body entirely).

Write your commit message in the imperative: "Fix bug" and not "Fixed
bug" or "Fixes bug." This convention matches up with commit messages
//
// UIAlertController+Action.swift
//
import UIKit
/**
Extension to `UIAlertController` to allow direct adding of `UIAlertAction` instances
*/
extension UIAlertController {
@saoudrizwan
saoudrizwan / ArrayClosures.swift
Created March 1, 2017 17:05
Get a lil functional with your arrays
// .filter() returns a new array of elements that satisfy a predicate declared in our closure
let evenNumbers = [1, 2, 3, 4, 5].filter { $0 % 2 == 0 }
// .map() returns a new array after applying a function to each element
let numbers = [1, 2, 3, 4, 5].map { $0 * 2 }
print(numbers) // [2, 4, 6, 8, 10]
// .flatMap() is like .map(), except it filters out any nil values
let arrayWithNoNils = [1, 2, 3, nil].flatMap { $0 }
print(arrayWithNoNils) // [1, 2, 3]
// http://nshipster.com/swift-objc-runtime/
extension UIViewController {
private struct AssociatedKeys {
static var DescriptiveName = "nsh_DescriptiveName"
}
var descriptiveName: String? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.DescriptiveName) as? String
}
@saoudrizwan
saoudrizwan / AppDelegate.swift
Created February 28, 2017 16:22
Set initial view controller when not working with storyboards (don't forget to remove Main.storyboard from Build Settings)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let vc = ViewController()
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = vc
window.makeKeyAndVisible()
self.window = window
return true