Skip to content

Instantly share code, notes, and snippets.

@ericdke
ericdke / getMACAddress.swift
Last active September 17, 2024 01:44
Swift OS X: get MAC address for en0
/ Returns an iterator containing the primary (built-in) Ethernet interface. The caller is responsible for
// releasing the iterator after the caller is done with it.
func FindEthernetInterfaces() -> io_iterator_t? {
let matchingDictUM = IOServiceMatching("IOEthernetInterface");
// Note that another option here would be:
// matchingDict = IOBSDMatching("en0");
// but en0: isn't necessarily the primary interface, especially on systems with multiple Ethernet ports.
if matchingDictUM == nil {
@ericdke
ericdke / makePNGFromView.swift
Created June 7, 2015 19:47
Swift: create PNG from NSView
func makePNGFromView(view: NSView) {
var rep = view.bitmapImageRepForCachingDisplayInRect(view.bounds)!
view.cacheDisplayInRect(view.bounds, toBitmapImageRep: rep)
if let data = rep.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [:]) {
data.writeToFile("/xxx/image.png", atomically: false)
}
}
@ericdke
ericdke / countryFlag.swift
Created May 22, 2015 18:31
Transform a country acronym to an emoji flag
func flag(country: String) -> String {
let base = 127397 // Root Unicode flags index
var usv = String.UnicodeScalarView() // Prepare a Unicode string
for i in country.utf16 { // Loop over the country acronym letters
let inc = Int(i) // The letter's ASCII index
let code = UnicodeScalar(base + inc) // Shift the letter index to the flags index
usv.append(code) // Append the grapheme to the Unicode string
}
return String(usv)
}
@ericdke
ericdke / osxVersion.swift
Last active January 10, 2024 14:18
Swift: get OSX version (major number, minor number, name)
class OSXVersion {
class func getOSXVersionNumber() -> (major: Int, minor: Int) {
let version = NSProcessInfo.processInfo().operatingSystemVersion
return (version.majorVersion, version.minorVersion)
}
class func getOSXVersionName(major major: Int, minor: Int) -> String {
if major == 10 {
switch minor {
@ericdke
ericdke / blur.swift
Last active November 30, 2023 14:26
Swift: blur an UIImage with the Accelerate framework
import UIKit
import XCPlayground
import Accelerate
extension UIImage {
public func blur(size: Float) -> UIImage! {
let boxSize = size - (size % 2) + 1
let image = self.CGImage
let inProvider = CGImageGetDataProvider(image)
@ericdke
ericdke / getMacUUID.swift
Last active October 31, 2023 05:04
Swift: get the Mac UUID
func getSystemUUID() -> String? {
let dev = IOServiceMatching("IOPlatformExpertDevice")
let platformExpert: io_service_t = IOServiceGetMatchingService(kIOMasterPortDefault, dev)
let serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert, kIOPlatformUUIDKey, kCFAllocatorDefault, 0)
IOObjectRelease(platformExpert)
let ser: CFTypeRef = serialNumberAsCFString.takeUnretainedValue()
if let result = ser as? String {
return result
}
return nil
@ericdke
ericdke / urlcharsets.txt
Created May 10, 2016 08:16
URL encoding NSCharacterSet characters
URLQueryAllowedCharacterSet
!$&'()*+,-./0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~
URLHostAllowedCharacterSet
!$&'()*+,-.0123456789:;=ABCDEFGHIJKLMNOPQRSTUVWXYZ[]_abcdefghijklmnopqrstuvwxyz~
URLPathAllowedCharacterSet
@ericdke
ericdke / download.cpp
Last active September 24, 2023 06:42
C++: Download a file using HTTP GET and store in in a std::string
/**
* HTTPDownloader.hpp
*
* A simple C++ wrapper for the libcurl easy API.
*
* Written by Uli Köhler (techoverflow.net)
* Published under CC0 1.0 Universal (public domain)
*/
#ifndef HTTPDOWNLOADER_HPP
#define HTTPDOWNLOADER_HPP
@ericdke
ericdke / splitBy.swift
Last active July 10, 2023 09:55
Swift: split array by chunks of given size
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
extension Array {
func splitBy(subSize: Int) -> [[Element]] {
return 0.stride(to: self.count, by: subSize).map { startIndex in
let endIndex = startIndex.advancedBy(subSize, limit: self.count)
return Array(self[startIndex ..< endIndex])
}
}
}
@ericdke
ericdke / resizeNSImage.swift
Last active March 3, 2023 13:47
Resize NSImage
func resize(image: NSImage, w: Int, h: Int) -> NSImage {
var destSize = NSMakeSize(CGFloat(w), CGFloat(h))
var newImage = NSImage(size: destSize)
newImage.lockFocus()
image.drawInRect(NSMakeRect(0, 0, destSize.width, destSize.height), fromRect: NSMakeRect(0, 0, image.size.width, image.size.height), operation: NSCompositingOperation.CompositeSourceOver, fraction: CGFloat(1))
newImage.unlockFocus()
newImage.size = destSize
return NSImage(data: newImage.TIFFRepresentation!)!
}