Skip to content

Instantly share code, notes, and snippets.

Response to https://twitter.com/sarahmei/status/862479305590284288

Whenever we’re going to make a medium or big change (feature, refactor, etc.) we’re always encouraged to create a design doc before doing actual coding:

  1. Create a design doc.
  2. Have people review it and comment on it (we use Google docs of course 😅).
  3. Then finally start coding and sending the change lists (pull requests) for review.

The content and format of the docs changes a lot between people, but typically includes: issue/feature/task at hand, screenshots of mocks if it’s a GUI change, possible solutions/designs, high and low level descriptions and justification of the chosen one, code snippets or pseudocode, and sometimes diagrams.

import XCTest
class CopyOnWriteWorkaroundTests: XCTestCase {
func testFirst() {
self.measure { //0.272 sec
var dict = ["a":[]]
for i in 0..<10000 {
if dict["a"] != nil {
dict["a"]!.append(i)
} else {
@Cananito
Cananito / Obama-on-Progress.md
Last active January 26, 2018 17:12
Transcript of Obama’s take on progress from his interview on Marc Maron’s WTF podcast: http://www.wtfpod.com/podcast/episodes/episode_613_-_president_barack_obama

From 00:28:23 to 00:35:15:

When I ran in 2008, there were those posters out there. Hope, and change. And those are capturing aspirations about where we should be going. A society that’s more just. A society that’s more equal. A society in which the dignity of every individual is respected. A society of tolerance. A society about oportunity.

And the question then is how do you operationalize those abstract concepts into something really concrete. How do we get somebody a job. How do we improve a school. How do we make sure that everybody gets decent healthcare.

As soon as you start talking about specifics, then the world is complicated, and there are choices you have to make. And it turns out that the trajectory of progress always happens in fits and starts. You have this big legacy systems that you have to wrestle with. And you have to balance what you want and where you’re going with what is and what has been.

>And… you know one of the interesting things is the conversations I have with supporters

@Cananito
Cananito / objc-protocol-throws.swift
Last active September 14, 2015 04:36
rdar://22678998 Swift 2.0 GM: Selector matching error when implementing an @objc protocol's function that throws.
@objc protocol P {
func execute() throws
// Solution: https://twitter.com/jckarter/status/643189428781846528
// @objc(executeAndReturnError:) func execute() throws
}
class A : P {
// Error: Type 'A' foes not conform to protocol 'P'

Canonical MVVM

Backstory

For a while now, I’ve seen the term MVVM being used in the iOS world. At first I dismissed it since it wasn’t really popular and it reminded me of C# world, which I tend to dislike. But then it started to get a bit more popular so I took a very brief glance at it and didn’t quite get the point.

What I saw was that people were combining it with ReactiveCocoa and/or treating ViewModels as dumb objects holding transformed data from a Model, for a View to consume. I ignored the ReactiveCocoa part since I wasn’t familiar with it (still know just a bit about it).

So based on my observation, it lead me to think: “Why are people bragging about it? They’re just adding a new layer to further separate concerns. Big deal.”.

func a(x: X) -> A {
defer {
print("Exiting the function")
}
if x == something() {
return A(x); // Console prints: "Exiting the function"
}
@Cananito
Cananito / ProtocolPropertySubclassing.swift
Last active August 29, 2015 14:21
Protocol Property Subclassing.
import UIKit
class ViewA: UIView {
}
class ViewB: UIView {
}
protocol ViewController: class {
typealias View: UIView

Keeping Performance in Mind

With Swift many people have been diving into the world of functional programming and value types. One of the many benefits of the former is the use of new abstractions in the way of functions: filter, map, and reduce.

I won’t talk about the pro’s and con’s of this approach, but for example, instead of writting custom loops:

  • If you want to convert an array of Ints to an array of Strings, you map: [1, 2, 3, 4].map({ String($0) }) // Produces ["1", "2", "3", "4"].
  • If you get the sum of all Ints of an array, you reduce: [1, 2, 3, 4].reduce(0, combine: { $0 + $1 }) // Produces 10.
  • If you want to know if an array doesn’t contain any object with a given flag, you filter and get the count. Or do you?
@Cananito
Cananito / findCondition.swift
Last active August 29, 2015 14:15
Functional name of this?
func findCondition(condition: (T) -> Bool) -> Bool {
for element in self {
if condition(element) {
return true
}
}
return false
}
@Cananito
Cananito / InitMutation.swift
Last active September 14, 2015 04:42
You can mutate constant properties in init. Swift 1.0 Beta 3.
class SomeClass {
let someArray: Array<String>
init() {
self.someArray = []
self.someArray.append("Fine") // No error
}
func someFunction() {
self.someArray.append("Not Fine") // Error