Sergei Cherepanov

Smart guy with low self esteem

Read this first

A solution to “Symbol not found: _objc_readClassPair”

I’m using [Greenhouse CI](, they’ve got test machines running under OS X Mavericks. My iOS project was failing during attempt to run tests. Some googling brought to me several similar OS X issues mentioning 10.10 test deployment target being a culprit, and this issue gets fixed by setting target to 10.9

It appeared that the same solution applies to iOS projects - you need to set OS X Deployment Target to 10.9 for your iOS tests, and everything starts to work.

View →

Thread safe variable access, functional way. Part I.

Correctness of the threaded code has always been a concern. The common pattern is guarding some shared state with a serial dispatch queue:

func setCounter(value: Int) {
  dispatch_async(syncQueue) {
    self.counter = value

func getCounter() -> Int {
  var result = 0
  dispatch_sync(syncQueue) {
    result = self.counter
  return result

This code has a big drawback - nothing prevents developer from accessing counter from outside the syncQueue.

We can easily fix that by wrapping the variable in a container:

class AccessibleOnQueue<T> {
    init(_ value: T) {
        self.value = value

    private var value: T

But how do we access that value? We don’t. Instead we provide a way to apply a function to the contained value:

extension AccessibleOnQueue {
    func accessor<U>(f: (inout T) -> U)
                -> AccessibleOnQueue<() -> U> {

Continue reading →

JSON mapping and reader monad [DRAFT]

Functional JSON mapping became a hot theme recently. Clear declarative syntax backed by cryptic concepts of monads and applicative functors has exploded many heads.
I’d like to talk about a little refinement of our mapping pattern to better isolate mapping algorithms from actual data.

Let’s start with some simple mapping code:

struct User {
let name: String
let age: Int

static func build(name: String)(age: Int) -> User {
    return User(name: name, age: age)


// TODO: write operators

let JSON = [“name”: “Sergei”, “age”: 30]
let user = >> stringForKey(JSON, “name”)
>>> intForKey(JSON, “age”)

The thing that bothers me is coupling of input JSON with our mapping logic. What if we could rectify our mapping to remove this dependency?

Let’s introduce a Reader monad. The simple idea behind is expressing our computation as a function of our...

Continue reading →

Write yourself KVO in Swift

KVO is a major part of Objective-C dynamic nature. Having virtually every property or ivar observable by its key path opens doors to simple bindings or advanced technologies like ReactiveCocoa.

But what about Swift? It’s static nature shuts the possibility to observe properties of any class not willing to do so (by means of willSet and didSet). Not nice.

If we are going to observe something static, we need at least two things: a list of names of observable fields and a way to get their values by that name. Reminds of anything? Of course, that’s what Dictionary does.

Let’s define a goal. Assume we have a Swift dictionary of Any, and we want to add some observer block bound to particular key to be called when the value of that key gets changed. As a first step we will ignore the complexity of key-path navigation and start with a single key observation.

What do we need? First, a class...

Continue reading →

Facebook iOS Infrastructure talk

I’d like to share this brilliant talk from Facebook regarding their experience in building Facebook app.

One of the most important problems for them was to tame model layer performance. As a part of their migration from HTML to native code, they used Core Data as a model layer for caching data (and I did the same for my Chat Tomat app). After some time as the number of models grew, the number of relations between them grew even faster, leading to exponential performance loss during startup.

The solution they came with is pretty similar to what I do in GuessMe app, the ancestor of Chat Tomat - using immutable objects stored in some plain storage like an array. The thing I liked the most from their talk is the architecture of Consistency Manager layer, notifying components about changes in related models from different components, say user’s profile picture displayed in dialogs. The...

Continue reading →

How to use your own frameworks in Swift playgrounds

With the new beta of Xcode Apple added support for importing custom frameworks to Swift Playgrounds. Let’s explore the way to use them.

We’ll start with a new Cocoa Touch Framework project:

Let’s call it MyFramework. I have selected Objective-C as a main language as I heard of some issues of creating pure Swift frameworks, and also there’s no problem in adding Swift files to Objective-C framework as well.

Now let’s add a class to our framework:


import <Foundation/Foundation.h>
@interface MyClass : NSObject
- (NSString *) greeting;


import "MyClass.h"
@implementation MyClass
- (NSString *) greeting {
    return @"Hello from Framework";

Next we need to add MyClass.h to framework umbrella header and make sure it will be copied to framework bundle:

Add this line to MyFramework.h:

import <MyFramework/MyClass.h>

And change MyClass.h visibility to...

Continue reading →