Skip to content

Instantly share code, notes, and snippets.

@fcanas
fcanas / AVAsset+Catalog.swift
Created May 29, 2019 02:09
Loading AVAssets from Asset Catalogs
//
// AVAsset+Catalog.swift
// Micro Album
//
// Created by Fabian Canas on 5/28/19.
// Copyright © 2019 Fabian Canas. All rights reserved.
//
import AVFoundation
import MobileCoreServices
@fcanas
fcanas / copy.swift
Created January 11, 2018 18:31
Copy Properties in Swift
import Foundation
class C {
@NSCopying var copiedName: NSString?
var noCopyName: NSString?
}
let f = NSMutableString(string: "First")
let obj = C()
@fcanas
fcanas / crash.swift
Last active November 12, 2017 19:42
cyclic metadata dependency detected, aborting
class C<T> {
enum E {
case c(T)
}
var e: E?
}
let o = C<Int>() // Crash : cyclic metadata dependency detected, aborting
@fcanas
fcanas / FontDescriptorBug.swift
Last active November 16, 2020 18:16
Ordering Matters in UIFontDescriptor, a bug
import UIKit
/*:
# Ordering Matters in UIFontDescriptor
Oct 21, 2017, iOS 11
When creating a derived font descriptor with a font family, existing traits
such as italic or bold are overwritten or ignored.
*/
@fcanas
fcanas / average.swift
Created October 7, 2017 00:19
Abstracted average function in swift
extension Sequence where Element: BinaryFloatingPoint {
func average() -> Element {
return abstractAverage(sequence: self, sum: +, div: /)
}
}
extension Sequence where Element: SignedInteger {
func average() -> Element {
return abstractAverage(sequence: self, sum: +, div: /)
}
}
@fcanas
fcanas / about.md
Last active July 13, 2022 21:33
JSON Serialization driven by Swift Reflection

A swift playground experiment in making a Swift struct to JSON serialization.

The approach currently fails for arrays, but those can be handled much like dictionaries and optionals.

@fcanas
fcanas / NSData+HexString.m
Last active September 2, 2015 14:12
Getting a hex string out of NSData
@implementation NSData (HexString)
- (NSString *)hex_hexString {
NSMutableString *out = [NSMutableString stringWithCapacity:self.length * 2];
[self enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
for (NSUInteger i = 0; i < byteRange.length; i++) {
[out appendFormat:@"%02x", ((unsigned char *)bytes)[i]];
}
}];
return out;
import Cocoa
import MapKit
import CompassOSX
import CoreGraphics
/*
// Elsewhere in the code, to initiate the trace action:
@IBAction func makeTrace(sender: AnyObject) {
let traceView = MapTraceView()
@fcanas
fcanas / squared_same_test.swift
Created May 27, 2015 15:04
Swift Squared Same
func squaredSame(a: [Int], b: [Int]) -> Bool {
if a.count == 0 || b.count == 0 {
return false
}
return Set(map(a, { $0 * $0 })) == Set(b)
}
let testResult = test(squaredSame)
@fcanas
fcanas / ride.swift
Created May 27, 2015 14:30
your swift ride is here
func encode(name :String) -> Int {
return reduce(map(name.unicodeScalars) { Int($0.value) - 64 }, 1, *) % 47
}
func ride(group: String, comet: String) -> String {
return encode(group) == encode(comet) ? "GO" : "STAY"
}
let testData = [
("COMETQ","HVNGAT","GO"),