Skip to content

Instantly share code, notes, and snippets.

@an0
an0 / NSObjectVSNSObjectProtocol.md
Last active August 29, 2015 14:26
NSObject VS NSObjectProtocol

NSObjectProtocol is more general than NSObject and isEqual and hash are defined in NSObjectProtocol. Logically, we should inherit NSObjectProtocol from Equatable and Hashable.

Why not? Why instead extend NSObject to conform to Equatable and Hashable?

The current design causes problems like this:

if obj.delegate == self {

Binary operator '==' cannot be applied to operands of type 'XXXDelegate' and 'XXXDelegate'

@an0
an0 / SelfReferential.swift
Last active August 29, 2015 14:27
Self Referential Limitation or Bug
final class Foo {
// static let _sharedInstance = self.init() // Why not work?
static let _sharedInstance = Foo()
static func sharedInstance() -> Self {
return _sharedInstance // Why 'Foo' is not convertible to 'Self'? It is `static` so `Self` can only be `Foo`. In fact since the class is final there won't be any subclasses at all.
// return self.init() // Why this works?
}
required init() {
@an0
an0 / Guard.swift
Created August 21, 2015 19:31
Guard
protocol Guard {}
class Adam: Guard {}
class Alfred: Adam {}
func conforms1<T>(x: Any.Type, y: T.Type) -> Bool {
return x is T.Type
}
// This also should be true but for some reason type is Guard.Protocol instead of Guard.Type.
print(conforms1(Adam.self, y: Guard.self))
@an0
an0 / CocoaSwiftMismatch.md
Last active February 28, 2016 23:38
Cocoa Swift Mismatch
@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(:) ---&gt; CALayerDelegate.drawLayer(:, inContext:) ---&gt; UIView.drawRect(:)
@an0
an0 / flatMap.md
Created December 5, 2015 03:34
flatMap

box1_of_e1.flatMap(e1 -> box2_of_e2) -> box1_of_e2

  1. Maps every element of the source box to a target box.
  2. If the target box is not empty, unwraps it and puts its content into the source box.
  3. If the target box is empty, ignores it.

So it filters out empty target boxes. But it doesn't filter out empty elements of the source box. The mapping function f must handle empty source elements.

@an0
an0 / isKindOfType.swift
Last active February 28, 2016 23:38
isKindOfType
public func isKindOfType<T>(type: T.Type, value: Any) -> Bool {
return value is T
}
class A {}
class B: A {}
let clsA = A.self
let b = B()
@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 / 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];