Skip to content

Instantly share code, notes, and snippets.

View stormychel's full-sized avatar
🍏

Michel Storms stormychel

🍏
View GitHub Profile
import Foundation
extension String {
var withoutPunctuations: String {
return self.components(separatedBy: CharacterSet.punctuationCharacters).joined(separator: "")
}
}
let originalStr:String = "hello, (Nice to- meet 한국 école %&$you all. 123 ! Давай العربية 😀"
@jrochkind
jrochkind / report.markdown
Created December 19, 2012 15:10
Is Set faster than Array? Not neccesarily, depending on size of collection and number of reads vs writes. (MRI) (anything you notice invalid or could be done better about the way these benchmarks are done? Let us know, and give us code and results for the better way :) )
# test_for == N / 5 -- approximate array average case?
{:benchmark_iterations=>10000, :class=>Array, :test_for=>5, :num_rewrites=>1, :col_size=>10, :num_reads=>10}
0.140000 0.000000 0.140000 ( 0.141915)
{:benchmark_iterations=>10000, :class=>Set, :test_for=>5, :num_rewrites=>1, :col_size=>10, :num_reads=>10}
0.190000 0.010000 0.200000 ( 0.198503)
----
{:benchmark_iterations=>10000, :class=>Array, :test_for=>50, :num_rewrites=>1, :col_size=>100, :num_reads=>10}
@susieyy
susieyy / PreviousViewController.swift
Created August 4, 2016 10:41
Access previous view controller in navigation stack
extension UIViewController {
var previousViewController: UIViewController? {
guard let navigationController = navigationController else { return nil }
let count = navigationController.viewControllers.count
return count < 2 ? nil : navigationController.viewControllers[count - 2]
}
}
@MaciejGad
MaciejGad / NSImageExtensions.swift
Created March 24, 2017 11:49
NSImage extensions for easy resizing, cropping and saving png images. Version updated for Swift 3. Originally by Raphael Hanneken https://gist.github.com/raphaelhanneken/cb924aa280f4b9dbb480
extension NSImage {
/// Returns the height of the current image.
var height: CGFloat {
return self.size.height
}
/// Returns the width of the current image.
var width: CGFloat {
return self.size.width
@norsez
norsez / JSONSaveLoad.swift
Created May 7, 2018 04:47
Load and Save JSON objects into a local file (written in Swift)
import Foundation
/**
Extension to save/load a JSON object by filename. (".json" extension is assumed and automatically added.)
*/
extension JSONSerialization {
static func loadJSON(withFilename filename: String) throws -> Any? {
let fm = FileManager.default
@proxpero
proxpero / Iso369_1.swift
Created March 19, 2018 13:18
A Swift 4.0 enum representing ISO 639-1 codes (used to classify languages)
// taken 2018-03-19 from wikipedia. https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
public enum Iso639_1: String {
case ab = "AB"
case aa = "AA"
case af = "AF"
case ak = "AK"
case sq = "SQ"
case am = "AM"
case ar = "AR"
@EnesKaraosman
EnesKaraosman / RandomColor.swift
Last active July 14, 2023 09:35
Generatin random color in SwiftUI & UIKit
#if canImport(UIKit)
import UIKit
extension UIColor {
static var random: UIColor {
return UIColor(
red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1)
)
@amro
amro / gist:52846ea9c6eb3238b793
Last active August 19, 2023 21:56
Using an Instance of NSOrderedSet from Swift
// How one might use NSMutableOrderedSet from Swift -- mainly an example of how to
// call Objective-C classes from Swift. Pretty much what one would expect.
var set = NSMutableOrderedSet()
set.addObject("foo");
set.addObject("bar");
set.addObjectsFromArray(["bar", "wee", "foo"])
for i in 0..set.count {
println(set.objectAtIndex(i))
@nbasham
nbasham / Data+Extensions.swift
Last active January 21, 2024 14:57
Get a random date between two values. Swift 4.2+ uses Random(in:).
import Foundation
extension Date {
static func randomBetween(start: String, end: String, format: String = "yyyy-MM-dd") -> String {
let date1 = Date.parse(start, format: format)
let date2 = Date.parse(end, format: format)
return Date.randomBetween(start: date1, end: date2).dateString(format)
}
func openAndSelectFile(filename:String){
let files = [NSURL(fileURLWithPath: filename)];
NSWorkspace.sharedWorkspace().activateFileViewerSelectingURLs(files);
}