Skip to content

Instantly share code, notes, and snippets.

@an0
an0 / CALayerDrawingAPI.md
Last active April 18, 2022 21:51
CALayer Drawing API

Call Chain:

CALayer.display() --- if delegate implements displayLayer ---> CALayerDelegate.displayLayer(:)
|                                                               /
|                                                              /
else                                                          /
|                                                            /
|                                                           |
v                                                           v
CALayer.drawInContext(:) ---> CALayerDelegate.drawLayer(:, inContext:) ---> UIView.drawRect(:)
@an0
an0 / quine.swift
Created January 2, 2019 16:39
Swift Quine without debugDescription
let q = Unicode.Scalar(34)!
let s =
"""
print("let q = Unicode.Scalar(34)!")
print("let s =")
print(q, q, q, separator: "")
print(s)
print(q, q, q, separator: "")
print(s)
"""
@an0
an0 / quine.swift
Created January 2, 2019 16:38
Swift Quine with debugDescription
let s=";print(\"let s=\"+s.debugDescription+s)";print("let s="+s.debugDescription+s)
@an0
an0 / AsAny.swift
Last active February 28, 2017 22:06
// Discussion: https://twitter.com/an0/status/644176341986856960
["a": "a"] as? [String: Any] // warning: conditional cast from '[String : Any]' to '[String : Any]' always succeeds
let a: Any = ["a": "a"]
let b = a as? [String: String]
let c = a as? [String: AnyObject]
let d = a as? [String: Any]
if let b = b {
@an0
an0 / keybase.md
Created February 10, 2017 01:45
Keybase Proof

Keybase proof

I hereby claim:

  • I am an0 on github.
  • I am an0 (https://keybase.io/an0) on keybase.
  • I have a public key ASCrKRZNGiZhsiJnfc9Ey5RkFBxrTSUvp-j2nwz_m3v-SQo

To claim this, I am signing this object:

@an0
an0 / WeakSaferThanStrong.md
Created November 4, 2016 19:38
When weak is safer than strong

http://clang.llvm.org/docs/AutomaticReferenceCounting.html#self:

The self parameter variable of an Objective-C method is never actually retained by the implementation.

But http://clang.llvm.org/docs/AutomaticReferenceCounting.html#semantics:

For __weak objects, the current pointee is retained and then released at the end of the current full-expression. This must execute atomically with respect to assignments and to the final release of the pointee. For all other objects, the lvalue is loaded with primitive semantics.

The whole scheme makes weak references safer then strong references sometimes:

__block NSArray *strongArray = @[@1, @2];
@an0
an0 / OptionalTypeCastingIssues.swift
Last active February 28, 2016 23:46
Optional Type Casting Issues
class Foo {
}
class Bar: Foo {
}
//let b: Bar? = nil
let b: Bar? = Bar()
@an0
an0 / MutateStructArray.swift
Last active February 28, 2016 23:43
Mutate structs in array
struct Foo {
var i: Int = 0
}
var arr = [Foo(), Foo()]
print(arr)
for var a in arr {
a.i = 1
}
@an0
an0 / A2Z.swift
Last active February 28, 2016 23:41
A...Z
extension Character: ForwardIndexType {
public func successor() -> Character {
return Character(String(self).successor())
}
}
extension String: ForwardIndexType {
public func successor() -> String {
let scalars = self.unicodeScalars
let scalar = scalars[scalars.startIndex]
// Source: https://twitter.com/AirspeedSwift/status/618546432954474497
prefix func !<T>(t: T?) -> T {
return t ?? Array<Int8>(count: sizeof(T), repeatedValue: 0).withUnsafeBufferPointer {
UnsafePointer($0.baseAddress).memory
}
}
prefix func !<T: ExtensibleCollectionType>(t: T?) -> T {
return t ?? T()