[
  {
    "path": "2013-06-07-containment-view-controller.md",
    "content": "---\ntitle:  \"View Controller Containment\"\ncategory: \"1\"\ndate: \"2013-06-07 08:00:00\"\nauthor:\n  - name: Ricki Gregersen\n    url: https://twitter.com/rickigregersen\ntags: article\n---\n\n\nBefore iOS 5, view controller containers were a privilege for Apple only.\nIn fact, there was a passage in the view controller programming guide\nstating that you should not use them. \nThe general advice from Apple on view controllers used to be that <q>a\nview controller manages a screenful of content</q>.\nThis has since changed to <q>a view controller manages a self-contained\nunit of content</q>.\nWhy didn't Apple want us to build our own tab bar controllers and\nnavigation controllers? Or put more precisely, what is the problem with: \n\n```objc\n[viewControllerA.view addSubView:viewControllerB.view]\n```\n\n![Inconsistent view hierarchy](/images/issue-1/view-insertion@2x.png)\n\nThe UIWindow, which serves as an app's root view, is where rotation and\ninitial layout messages originate from. In the\nillustration above, the child view controller, whose view was inserted\nin the root view controller view hierarchy, is excluded from those events.\nView event methods like `viewWillAppear:` will not get called.\n\nThe custom view controller containers built before iOS 5 would keep a\nreference to the child view controllers and manually relay all the\nview event methods called on the parent view controller, which is\npretty difficult to get right.\n\n## An Example\n\nWhen you were a kid playing in the sand, did your parents ever tell you that if you kept digging with your little shovel you would end up in China?\nMine did, and I made a small demo app called *Tunnel* to test this claim. \nYou can clone the [GitHub\nrepo](https://github.com/objcio/issue-1-view-controller-containment) and run the\napp, which will make it easier to understand the example code.\n*(Spoiler: digging through the earth from western Denmark lands you somewhere in the South Pacific Ocean.)*\n\n![Tunnel screenshot](/images/issue-1/tunnel-screenshot@2x.png)\n\nTo find the antipode, as the opposite location is called, move the little guy with the shovel around and the map will tell you where your exit location is. Tap the radar button and the map will flip to reveal the name of the location.\n\nThere are two map view controllers on screen. Each of them has to deal with dragging, annotation, and updating the map.\nFlipping these over reveals two new view controllers which reverse geocode the locations.\nAll the view controllers are contained inside a parent view controller, which holds their views, and ensures that layout and rotation behaves as expected.\n\nThe root view controller has two container views. These are added to\nmake it easier to layout and animate the views of child view\ncontrollers, as we will see later on.\n\n```objc\n- (void)viewDidLoad\n{\n    [super viewDidLoad];\n\n    //Setup controllers\n    _startMapViewController = [RGMapViewController new];\n    [_startMapViewController setAnnotationImagePath:@\"man\"];\n    [self addChildViewController:_startMapViewController];          //  1\n    [topContainer addSubview:_startMapViewController.view];         //  2\n    [_startMapViewController didMoveToParentViewController:self];   //  3\n    [_startMapViewController addObserver:self\n                              forKeyPath:@\"currentLocation\" \n                                 options:NSKeyValueObservingOptionNew \n                                 context:NULL];\n\n    _startGeoViewController = [RGGeoInfoViewController new];        //  4\n}\n```\n\nThe `_startMapViewController`, which displays the starting position, is instantiated and set up with the annotation image.\n\n1. The `_startMapViewController` is added as a child of the root view\n   controller. This automatically calls\nthe method `willMoveToParentViewController:` on the child.\n2. The child's view is added as a subview of the first container view.\n3. The child is notified that it now has a parent view controller.\n4. The child view controller that does the geocoding is instantiated,\n   but not inserted in any view or controller hierarchy yet.\n\n\n\n## Layout\n\nThe root view controller defines the two container views,\nwhich determine the size of the child view controllers.\nThe child view controllers do not know which container they will be\nadded to, and therefore have to be flexible in size:\n\n```objc\n- (void) loadView\n{\n    mapView = [MKMapView new];\n    mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;\n    [mapView setDelegate:self];\n    [mapView setMapType:MKMapTypeHybrid];\n\n    self.view = mapView;\n}\n```\n\nNow they will layout using the bounds of their super view. This increases the reusability of the child view controller; if we were to push it on the stack of a navigation controller, it would still layout correctly.\n\n## Transitions\n\nApple has made the view controller containment API so fine-grained that\nit is possible to construct and animate any containment scenario we can\nthink of.\nApple also provides a block-based convenience method for exchanging two controller views on the screen.\nThe method\n`transitionFromViewController:toViewController:(...)`\ntakes care of a lot of the details for us: \n\n```objc\n- (void) flipFromViewController:(UIViewController*) fromController \n               toViewController:(UIViewController*) toController  \n                  withDirection:(UIViewAnimationOptions) direction\n{\n    toController.view.frame = fromController.view.bounds;                           //  1\n    [self addChildViewController:toController];                                     //  \n    [fromController willMoveToParentViewController:nil];                            //  \n    \n    [self transitionFromViewController:fromController\n                      toViewController:toController\n                              duration:0.2\n                               options:direction | UIViewAnimationOptionCurveEaseIn\n                            animations:nil\n                            completion:^(BOOL finished) {\n                                \n                                [toController didMoveToParentViewController:self];  //  2\n                                [fromController removeFromParentViewController];    //  3\n                            }];\n}\n```\n\n1. Before the animation we add the `toController` as a child and we\n   inform the `fromController` that it will be removed. If the fromController's view is part of the container's view hierarchy, this is where `viewWillDisappear:` is called.\n2. `toController` is informed of its new parent, and appropriate view\n   event methods will be called.\n3. The `fromController` is removed.\n\nThis convenience method for view controller transitions automatically swaps out the old view controller's view for the new one. However, if you implement your own transition and you wish to only display one view at a time, you have to call `removeFromSuperview` on the old view and `addSubview:` for the new view yourself. Getting the sequence of method calls wrong will most likely result in an `UIViewControllerHierarchyInconsistency` warning. For example, this will\nhappen if you call `didMoveToParentViewController:` before you added the view.\n\nIn order to be able to use the `UIViewAnimationOptionTransitionFlipFromTop` animation, we had to add the children's views to our view containers instead of to the root view controller's view. Otherwise the animation would result in the entire root view flipping over.\n\n\n## Communication\n\nView controllers should be reusable and self-contained entities. Child view controllers are no exception to this rule of thumb. In order to achieve this, the parent view controller should only be concerned with two tasks: laying out the child view controller's root view, and communicating with the child view controller through its exposed API. It should never modify the child's view tree or other internal state directly.\n\nChild view controllers should contain the necessary logic to manage their view trees themselves -- don't treat them as dumb views. This will result in a clearer separation of concerns and better reusability.\n\nIn the Tunnel example app, the parent view controller observes a property called `currentLocation` on the map view controllers:\n\n```objc\n[_startMapViewController addObserver:self \n                          forKeyPath:@\"currentLocation\"\n                             options:NSKeyValueObservingOptionNew\n                             context:NULL];\n```\n\nWhen this property changes in response to moving the little guy with the shovel around on the map, the parent view controller communicates the antipode of the new location to the other map:\n\n```objc\n[oppositeController updateAnnotationLocation:[newLocation antipode]];\n```\n\n\nLikewise, when you tap the radar button, the parent view controller sets the locations to be reverse geocoded on the new child view controllers: \n\n```objc\n[_startGeoViewController setLocation:_startMapViewController.currentLocation];\n[_targetGeoViewController setLocation:_targetMapViewController.currentLocation];\n```\n\nIndependent of the technique you choose to communicate from child to parent view controllers (KVO, notifications, or the delegate pattern), the goal always stays the same: the child view controllers should be independent and reusable. In our example we could push one of the child view controllers on a navigation stack, but the communication would still work through the same API. \n"
  },
  {
    "path": "2013-06-07-introduction.markdown",
    "content": "---\ntitle:  \"Introduction\"\ncategory: \"1\"\ndate: \"2013-06-07 12:00\"\ntags: editorial\n---\n\nWelcome to the first edition of objc.io, a periodical about best practices and advanced techniques in Objective-C!\n\nobjc.io was founded in Berlin by [Chris Eidhof](https://twitter.com/chriseidhof), [Daniel Eggert](https://twitter.com/danielboedewadt), and [Florian Kugler](https://twitter.com/floriankugler). We started objc.io to create a regular platform for in-depth technical topics relevant to all iOS and OS X developers.\n\nEach edition of objc.io has a focus on one particular subject, with multiple articles covering different aspects of that topic. The subject of this first edition is *Lighter View Controllers*, and it contains four articles – three from the founding team and one from [Ricki Gregersen](https://twitter.com/rickigregersen), whom we would like to welcome as our first guest writer! \n\nIt's a common problem in the code base of iOS apps that view controllers get out of hand, because they do too much. By factoring out reusable code, they are easier to understand, easier to maintain and easier to test. This issue will focus on best practices and techniques how you can keep view controllers clean.\n\nWe will look at how to make view controllers primarily coordinating objects by factoring out view and model code, as well as introducing other controller objects in addition to view controllers. Furthermore, we will look at splitting up view controllers using the view controller containment mechanism, and finally, discuss how to test clean view controllers.\n\nIn upcoming editions we will have more articles from great guest writers of the Objective-C community; [Loren Brichter](https://twitter.com/lorenb), [Peter Steinberger](https://twitter.com/steipete), [Brent Simmons](https://twitter.com/brentsimmons), and [Ole Begemann](https://twitter.com/olebegemann) have committed to writing in the future. [Contact us](mailto:mail@objc.io) if you have an idea for an interesting topic and you would like to contribute an article about it to objc.io.\n\nChris, Daniel, and Florian.\n"
  },
  {
    "path": "2013-06-07-lighter-view-controllers.md",
    "content": "---\ntitle:  \"Lighter View Controllers\"\ncategory: \"1\"\ndate: \"2013-06-07 11:00:00\"\nauthor:\n  - name: Chris Eidhof\n    url: http://twitter.com/chriseidhof\ntags: article\n---\n\nView controllers are often the biggest files in iOS\nprojects, and they often contain way more code than necessary. Almost always,\nview controllers are the least reusable part of the code. We will look\nat techniques to slim down your view controllers, make code reusable, and move code to more appropriate places.\n\nThe [example project](https://github.com/objcio/issue-1-lighter-view-controllers) for this issue is on GitHub.\n\n## Separate Out Data Source and Other Protocols\n\nOne of the most powerful techniques to slim down your view controller is\nto take the `UITableViewDataSource` part of your code, and move it to\nits own class. If you do this more than once, you will start to see\npatterns and create reusable classes for this.\n\nFor example, in our example project, there is a class\n`PhotosViewController` which had the following methods:\n\n```objc\n# pragma mark Pragma \n\n- (Photo*)photoAtIndexPath:(NSIndexPath*)indexPath {\n    return photos[(NSUInteger)indexPath.row];\n}\n\n- (NSInteger)tableView:(UITableView*)tableView \n numberOfRowsInSection:(NSInteger)section {\n    return photos.count;\n}\n\n- (UITableViewCell*)tableView:(UITableView*)tableView \n        cellForRowAtIndexPath:(NSIndexPath*)indexPath {\n    PhotoCell* cell = [tableView dequeueReusableCellWithIdentifier:PhotoCellIdentifier \n                                                      forIndexPath:indexPath];\n    Photo* photo = [self photoAtIndexPath:indexPath];\n    cell.label.text = photo.name;\n    return cell;\n}\n```\n\n\n<a id=\"controllers\"> </a>\nA lot of this code has to do with arrays, and some of it is\nspecific to the photos that the view controller manages. So let's try to move the array-related code into its [own\nclass](https://github.com/objcio/issue-1-lighter-view-controllers/blob/master/PhotoData/ArrayDataSource.h).\nWe use a block for configuring the cell, but it might as well be\na delegate, depending on your use-case and taste.\n\n```objc\n@implementation ArrayDataSource\n\n- (id)itemAtIndexPath:(NSIndexPath*)indexPath {\n    return items[(NSUInteger)indexPath.row];\n}\n\n- (NSInteger)tableView:(UITableView*)tableView \n numberOfRowsInSection:(NSInteger)section {\n    return items.count;\n}\n\n- (UITableViewCell*)tableView:(UITableView*)tableView \n        cellForRowAtIndexPath:(NSIndexPath*)indexPath {\n    id cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier\n                                              forIndexPath:indexPath];\n    id item = [self itemAtIndexPath:indexPath];\n    configureCellBlock(cell,item);\n    return cell;\n}\n\n@end\n```\n\nThe three methods that were in your view controller can go, and instead\nyou can create an instance of this object and set it as the table view's\ndata source.\n\n```objc\nvoid (^configureCell)(PhotoCell*, Photo*) = ^(PhotoCell* cell, Photo* photo) {\n   cell.label.text = photo.name;\n};\nphotosArrayDataSource = [[ArrayDataSource alloc] initWithItems:photos\n                                                cellIdentifier:PhotoCellIdentifier\n                                            configureCellBlock:configureCell];\nself.tableView.dataSource = photosArrayDataSource;\n```\n\nNow you don't have to worry about mapping an index path to a\nposition in the array, and every time you want to display an\narray in a table view you can reuse this code. You can also implement\nadditional methods such as\n`tableView:commitEditingStyle:forRowAtIndexPath:` and share that code among\nall your table view controllers.\n\nThe nice thing is that we can [test this class](/issues/1-view-controllers/testing-view-controllers/#testing-a-data-source) separately,\nand never have to worry about writing it again. The same principle\napplies if you use something else other than arrays.\n\nIn one of the applications we were working on this year,\nwe made heavy use of Core Data. We created a similar class, but instead\nof being backed by an array, it is backed by a\nfetched results controller. It implements all the logic for\nanimating the updates, doing section headers, and deletion. You can\nthen create an instance of this object and feed it a fetch request and a\nblock for configuring the cell, and the rest will be taken care of.\n\nFurthermore, this approach extends to other protocols as well. One\nobvious candidate is `UICollectionViewDataSource`. This gives you\ntremendous flexibility; if, at some point during the development, you\ndecide to have a `UICollectionView` instead of a `UITableView`, you\nhardly have to change anything in your view controller. You could even\nmake your data source support both protocols.\n\n## Move Domain Logic into the Model\n\n\nHere is an example of code in view controller (from another project) that is supposed to find a list of active\npriorities for a user:\n\n```objc\n- (void)loadPriorities {\n  NSDate* now = [NSDate date];\n  NSString* formatString = @\"startDate <= %@ AND endDate >= %@\";\n  NSPredicate* predicate = [NSPredicate predicateWithFormat:formatString, now, now];\n  NSSet* priorities = [self.user.priorities filteredSetUsingPredicate:predicate];\n  self.priorities = [priorities allObjects];\n}\n```\n\nHowever, it is much cleaner to move this code to a category on the `User` class. Then\nit looks like this in `View Controller.m`:\n\n```objc\n- (void)loadPriorities {\n  self.priorities = [self.user currentPriorities];\n}\n```\n\nand in `User+Extensions.m`:\n\n\n```objc\n- (NSArray*)currentPriorities {\n  NSDate* now = [NSDate date];\n  NSString* formatString = @\"startDate <= %@ AND endDate >= %@\";\n  NSPredicate* predicate = [NSPredicate predicateWithFormat:formatString, now, now];\n  return [[self.priorities filteredSetUsingPredicate:predicate] allObjects];\n}\n```\n\nSome code cannot be easily moved into a model object but is still\nclearly associated with model code, and for this, we can use a `Store`:\n\n## Creating the Store Class\n\nIn the first version of our example application, we had some code to\nload data from a file and parse it. This code was in the view\ncontroller:\n\n```objc\n- (void)readArchive {\n    NSBundle* bundle = [NSBundle bundleForClass:[self class]];\n    NSURL *archiveURL = [bundle URLForResource:@\"photodata\"\n                                 withExtension:@\"bin\"];\n    NSAssert(archiveURL != nil, @\"Unable to find archive in bundle.\");\n    NSData *data = [NSData dataWithContentsOfURL:archiveURL\n                                         options:0\n                                           error:NULL];\n    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];\n    _users = [unarchiver decodeObjectOfClass:[NSArray class] forKey:@\"users\"];\n    _photos = [unarchiver decodeObjectOfClass:[NSArray class] forKey:@\"photos\"];\n    [unarchiver finishDecoding];\n}\n```\n\nThe view controller should not have to know about this.\nWe created a *Store* object that does just this. By separating it\nout, we can reuse that code, test it separately and keep our view\ncontroller small. The store can take care of data loading, caching,\nand setting up the database stack. This store is also often called a\n*service layer* or a *repository*. \n\n\n## Move Web Service Logic to the Model Layer\n\nThis is very similar to the topic above: don't do web service logic in\nyour view controller. Instead, encapsulate this in a different class.\nYour view controller can then call methods on this class with a callback\nhandler (for example, a completion block).\nThe nice thing is that you can do all your caching and error handling in\nthis class too.\n\n## Move View Code into the View Layer\n\nBuilding complicated view hierarchies shouldn't be done in view\ncontrollers. Either use interface builder, or encapsulate views into\ntheir own `UIView` subclasses. For example, if you build your own date\npicker control, it makes more sense to put this into a\n`DatePickerView` class than creating the whole thing in the view\ncontroller. Again, this increases reusability and simplicity.\n\nIf you like Interface Builder, then you can also do this in\nInterface Builder. Some people assume you can only use this for view\ncontrollers, but you can also load separate nib files with your custom\nviews. In our example app, we created a [`PhotoCell.xib`](https://github.com/objcio/issue-1-lighter-view-controllers/blob/master/PhotoData/PhotoCell.xib) that\ncontains the layout for a photo cell:\n\n![PhotoCell.xib screenshot](/images/issue-1/photocell.png) \n\nAs you can see, we created properties on the view (we don't use the\nFile's Owner object in this xib) and connect them to specific subviews.\nThis technique is also very handy for other custom views.\n   \n## Communication\n\nOne of the other things that happen a lot in view controllers is\ncommunication with other view controllers, the model, and the views.\nWhile this is exactly what a controller should do, it is also something\nwe'd like to achieve with as minimal code as possible.\n\nThere are a lot of well-explained techniques for communication between\nyour view controllers and your model objects (such as KVO and fetched\nresults controllers), however, communication between view controllers is\noften a bit less clear. \n\nWe often have the problem where one view controller has some state and\ncommunicates with multiple other view controllers. Often, it then makes\nsense to put this state into a separate object and pass it around the\nview controllers, which then all observe and modify that state. The\nadvantage is that it's all in one place, and we don't end up entangled\nin nested delegate callbacks. This is a complex subject, and we might\ndedicate a whole issue to this in the future.\n\n## Conclusion\n\nWe've seen some techniques for creating smaller view controllers. We\ndon't strive to apply these techniques wherever possible, as we have only\none goal: to write maintainable code. By knowing these patterns, we have better\nchances of taking unwieldy view controllers and making them clearer.\n\n### Further Reading\n\n* [View Controller Programming Guide for iOS](http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/BasicViewControllers/BasicViewControllers.html)\n* [Cocoa Core Competencies: Controller Object](http://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/ControllerObject.html)\n* [Writing high quality view controllers](http://subjective-objective-c.blogspot.de/2011/08/writing-high-quality-view-controller.html)\n* [Programmers Stack Exchange: Model View Controller Store](http://programmers.stackexchange.com/questions/184396/mvcs-model-view-controller-store)\n* [Unburdened View Controllers](https://speakerdeck.com/trianglecocoa/unburdened-viewcontrollers-by-jay-thrash)\n* [Programmers Stack Exchange: How to avoid big and clumsy `UITableViewControllers` on iOS](http://programmers.stackexchange.com/questions/177668/how-to-avoid-big-and-clumsy-uitableviewcontroller-on-ios)\n\n"
  },
  {
    "path": "2013-06-07-table-views.md",
    "content": "---\ntitle: \"Clean Table View Code\"\ncategory: \"1\"\ndate: \"2013-06-07 10:00:00\"\nauthor:\n  - name: Florian Kugler\n    url: http://twitter.com/floriankugler\ntags: article\n---\n\nTable views are an extremely versatile building block for iOS apps. Therefore, a lot of code is directly or indirectly related to table view tasks, including supplying data, updating the table view, controlling its behavior, and reacting to selections, to name just a few. In this article, we will present techniques to keep this code clean and well-structured. \n\n## UITableViewController vs. UIViewController\n\nApple provides `UITableViewController` as dedicated view controller class for table views. Table view controllers implement a handful of very useful features which can help you to avoid writing the same boilerplate code over and over. On the flip side, table view controllers are restricted to managing exactly one table view, which is presented full screen. However, in many cases, this is all you need, and if it's not, there are ways to work around this, as we will show below.\n\n### Features of Table View Controllers\n\nTable view controllers help you with loading the table view's data\nwhen it is shown for the first time. More specifically, it helps with toggling the table view's\nediting mode, with reacting to the keyboard notifications, and\nwith a few small tasks like flashing the scroll indicator and clearing\nthe selection. In order for these features to work, it is important that\nyou call super on any view event methods (such as `viewWillAppear:` and `viewDidAppear:`) that you may override in your custom subclass.\n\nTable view controllers have one unique selling point over standard view controllers, and that's the support for Apple's implementation for \"pull to refresh.\" At the moment, the only documented way of using `UIRefreshControl` is within a table view controller. There are ways to make it work in other contexts, but these could easily not work with the next iOS update.\n\nThe sum of all these elements provides much of the standard table view interface behavior as Apple has defined it. If your app conforms to these standards, it is a good idea to stick with table view controllers in order to avoid writing boilerplate code.\n\n### Limitations of Table View Controllers\n\nThe view property of table view controllers always has to be set to a table view. If you decide later on that you want to show something else on the screen aside from the table view (e.g. a map), you are out of luck if you don't want to rely on awkward hacks. \n\nIf you have defined your interface in code or using a .xib file, then it is pretty easy to transition to a standard view controller. If you're using storyboards, then this process involves a few more steps. With storyboards, you cannot change a table view controller to a standard view controller without recreating it. This means you have to copy all the contents over to the new view controller and wire everything up again.\n\nFinally, you need to add back the features of table view controller that you lost in this transition. Most of them are simple single-line statements in `viewWillAppear` or `viewDidAppear`. Toggling the editing state requires implementing an action method which flips the table view's `editing` property. The most work lies in recreating the keyboard support.\n\nBefore you go down this route though, here is an easy alternative that has the additional benefit of separating concerns:\n\n### Child View Controllers\n\nInstead of getting rid of the table view controller entirely, you could\nalso add it as a child view controller to another view controller (see\nthe [article about view controller containment](/issues/1-view-controllers/containment-view-controller/) in this issue). Then the table view controller continues to manage only the table view and the parent view controller can take care of whatever additional interface elements you might need. \n\n    \n```objc\n- (void)addPhotoDetailsTableView\n{\n    DetailsViewController *details = [[DetailsViewController alloc] init];\n    details.photo = self.photo;\n    details.delegate = self;\n    [self addChildViewController:details];\n    CGRect frame = self.view.bounds;\n    frame.origin.y = 110;\n    details.view.frame = frame;\n    [self.view addSubview:details.view];    \n    [details didMoveToParentViewController:self];\n}\n```\n\nIf you use this solution you have to create a communication channel from\nthe child to the parent view controller. For example, if the user selects a cell in the table view, the parent view controller needs to know about this in order to push another view controller. Depending on the use case, often the cleanest way to do this is to define a delegate protocol for the table view controller, which you then implement on the parent view controller. \n\n```objc\n@protocol DetailsViewControllerDelegate\n- (void)didSelectPhotoAttributeWithKey:(NSString *)key;\n@end\n\n@interface PhotoViewController () <DetailsViewControllerDelegate>\n@end\n\n@implementation PhotoViewController\n// ...\n- (void)didSelectPhotoAttributeWithKey:(NSString *)key\n{\n    DetailViewController *controller = [[DetailViewController alloc] init];\n    controller.key = key;\n    [self.navigationController pushViewController:controller animated:YES];\n}\n@end\n```\n\nAs you can see, this construction comes with the price of some overhead to communicate between the view controllers in return for a clean separation of concerns and better reusability. Depending on the specific use case, this can either end up making things more simple or more complex than necessary. That's for you to consider and decide.\n\n\n## Separating Concerns\n\nWhen dealing with table views there are a variety of different tasks involved which cross the borders between models, controllers, and views. In order to prevent view controllers from becoming the place for all these tasks, we will try to isolate as many of these tasks as possible in more appropriate places. This helps readability, maintainability, and testability. \n\nThe techniques described here extend and elaborate upon the concepts\ndemonstrated in the article [Lighter view controllers](/issues/1-view-controllers/lighter-view-controllers/). Please refer to this article for how to factor our data source and model logic. In the context of table views, we will specifically look at how to separate concerns between view controllers and views.\n\n### Bridging the Gap Between Model Objects and Cells\n\nAt some point we have to hand over the data we want to display into the view layer. Since we still want to maintain a clear separation between the model and the view, we often offload this task to the table view's data source:\n\n```objc\n- (UITableViewCell *)tableView:(UITableView *)tableView \n         cellForRowAtIndexPath:(NSIndexPath *)indexPath\n{\n    PhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:@\"PhotoCell\"];\n    Photo *photo = [self itemAtIndexPath:indexPath];\n    cell.photoTitleLabel.text = photo.name;\n    NSString* date = [self.dateFormatter stringFromDate:photo.creationDate];\n    cell.photoDateLabel.text = date;\n}\n```\n\nThis kind of code clutters the data source with specific knowledge about the design of the cell. We are better off factoring this out into a category of the cell class:\n\n```objc\n@implementation PhotoCell (ConfigureForPhoto)\n\n- (void)configureForPhoto:(Photo *)photo\n{\n    self.photoTitleLabel.text = photo.name;\n    NSString* date = [self.dateFormatter stringFromDate:photo.creationDate];\n    self.photoDateLabel.text = date;\n}\n\n@end\n```\n\nWith this in place, our data source method becomes very simple.\n\n```objc\n- (UITableViewCell *)tableView:(UITableView *)tableView\n         cellForRowAtIndexPath:(NSIndexPath *)indexPath\n{\n    PhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:PhotoCellIdentifier];\n    [cell configureForPhoto:[self itemAtIndexPath:indexPath]];\n    return cell;\n}\n```\n\nIn our example code, the data source for this table view is [factored\nout into its own controller object](/issues/1-view-controllers/lighter-view-controllers/#separate-out-data-source-and-other-protocols), which gets initialized with a cell configuration block. In this case, the block becomes as simple as this:\n\n```objc\nTableViewCellConfigureBlock block = ^(PhotoCell *cell, Photo *photo) {\n    [cell configureForPhoto:photo];\n};\n```\n\n### Making Cells Reusable\n\nIn cases where we have multiple model objects that can be presented using the same cell type, we can even go one step further to gain reusability of the cell. First, we define a protocol on the cell to which an object must conform in order to be displayed by this cell type. Then we simply change the configure method in the cell category to accept any object conforming to this protocol. These simple steps decouple the cell from any specific model object and make it applicable to different data types.\n    \n### Handling Cell State Within the Cell\n\nIf we want to do something beyond the standard highlighting or selection behavior of table views, we could implement two delegate methods, which modify the tapped cell in the way we want. For example:\n\n```objc\n- (void)tableView:(UITableView *)tableView\n        didHighlightRowAtIndexPath:(NSIndexPath *)indexPath\n{\n    PhotoCell *cell = [tableView cellForRowAtIndexPath:indexPath];\n    cell.photoTitleLabel.shadowColor = [UIColor darkGrayColor];\n    cell.photoTitleLabel.shadowOffset = CGSizeMake(3, 3);\n}\n\n- (void)tableView:(UITableView *)tableView\n        didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath\n{\n    PhotoCell *cell = [tableView cellForRowAtIndexPath:indexPath];\n    cell.photoTitleLabel.shadowColor = nil;\n}\n```\n\nHowever, the implementation of these two delegate methods relies again on specific knowledge about how the cell is implemented. If we want to swap out the cell or redesign it in a different way, we also have to adapt the delegate code. The implementation details of the view are complected with the implementation of the delegate. Instead, we should move this logic into the cell itself.\n\n```objc\n@implementation PhotoCell\n// ...\n- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated\n{\n    [super setHighlighted:highlighted animated:animated];\n    if (highlighted) {\n        self.photoTitleLabel.shadowColor = [UIColor darkGrayColor];\n        self.photoTitleLabel.shadowOffset = CGSizeMake(3, 3);\n    } else {\n        self.photoTitleLabel.shadowColor = nil;\n    }\n}\n@end\n```\n\nGenerally speaking, we strive to separate the implementation details of the view layer from the implementation details of the controller layer. A delegate has to know about the different states a view can be in, but it shouldn't have to know how to modify the view tree or which attributes to set on some subviews in order to get it into the right state. All this logic should be encapsulated within the view, which then provides a simple API to the outside.\n        \n### Handling Multiple Cell Types\n\nIf you have multiple different cell types within one table view, the data source methods can quickly get out of hand. In our example app we have two different cell types for the photo details table: one cell to display a star rating, and a generic cell to display a key-value pair. In order to separate the code dealing with these different cell types, the data source method simply dispatches the request to specialized methods for each cell type.\n\n```objc\n- (UITableViewCell *)tableView:(UITableView *)tableView  \n         cellForRowAtIndexPath:(NSIndexPath *)indexPath\n{\n    NSString *key = self.keys[(NSUInteger) indexPath.row];\n    id value = [self.photo valueForKey:key];\n    UITableViewCell *cell;\n    if ([key isEqual:PhotoRatingKey]) {\n        cell = [self cellForRating:value indexPath:indexPath];\n    } else {\n        cell = [self detailCellForKey:key value:value];\n    }\n    return cell;\n}\n\n- (RatingCell *)cellForRating:(NSNumber *)rating\n                    indexPath:(NSIndexPath *)indexPath\n{\n    // ...\n}\n\n- (UITableViewCell *)detailCellForKey:(NSString *)key\n                                value:(id)value\n{\n    // ...\n}\n```\n\n### Table View Editing\n\nTable views provide easy-to-use editing features, which allow for\nreordering and deletion of cells. In case of these events, the table\nview's data source gets notified via [delegate methods](http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDataSource/tableView:commitEditingStyle:forRowAtIndexPath:). Therefore, we often see domain logic in these delegate methods that performs the actual modification on the data.\n\nModifying data is a task that clearly belongs in the model layer. The model should expose an API for things like deletion and reordering, which we can then call from the data source methods. This way, the controller plays the role of a coordinator between the view and the model, but does not have to know about the implementation details of the model layer. As an added benefit, the model logic also becomes easier to test, because it is not interweaved with other tasks of view controllers anymore.\n\n\n## Conclusion\n\nTable view controllers (and other controller objects!) should mostly have a [coordinating and mediating role](http://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/ControllerObject.html) between model and view objects. They should not be concerned with tasks that clearly belong to the view or the model layer. If you keep this in mind, the delegate and data source methods will become much smaller and mostly contain simple boilerplate code.\n\nThis not only reduces the size and complexity of table view controllers, but it puts the domain logic and view logic in much more appropriate places. Implementation details below and above the controller layer are encapsulated behind a simple API, which ultimately makes it much easier to understand the code and to work on it collaboratively.\n\n### Further Reading\n\n* [Blog: Skinnier Controllers using View Categories](https://web.archive.org/web/20160423142402/http://www.sebastianrehnby.com/blog/2013/01/01/skinnier-controllers-using-view-categories/)\n* [Table View Programming Guide](http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/AboutTableViewsiPhone/AboutTableViewsiPhone.html)\n* [Cocoa Core Competencies: Controller Object](http://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/ControllerObject.html)\n"
  },
  {
    "path": "2013-06-07-testing-view-controllers.md",
    "content": "---\ntitle: \"Testing View Controllers\"\ncategory: \"1\"\ndate: \"2013-06-07 09:00\"\nauthor:\n  - name: Daniel Eggert\n    url: https://twitter.com/danielboedewadt\ntags: article\n---\n\nLet's not be religious about testing. It should help us speed up development and make things more fun.\n\n## Keeping Things Simple\n\nTesting simple things is simple, and testing complex things is complex.  As we point out in the other articles, keeping things small and simple is good in general. And as a side effect, it also helps testing. It's a win-win. Take a look at [test-driven development](https://en.wikipedia.org/wiki/Test-driven_development) (known as TDD among friends) -- some people love it, some don't. We won't go into detail about it here, but we will say that with TDD, you write the test for your code before you write the code. Check out the Wikipedia article if you’re curious. We would also like to note that refactoring and testing go very well together.\n\nTesting UI components is often tricky because there are too many moving parts involved. More often than not, the view controller interacts with a lot of classes from both the model and the view layer. In order to be able to test the view controller, we need things to work in isolation.\n\nThere's hope, though: The techniques we describe to make [lighter view controllers](/issues/1-view-controllers/lighter-view-controllers/) also make testing easier. Generally, if you find something difficult to test, that's a hint that your design may be broken and that you should refactor some of it. Again, refer to the article about [lighter view controllers](/issues/1-view-controllers/lighter-view-controllers/) for some hints. An overall design goal is to have clear separation of concerns. Each class should do only one thing, and do that one thing well. That will then allow for testing of that one thing.\n\nRemember: You'll get diminishing returns as you add more tests. First and foremost, add simple tests. Branch into more sophisticated territory as you start to feel comfortable with it.\n\n\n\n## Mocking\n\nWhen we break things up into small components (i.e. small classes), we can test each class on its own. The class that we're testing interacts with other classes. We get around this by using a so-called *mock* or *stub*. Think of a *mock object* as a placeholder. The class we’re testing will interact with placeholders instead of real objects. That way, we focus our test and ensure that it doesn’t depend on other parts of our app.\n\nThe example app has an array data source that we'll test. The data source will at some point dequeue a cell from a table view. During testing, we don't have a table view, but by passing a *mock* table view, we can test the data source without a *real* table view, as you'll see below. It's a bit confusing at first, but very powerful and straightforward once you've seen it a few times.\n\nThe power tool for mocking in Objective-C is called [OCMock](http://ocmock.org). It's a very mature project that leverages the power and flexibility of the Objective-C runtime. It pulls some cool tricks to make testing with mock objects fun.\n\nThe data source test below shows, in more detail, how all of this plays out together.\n\n\n## SenTestKit\n\nThe other tool we'll use is the test framework that comes as part of the developer tools: SenTestingKit by [Sente](http://www.sente.ch). This dinosaur has been around for Objective-C developers since 1997 -- ten years before the iPhone was released. Today, it's built into Xcode.\n\nSenTestingKit is what will run your tests. With SenTestingKit, you organize tests into classes. You create one test class for each class you want to test. This class will have a name ending in `Tests`, and the name reflects what the class is about.\n\nThe methods inside each of these *tests* classes will do the actual testing. The method name has to start with `test`, as that's what triggers it to get run as a test. There are special `-setUp` and `-tearDown` methods you can override to set up each test. Remember that your test class is just a class: If it helps you structure your tests, feel free to add properties and helper methods.\n\nA nice pattern when testing is to create a custom base class for the\ntests. We then put convenience logic in there to make our tests easier\nand more focused. Check out the [example project](https://github.com/objcio/issue-1-lighter-view-controllers/blob/master/PhotoDataTests/PhotoDataTestCase.h) for some samples of when this might be useful. We’re also not using the Xcode templates for tests -- we’re going for something simpler and more efficient: We add a single `.m` file. By convention the tests class have a name ending in `Tests`. The name should reflect what we're testing.\n\n## Integration with Xcode\n\nTests are built into a bundle of a dynamic library plus resources of your choice. If you need particular resource files for your testing, add them to the test target, and Xcode will put them inside the bundle. You can then locate them with `NSBundle`. The example project implements a `-URLForResource:withExtension:` method to make it easy to use.\n\nEach *scheme* in Xcode defines what the corresponding test bundle should be. While ⌘-R runs your app, ⌘-U will run your tests.\n\nThe way the tests are run, your app is actually launched, and the test bundle is *injected*. You probably don't want your app to do much, as it may interfere with the testing. Put something like this into your app delegate:\n\n```objc\nstatic BOOL isRunningTests(void) __attribute__((const));\n\n- (BOOL)application:(UIApplication *)application   \n        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions\n{\n    if (isRunningTests()) {\n        return YES;\n    }\n    \n    //\n    // Normal logic goes here\n    //\n    \n    return YES;\n}\n\nstatic BOOL isRunningTests(void)\n{\n    NSDictionary* environment = [[NSProcessInfo processInfo] environment];\n    NSString* injectBundle = environment[@\"XCInjectBundle\"];\n    return [[injectBundle pathExtension] isEqualToString:@\"octest\"];\n}\n```\n\nEditing your scheme in Xcode gives you a great deal of flexibility. You can run scripts before and after the tests, and you can have multiple test bundles. This can be useful for larger projects. Most importantly, you can turn on and off individual tests. This can be useful for debugging tests -- just remember to turn them all back on.\n\nAlso remember that you can set breakpoints in your code and in test cases and the debugger will stop there as the tests are executed.\n\n\n## Testing a Data Source\n\nLet's get started. We've made testing easier by splitting up the view controller. Now we'll test the `ArrayDataSource`. First, we create a new and empty basic setup. We put both the interface and implementation into the same file; no one needs to include the `@interface` anywhere else, as it's all nice and tidy inside one file:\n\n```objc\n#import \"PhotoDataTestCase.h\"\n\n@interface ArrayDataSourceTest : PhotoDataTestCase\n@end\n\n@implementation ArrayDataSourceTest\n- (void)testNothing;\n{\n    STAssertTrue(YES, @\"\");\n}\n@end\n```\n\nThis will not do much. It shows the basic test setup. When we run the tests, the `-testNothing` method will run. The special `STAssert` macro will do its trivial check. Note that `ST` originates from SenTestingKit. These macros integrate with Xcode and will make failures show up in the *Issues* navigator.\n\n## Our First Test\n\nWe'll now replace the `testNothing` method with a simple, but real test:\n\n```objc\n- (void)testInitializing;\n{\n    STAssertNil([[ArrayDataSource alloc] init], @\"Should not be allowed.\");\n    TableViewCellConfigureBlock block = ^(UITableViewCell *a, id b){};\n    id obj1 = [[ArrayDataSource alloc] initWithItems:@[]\n                                      cellIdentifier:@\"foo\"\n                                  configureCellBlock:block];\n    STAssertNotNil(obj1, @\"\");\n}\n```\n\n## Putting Mocking into Practice\n\nNext, we want to test the\n\n```objc\n- (UITableViewCell *)tableView:(UITableView *)tableView\n         cellForRowAtIndexPath:(NSIndexPath *)indexPath;\n```\n\nmethod that the ArrayDataSource implements. For that we create a\n\n```objc\n- (void)testCellConfiguration;\n```\n\ntest method.\n\n\nFirst we create a data source:\n\n```objc\n__block UITableViewCell *configuredCell = nil;\n__block id configuredObject = nil;\nTableViewCellConfigureBlock block = ^(UITableViewCell *a, id b){\n    configuredCell = a;\n    configuredObject = b;\n};\nArrayDataSource *dataSource = [[ArrayDataSource alloc] initWithItems:@[@\"a\", @\"b\"] \n                                                      cellIdentifier:@\"foo\"\n                                                  configureCellBlock:block];\n```\n\nNote that the `configureCellBlock` doesn't do anything except store the objects that it was called with. This allows us to easily test it.\n\nNext, we'll create a *mock object* for a table view:\n\n```objc\nid mockTableView = [OCMockObject mockForClass:[UITableView class]];\n```\n\nThe data source is going to call `-dequeueReusableCellWithIdentifier:forIndexPath:` on the passed-in table view. We'll tell the mock object what to do when it gets this message. We first create a `cell` and then set up the *mock*:\n\n```objc\nUITableViewCell *cell = [[UITableViewCell alloc] init];\nNSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];\n[[[mockTableView expect] andReturn:cell]\n        dequeueReusableCellWithIdentifier:@\"foo\"\n                             forIndexPath:indexPath];\n```\n\nThis will look a bit confusing at first. What's going on here, is that the mock is *recording* this particular call. The mock is not a table view; we're just pretending that it is. The special `-expect` method allows us to set up the mock so that it knows what to do when this method gets called on it.\n\nIn addition, the `-expect` method tells the mock that this call *must* happen. When we later call `-verify` on the mock, the test will fail if the method didn't get called. The corresponding `-stub` method also sets up the mock object, but doesn’t care if the method will get called.\n\nNow we'll trigger the code to get run. We'll call the method we want to test:\n\n```objc\nNSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];\nid result = [dataSource tableView:mockTableView\n            cellForRowAtIndexPath:indexPath];\n```\n\nand then we'll test that things went well:\n\n```objc\nSTAssertEquals(result, cell, @\"Should return the dummy cell.\");\nSTAssertEquals(configuredCell, cell, @\"This should have been passed to the block.\");\nSTAssertEqualObjects(configuredObject, @\"a\", @\"This should have been passed to the block.\");\n[mockTableView verify];\n```\n\nThe `STAssert` macros test that the values are identical. Note that we use pointer comparison for the first two tests; we don't want to use `-isEqual:`. We actually want to test that `result` and `cell` and `configuredCell` all are the very same object. The third test uses `-isEqual:`, and finally we call `-verify` on our mock.\n\nNote that in the example, we're setting up the mock with\n\n```objc\nid mockTableView = [self autoVerifiedMockForClass:[UITableView class]];\n```\n\nThis is a convenience wrapper in our base test class which automatically calls `-verify` at the end of the test.\n\n\n## Testing a UITableViewController\n\nNext, we turn toward the `PhotosViewController`. It's a `UITableViewController` subclass and it uses the data source we've just tested. The code that remains in the view controller is pretty simple.\n\nWe want to test that tapping on a cell takes us to the detail view, i.e. an instance of `PhotoViewController` is pushed onto the navigation controller. We'll again use mocking to make the test depend as little as possible on other parts.\n\nFirst we create a `UINavigationController` mock:\n\n```objc\nid mockNavController = [OCMockObject mockForClass:[UINavigationController class]];\n```\n\nNext up, we'll use *partial mocking*. We want our `PhotosViewController` instance to return the `mockNavController` as its `navigationController`. We can't set the navigation controller directly, so we'll simply stub only that method to return our `mockNavController` and forward everything else to the `PhotosViewController` instance:\n\n```objc\nPhotosViewController *photosViewController = [[PhotosViewController alloc] init];\nid photosViewControllerMock = [OCMockObject partialMockForObject:photosViewController];\n[[[photosViewControllerMock stub] andReturn:mockNavController] navigationController];\n```\n\nNow, whenever the `-navigationController` method is called on `photosViewController`, it will return the `mockNavController`. This is a very powerful trick that OCMock has up its sleeve.\n\nWe now tell the navigation controller mock what we expect to be called, i.e. a detail view controller with `photo` set to a non-nil value:\n\n```objc\nUIViewController* viewController = [OCMArg checkWithBlock:^BOOL(id obj) {\n    PhotoViewController *vc = obj;\n    return ([vc isKindOfClass:[PhotoViewController class]] &&\n            (vc.photo != nil));\n}];\n[[mockNavController expect] pushViewController:viewController animated:YES];\n```\n\nNow we trigger the view to be loaded and simulate the row to be tapped:\n\n\n```objc\nUIView *view = photosViewController.view;\nSTAssertNotNil(view, @\"\");\nNSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];\n[photosViewController tableView:photosViewController.tableView \n        didSelectRowAtIndexPath:indexPath];\n```\n\nFinally we verify that the expected method was called on the mocks:\n\n```objc\n[mockNavController verify];\n[photosViewControllerMock verify];\n```\n\nWe now have a test that tests interaction with the navigation controller and creation of the correct view controller.\n\nAgain, in the example project, we're using our own convenience methods\n\n```objc\n- (id)autoVerifiedMockForClass:(Class)aClass;\n- (id)autoVerifiedPartialMockForObject:(id)object;\n```\n\nand hence we don't have to remember to call `-verify`.\n\n\n## Further Possibilities\n\nAs you've seen above, *partial mocking* is extremely powerful. If you take a look at the source code of the `-[PhotosViewController setupTableView]` method, you'll see how it gets the model objects through the app delegate:\n\n```objc\n NSArray *photos = [AppDelegate sharedDelegate].store.sortedPhotos;\n```\n\nThe above test depends on this. One way to break this dependency would be to again use *partial mocking* to make the app delegate return predefined data like so:\n\n```objc\nid storeMock; // assume we've set this up\nid appDelegate = [AppDelegate sharedDelegate]\nid appDelegateMock = [OCMockObject partialMockForObject:appDelegate];\n[[[appDelegateMock stub] andReturn:storeMock] store];\n```\n\nNow whenever `[AppDelegate sharedDelegate].store` gets called, it will return the `storeMock`. This can be taken to extremes. Make sure to keep your tests as simple as possible and only as complex as needed.\n\n## Things to Remember\n\nPartial mocks alter the object they're mocking for as long as they're around. You can stop that behavior early by calling `[aMock stopMocking]`. Most of the time, you want the partial mock to stay active for the entire duration of the test. Make sure that happens by putting a `[aMock verify]` at the end of the test method. Otherwise ARC might dealloc the mock early. And you probably want that `-verify` anyway.\n\n## Testing NIB Loading\n\nThe `PhotoCell` is setup in a NIB. We can write a simple test that checks that the outlets are set up correctly. Let's review the `PhotoCell` class:\n\n```objc\n @interface PhotoCell : UITableViewCell\n \n + (UINib *)nib;\n \n @property (weak, nonatomic) IBOutlet UILabel* photoTitleLabel;\n @property (weak, nonatomic) IBOutlet UILabel* photoDateLabel;\n \n @end\n```\n\nOur simple test implementation looks like this\n\n```objc\n@implementation PhotoCellTests\n\n- (void)testNibLoading;\n{\n    UINib *nib = [PhotoCell nib];\n    STAssertNotNil(nib, @\"\");\n    \n    NSArray *a = [nib instantiateWithOwner:nil options:@{}];\n    STAssertEquals([a count], (NSUInteger) 1, @\"\");\n    PhotoCell *cell = a[0];\n    STAssertTrue([cell isMemberOfClass:[PhotoCell class]], @\"\");\n    \n    // Check that outlets are set up correctly:\n    STAssertNotNil(cell.photoTitleLabel, @\"\");\n    STAssertNotNil(cell.photoDateLabel, @\"\");\n}\n\n@end\n```\n\nVery basic, but it does its job.\n\nOne may argue that we now need to update both the test and the class / nib when we change things. That's true. We need to weigh this against the likelihood of breaking the outlets. If you've worked with `.xib` files, you've probably noticed that this is a commonly occurring thing.\n\n## Side Note About Classes and Injection\n\nAs we noted under *Integration with Xcode* the test bundle gets injected into the app. Without getting into too much detail about how injection works (it's a huge topic in its own right): Injection adds the Objective-C classes from the injected bundle (our test bundle) to the running app. That's good, because it allows us to run our tests.\n\nOne thing that can be very confusing, though, is if we add a class to both the app and the test bundle. If we, in the above example, would (by accident) have added the `PhotoCell` class to both the test bundle and the app, then the call to `[PhotoCell class]` would return a different pointer when called from inside our test bundle - that from within the app. And hence our test\n\n```objc\nSTAssertTrue([cell isMemberOfClass:[PhotoCell class]], @\"\");\n```\n\nwould fail. Again: Injection is complex. Your take away should be: Don't add `.m` files from your app to your test target. You'll get unexpected behavior.\n\n## Additional Thoughts\n\nIf you have a Continuous Integration solution, getting your tests up and running there is a great idea. Details are outside the scope of this article. The scripts are triggered by the `RunUnitTests` script, and there's a `TEST_AFTER_BUILD` environment variable.\n\nAnother interesting option is to create an independent test bundle for automated performance tests. You're free to do whatever you want inside your test methods. Timing certain calls and using `STAssert` to check that they're within a certain threshold would be one option.\n\n### Further Reading\n\n* [Test-driven development](https://en.wikipedia.org/wiki/Test-driven_development)\n* [OCMock](http://ocmock.org)\n* [Xcode Unit Testing Guide](https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/UnitTesting/)\n* [Book: Test Driven Development: By Example](http://www.amazon.com/Test-Driven-Development-Kent-Beck/dp/0321146530)\n* [Blog: Quality Coding](http://qualitycoding.org)\n* [Blog: iOS Unit Testing](http://iosunittesting.com)\n* [Blog: Secure Mac Programing](http://blog.securemacprogramming.com/?s=testing&searchsubmit=Search)\n"
  },
  {
    "path": "2013-07-07-async-testing.md",
    "content": "---\ntitle:  \"Testing Concurrent Applications\"\ncategory: \"2\"\ntags: article\ndate: \"2013-07-07 06:00:00\"\nauthor:\n  - name: Tobias Kräntzer\n    url: http://twitter.com/anagrom_ataf/\n---\n\nTesting is an important tool during the development process to create high quality applications. In the past, when concurrency was not such an important part of application architecture, testing was straightforward. Over the past few years it has become more and more important to use concurrent design patterns and we were challenged to develop new best practices to test them.\n\nThe main challenge of testing concurrent code is that the program or information flow is not reflected in the call stack any more. Functions do not return their result to the caller immediately, but deliver it later via callback functions, blocks, notifications, or similar mechanisms, which makes testing more difficult.\n\nHowever, testing asynchronous code comes with the benefit of uncovering poor design decisions and facilitating clean implementations.\n\n\n## The Problem with Asynchronous Testing\n\nLet's first recall an example of a simple synchronous unit test. This method of a simple calculator should sum up two numbers:\n\n```objc\n+ (int)add:(int)a to:(int)b {\n    return a + b;\n}\n```\n\nTesting this method is as simple as calling the method and comparing the result to the expected value. If the values don't match, the test fails.\n\n```objc\n- (void)testAddition {\n    int result = [Calculator add:2 to:2];\n    STAssertEquals(result, 4, nil);\n}\n```\n\nNow let's change the method to return its result asynchronously via a completion block. We will also add a bug to the implementation, so that we can expect a failing test:\n\n```objc\n+ (int)add:(int)a to:(int)b block:(void(^)(int))block {\n    [[NSOperationQueue mainQueue] addOperationWithBlock^{\n        block(a - b); // Buggy implementation\n    }];\n}\n```\n\nOf course this is a contrived example, but it reflects the general pattern you would use often if the operation would be more computationally intensive.\n\nA naive approach to testing this method would just move the assertion into the completion block. However, such a test simply never fails, in spite of the bug in our implementation:\n\n```objc\n// don't use this code!\n- (void)testAdditionAsync {\n    [Calculator add:2 to:2 block^(int result) {\n        STAssertEquals(result, 4, nil); // Never fails!\n    }];\n}\n```\n\nWhy doesn't this assertion fail?\n\n\n## SenTestingKit Under the Hood\n\nThe testing framework used by Xcode 4 is based on [OCUnit][4], which allows us to have a closer look at the internals. To understand the problem with the asynchronous test, we need to have a look at the execution order of the different parts of the test suite. This diagram shows a simplified flow.\n\n![SenTestingKit call stack](/images/issue-2/SenTestingKit-call-stack@2x.png)\n\nAfter the testing kit is started on the main run loop, it executes the following main steps:\n\n1. It sets up a test suite containing all relevant tests (as specified e.g. in the project scheme).\n2. It runs the suite, which internally invokes all methods of the test cases starting with _test_. This run returns an object, containing the results of each single test.\n3. It exits the process by calling `exit()`.\n\nThe interesting part is how each individual test is invoked. During the asynchronous test, the completion block containing the assertion gets enqueued on the main run loop. Since the testing framework exits the process after all tests run, this block never gets executed and therefore never causes the test to fail.\n\nThere are several approaches to solve this problem. But all of them have to run the main run loop and handle the enqueued operations before the test method returns and the framework checks the result.\n\n[Kiwi][5] uses a probe poller, which can be invoked within the test method. [GHUnit][6] provides a separate test class, which has to be prepared within the test method and which needs a notification at the end. In both cases we have to write some code, which ensures that the test method will not return until the test finishes. \n\n\n## Async Extension to SenTestingKit\n\nOur solution to this problem is an [extension][2] to the built-in testing kit, which winds up the synchronous execution on the stack and enqueues each part as a block on the main queue. As you can see in the diagram below, the block that reports the success or failure of the asynchronous test is enqueued before the results of the entire suite are checked. This execution order allows us to fire up a test and wait for its result.\n\n![SenTestingKitAsync call stack](/images/issue-2/SenTestingKitAsync-call-stack@2x.png)\n\nTo give the framework a hint that a test should be treated as asynchronous, the method name has to end with __Async__. Furthermore, in asynchronous tests, we have to report the success of the test case manually and include a timeout, in case the completion block never gets called. We can rewrite our faulty test from above like this:\n\n```objc\n- (void)testAdditionAsync {\n    [Calculator add:2 to:2 block^(int result) {\n        STAssertEquals(result, 4, nil);\n        STSuccess(); // Calling this macro reports success\n    }];\n    STFailAfter(2.0, @\"Timeout\");\n}\n```\n\n## Designing Asynchronous Tests\n\nAs with their synchronous counterparts, asynchronous tests should always be a magnitude simpler than the implementation they are testing. Complex tests don't promote better code quality, because the possibility of bugs in the tests increases. In a test-driven development process, simple tests let us think more clearly about the borders of components, their interfaces, and the expected behavior of the architecture.\n\n\n### Example Project\n\nTo put all this into practice, we create an example framework called [PinacotecaCore][3], which requests information of images from a hypothetical server. It has a resource manager, which is the developer-facing interface, providing a method to get an image object with an image id. In the background, the resource manager fetches the information from the server and updates the properties in the database.\n\nAlthough this is only an example project for the sake of demonstration, it shares the pattern we use in several of our apps.\n\n![PinacotecaCore architecture](/images/issue-2/PinacotecaCore@2x.png)\n\nWith this high level overview of the architecture we can dive into the tests of the framework. In general there are three components which should be tested:\n\n1. the model layer\n2. the server API controller, which abstracts the requests to the server\n3. the resource manager, which manages the core data stack and ties the model layer and the API controller together\n\n### Model Layer\n\nTests should be synchronous whenever possible, and the model layer is a good example. As long as there are no complicated dependencies between different managed object contexts, the test cases should set up their own core data stack with a context on the main thread in which to execute their operations.\n\nIn this example, the [test case][7] sets up the core data stack in `setUp`, checks if the entity description for `PCImage` is present, creates an object with the constructor, and updates its values. As this has nothing to do with asynchronous testing, we won't go into further details here.\n\n### Server API Controller\n\nThe second building block of the architecture is the server API controller. It contains the logic to manage the mapping of the server API to the model and handles the requests. In general, we want to evaluate the behavior of the following method:\n\n```objc\n- [PCServerAPIController fetchImageWithId:queue:completionHandler:]\n```\n\nIt should be called with the id of an image and call the completion handler on the given queue.\n\nBecause the server doesn't exist yet, and because it's a good habit, we will stub the network request with [OHHTTPStubs][8]. With the newest version, the project can contain a bundle with example responses, which will be delivered to the client.\n\nTo stub a request, OHHTTPStubs has to be configured either in our test setup or in the test itself. First we have to load the bundle containing the responses:\n\n```objc\nNSURL *url = [[NSBundle bundleForClass:[self class]]\n                        URLForResource:@\"ServerAPIResponses\"\n                         withExtension:@\"bundle\"];\n                                             \nNSBundle *bundle = [NSBundle url];\n```\n\nThen we can load the response from the bundle and specify for which request it should be returned:\n    \n```objc\nOHHTTPStubsResponse *response;\nresponse = [OHHTTPStubsResponse responseNamed:@\"images/123\"\n                                   fromBundle:responsesBundle\n                                 responseTime:0.1];\n\n[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {\n    return YES /* true, if it's the expected request */;\n} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {\n    return response;\n}];\n```\n\nWith this setup, the simplified version of the [API controller test][9] looks like this:\n\n```objc\n- (void)testFetchImageAsync\n{\n    [self.server\n        fetchImageWithId:@\"123\"\n                   queue:[NSOperationQueue mainQueue]\n       completionHandler:^(id imageData, NSError *error) {\n          STAssertEqualObjects([NSOperationQueue currentQueue], queue, nil);\n          STAssertNil(error, [error localizedDescription]);\n          STAssertTrue([imageData isKindOfClass:[NSDictionary class]], nil);\n                \n          // Check the values of the returned dictionary.\n                \n          STSuccess();\n       }];\n    STFailAfter(2.0, nil);    \n}\n```\n\n### Resource Manager\n\nThe last component is the resource manager, which ties the model layer and the API controller together and manages the core data stack. Here we want to test the method to get an image object:\n\n```objc\n-[PCResourceManager imageWithId:usingManagedObjectContext:queue:updateHandler:]\n```\n\nThis method should return an image object for the given id. If this image is not in the database, it will return a new object containing only the id and call the API controller to request the detailed information. \n\nSince the test of the resource manager should not depend on the API controller, we will stub it with [OCMock][10], which is ideal for partial stubs of methods. This is done in the [resource manager test][11]:\n\n```objc\nOCMockObject *mo;\nmo = [OCMockObject partialMockForObject:self.resourceManager.server];\n\nid exp = [[serverMock expect] \n             andCall:@selector(fetchImageWithId:queue:completionHandler:)\n            onObject:self];\n[exp fetchImageWithId:OCMOCK_ANY queue:OCMOCK_ANY completionHandler:OCMOCK_ANY];\n```\n\nInstead of calling the real method of the API controller, the test will use the method implemented in the test case itself.\n\nWith this in place, the test of the resource manager is straightforward. It calls the manager to get a resource, which internally will call the stubbed method on the API controller. There we can check if the controller is called with the correct parameters. After invoking the result handler, the resource manager updates the model and will call the result handler of our test.\n\n```objc\n- (void)testGetImageAsync\n{\n    NSManagedObjectContext *ctx = self.resourceManager.mainManagedObjectContext;\n    __block PCImage *img;\n    img = [self.resourceManager imageWithId:@\"123\"\n                  usingManagedObjectContext:ctx\n                                      queue:[NSOperationQueue mainQueue]\n                              updateHandler:^(NSError *error) {\n                                       // Check if the error is nil and \n                                       // if the image has been updated.\n                                       STSuccess();\n                                   }];    \n    STAssertNotNil(img, nil);\n    STFailAfter(2.0, @\"Timeout\");\n}\n```\n\n## Conclusion\n\nTesting applications using concurrent design patterns can be challenging at first, but once you understand the differences and establish best practices, it is easy and a lot of fun.\n\nAt [nxtbgthng][1] we are using the process described, [SenTestingKitAsync][2], on a daily basis. But the other approaches, like [Kiwi][5] or [GHUnit][6], are also good ways of doing asynchronous testing. You should try them all, find your preferred tool, and start testing asynchronously.\n\n\n[1]: http://nxtbgthng.com  \"nxtbgthng\"\n[2]: https://github.com/nxtbgthng/SenTestingKitAsync \"SenTestingKitAsync\"\n[3]: https://github.com/objcio/issue-2-async-testing \"Pinacoteca Core: Cocoa Framework for an Imaginary Image Service\"\n[4]: http://www.sente.ch/software/ocunit/ \"OCUnit\"\n[5]: https://github.com/allending/Kiwi \"Kiwi\"\n[6]: https://github.com/gabriel/gh-unit/ \"GHUnit\"\n[7]: https://github.com/objcio/issue-2-async-testing/blob/master/PinacotecaCore/PinacotecaCoreTests/PCModelLayerTests.m \"Pinacoteca Core Model Layer Tests\"\n[8]: https://github.com/AliSoftware/OHHTTPStubs \"OHHTTPStubs\"\n[9]: https://github.com/objcio/issue-2-async-testing/blob/master/PinacotecaCore/PinacotecaCoreTests/PCServerAPIControllerTests.m \"Pinacoteca Core Server API Controller Tests\"\n[10]: http://ocmock.org \"OCMock\"\n[11]: https://github.com/objcio/issue-2-async-testing/blob/master/PinacotecaCore/PinacotecaCoreTests/PCResourceManagerTests.m \"Pinacoteca Core Resource Manager Tests\"\n\n"
  },
  {
    "path": "2013-07-07-common-background-practices.md",
    "content": "---\ntitle:  \"Common Background Practices\"\ncategory: \"2\"\ndate: \"2013-07-07 09:00:00\"\nauthor:\n  - name: Chris Eidhof\n    url: http://twitter.com/chriseidhof\ntags: article\n---\n\n\nIn this article we will describe best practices for doing common\ntasks in the background. We will look at how to use Core Data concurrently,\nhow to draw concurrently, and how to do asynchronous networking. Finally,\nwe'll look at how to process large files asynchronously while keeping a\nlow memory profile. \n\nWith asynchronous programming it is very easy to make mistakes. Therefore, all \nexamples in this article will use a very simple approach. Using \nsimple structures helps us to think through our code and to maintain an overview.\nIf you end up with complicated nested callbacks, you should probably revise some of your design decisions.\n\n## Operation Queues vs. Grand Central Dispatch\n\nCurrently, there are two main modern concurrency APIs available on iOS and OS X:\n[operation queues](http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/NSOperationQueue_class/Reference/Reference.html)\nand [Grand Central Dispatch](https://developer.apple.com/library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html) (GCD). \nGCD is a low-level C API, whereas operation queues are implemented\non top of GCD and provide an Objective-C API. For a more comprehensive\noverview of available concurrency APIs see the [concurrency APIs and challenges][100] \narticle in this issue.\n\nOperation queues offer some useful convenience features not easily \nreproducible with GCD. In practice, one of the most important ones is \nthe possibility to cancel operations in the queue, as we will demonstrate below.\nOperation queues also make it a bit easier to manage dependencies between operations. \nOn the flip side, GCD gives you more control and low-level functionality that\nis not available with operation queues. Please refer to the\n[low level concurrency APIs][300] article for more details.\n\nFurther reading:\n\n* [StackOverflow: NSOperation vs. Grand Central Dispatch](http://stackoverflow.com/questions/10373331/nsoperation-vs-grand-central-dispatch)\n* [Blog: When to use NSOperation vs. GCD](http://eschatologist.net/blog/?p=232)\n\n\n### Core Data in the Background\n\n**Update March 2015**: This is based off of older recommendations in the now outdated Core Data Concurrency Guide.\n\nBefore doing anything concurrent with Core Data, it is important to get\nthe basics right. We strongly recommend reading through Apple's [Concurrency\nwith Core Data](https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreData/Articles/cdConcurrency.html) guide.\nThis document lays down the ground rules, such as never passing managed objects between\nthreads. This doesn't just mean that you should never modify a managed object on another\nthread, but also that you should never read any properties from it. To pass around\nan object, pass its object ID and retrieve the object from the context associated to the\nother thread.\n\nDoing concurrent programming with Core Data is simple when you stick to those rules \nand use the method described in this article.\n\nThe standard setup for Core Data in the Xcode templates is one\npersistent store coordinator with one managed object context that runs\non the main thread. For many use cases, this is just fine.\nCreating some new objects and modifying existing objects is very cheap and can\nbe done on the main thread without problems.\nHowever, if you want to do big chunks of work, then it makes sense to do this \nin a background context. A prime example for this is importing large data sets\ninto Core Data.\n\nOur approach is very simple, and well-covered in existing literature:\n\n1. We create a separate operation for the import work\n2. We create a managed object context with the same persistent store\n   coordinator as the main managed object context\n3. Once the import context saves, we notify the main managed object\n   context and merge the changes\n\nIn the [example application](https://github.com/objcio/issue-2-background-core-data), we will import a big set of transit data for\nthe city of Berlin. \nDuring the import, we show a progress indicator, and we'd like to be able \nto cancel the current\nimport if it's taking too long. Also, we show a table view with all the\ndata available so far, which automatically updates when new data comes in.\nThe example data set is publicly available under the Creative\nCommons license, and you can download it [here](http://stg.daten.berlin.de/datensaetze/vbb-fahrplan-2013). It conforms to the [General Transit\nFeed](https://developers.google.com/transit/gtfs/reference) format, an\nopen standard for transit data. \n\nWe create an `ImportOperation` as a subclass of `NSOperation`, which will handle\nthe import. We override the `main` method, which is\nthe method that will do all the work. Here we create a separate\nmanaged object context with the private queue concurrency type. This means that\nthis context will manage its own queue, and all operations on it\nneed to be performed using `performBlock` or `performBlockAndWait`.\nThis is crucial to make sure that they will be executed on the right thread.\n\n```objc\nNSManagedObjectContext* context = [[NSManagedObjectContext alloc]\n    initWithConcurrencyType:NSPrivateQueueConcurrencyType];\ncontext.persistentStoreCoordinator = self.persistentStoreCoordinator;\ncontext.undoManager = nil;\n[self.context performBlockAndWait:^\n{\n    [self import];\n}];\n```\n\nNote that we reuse the existing persistent store coordinator.\nIn modern code, you should initialize managed object contexts with either\nthe `NSPrivateQueueConcurrencyType` or the `NSMainQueueConcurrencyType`. \nThe third concurrency type constant, `NSConfinementConcurrencyType`, is for \nlegacy code, and our advice is to not use it anymore.\n\nTo do the import, we iterate over the lines in our file and create a\nmanaged object for each line that we can parse:\n\n```objc\n[lines enumerateObjectsUsingBlock:\n  ^(NSString* line, NSUInteger idx, BOOL* shouldStop)\n  {\n      NSArray* components = [line csvComponents];\n      if(components.count < 5) {\n          NSLog(@\"couldn't parse: %@\", components);\n          return;\n      }\n      [Stop importCSVComponents:components intoContext:context];\n  }];\n```\n\nTo start this operation, we perform the following code from our view\ncontroller:\n\n```objc\nImportOperation* operation = [[ImportOperation alloc] \n     initWithStore:self.store fileName:fileName];\n[self.operationQueue addOperation:operation];\n```\n\nFor importing in the background, that's all you have to do. Now, we will add\nsupport for cancelation, and luckily, it's as simple as adding one check\ninside the enumeration block:\n\n```objc\nif(self.isCancelled) {\n    *shouldStop = YES;\n    return;\n}\n```\n\nFinally, to support progress indication, we create a `progressCallback`\nproperty on our operation. It is vital that we update our progress indicator on the main thread, otherwise UIKit will crash. \n\n```objc\noperation.progressCallback = ^(float progress) \n{\n    [[NSOperationQueue mainQueue] addOperationWithBlock:^\n    {\n        self.progressIndicator.progress = progress;\n    }];\n};\n```\n\nTo call the progress block, we add the following line in the enumeration block:\n\n```objc\nself.progressCallback(idx / (float) count);\n```\n\nHowever, if you run this code, you will see that everything slows down\nenormously. Also, it looks like the operation doesn't cancel\nimmediately. The reason for this is that the main operation queue fills up with\nblocks that want to update the progress indicator. A simple solution is to decrease \nthe granularity of updates, i.e. we only call the progress callback for\none percent of the lines imported:\n\n```objc\nNSInteger progressGranularity = lines.count / 100;\n\nif (idx % progressGranularity == 0) {\n    self.progressCallback(idx / (float) count);\n}\n```\n\n### Updating the Main Context\n\nThe table view in our app is backed by a fetched results controller \non the main thread. During and after the import, we'd like\nto show the results of the import in our table view.\n\nThere is one missing piece to make this work; the data imported into the \nbackground context will not propagate to the main context unless we explicitly\ntell it to do so. We add the following line to the `init` method of the `Store` class where we set up the Core Data stack:\n\n```objc\n[[NSNotificationCenter defaultCenter] \n    addObserverForName:NSManagedObjectContextDidSaveNotification\n                object:nil\n                 queue:nil\n            usingBlock:^(NSNotification* note)\n{\n    NSManagedObjectContext *moc = self.mainManagedObjectContext;\n    if (note.object != moc)\n        [moc performBlock:^(){\n            [moc mergeChangesFromContextDidSaveNotification:note];\n        }];\n    }];\n}];\n```\n\nNote that by calling `performBlock:` on the main managed object context, the block will be called on the main thread. \nIf you now start the app, you will notice that the table view reloads \nits data at the end of the import. However, this blocks the user interface \nfor a couple of seconds.\n\nTo fix this, we need to do something that we should have\ndone anyway: save in batches. When doing large imports, you want to\nensure that you save regularly, otherwise you might run out of memory, and \nperformance generally will get worse. Furthermore, saving regularly \nspreads out the work on the main thread to update the table view over time.\n\nHow often you save is a matter of trial and\nerror. Save too often, and you'll spend too much time doing I/O. Save too\nlittle, and the app will become unresponsive. We set the batch\nsize to 250 after trying out some different numbers. Now the import is\nsmooth, updates the table view, and doesn't block the main context for\ntoo long.\n\n\n### Other Considerations\n\nIn the import operation, we read the entire file into a string and then\nsplit that into lines. This will work for relatively small files,\nbut for larger files, it makes sense to lazily read the file line by line. \nThe last example in this article will do exactly that by using input streams.\nThere's also an excellent [write-up on\nStackOverflow](http://stackoverflow.com/questions/3707427/how-to-read-data-from-nsfilehandle-line-by-line/3711079#3711079)\nby Dave DeLong that shows how to do this.\n\nInstead of importing a large data set into core data when the app first runs, \nyou could also ship an sqlite file within your app bundle, or download it \nfrom a server, where you could even generate it dynamically. \nIf your particular use case works \nwith this solution, it will be a lot faster and save processing time on the device.\n\nFinally, there is a lot of noise about child contexts these days. Our\nadvice is not to use them for background operations. If you create a\nbackground context as a child of the main context, saving the background\ncontext [will still block the main thread](http://floriankugler.com/blog/2013/4/29/concurrent-core-data-stack-performance-shootout) a lot. If you create\nthe main context as a child of a background context, you actually don't\ngain anything compared to a more traditional setup with two independent\ncontexts, because you still have to merge the changes from the background to \nthe main context manually. \n\nThe setup with one persistent store coordinator and\ntwo independent contexts is the proven way of doing core data in the background. Stick with it unless you have really good reasons not to.\n\nFurther reading:\n\n* [Core Data Programming Guide: Efficiently importing data](http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/Articles/cdImporting.html)\n* [Core Data Programming Guide: Concurrency with Core Data](http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/Articles/cdConcurrency.html#//apple_ref/doc/uid/TP40003385-SW1j)\n* [StackOverflow: Rules for working with Core Data](http://stackoverflow.com/questions/2138252/core-data-multi-thread-application/2138332#2138332)\n* [WWDC 2012 Video: Core Data Best Practices](https://developer.apple.com/videos/wwdc/2012/?id=214)\n* [Book: Core Data by Marcus Zarra](http://pragprog.com/book/mzcd/core-data)\n\n \n## UI Code in the Background\n\nFirst of all: UIKit only works on the main thread. That said,\nthere are some parts of UI code which are not directly related to UIKit \nand which can take a significant amount of time. These tasks can be moved\nto the background to not block the main thread for too long.\nBut before you start moving parts of your UI code into background queues,\nit's important to measure which part of your code really is the problem.\nThis is vital, otherwise you might be optimizing the wrong thing.\n\nIf you have identified an expensive operation that you can isolate, \nput it in an operation queue:\n\n```objc\n__weak id weakSelf = self;\n[self.operationQueue addOperationWithBlock:^{\n    NSNumber* result = findLargestMersennePrime();\n    [[NSOperationQueue mainQueue] addOperationWithBlock:^{\n        MyClass* strongSelf = weakSelf;\n        strongSelf.textLabel.text = [result stringValue];\n    }];\n}];\n```\n\nAs you can see, this is not completely straightforward; we need to make\na weak reference to self, otherwise we create a retain cycle (the block\nretains self, the private operation queue retains the block, and self\nretains the operation queue). Within the block we convert it\nback to a strong reference to make sure it doesn't get deallocated while\nrunning the block. \n\n### Drawing in the Background\n\nIf your measurements show that `drawRect`: is your performance bottleneck, \nyou can move this drawing code to the background. Before\nyou do that though, check if there are other ways to achieve the same effect, \ne.g. by using core animation layers or pre-rendered images instead of plain \nCore Graphics drawing. See [this\npost](http://floriankugler.com/blog/2013/5/24/layer-trees-vs-flat-drawing-graphics-performance-across-ios-device-generations)\nby Florian for graphic performance measurements on current devices, or\n[this comment](https://lobste.rs/s/ckm4uw/a_performance-minded_take_on_ios_design/comments/itdkfh)\nby Andy Matuschak, a UIKit engineer, to get a good feel for all the\nsubtleties involved.\n\nIf you do decide that your best option is to execute the drawing code\nin the background, the solution is quite simple. Take the code in your\n`drawRect:` method and put it in an operation. Then replace the\noriginal view with an image view that gets updated once the operation has\ncompleted. In your drawing method, use\n`UIGraphicsBeginImageContextWithOptions` instead of `UIGraphicsGetCurrentContext`:\n\n```objc\nUIGraphicsBeginImageContextWithOptions(size, NO, 0);\n// drawing code here\nUIImage *i = UIGraphicsGetImageFromCurrentImageContext();\nUIGraphicsEndImageContext();\nreturn i;\n```\n\nBy passing in 0 as the third parameter, the scale of the device's main screen\nwill be automatically filled in, and the image will look great on both\nretina and non-retina devices.\n\nIf you do custom drawing in table view or collection view cells, it makes sense to put\nall that into operation subclasses. You can add them to a background operation queue, and\ncancel them when the user scrolls cells out of bounds from the `didEndDisplayingCell`\ndelegate method. All of this is explained in detail in [WWDC 2012 Session 211 -- Building Concurrent User Interfaces on iOS](https://developer.apple.com/videos/wwdc/2012/).\n\nInstead of scheduling the drawing code in the background yourself, you should \nalso experiment with the `drawsAsynchronously` property of `CALayer`. However, make sure to measure the effect of this. Sometimes it speeds things up, and sometimes\nit's counterproductive.\n\n\n## Asynchronous Networking\n\nAll your networking should be done asynchronously.\nHowever, with Grand Central Dispatch, you sometimes see code like this:\n\n```objc\n// Warning: please don't use this code.\ndispatch_async(backgroundQueue, ^{\n   NSData* contents = [NSData dataWithContentsOfURL:url]\n   dispatch_async(dispatch_get_main_queue(), ^{\n      // do something with the data.\n   });\n});\n```\n\nThis might look quite smart, but there is a big problem with this code: there\nis no way to cancel this synchronous network call. It will block the\nthread until it's done. In case the operation times out, this might take a\nvery long time (e.g. `dataWithContentsOfURL` has a timeout of 30 seconds). \n\nIf the queue is a serial queue, then it will be blocked for the whole time. If the queue is concurrent, then GCD has to spin up a new thread in order to make up for the thread which you are blocking. Both cases are not good. It's best to avoid blocking altogether.\n\nTo improve upon this situation, we will use the asynchronous methods of \n`NSURLConnection` and wrap everything up in an operation. This way we get the\nfull power and convenience of operation queues; we can easily control\nthe number of concurrent operations, add dependencies, and cancel\noperations. \n\nHowever, there is something to watch out for when doing this: URL\nconnections deliver their events in a run loop. It is easiest to just\nuse the main run loop for this, as the data delivery doesn't take much\ntime. Then we can dispatch the processing of the incoming data onto\na background thread. \n\nAnother possibility is the approach that libraries like [AFNetworking](http://afnetworking.com) take:\ncreate a separate thread, set up a run loop on this thread, and schedule \nthe url connection there. But you probably wouldn't want to do this yourself.\n\nTo kick off the URL connection, we override the `start` method in our custom operation subclass:\n\n```objc\n- (void)start\n{\n    NSURLRequest* request = [NSURLRequest requestWithURL:self.url];\n    self.isExecuting = YES;\n    self.isFinished = NO;\n    [[NSOperationQueue mainQueue] addOperationWithBlock:^\n    {\n        self.connection = [NSURLConnection connectionWithRequest:request\n                                                        delegate:self];\n    }];\n}\n```\n\nSince we overrode the `start` method, we now must manage the \noperation's state properties, `isExecuting` and `isFinished`, ourselves. \nTo cancel an operation, we need to cancel the connection and then set\nthe right flags so the operation queue knows the operation is done.\n\n```objc\n- (void)cancel\n{\n    [super cancel];\n    [self.connection cancel];\n    self.isFinished = YES;\n    self.isExecuting = NO;\n}\n```\n\nWhen the connection finishes loading, it sends a delegate callback:\n\n```objc\n- (void)connectionDidFinishLoading:(NSURLConnection *)connection \n{\n    self.data = self.buffer;\n    self.buffer = nil;\n    self.isExecuting = NO;\n    self.isFinished = YES;\n}\n```\n\nAnd that's all there is to it. Check the [example project on GitHub](https://github.com/objcio/issue-2-background-networking)\nfor the full source code. \nTo conclude, we would like to recommend either taking your time to do this\nright, or to use a library like\n[AFNetworking](http://afnetworking.com). They\nprovide handy utilities like a category on `UIImageView` that asynchronously loads an image from a URL.\nUsing this in your table view code will automatically take care of\ncanceling image loading operations.\n\nFurther reading:\n\n* [Concurrency Programming Guide](http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008091-CH1-SW1)\n* [NSOperation Class Reference: Concurrent vs. Non-Concurrent Operations](http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/NSOperation_class/Reference/Reference.html%23http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/NSOperation_class/Reference/Reference.html%23//apple_ref/doc/uid/TP40004591-RH2-SW15)\n* [Blog: synchronous vs. asynchronous NSURLConnection](http://www.cocoaintheshell.com/2011/04/nsurlconnection-synchronous-asynchronous/)\n* [GitHub: `SDWebImageDownloaderOperation.m`](https://github.com/rs/SDWebImage/blob/master/SDWebImage/SDWebImageDownloaderOperation.m)\n* [Blog: Progressive image download with ImageIO](http://www.cocoaintheshell.com/2011/05/progressive-images-download-imageio/)\n* [WWDC 2012 Session 211: Building Concurrent User Interfaces on iOS](https://developer.apple.com/videos/wwdc/2012/)\n\n\n## Advanced: File I/O in the Background\n\nIn our core data background example, we read the entire file \nthat is to be imported into memory.\nThis works for smaller files, but for larger files this is not\nfeasible, because memory is limited on iOS devices.\nTo resolve this problem, we will build a class that does two things:\nit reads a file line by line without having the entire file in\nmemory, and process the file on a background queue so the app stays\nresponsive.\n\nFor this purpose we use `NSInputStream`, which will let us do asynchronous \nprocessing of a file. As [the documentation](http://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/TechniquesforReadingandWritingCustomFiles/TechniquesforReadingandWritingCustomFiles.html)\nsays: <q>If you always read or write a file’s contents from start to\nfinish, streams provide a simple interface for doing so\nasynchronously.</q>.\n\nWhether you use streams or not, the general pattern for reading a file\nline-by-line is as follows:\n\n1. Have an intermediate buffer that you append to while not finding a newline\n2. Read a chunk from the stream\n3. For each newline found in the chunk, take the intermediate buffer,\n   append data from the stream up to (and including) the newline, and output that\n4. Append the remaining bytes to the intermediate buffer\n5. Go back to 2 until the stream closes\n\nTo put this into practice, we created a [sample application](https://github.com/objcio/issue-2-background-file-io) with a\n`Reader` class that does just this. The interface is very simple:\n\n```objc\n@interface Reader : NSObject\n- (void)enumerateLines:(void (^)(NSString*))block\n            completion:(void (^)())completion;\n- (id)initWithFileAtPath:(NSString*)path;\n@end\n```\n\nNote that this is not a subclass of `NSOperation`. Like URL connections, \ninput streams deliver\ntheir events using a run loop. Therefore, we will use the main run loop \nagain for event delivery, and then dispatch the processing of the data\nonto a background operation queue.\n\n```objc\n- (void)enumerateLines:(void (^)(NSString*))block\n            completion:(void (^)())completion\n{\n    if (self.queue == nil) {\n        self.queue = [[NSOperationQueue alloc] init];\n        self.queue.maxConcurrentOperationCount = 1;\n    }\n    self.callback = block;\n    self.completion = completion;\n    self.inputStream = [NSInputStream inputStreamWithURL:self.fileURL];\n    self.inputStream.delegate = self;\n    [self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]\n                                forMode:NSDefaultRunLoopMode];\n    [self.inputStream open];\n}\n```\n\nNow the input stream will send us delegate messages (on the main\nthread), and we do the processing on the operation\nqueue by adding a block operation:\n\n```objc\n- (void)stream:(NSStream*)stream handleEvent:(NSStreamEvent)eventCode\n{\n    switch (eventCode) {\n        ...\n        case NSStreamEventHasBytesAvailable: {\n            NSMutableData *buffer = [NSMutableData dataWithLength:4 * 1024];\n            NSUInteger length = [self.inputStream read:[buffer mutableBytes] \n                                             maxLength:[buffer length]];\n            if (0 < length) {\n                [buffer setLength:length];\n                __weak id weakSelf = self;\n                [self.queue addOperationWithBlock:^{\n                    [weakSelf processDataChunk:buffer];\n                }];\n            }\n            break;\n        }\n        ...\n    }\n}\n```\n\nProcessing a data chunk looks at the current buffered data and appends\nthe newly streamed chunk. It then breaks that into components, separated\nby newlines, and emits each line. The remainder gets stored again:\n\n```objc\n- (void)processDataChunk:(NSMutableData *)buffer;\n{\n    if (self.remainder != nil) {\n        [self.remainder appendData:buffer];\n    } else {\n        self.remainder = buffer;\n    }\n    [self.remainder obj_enumerateComponentsSeparatedBy:self.delimiter\n                                            usingBlock:^(NSData* component, BOOL last) {\n        if (!last) {\n            [self emitLineWithData:component];\n        } else if (0 < [component length]) {\n            self.remainder = [component mutableCopy];\n        } else {\n            self.remainder = nil;\n        }\n    }];\n}\n```\n\nIf you run the sample app, you will see that the app stays very\nresponsive, and the memory stays very low (in our test runs, the heap\nsize stayed under 800 KB, regardless of the file size). For processing\nlarge files chunk by chunk, this technique is probably what you want.\n\nFurther reading:\n\n* [File System Programming Guide: Techniques for Reading and Writing Files Without File Coordinators](http://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/TechniquesforReadingandWritingCustomFiles/TechniquesforReadingandWritingCustomFiles.html)\n* [StackOverflow: How to read data from NSFileHandle line by line?](http://stackoverflow.com/questions/3707427/how-to-read-data-from-nsfilehandle-line-by-line)\n\n## Conclusion\n\nIn the examples above we demonstrated how to perform common tasks\nasynchronously in the background. In all of these solutions, we tried \nto keep our code simple, because it's very easy to make mistakes with \nconcurrent programming without noticing.\n\nOftentimes you might get away with just doing your work on the main thread,\nand when you can, it'll make your life a lot easier. But if you find performance bottlenecks, put these tasks into the background using the \nsimplest approach possible. \n\nThe pattern we showed in the examples above is a safe choice for other \ntasks as well. Receive events or data on the main queue, then\nuse a background operation queue to perform the actual work before getting \nback onto the main queue to deliver the results.\n\n\n[90]: /issues/2-concurrency/editorial/\n[100]: /issues/2-concurrency/concurrency-apis-and-pitfalls/\n[101]: /issues/2-concurrency/concurrency-apis-and-pitfalls/#challenges\n[102]: /issues/2-concurrency/concurrency-apis-and-pitfalls/#priority_inversion\n[103]: /issues/2-concurrency/concurrency-apis-and-pitfalls/#shared_resources\n[104]: /issues/2-concurrency/concurrency-apis-and-pitfalls/#dead_locks\n[200]: /issues/2-concurrency/common-background-practices/\n[300]: /issues/2-concurrency/low-level-concurrency-apis/\n[301]: /issues/2-concurrency/low-level-concurrency-apis/#async\n[302]: /issues/2-concurrency/low-level-concurrency-apis/#multiple-readers-single-writer\n[400]: /issues/2-concurrency/thread-safe-class-design/\n"
  },
  {
    "path": "2013-07-07-concurrency-apis-and-pitfalls.md",
    "content": "---\ntitle: \"Concurrent Programming: APIs and Challenges\"\ncategory: \"2\"\ndate: \"2013-07-07 10:00:00\"\nauthor:\n  - name: Florian Kugler\n    url: http://twitter.com/floriankugler\ntags: article\n---\n\n\n[Concurrency](http://en.wikipedia.org/wiki/Concurrency_%28computer_science%29) describes the concept of running several tasks at the same time. This can either happen in a [time-shared](http://en.wikipedia.org/wiki/Preemption_%28computing%29) manner on a single CPU core, or truly in parallel if multiple CPU cores are available.\n\nOS X and iOS provide several different APIs to enable concurrent programming. Each of these APIs has different capabilities and limitations, making them suitable for different tasks. They also sit on very different levels of abstraction. We have the possibility to operate very close to the metal, but this also comes with great responsibility to get things right. \n\nConcurrent programming is a very difficult subject with many intricate problems and pitfalls, and it's easy to forget this while using APIs like Grand Central Dispatch or `NSOperationQueue`. This article will first give an overview of the different concurrency APIs on OS X and iOS, and then dive deeper into the inherent challenges of concurrent programming, which are independent of the specific API you use.\n\n\n## Concurrency APIs on OS X and iOS\n\nApple's mobile and desktop operating systems provide the same APIs for concurrent programming. In this article we are going to take a look at `pthread` and `NSThread`, Grand Central Dispatch, `NSOperationQueue`, and `NSRunLoop`. Technically, run loops are the odd ones out in this list, because they don't enable true parallelism. But they are related closely enough to the topic that it's worth having a closer look.\n\nWe'll start with the lower-level APIs and move our way up to the higher-level ones. We chose this route because the higher-level APIs are built on top of the lower-level APIs. However, when choosing an API for your use case, you should consider them in the exact opposite order: choose the highest level abstraction that gets the job done and keep your concurrency model very simple.\n\nIf you're wondering why we are so persistent recommending high-level abstractions and very simple concurrency code, you should read the second part of this article, [challenges of concurrent programming](#challenges-of-concurrent-programming), as well as [Peter Steinberger's thread safety article][400].\n\n\n### Threads\n\n[Threads](http://en.wikipedia.org/wiki/Thread_%28computing%29) are subunits of processes, which can be scheduled independently by the operating system scheduler. Virtually all concurrency APIs are built on top of threads under the hood -- that's true for both Grand Central Dispatch and operation queues.\n\nMultiple threads can be executed at the same time on a single CPU core (or at least perceived as at the same time). The operating system assigns small slices of computing time to each thread, so that it seems to the user as if multiple tasks are executed at the same time. If multiple CPU cores are available, then multiple threads can be executed truly in parallel, therefore lessening the total time needed for a certain workload.\n\nYou can use the [CPU strategy view](http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/AnalysingCPUUsageinYourOSXApp/AnalysingCPUUsageinYourOSXApp.html) in Instruments to get some insight of how your code or the framework code you're using gets scheduled for execution on multiple CPU cores.\n\nThe important thing to keep in mind is that you have no control over where and when your code gets scheduled, and when and for how long its execution will be paused in order for other tasks to take their turn. This kind of thread scheduling is a very powerful technique. However, it also comes with great complexity, which we will investigate later on.\n\nLeaving this complexity aside for a moment, you can either use the [POSIX thread](http://en.wikipedia.org/wiki/POSIX_Threads) API, or the Objective-C wrapper around this API, `NSThread`, to create your own threads. Here's a small sample that finds the minimum and maximum in a set of 1 million numbers using `pthread`. It spawns off 4 threads that run in parallel. It should be obvious from this example why you wouldn't want to use pthreads directly.\n\n```objc\n#import <pthread.h>\n\nstruct threadInfo {\n    uint32_t * inputValues;\n    size_t count;\n};\n\nstruct threadResult {\n    uint32_t min;\n    uint32_t max;\n};\n\nvoid * findMinAndMax(void *arg)\n{\n    struct threadInfo const * const info = (struct threadInfo *) arg;\n    uint32_t min = UINT32_MAX;\n    uint32_t max = 0;\n    for (size_t i = 0; i < info->count; ++i) {\n        uint32_t v = info->inputValues[i];\n        min = MIN(min, v);\n        max = MAX(max, v);\n    }\n    free(arg);\n    struct threadResult * const result = (struct threadResult *) malloc(sizeof(*result));\n    result->min = min;\n    result->max = max;\n    return result;\n}\n\nint main(int argc, const char * argv[])\n{\n    size_t const count = 1000000;\n    uint32_t inputValues[count];\n    \n    // Fill input values with random numbers:\n    for (size_t i = 0; i < count; ++i) {\n        inputValues[i] = arc4random();\n    }\n    \n    // Spawn 4 threads to find the minimum and maximum:\n    size_t const threadCount = 4;\n    pthread_t tid[threadCount];\n    for (size_t i = 0; i < threadCount; ++i) {\n        struct threadInfo * const info = (struct threadInfo *) malloc(sizeof(*info));\n        size_t offset = (count / threadCount) * i;\n        info->inputValues = inputValues + offset;\n        info->count = MIN(count - offset, count / threadCount);\n        int err = pthread_create(tid + i, NULL, &findMinAndMax, info);\n        NSCAssert(err == 0, @\"pthread_create() failed: %d\", err);\n    }\n    // Wait for the threads to exit:\n    struct threadResult * results[threadCount];\n    for (size_t i = 0; i < threadCount; ++i) {\n        int err = pthread_join(tid[i], (void **) &(results[i]));\n        NSCAssert(err == 0, @\"pthread_join() failed: %d\", err);\n    }\n    // Find the min and max:\n    uint32_t min = UINT32_MAX;\n    uint32_t max = 0;\n    for (size_t i = 0; i < threadCount; ++i) {\n        min = MIN(min, results[i]->min);\n        max = MAX(max, results[i]->max);\n        free(results[i]);\n        results[i] = NULL;\n    }\n    \n    NSLog(@\"min = %u\", min);\n    NSLog(@\"max = %u\", max);\n    return 0;\n}\n```\n\n`NSThread` is a simple Objective-C wrapper around pthreads. This makes the code look more familiar in a Cocoa environment. For example, you can define a thread as a subclass of NSThread, which encapsulates the code you want to run in the background. For the previous example, we could define an `NSThread` subclass like this:\n\n```objc\n@interface FindMinMaxThread : NSThread\n@property (nonatomic) NSUInteger min;\n@property (nonatomic) NSUInteger max;\n- (instancetype)initWithNumbers:(NSArray *)numbers;\n@end\n\n@implementation FindMinMaxThread {\n    NSArray *_numbers;\n}\n\n- (instancetype)initWithNumbers:(NSArray *)numbers \n{\n    self = [super init];\n    if (self) {\n        _numbers = numbers;\n    }\n    return self;\n}\n\n- (void)main\n{\n    NSUInteger min;\n    NSUInteger max;\n    // process the data\n    self.min = min;\n    self.max = max;\n}\n@end\n```\n\nTo start new threads, we need to create new thread objects and call their `start` methods:\n\n```objc\nNSMutableSet *threads = [NSMutableSet set];\nNSUInteger numberCount = self.numbers.count;\nNSUInteger threadCount = 4;\nfor (NSUInteger i = 0; i < threadCount; i++) {\n    NSUInteger offset = (numberCount / threadCount) * i;\n    NSUInteger count = MIN(numberCount - offset, numberCount / threadCount);\n    NSRange range = NSMakeRange(offset, count);\n    NSArray *subset = [self.numbers subarrayWithRange:range];\n    FindMinMaxThread *thread = [[FindMinMaxThread alloc] initWithNumbers:subset];\n    [threads addObject:thread];\n    [thread start];\n}\n```\n\nNow we could observe the threads' `isFinished` property to detect when all our newly spawned threads have finished before evaluating the result. We will leave this exercise to the interested reader though. The main point is that working directly with threads, using either the `pthread` or the `NSThread` APIs, is a relatively clunky experience and doesn't fit our mental model of coding very well. \n\nOne problem that can arise from directly using threads is that the number of active threads increases exponentially if both your code and underlying framework code spawn their own threads. This is actually a quite common problem in big projects. For example, if you create eight threads to take advantage of eight CPU cores, and the framework code you call into from these threads does the same (as it doesn't know about the threads you already created), you can quickly end up with dozens or even hundreds of threads. Each part of the code involved acted responsibly in itself; nevertheless, the end result is problematic. Threads don't come for free. Each thread ties up memory and kernel resources.\n\nNext up, we'll discuss two queue-based concurrency APIs: Grand Central Dispatch and operation queues. They alleviate this problem by centrally managing a [thread pool](http://en.wikipedia.org/wiki/Thread_pool_pattern) that everybody uses collaboratively.\n\n\n### Grand Central Dispatch\n\nGrand Central Dispatch (GCD) was introduced in OS X 10.6 and iOS 4 in order to make it easier for developers to take advantage of the increasing numbers of CPU cores in consumer devices. We will go into more detail about GCD in our [article about low-level concurrency APIs][300].\n\nWith GCD you don't interact with threads directly anymore. Instead you add blocks of code to queues, and GCD manages a [thread pool](http://en.wikipedia.org/wiki/Thread_pool_pattern) behind the scenes. GCD decides on which particular thread your code blocks are going to be executed on, and it manages these threads according to the available system resources. This alleviates the problem of too many threads being created, because the threads are now centrally managed and abstracted away from application developers.\n\nThe other important change with GCD is that you as a developer think about work items in a queue rather than threads. This new mental model of concurrency is easier to work with.\n\nGCD exposes five different queues: the main queue running on the main thread, three background queues with different priorities, and one background queue with an even lower priority, which is I/O throttled. Furthermore, you can create custom queues, which can either be serial or concurrent queues. While custom queues are a powerful abstraction, all blocks you schedule on them will ultimately trickle down to one of the system's global queues and its thread pool(s).\n\n![GCD Queues](/images/issue-2/gcd-queues@2x.png)\n\nMaking use of several queues with different priorities sounds pretty straightforward at first. However, we strongly recommend that you use the default priority queue in almost all cases. Scheduling tasks on queues with different priorities can quickly result in unexpected behavior if these tasks access shared resources. This can lead as far as causing your whole program to come to a grinding halt because some low-priority tasks are blocking a high-priority task from executing. You can read more about this phenomenon, called priority inversion, [below](#priority-inversion).\n\nAlthough GCD is a low-level C API, it's pretty straightforward to use. This makes it easy to forget that all caveats and pitfalls of concurrent programming still apply while dispatching blocks onto GCD queues. Please make sure to read about the [challenges of concurrent programming](#challenges-of-concurrent-programming) below, in order to be aware of the potential problems. Furthermore, we have an excellent [walkthrough of the GCD API][300] in this issue that contains many in-depth explanations and valuable hints.\n\n\n### Operation Queues\n\nOperation queues are a Cocoa abstraction of the queue model exposed by GCD. While GCD offers more low-level control, operation queues implement several convenient features on top of it, which often makes it the best and safest choice for application developers.\n\nThe `NSOperationQueue` class has two different types of queues: the main queue and custom queues. The main queue runs on the main thread, and custom queues are processed in the background. In any case, the tasks which are processed by these queues are represented as subclasses of `NSOperation`. \n\nYou can define your own operations in two ways: either by overriding `main`, or by overriding `start`. The former is very simple to do, but gives you less flexibility. In return, the state properties like `isExecuting` and `isFinished` are managed for you, simply by assuming that the operation is finished when `main` returns.\n\n```objc\n@implementation YourOperation\n    - (void)main\n    {\n        // do your work here ...\n    } \n@end\n```\n\nIf you want more control and to maybe execute an asynchronous task within the operation, you can override `start`:\n\n```objc\n@implementation YourOperation\n    - (void)start\n    {\n        self.isExecuting = YES;\n        self.isFinished = NO;\n        // start your work, which calls finished once it's done ...\n    }\n    \n    - (void)finished\n    {\n        self.isExecuting = NO;\n        self.isFinished = YES;\n    }\n@end\n```\n\nNotice that in this case, you have to manage the operation's state manually. In order for an operation queue to be able to pick up a such a change, the state properties have to be implemented in a KVO-compliant way. So make sure to send proper KVO messages in case you don't set them via default accessor methods.\n\nIn order to benefit from the cancelation feature exposed by operation queues, you should regularly check the `isCancelled` property for longer-running operations:\n\n```objc\n- (void)main\n{\n    while (notDone && !self.isCancelled) {\n        // do your processing\n    }\n}\n```\n\nOnce you have defined your operation class, it's very easy to add an operation to a queue:\n\n```objc\nNSOperationQueue *queue = [[NSOperationQueue alloc] init];\nYourOperation *operation = [[YourOperation alloc] init];\n[queue  addOperation:operation];\n```\n\nAlternatively, you can also add blocks to operation queues. This comes in handy, e.g. if you want to schedule one-off tasks on the main queue:\n\n```objc\n[[NSOperationQueue mainQueue] addOperationWithBlock:^{\n    // do something...\n}];\n```\n\nWhile this is a very convenient way of scheduling work onto a queue, defining your own NSOperation subclasses can be very helpful during debugging. If you override the operation's `description` method, you can easily identify all the operations currently scheduled in a certain queue.\n\nBeyond the basics of scheduling operations or blocks, operation queues offer some features which would be non-trivial to get right in GCD. For example, you can easily control how many operations of a certain queue may be executed concurrently with the `maxConcurrentOperationCount` property. Setting it to one gives you a serial queue, which is great for isolation purposes.\n\nAnother convenient feature is the sorting of operations within a queue according to their priorities. This is not the same as GCD's queue priorities. It solely influences the execution order of all operations scheduled in one queue. If you need more control over the sequence of execution beyond the five standard priorities, you can specify dependencies between operations like this:\n\n```objc\n[intermediateOperation addDependency:operation1];\n[intermediateOperation addDependency:operation2];\n[finishedOperation addDependency:intermediateOperation];\n```\n\nThis simple code guarantees that `operation1` and `operation2` will be executed before `intermediateOperation`, which, in turn, will be executed before `finishedOperation`. Operation dependencies are a very powerful mechanism to specify a well-defined execution order. This lets you create things like operation groups, which are guaranteed to be executed before the dependent operation, or serial operations within an otherwise concurrent queue. \n\nBy the very nature of abstractions, operation queues come with a small performance hit compared to using the GCD API. However, in almost all cases, this impact is negligible and operation queues are the tool of choice.\n\n\n### Run Loops\n\nRun loops are not technically a concurrency mechanism like GCD or operation queues, because they don't enable the parallel execution of tasks. However, run loops tie in directly with the execution of tasks on the main dispatch/operation queue and they provide a mechanism to execute code asynchronously.\n\nRun loops can be a lot easier to use than operation queues or GCD, because you don't have to deal with the complexity of concurrency and still get to do things asynchronously.\n\nA run loop is always bound to one particular thread. The main run loop associated with the main thread has a central role in each Cocoa and CocoaTouch application, because it handles UI events, timers, and other kernel events. Whenever you schedule a timer, use a `NSURLConnection`. or call `performSelector:withObject:afterDelay:`, the run loop is used behind the scenes in order to perform these asynchronous tasks.\n\nWhenever you use a method which relies on the run loop, it is important to remember that run loops can be run in different modes. Each mode defines a set of events the run loop is going to react to. This is a clever way to temporarily prioritize certain tasks over others in the main run loop.\n\nA typical example of this is scrolling on iOS. While you're scrolling, the run loop is not running in its default mode, and therefore, it's not going to react to, for example, a timer you have scheduled before. Once scrolling stops, the run loop returns to the default mode and the events which have been queued up are executed. If you want a timer to fire during scrolling, you need to add it to the run loop in the `NSRunLoopCommonModes` mode.\n\nThe main thread always has the main run loop set up and running. Other threads though don't have a run loop configured by default. You can set up a run loop for other threads too, but you will rarely need to do this. Most of the time it is much easier to use the main run loop. If you need to do heavier work that you don't want to execute on the main thread, you can still dispatch it onto another queue after your code is called from the main run loop. Chris has some good examples of this pattern in his article about [common background practices][200].\n\nIf you really need to set up a run loop on another thread, don't forget to add at least one input source to it. If a run loop has no input sources configured, every attempt to run it will exit immediately. \n\n\n## Challenges of Concurrent Programming\n\nWriting concurrent programs comes with many pitfalls. As soon as you're doing more than the most basic things, it becomes difficult to oversee all the different states in the interplay of multiple tasks being executed in parallel. Problems can occur in a non-deterministic way, which makes it even more difficult to debug concurrent code.\n\nThere is a prominent example for unforeseen behavior of concurrent programs: In 1995, NASA sent the Pathfinder mission to Mars. Not too long after a successful landing on our red neighboring planet, the mission almost [came to an abrupt end](http://research.microsoft.com/en-us/um/people/mbj/Mars_Pathfinder/Mars_Pathfinder.html). The Mars rover kept rebooting for unknown reasons -- it suffered from a phenomenon called *priority inversion*, where a low-priority thread kept blocking a high-priority one. We are going to explore this particular issue in more detail below. But this example should demonstrate that even with vast resources and lots of engineering talent available, concurrency can come back to bite you in many ways.\n\n\n### Sharing of Resources\n\nThe root of many concurrency related evils is the access of shared resources from multiple threads. A resource can be a property or an object, memory in general, a network device, a file, etc. Anything you share between multiple threads is a potential point of conflict, and you have to take safety measures to prevent these kind of conflicts.\n\nIn order to demonstrate the problem, let's look at a simple example of a resource in the form of an integer property which you're using as a counter. Let's say we have two threads running in parallel, A and B, and both try to increment the counter at the same time. The problem is that what you write as one statement in C or Objective-C is mostly not just one machine instruction for the CPU. To increment our counter, the current value has to be read from memory. Then the value is incremented by one and finally written back to memory. \n\nImagine the hazards that can happen if both threads try to do this simultaneously. For example, thread A and thread B both read the value of the counter from memory; let's say it is `17`. Then thread A increments the counter by one and writes the resulting `18` back to memory. At the same time, thread B also increments the counter by one and writes a `18` back to memory, just after thread A. At this point the data has become corrupted, because the counter holds an `18` after it was incremented twice from a `17`. \n\n![Race condition](/images/issue-2/race-condition@2x.png)\n\nThis problem is called a [race condition](http://en.wikipedia.org/wiki/Race_conditions#Software) and can always happen if multiple threads access a shared resource without making sure that one thread is finished operating on a resource before another one begins accessing it. If you're not only writing a simple integer but a more complex structure to memory, it might even happen that a second thread tries to read from this memory while you're in the midst of writing it, therefore seeing half new and half old or uninitialized data. In order to prevent this, multiple threads need to access shared resources in a mutually exclusive way.\n\nIn reality, the situation is even more complicated than this, because modern CPUs change the sequence of reads and writes to memory for optimization purposes ([Out-of-order execution](http://en.wikipedia.org/wiki/Out-of-order_execution)). \n\n\n\n### Mutual Exclusion\n\n[Mutual exclusive](http://en.wikipedia.org/wiki/Mutex) access means that only one thread at a time gets access to a certain resource. In order to ensure this, each thread that wants to access a resource first needs to acquire a [*mutex* lock](http://en.wikipedia.org/wiki/Lock_%28computer_science%29) on it. Once it has finished its operation, it releases the lock, so that other threads get a chance to access it. \n\n![Mutex locking](/images/issue-2/locking@2x.png)\n\nIn addition to ensuring mutual exclusive access, locks must also handle the problem caused by out-of-order execution. If you cannot rely on the CPU accessing the memory in the sequence defined by your program instructions, guaranteeing mutually exclusive access alone is not enough. To work around this side effect of CPU optimization strategies, [memory barriers](http://en.wikipedia.org/wiki/Memory_barrier) are used. Setting a memory barrier makes sure that no out-of-order execution takes place across the barrier.\n\nOf course the implementation of a mutex lock in itself needs to be race-condition free. This is a non-trivial undertaking and requires use of special instructions on modern CPUs. You can read more about atomic operations in Daniel's [low-level concurrency techniques][300] article.\n\nObjective-C properties come with language level support for locking in the form of declaring them as atomic. In fact, properties are even atomic by default. Declaring a property as atomic results in implicit locking/unlocking around each access of this property. It might be tempting to just declare all properties as atomic, just in case. However, locking comes at a cost.\n\nAcquiring a lock on a resource always comes with a performance cost. Acquiring and releasing a lock needs to be race-condition free, which is non-trivial on multi-core systems. And when acquiring a lock, the thread might have to wait because some other thread already holds the lock. In this case, that thread will sleep and has to be notified when the other thread relinquishes the lock. All of these operations are expensive and complicated. \n\nThere are different kinds of locks. Some locks are very cheap when there's no lock contention but perform poorly under contention. Other locks are more expensive at a base level, but degrade better under contention ([Lock contention](http://en.wikipedia.org/wiki/Lock_%28computer_science%29#Granularity) is the situation when one or more threads try to take a lock that has already been taken).\n\nThere is a trade-off to be made here: acquiring and releasing locks comes at a price (lock overhead). Therefore you want to make sure you're not constantly entering and exiting [critical sections](http://en.wikipedia.org/wiki/Critical_section) (i.e. acquiring and releasing locks). At the same time, if you acquire a lock for too large of a region of code, you run the risk of lock contention where other threads are often unable to do work because they're waiting to acquire a lock. It's not an easy task to solve.\n\nIt is quite common to see code which is supposed to run concurrently, but which actually results in only one thread being active at a time, because of the way locks for shared resources are set up. It's often non-trivial to predict how your code will get scheduled on multiple cores. You can use Instrument's [CPU strategy view](http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/AnalysingCPUUsageinYourOSXApp/AnalysingCPUUsageinYourOSXApp.html) to get a better idea of whether you're efficiently using the available CPU cores or not.\n\n\n### Dead Locks\n\nMutex locks solve the problem of race conditions, but unfortunately they also introduce a new problem ([amongst others](http://en.wikipedia.org/wiki/Lock_%28computer_science%29#The_problems_with_locks)) at the same time: [dead locks](http://en.wikipedia.org/wiki/Deadlock). A dead lock occurs when multiple threads are waiting on each other to finish and get stuck.\n\n![Dead locks](/images/issue-2/dead-lock@2x.png)\n\nConsider the following example code, which swaps the values of two variables:\n\n```objc\nvoid swap(A, B)\n{\n    lock(lockA);\n    lock(lockB);\n    int a = A;\n    int b = B;\n    A = b;\n    B = a;\n    unlock(lockB);\n    unlock(lockA);\n}\n```\n\nThis works quite well most of the time. But when by chance two threads call it at the same time with opposite variables\n\n```objc\nswap(X, Y); // thread 1\nswap(Y, X); // thread 2\n```\n\nwe can end up in a dead lock. Thread 1 acquires a lock on X, thread 2 acquires a lock on Y. Now they're both waiting for the other lock, but will never be able to acquire it.\n\nAgain, the more resources you share between threads and the more locks you take, the greater your risk of running into a dead lock situation. This is one more reason to keep things as simple as possible and to share as few resources as possible between threads. Make sure to also read the section about [doing things asynchronously][301] in the [low-level concurrency APIs][300] article.\n\n\n### Starvation\n\nJust when you thought that there are enough problems to think of, a new one comes around the corner. Locking shared resources can result in the [readers-writers problem](http://en.wikipedia.org/wiki/Readers-writers_problem). In many cases, it would be wasteful to restrict reading access to a resource to one access at a time. Therefore, taking a reading lock is allowed as long as there is no writing lock on the resource. In this situation, a thread that is waiting to acquire a write lock can be starved by more read locks occurring in the meantime.\n\nIn order to solve this issue, more clever solutions than a simple read/write lock are necessary, e.g. giving [writers preference](http://en.wikipedia.org/wiki/Readers–writer_lock) or using the [read-copy-update](http://en.wikipedia.org/wiki/Read-copy-update) algorithm. Daniel shows in his [low-level concurrency techniques][302] article how to implement a multiple reader/single writer pattern with GCD which doesn't suffer from writer starvation.\n\n\n### Priority Inversion\n\nWe started this section with the example of NASA's Pathfinder rover on Mars suffering from a concurrency problem. Now we will have a closer look why Pathfinder almost failed, and why your application can suffer from the same problem, called [priority inversion](http://en.wikipedia.org/wiki/Priority_inversion). \n\nPriority inversion describes a condition where a lower priority task blocks a higher priority task from executing, effectively inverting task priorities. Since GCD exposes background queues with different priorities, including one which even is I/O throttled, it's good to know about this possibility.\n\nThe problem can occur when you have a high-priority and a low-priority task share a common resource. When the low-priority task takes a lock to the common resource, it is supposed to finish off quickly in order to release its lock and to let the high-priority task execute without significant delays. Since the high-priority task is blocked from running as long as the low-priority task has the lock, there is a window of opportunity for medium-priority tasks to run and to preempt the low-priority task, because the medium-priority tasks have now the highest priority of all currently runnable tasks. At this moment, the medium-priority tasks hinder the low-priority task from releasing its lock, therefore effectively gaining priority over the still waiting, high-priority tasks.\n\n![Priority Inversion](/images/issue-2/priority-inversion@2x.png)\n\nIn your own code, things might not be as dramatic as the rebooting that occurred in the Mars rover, as priority inversion happens quite often in a less severe manner.\n\nIn general, don't use different priorities. Often you will end up with high-priority code waiting on low-priority code to finish. When you're using GCD, always use the default priority queue (directly, or as a target queue). If you're using different priorities, more likely than not, it's actually going to make things worse.\n\nThe lesson from this is that using multiple queues with different priorities sounds good on paper, but it adds even more complexity and unpredictability to concurrent programs. And if you ever run into a weird problem where your high-priority tasks seem to get stuck for no reason, maybe you will remember this article and the problem called priority inversion, which even the NASA engineers encountered.\n\n\n## Conclusion\n\nWe hope to have demonstrated the complexity of concurrent programming and its problems, no matter how straightforward an API may look. The resulting behavior quickly gets very difficult to oversee, and debugging these kind of problems is often very hard.\n\nOn the other hand, concurrency is a powerful tool to take advantage of the computing power of modern multicore CPUs. The key is to keep your concurrency model as simple as possible, so that you can limit the amount of locking necessary. \n\nA safe pattern we recommend is this: pull out the data you want to work on the main thread, then use an operation queue to do the actual work in the background, and finally get back onto the main queue to deliver the result of your background work. This way, you don't need to do any locking yourself, which greatly reduces the chances for mistakes.\n\n\n\n\n[90]: /issues/2-concurrency/editorial/\n[100]: /issues/2-concurrency/concurrency-apis-and-pitfalls/\n[101]: /issues/2-concurrency/concurrency-apis-and-pitfalls/#challenges-of-concurrent-programming\n[102]: /issues/2-concurrency/concurrency-apis-and-pitfalls/#priority-inversion\n[103]: /issues/2-concurrency/concurrency-apis-and-pitfalls/#sharing-of-resources\n[104]: /issues/2-concurrency/concurrency-apis-and-pitfalls/#dead_locks\n[200]: /issues/2-concurrency/common-background-practices/\n[300]: /issues/2-concurrency/low-level-concurrency-apis/\n[301]: /issues/2-concurrency/low-level-concurrency-apis/#async\n[302]: /issues/2-concurrency/low-level-concurrency-apis/#multiple-readers-single-writer\n[400]: /issues/2-concurrency/thread-safe-class-design/\n"
  },
  {
    "path": "2013-07-07-editorial.markdown",
    "content": "---\ntitle:  \"Editorial\"\ncategory: \"2\"\ndate: \"2013-07-07 11:00:00\"\ntags: editorial\n---\n\nWelcome to objc.io issue #2!\n\nFirst of all, we would like to thank everyone for the overwhelming response to our first issue -- we couldn't have hoped for a better start.\n\nIn this second issue we are going to dive deep into the subject of concurrent programming. These days, we have multiple CPU cores at our disposal, even on mobile devices. If we get concurrency right, it can give our applications a big performance boost.\n\nUnfortunately, concurrent programming inherently has many intricate problems and pitfalls, no matter how simple the API makes it seem. We hope that the articles in this issue will give you a deeper understanding of the subject and help you navigate around the many potential pitfalls.\n\nFor this issue we are very glad to have [Peter Steinberger](https://twitter.com/steipete) and [Tobias Kräntzer](http://twitter.com/anagrom_ataf) as guest authors. Peter shares his experiences with regard to concurrent programming from his well known [PSPDFKit](http://pspdfkit.com/) library, and Tobias writes about testing asynchronous code. \n\nIf you have a topic in mind that you would like to contribute to objc.io in the future, please [get it in touch](mailto:mail@objc.io) with us. \n\nLastly, we have good news for all of you who asked for an RSS feed: you can now subscribe to objc.io at [objc.io/feed.xml](/feed.xml).\n\nHappy reading!\n\nChris, Daniel, and Florian.\n"
  },
  {
    "path": "2013-07-07-low-level-concurrency-apis.md",
    "content": "---\ntitle:  \"Low-Level Concurrency APIs\"\ncategory: \"2\"\ndate: \"2013-07-07 8:00:00\"\nauthor:\n  - name: Daniel Eggert\n    url: https://twitter.com/danielboedewadt\ntags: article\n---\n\n\n\nIn this article we'll talk about some low-level APIs available on both iOS and OS X. Except for `dispatch_once`, we generally discourage using any of this.\n\nBut we wanted to show what's available under the covers. These low-level APIs provide a huge amount of flexibility, yet with that flexibility comes a lot of complexity and responsibility. The higher-level APIs and patterns that we mention in our [article about common background practices](/issues/2-concurrency/common-background-practices/) let you focus on your task at hand and save you a lot of trouble. And generally, the higher-level APIs will provide better performance unless you can afford the time and effort to tweak and debug code that uses lower-level APIs.\n\nKnowing how things work further down the software stack is good, though. We hope this article will give you a better understanding of the platform, and at the same time, will make you appreciate the higher-level APIs more.\n\nFirst, we'll go through most of the bits and pieces that make up *Grand Central Dispatch*. It's been around for some years and Apple keeps adding to it and improving it. Apple has open sourced it, which means it's available for other platforms, too. Finally, we'll take a look at [atomic operations](#atomic_operations) -- another set of low-level building blocks.\n\n\nProbably the best book ever written about concurrent programming is *M. Ben-Ari*: \"Principles of Concurrent Programming\", [ISBN 0-13-701078-8](https://en.wikipedia.org/wiki/Special:BookSources/0-13-701078-8). If you're doing anything with concurrent programming, you need to read this. It's more than 30 years old, and is still unsurpassed. Its concise writing, excellent examples, and exercises take you through the fundamental building blocks of concurrent programming. It's out of print, but there are still some copies floating around. There's a new version called \"Principles of Concurrent and Distributed Programming\", [ISBN 0-321-31283-X](https://en.wikipedia.org/wiki/Special:BookSources/0-321-31283-X) that seems to cover much of the same, but I haven't read it myself.\n\n\n\n## Once Upon a Time…\n\nProbably the most widely used and misused feature in GCD is `dispatch_once`. Correctly used, it will look like this:\n\n```objc\n+ (UIColor *)boringColor;\n{\n    static UIColor *color;\n    static dispatch_once_t onceToken;\n    dispatch_once(&onceToken, ^{\n        color = [UIColor colorWithRed:0.380f green:0.376f blue:0.376f alpha:1.000f];\n    });\n    return color;\n}\n```\n\nThe block will only get run once. And during successive calls, the check is very performant. You can use this to initialize global data such as singletons. Beware that using `dispatch_once_t` makes testing very difficult -- singletons and testing don't go well together.\n\nMake sure that the `onceToken` is declared `static` or has global scope. Anything else causes undefined behavior. In other words: Do **not** put a `dispatch_once_t` as a member variable into an object or the like.\n\nBack in ancient times (i.e. a few years ago), people would use `pthread_once`, which you should never use again, as `dispatch_once` is easier to use and less error-prone.\n\n## Delaying / After\n\nAnother common companion is `dispatch_after`. It lets you do work *a little bit later*. It's powerful, but beware: You can easily get into a lot of trouble. Common usage looks like this:\n\n```objc\n- (void)foo\n{\n    double delayInSeconds = 2.0;\n    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t) (delayInSeconds * NSEC_PER_SEC));\n    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){\n        [self bar];\n    });\n}\n```\n\nThis looks awesome at first sight. There are a few drawbacks, though. We can't (directly) cancel the block we're submitting to `dispatch_after`. It will run.\n\nAnother thing to note is there's a problematic tendency where people use `dispatch_after` to work around timing bugs they have in their code. Some code gets run too early and you may not know why, so you put it inside a `dispatch_after`. Now everything works. But some weeks later it stops working and since you have no clear indication of in which order your code runs, debugging turns into a nightmare. Don't do this. Most of the time, you're better of putting your code into the right place. If inside `-viewWillAppear` is too early, perhaps `-viewDidAppear` is the right place.\n\nYou'll save yourself a lot of trouble by creating direct calls (analogous to `-viewDidAppear`) in your own code instead of relying on `dispatch_after`.\n\nIf you need something to run at a specific point in time, `dispatch_after` may be the right thing, though. Be sure to check out `NSTimer`, too. That API is a tiny bit more cumbersome, but it allows you to cancel the firing of the timer.\n\n\n## Queues\n\nOne of the basic building blocks of GCD is queues. Below, we'll give a few examples on how you can put them to use. When using queues, you'll do yourself a favor when giving them a good label. While debugging, this label is displayed within Xcode (and lldb), and will help you understand what your app is up to:\n\n```objc\n- (id)init;\n{\n    self = [super init];\n    if (self != nil) {\n        NSString *label = [NSString stringWithFormat:@\"%@.isolation.%p\", [self class], self];\n        self.isolationQueue = dispatch_queue_create([label UTF8String], NULL);\n        \n        label = [NSString stringWithFormat:@\"%@.work.%p\", [self class], self];\n        self.workQueue = dispatch_queue_create([label UTF8String], NULL);\n    }\n    return self;\n}\n```\n\nQueues can be either *concurrent* or *serial*. By default, they're serial, which means that only a single block runs at any given time. That's how isolation queues work, which we'll get to in a bit. Queues can also be concurrent, which allows multiple blocks to run at the same time.\n\nGCD queues use threads internally. GCD manages these threads, and thus when using GCD you don't need to create threads yourself. But the important takeaway is that GCD presents to you, the user of the API, a very different abstraction level. When you use GCD to do concurrent work, you don't think in terms of threads, but instead in terms of queues and work items (blocks submitted to queues). While down below, there're still threads, GCD's abstraction level lends itself way better to how you're usually writing code.\n\nThe queues and work items also solve a common problem of consecutive fanning out: If we're using threads directly, and want to do something concurrently, we may split our work up into 100 smaller work items, and then create threads according to the number of available CPU cores, let's say eight. We send the work items to those eight threads. As we process those items, we might call some function as part of our work. The person who wrote that function also wanted to use concurrency, and hence also creates eight threads when you call the function. Now you have 8 x 8 = 64 threads, even though you only have eight cores -- i.e. only 12% of the threads can actually run at any point in time while the other 88% are not doing anything. With GCD you don't have this problem, and GCD can even adjust the number of threads when the system turns off cores to save power.\n\nGCD creates a so-called [thread pool](http://en.wikipedia.org/wiki/Thread_pool_pattern) that roughly matches the number of cores. Remember that threads don't come for free. Each thread ties up memory and kernel resources. There's one problem though: If you're submitting a block to GCD, and that code blocks the thread, this thread is no longer available at that point in time to do other work -- it's blocked. In order to keep processing work items (blocks) on the queues, GCD has to create a new thread and add it to the pool. \n\nIf your code is blocking many threads, that can become quite problematic. First off, threads consume resources, but moreover, it's expensive to create them. And it takes some time. And during that time, GCD can't process work items at full speed. There are quite a few things that can cause a thread to block, but the most common are I/O related, i.e. reading and writing from / to a file or the network. You should not do that on a GCD queue in a blocking manner for this very reason. Take a look at the [Input / Output section](#input_output) below for information about how to do I/O in a way that plays nicely with GCD.\n\n\n### Target Queue\n\nYou can set a **target queue** for any queue that you create. This can be very powerful. And it helps debugging.\n\nIt is generally considered good style for a class to create its own queue instead of using a global queue. This way you can set the name of that queue, which eases debugging a lot -- Xcode lets you see the names of all queues in the Debug Navigator, or if you're using `lldb` directly, `(lldb) thread list` will print out the queue names. Once you're using a lot of async stuff, this is very valuable help.\n\nUsing a private queue also enforces encapsulation. It's your queue; you get to decide how to use it.\n\nBy default, a newly created queue forwards into the default priority global queue. We'll talk more about priorities in a bit.\n\nYou can change which queue your queue forwards into -- you can set your queue's target queue. This way you can chain multiple queues together. Your class `Foo` has a queue which forwards into the queue of class `Bar` which forwards into a global queue.\n\nThis can be very useful when you use a queue for isolation (which we'll also talk about). `Foo` has an isolation queue, and by forwarding into `Bar`'s isolation queue, it will automatically be thread-safe with respect to the resources that `Bar`'s isolation queue protects.\n\nMake sure to make your private queue concurrent if you want multiple blocks to run on it. And note that if a queue's target queue is serial (i.e. non-concurrent), it effectively turns into a serial queue as well.\n\n\n### Priorities\n\nYou change the priority of your own queue by setting its target queue to be one of the global queues. But you should refrain from the temptation to do so.\n\nIn most cases, changing the priority is not going to do what you intend. What may seem straightforward is actually a very complex problem. You'll very easily run into what is known as [Priority Inversion](http://en.wikipedia.org/wiki/Priority_inversion). Our [article about concurrency APIs and pitfalls](/issues/2-concurrency/concurrency-apis-and-pitfalls/#challenges) has more info on this problem which almost bricked NASA's Pathfinder rover on Mars.\n\nFurthermore, you need to be particularly careful with the `DISPATCH_QUEUE_PRIORITY_BACKGROUND` queue. Don't use it unless you understand what *throttled I/O* and *background status as per setpriority(2)* mean. Otherwise the system might end up putting your app to a grinding halt. It is mostly intended for doing I/O in a way such that it doesn't interfere with other parts of the system doing I/O. But combined with priority inversion, it can easily become a dangerous cocktail.\n\n\n## Isolation\n\nIsolation queues are one of the most common patterns in GCD queue usage. There are two variations.\n\n### Protecting a Resource\n\nThe most common scenario in multi-threaded programming is that you have a resource that only one thread is allowed to access at a time.\n\nOur [article about concurrency techniques](/issues/2-concurrency/concurrency-apis-and-pitfalls/) talks a bit more about what *resource* means in concurrent programming. It's often a piece of memory or an object that only one thread must access at a time.\n\nLet's say we need to access a `NSMutableDictionary` from multiple threads (queues). We would do something like this:\n\n```objc\n- (void)setCount:(NSUInteger)count forKey:(NSString *)key\n{\n    key = [key copy];\n    dispatch_async(self.isolationQueue, ^(){\n        if (count == 0) {\n            [self.counts removeObjectForKey:key];\n        } else {\n            self.counts[key] = @(count);\n        }\n    });\n}\n\n- (NSUInteger)countForKey:(NSString *)key;\n{\n    __block NSUInteger count;\n    dispatch_sync(self.isolationQueue, ^(){\n        NSNumber *n = self.counts[key];\n        count = [n unsignedIntegerValue];\n    });\n    return count;\n}\n```\n\nWith this, only one thread will access the `NSMutableDictionary` instance.\n\nNote four things:\n\n1. Don't use this code. First read about [multiple readers, single writer](#multiple-readers-single-writer) and also about [contention](#contention).\n\n2. We're using `async` when storing a value. This is important. We don't want to and don't need to block the current thread for the *write* to complete. When reading, we're using `sync` since we need to return the result.\n\n3. According to the method interface, `-setCount:forKey:` takes an `NSString`, which we're passing onto `dispatch_async`. The caller is free to pass in an `NSMutableString` and can modify it after the method returns, but before the block executes. Hence we *have* to copy the string to guarantee that the method works correctly. If the passed-in string isn't mutable (i.e. a normal `NSString`) the call to `-copy` is basically a no-op.\n\n4. The `isolationQueue` needs to have been created with a `dispatch_queue_attr_t` of `DISPATCH_QUEUE_SERIAL` (or `0`).\n\n<a name=\"multiple-readers-single-writer\" id=\"multiple-readers-single-writer\"> </a>\n\n### One Resource, Multiple Readers, and a Single Writer\n\nWe can improve upon the above example. GCD has concurrent queues on which multiple threads can run. We can safely read from the `NSMutableDictionary` on multiple threads as long as we don't mutate it at the same time. When we need to change the dictionary, we dispatch the block with a *barrier*. Such a block runs once all previously scheduled blocks have completed and before any following blocks are run.\n\nWe'll create the queue with:\n\n```objc\nself.isolationQueue = dispatch_queue_create([label UTF8String], DISPATCH_QUEUE_CONCURRENT);\n```\n\nand then change the setter like this:\n\n```objc\n- (void)setCount:(NSUInteger)count forKey:(NSString *)key\n{\n    key = [key copy];\n    dispatch_barrier_async(self.isolationQueue, ^(){\n        if (count == 0) {\n            [self.counts removeObjectForKey:key];\n        } else {\n            self.counts[key] = @(count);\n        }\n    });\n}\n```\n\nWhen you use concurrent queues, make sure that all *barrier* calls are *async*. If you're using `dispatch_barrier_sync` you'll quite likely get yourself (or rather: your code) into a deadlock. Writes *need* a barrier, and *can* be async.\n\n<a name=\"contention\" id=\"contention\"> </a>\n\n### A Word on Contention\n\nFirst off, a word of warning here: The resource we're protecting in this simple example is an `NSMutableDictionary`. This serves the purpose of an example very well. But in real code, it is important to put the isolation at the right complexity level.\n\nIf you're accessing the `NSMutableDictionary` very frequently, you'll be running into what's known as lock contention. Lock contention is in no way specific to GCD or queues. Any form of locking mechanism will have the same problem -- different locking mechanisms in different ways.\n\nAll calls to `dispatch_async`, `dispatch_sync`, etc. need to perform some form of locking -- making sure that only one thread or specific threads run a given block. GCD can avoid locks to some extent and use scheduling instead, but at the end of the day the problem just shifts. The underlying problem remains: if you have a **lot** of threads hitting the same lock or queue at the same time, you'll see performance hits. Performance can degrade severely.\n\nYou need to isolate at the right complexity level. When you see performance degrade, it's a clear sign of a design problem in your code. There are two costs that you need to balance. First is the cost of being inside a critical section for so long that other threads are blocked from entering a critical section, and second is the cost of constantly entering and leaving critical sections. In the world of GCD, the first cost is describing the fact that if a block runs on your isolation queue, it may potentially block other code from running on your isolation queue. The second cost describes the fact that calling `dispatch_async` and `dispatch_sync`, while highly optimized, don't come for free.\n\nSadly, there can be no general rule of what the right balance is: You need to measure and adjust. Spin up Instruments and see what your app is up to.\n\nIf we look at the example code above, our critical code section is only doing very simple things. That may or may not be good, depending on how it's used.\n\nIn your own code, consider if you're better off by protecting with an isolation queue on a higher level. For example, instead of the class `Foo` having an isolation queue and it itself protecting access to its `NSMutableDictionary`, you could have the class `Bar` that uses `Foo` have an isolation queue which protects all use of the class `Foo`. In other words: you would change `Foo` so that it is no longer thread-safe, (no isolation queue) and then inside `Bar`, use an isolation queue to make sure that only one thread is using `Foo` at any point in time.\n\n\n<a name=\"async\" id=\"async\"> </a>\n\n### Going Fully Asynchronous\n\nLet's sidetrack a bit here. As you've seen above, you can dispatch a block, a work unit, both synchronously and asynchronously. A very common problem that we talk about in our [article about concurrency APIs and pitfalls](/issues/2-concurrency/concurrency-apis-and-pitfalls/) is [dead locks](http://en.wikipedia.org/wiki/Deadlock). It's quite easy to run into the problem with GCD with synchronous dispatching. The trivial case is:\n\n```objc\ndispatch_queue_t queueA; // assume we have this\ndispatch_sync(queueA, ^(){\n    dispatch_sync(queueA, ^(){\n        foo();\n    });\n});\n```\n\nOnce we hit the second `dispatch_sync` we'll deadlock: We can't dispatch onto queueA, because someone (the current thread) is already on that queue and is never going to leave it. But there are more subtle ways:\n\n```objc\ndispatch_queue_t queueA; // assume we have this\ndispatch_queue_t queueB; // assume we have this\n\ndispatch_sync(queueA, ^(){\n    foo();\n});\n\nvoid foo(void)\n{\n    dispatch_sync(queueB, ^(){\n        bar();\n    });\n}\n\nvoid bar(void)\n{\n    dispatch_sync(queueA, ^(){\n        baz();\n    });\n}\n```\n\nEach call to `dispatch_sync()` on its own looks good, but in combination, they'll deadlock.\n\nThis is all inherent to being synchronous. If we use asynchronous dispatching such as\n\n```objc\ndispatch_queue_t queueA; // assume we have this\ndispatch_async(queueA, ^(){\n    dispatch_async(queueA, ^(){\n        foo();\n    });\n});\n```\n\nthings will work just fine. *Asynchronous calls will not deadlock*. It is therefore very desirable to use asynchronous calls whenever possible. Instead of writing a function or method that returns a value (and hence has to be synchronous), we use a method that calls a result block asynchronously. That way we're less likely to run into problems with deadlocks.\n\nThe downside of asynchronous calls is that they're difficult to debug. When we stop the code in the debugger, there's no meaningful backtrace to look at.\n\nKeep both of these in mind. Deadlocks are generally the tougher problem to deal with.\n\n\n### How to Write a Good Async API\n\nIf you're designing an API for others to use (or even for yourself), there are a few good practices to keep in mind.\n\nAs we just mentioned, you should prefer asynchronous API. When you create an API, you can get called in ways that are outside your control, and if your code could deadlock, it will.\n\nYou should write your functions or methods so that the function calls `dispatch_async()`. Don't make the caller call it. The caller should be able to call into your function or method.\n\nIf your function or method has a result, deliver it asynchronously through a callback handler. The API should be such that your function or method takes both a result block and a queue to deliver the result on. The caller of your function should not have to dispatch onto the queue themself. The reason for this is quite simple: Almost all the time, the caller needs to be on a certain queue, and this way the code is easier to read. And your function will (should) call `dispatch_async()` anyhow to run the callback handler, so it might as well do that on the queue that the caller needs it to be on.\n\nIf you're writing a class, it is probably a good option to let the user of the class set a queue that all callbacks will be delivered on. Your code would look like this:\n\n```objc\n- (void)processImage:(UIImage *)image completionHandler:(void(^)(BOOL success))handler;\n{\n    dispatch_async(self.isolationQueue, ^(void){\n        // do actual processing here\n        dispatch_async(self.resultQueue, ^(void){\n            handler(YES);\n        });\n    });\n}\n```\n\nIf you write your classes this way, it's easy to make classes work together. If class A uses class B, it will set the callback queue of B to be its own isolation queue.\n\n\n## Iterative Execution\n\nIf you're doing some number crunching and the problem at hand can be dissected in smaller parts of identical nature, `dispatch_apply` can be very useful.\n\nIf you have some code like this\n\n```objc\nfor (size_t y = 0; y < height; ++y) {\n    for (size_t x = 0; x < width; ++x) {\n        // Do something with x and y here\n    }\n}\n```\n\nyou may be able to speed it up by simply changing it to\n\n```objc\ndispatch_apply(height, dispatch_get_global_queue(0, 0), ^(size_t y) {\n    for (size_t x = 0; x < width; x += 2) {\n        // Do something with x and y here\n    }\n});\n```\n\nHow well this works depends a lot on exactly what you're doing inside that loop.\n\nThe work done by the block must be non trivial, otherwise the overhead is too big. Unless the code is bound by computational bandwidth, it is critical that the memory that each work unit needs to read from and write to fits nicely into the cache size. This can have dramatic effects on performance. Code bound by critical sections may not perform well at all. Going into detail about these problems is outside the scope of this article. Using `dispatch_apply` may help performance, yet performance optimization is a complex topic on its own. Wikipedia has an article about [Memory-bound function](https://en.wikipedia.org/wiki/Memory_bound). Memory access speed changes dramatically between L2, L3, and main memory. Seeing a performance drop of 10x is not uncommon when your data access pattern doesn’t fit within the cache size.\n\n\n## Groups\n\nQuite often, you'll find yourself chaining asynchronous blocks together to perform a given task. Some of these may even run in parallel. Now, if you want to run some code once this task is complete, i.e. all blocks have completed, \"groups\" are the right tool. Here's a sample:\n\n```objc\ndispatch_group_t group = dispatch_group_create();\n\ndispatch_queue_t queue = dispatch_get_global_queue(0, 0);\ndispatch_group_async(group, queue, ^(){\n    // Do something that takes a while\n    [self doSomeFoo];\n    dispatch_group_async(group, dispatch_get_main_queue(), ^(){\n        self.foo = 42;\n    });\n});\ndispatch_group_async(group, queue, ^(){\n    // Do something else that takes a while\n    [self doSomeBar];\n    dispatch_group_async(group, dispatch_get_main_queue(), ^(){\n        self.bar = 1;\n    });\n});\n\n// This block will run once everything above is done:\ndispatch_group_notify(group, dispatch_get_main_queue(), ^(){\n    NSLog(@\"foo: %d\", self.foo);\n    NSLog(@\"bar: %d\", self.bar);\n});\n```\n\nThe important thing to note is that all of this is entirely non-blocking. At no point are we telling the current thread to wait until something else is done. Quite contrary, we're simply enqueuing multiple blocks. Since this code pattern doesn't block, it's not going to cause a deadlock.\n\nAlso note how we're switching between different queues in this small and simple example.\n\n\n\n### Using dispatch\\_group\\_t with Existing API\n\nOnce you've added groups to your tool belt, you'll be wondering why most async API doesn't take a `dispatch_group_t` as an optional argument. But there's no reason to despair: It's easy to add that yourself, although you have to be more careful to make sure your code is *balanced*.\n\nWe can, for example, add it to Core Data's `-performBlock:` API like this:\n\n```objc\n- (void)withGroup:(dispatch_group_t)group performBlock:(dispatch_block_t)block\n{\n    if (group == NULL) {\n        [self performBlock:block];\n    } else {\n        dispatch_group_enter(group);\n        [self performBlock:^(){\n            block();\n            dispatch_group_leave(group);\n        }];\n    }\n}\n```\n\nThis allows us to use `dispatch_group_notify` to run a block when a set of operations on Core Data (possibly combined with other blocks) completes.\n\nWe can obviously do the same for `NSURLConnection`:\n\n```objc\n+ (void)withGroup:(dispatch_group_t)group \n        sendAsynchronousRequest:(NSURLRequest *)request \n        queue:(NSOperationQueue *)queue \n        completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler\n{\n    if (group == NULL) {\n        [self sendAsynchronousRequest:request \n                                queue:queue \n                    completionHandler:handler];\n    } else {\n        dispatch_group_enter(group);\n        [self sendAsynchronousRequest:request \n                                queue:queue \n                    completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){\n            handler(response, data, error);\n            dispatch_group_leave(group);\n        }];\n    }\n}\n```\n\nIn order for this to work, you need to make sure that\n\n* `dispatch_group_enter()` is guaranteed to run before `dispatch_group_leave()`\n* Calls to `dispatch_group_enter()` and `dispatch_group_leave()` are always balanced (even when errors happen)\n\n\n## Sources\n\nOne of the lesser-known features of GCD is the event sources `dispatch_source_t`.\n\nJust like most of GCD, this is pretty low-level stuff. When you need it, it can be extremely useful, though. Some of it is extremely esoteric, and we'll just touch upon a few of the uses. A lot of this isn't very useful on iOS where you can't launch processes (hence no point in watching them) and you can't write outside your app bundle (hence no need to watch files), etc.\n\nGCD sources are implemented in an extremely resource-efficient way.\n\n### Watching Processes\n\nIf some process is running and you want to know when it exits, GCD has you covered. You can also use it to check when that process forks, i.e. spawns child processes or a signal was delivered to the process (e.g. `SIGTERM`).\n\n```objc\n@import AppKit;\n// ...\nNSArray *array = [NSRunningApplication \n  runningApplicationsWithBundleIdentifier:@\"com.apple.mail\"];\nif (array == nil || [array count] == 0) {\n    return;\n}\npid_t const pid = [[array firstObject] processIdentifier];\nself.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, \n  DISPATCH_PROC_EXIT, DISPATCH_TARGET_QUEUE_DEFAULT);\ndispatch_source_set_event_handler(self.source, ^(){\n    NSLog(@\"Mail quit.\");\n    // If you would like continue watching for the app to quit,\n    // you should cancel this source with dispatch_source_cancel and create new one\n    // as with next run app will have another process identifier.\n});\ndispatch_resume(self.source);\n```\n\nThis will print **Mail quit.** when the Mail.app exits.\n\nNote that you must call `dispatch_resume()` before any events will be delivered to your event handler.\n\n\n<a name=\"watching_files\" id=\"watching_files\"> </a>\n\n### Watching Files\n\nThe possibilities seem near endless. You can watch a file directly for changes, and the source's event handler will get called when a change happens.\n\nYou can also use this to watch a directory, i.e. create a *watch folder*:\n\n\n```objc\nNSURL *directoryURL; // assume this is set to a directory\nint const fd = open([[directoryURL path] fileSystemRepresentation], O_EVTONLY);\nif (fd < 0) {\n    char buffer[80];\n    strerror_r(errno, buffer, sizeof(buffer));\n    NSLog(@\"Unable to open \\\"%@\\\": %s (%d)\", [directoryURL path], buffer, errno);\n    return;\n}\ndispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, fd, \n  DISPATCH_VNODE_WRITE | DISPATCH_VNODE_DELETE, DISPATCH_TARGET_QUEUE_DEFAULT);\ndispatch_source_set_event_handler(source, ^(){\n    unsigned long const data = dispatch_source_get_data(source);\n    if (data & DISPATCH_VNODE_WRITE) {\n        NSLog(@\"The directory changed.\");\n    }\n    if (data & DISPATCH_VNODE_DELETE) {\n        NSLog(@\"The directory has been deleted.\");\n    }\n});\ndispatch_source_set_cancel_handler(source, ^(){\n    close(fd);\n});\nself.source = source;\ndispatch_resume(self.source);\n```\n\nYou should probably always add `DISPATCH_VNODE_DELETE` to check if the file or directory has been deleted -- and then stop monitoring it.\n\n\n### Timers\n\nIn most cases, `NSTimer` is your go-to place for timer events. GCD's version is lower-level. It gives you more control -- use that carefully.\n\nIt is extremely important to point out that specifying a low *leeway* value for GCD timers interferes with the OS's attempt to conserve power. You'll be burning more battery if you unnecessarily specify a low leeway value.\n\nHere we're setting up a timer to fire every 5 seconds and allow for a leeway of 1/10 of a second:\n\n```objc\ndispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, \n  0, 0, DISPATCH_TARGET_QUEUE_DEFAULT);\ndispatch_source_set_event_handler(source, ^(){\n    NSLog(@\"Time flies.\");\n});\ndispatch_source_set_timer(source, DISPATCH_TIME_NOW, 5ull * NSEC_PER_SEC, \n  100ull * NSEC_PER_MSEC);\nself.source = source;\ndispatch_resume(self.source);\n```\n\n### Canceling\n\nAll sources allow you to add a *cancel handler*. This can be useful to clean up any resources you've created for the event source, such as closing file descriptors. GCD guarantees that all calls to the event handler have completed before the cancel handler gets called.\n\nSee the use of `dispatch_source_set_cancel_handler()` in the above [example for a watch folder](#watching_files).\n\n\n<a name=\"input_output\" id=\"input_output\"> </a>\n\n## Input / Output\n\nWriting code that performs well while doing heavy I/O is extremely tricky. GCD has a few tricks up its sleeve to help. Without going into too much detail, we'll shortly touch upon what these problems are, and how GCD approaches them. \n\nTraditionally when you were reading data from a network socket, you'd either have to do a blocking read, i.e. parking a thread until data became available, or you'd have to repeatedly poll. Both approaches are wasteful and don't scale. However, `kqueue` solved the polling by posting an event once data became available, and GCD uses the same approach, although more elegantly. When writing data to a socket, the identical problems exist, where you have to either perform a blocking write, or wait for the socket to be able to accept data.\n\nThe second problem when performing I/O is that data arrives in small chunks; when reading from a network, the chunk size is typically around 1.5k bytes due to the MTU -- [maximum transmission unit](https://en.wikipedia.org/wiki/Maximum_transmission_unit). This can be anything, though. Once this data arrives, you're often interested in data that spans multiple chunks, and traditionally you would concatenate the data into one larger buffer and then process that. Let's say (contrived example), you receive these eight chunks\n\n```\n0: HTTP/1.1 200 OK\\r\\nDate: Mon, 23 May 2005 22:38\n1: :34 GMT\\r\\nServer: Apache/1.3.3.7 (Unix) (Red-H\n2: at/Linux)\\r\\nLast-Modified: Wed, 08 Jan 2003 23\n3: :11:55 GMT\\r\\nEtag: \"3f80f-1b6-3e1cb03b\"\\r\\nCon\n4: tent-Type: text/html; charset=UTF-8\\r\\nContent-\n5: Length: 131\\r\\nConnection: close\\r\\n\\r\\n<html>\\r\n6: \\n<head>\\r\\n  <title>An Example Page</title>\\r\\n\n7: </head>\\r\\n<body>\\r\\n  Hello World, this is a ve\n```\n\nIf you were looking for an HTTP header, it'd be much simpler to concatenate all data chunks into a larger buffer and then scan for `\\r\\n\\r\\n`. But in doing so, you'd be copying around data a lot. The other problem with a lot of the *old* C APIs is that there's no ownership of buffers so that functions had to copy data into their own buffers -- another copy. Copying data may seem trivial, but when you're doing a lot of I/O you'll see those copies show up in your profiling tool (Instruments). Even if you only copy each memory region once, you're incurring twice the memory bandwidth and you're burning through twice the amount of memory cache.\n\n### GCD and Buffers\n\nThe more straightforward piece is about data buffers. GCD has a `dispatch_data_t` type that to some extent is similar to what `NSData` does for Objective-C. It can do other things, though, and is more generic.\n\nNote that `dispatch_data_t` can be retained and released, and the `dispatch_data_t` object *owns* the buffer it holds onto.\n\nThis may seem trivial, but we have to remember that GCD is a plain C API, and can't use Objective-C. The traditional way was to have a buffer either backed by the stack or a `malloc`'d memory region -- these don't have ownership.\n\nOne relatively unique property of `dispatch_data_t` is that it can be backed by disjoint memory regions. That solves the concatenation problem we just mentioned. When you concatenate two data objects with\n\n```objc\ndispatch_data_t a; // Assume this hold some valid data\ndispatch_data_t b; // Assume this hold some valid data\ndispatch_data_t c = dispatch_data_create_concat(a, b);\n```\n\nthe data object `c` will *not* copy `a` and `b` into a single, larger memory region. Instead it will simply retain both `a` and `b`. You can then traverse the memory regions represented by `c` with `dispatch_data_apply`:\n    \n```objc\ndispatch_data_apply(c, ^bool(dispatch_data_t region, size_t offset, const void *buffer, size_t size) {\n    fprintf(stderr, \"region with offset %zu, size %zu\\n\", offset, size);\n    return true;\n});\n```\n\nSimilarly you can create a subrange with `dispatch_data_create_subrange` that won't do any copying.\n\n\n### Reading and Writing\n\nAt its core, *Dispatch I/O* is about so-called *channels*. A dispatch I/O channel provides a different way to read and write from a file descriptor. The most basic way to create such a channel is by calling\n\n```objc\ndispatch_io_t dispatch_io_create(dispatch_io_type_t type, dispatch_fd_t fd, \n  dispatch_queue_t queue, void (^cleanup_handler)(int error));\n```\n\nThis returns the created channel which then *owns* the file descriptor. You must not modify the file descriptor in any way after you've created a channel from it.\n\nThere are two fundamentally different *types* of channels: streams and random access. If you open a file on disk, you can use it to create a random access channel (because such a file descriptor is `seek`able). If you open a socket, you can create a stream channel.\n\nIf you want to create a channel for a file, you're better off using `dispatch_io_create_with_path`, which takes a path, and lets GCD open the file. This is beneficial, since GCD can postpone opening the file -- hence limiting the number of files that are open at the same time.\n\nAnalogous to the normal `read(2)`, `write(2)`, and `close(2)`, GCD offers `dispatch_io_read`, `dispatch_io_write`, and `dispatch_io_close`. Reading and writing is done via a callback block that is called whenever data is read or written. This effectively implements non-blocking, fully async I/O.\n\nWe can't go into all details here, but here's an example for setting up a TCP server:\n\nFirst we create a listening socket and set up an event source for incoming connections:\n\n```objc\n_isolation = dispatch_queue_create([[self description] UTF8String], NULL);\n_nativeSocket = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);\nstruct sockaddr_in sin = {};\nsin.sin_len = sizeof(sin);\nsin.sin_family = AF_INET6;\nsin.sin_port = htons(port);\nsin.sin_addr.s_addr= INADDR_ANY;\nint err = bind(result.nativeSocket, (struct sockaddr *) &sin, sizeof(sin));\nNSCAssert(0 <= err, @\"\");\n\n_eventSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, _nativeSocket, 0, _isolation);\ndispatch_source_set_event_handler(result.eventSource, ^{\n    acceptConnection(_nativeSocket);\n});\n```\n\nWhen accepting a connection, we create an I/O channel:\n\n\n```objc\ntypedef union socketAddress {\n    struct sockaddr sa;\n    struct sockaddr_in sin;\n    struct sockaddr_in6 sin6;\n} socketAddressUnion;\n\nsocketAddressUnion rsa; // remote socket address\nsocklen_t len = sizeof(rsa);\nint native = accept(nativeSocket, &rsa.sa, &len);\nif (native == -1) {\n    // Error. Ignore.\n    return nil;\n}\n\n_remoteAddress = rsa;\n_isolation = dispatch_queue_create([[self description] UTF8String], NULL);\n_channel = dispatch_io_create(DISPATCH_IO_STREAM, native, _isolation, ^(int error) {\n    NSLog(@\"An error occured while listening on socket: %d\", error);\n});\n\n//dispatch_io_set_high_water(_channel, 8 * 1024);\ndispatch_io_set_low_water(_channel, 1);\ndispatch_io_set_interval(_channel, NSEC_PER_MSEC * 10, DISPATCH_IO_STRICT_INTERVAL);\n\nsocketAddressUnion lsa; // remote socket address\nsocklen_t len = sizeof(rsa);\ngetsockname(native, &lsa.sa, &len);\n_localAddress = lsa;\n```\n\nIf we want to set `SO_KEEPALIVE` (if we're using HTTP-level keep-alive), we need to do so before calling `dispatch_io_create`.\n\nHaving created a `dispatch_io` channel, we can set up the read handler:\n\n```objc\ndispatch_io_read(_channel, 0, SIZE_MAX, _isolation, ^(bool done, dispatch_data_t data, int error){\n    if (data != NULL) {\n        if (_data == NULL) {\n            _data = data;\n        } else {\n            _data = dispatch_data_create_concat(_data, data);\n        }\n        [self processData];\n    }\n});\n```\n\nIf all you want to do is to read from or write to a file, GCD provides two convenience wrappers: `dispatch_read` and `dispatch_write`. You pass `dispatch_read` a file path and a block to be called for each data block that's read. Similarly, `dispatch_write` takes a file path and a `dispatch_data_t` object to be written.\n\n\n## Benchmarking\n\nIn the obscure corners of GCD, you'll find a neat little tool for optimizing your code:\n\n```objc\nuint64_t dispatch_benchmark(size_t count, void (^block)(void));\n```\n\nPut this declaration into your code, and you can measure the average number of nanoseconds the given block takes to execute. E.g.\n\n```objc\nsize_t const objectCount = 1000;\nuint64_t n = dispatch_benchmark(10000, ^{\n    @autoreleasepool {\n        id obj = @42;\n        NSMutableArray *array = [NSMutableArray array];\n        for (size_t i = 0; i < objectCount; ++i) {\n            [array addObject:obj];\n        }\n    }\n});\nNSLog(@\"-[NSMutableArray addObject:] : %llu ns\", n);\n```\n\nOn my machine this outputs\n\n```objc\n-[NSMutableArray addObject:] : 31803 ns\n```\n\ni.e. that adding 1000 objects to an NSMutableArray takes 31803 ns, or about 32 ns per object.\n\nAs the [man page](http://opensource.apple.com/source/libdispatch/libdispatch-84.5/man/dispatch_benchmark.3) for `dispatch_benchmark` points out, measuring performance isn't as trivial as it may seem. Particularly when comparing concurrent code with non-concurrent, you need to pay attention to computational bandwidth and memory bandwidth of the particular piece of hardware you're running on. It will change a lot from machine to machine. And the contention problem we mentioned above will affect code if its performance is bound by access to critical sections.\n\nDon't put this into shipping code. Aside from the fact that it'd be pretty pointless to do so, it's also private API. It's intended for debugging and performance analysis work only.\n\nView the man page with\n\n```objc\ncurl \"http://opensource.apple.com/source/libdispatch/libdispatch-84.5/man/dispatch_benchmark.3?txt\" \n  | /usr/bin/groffer --tty -T utf8\n```\n\n<a name=\"atomic_operations\" id=\"atomic_operations\"> </a>\n\n## Atomic Operations\n\nThe header file `libkern/OSAtomic.h` has lots of powerful functions for lower-level multi-threaded programming. Although it's part of the kernel header files, it's intended to also be used outside kernel and driver programming.\n\n\nThese functions are lower-level, and there are a few extra things you need to be aware of. If you do, though, you might find a thing or two here, that you'd otherwise not be able to do -- or not do as easily. It's mostly interesting if you're working on high-performance code and / or are implementing lock-free and wait-free algorithms.\n\nThese functions are all summarized in the `atomic(3)` man page -- run `man 3 atomic` to get the complete documentation. You'll see it talking about memory barriers. Check out the [Wikipedia article about memory barriers](https://en.wikipedia.org/wiki/Memory_barrier). If you're in doubt, you probably need a memory barrier.\n\n### Counters\n\nThere's a long list of `OSAtomicIncrement` and `OSAtomicDecrement` functions that allow you to increment and decrement an integer value in an atomic way -- thread safe without having to take a lock (or use queues). These can be useful if you need to increment global counters from multiple threads for statistics. If all you do is increment a global counter, the barrier-free `OSAtomicIncrement` versions are fine, and when there's no contention, they're cheap to call.\n\nSimilarly, the `OSAtomicOr`, `OSAtomicAnd`, and `OSAtomicXor` functions can be used to perform logical operations, and `OSAtomicTest` functions to set or clear bits.\n\n### Compare and Swap\n\nThe `OSAtomicCompareAndSwap` can be useful to do lock-free lazy initializations like this:\n\n```objc\nvoid * sharedBuffer(void)\n{\n    static void * buffer;\n    if (buffer == NULL) {\n        void * newBuffer = calloc(1, 1024);\n        if (!OSAtomicCompareAndSwapPtrBarrier(NULL, newBuffer, &buffer)) {\n            free(newBuffer);\n        }\n    }\n    return buffer;\n}\n```\n\nIf there's no buffer, we create one and then atomically write it to `buffer` if `buffer` is NULL. In the rare case that someone else set `buffer` at the same time as the current thread, we simply free it. Since the compare-and-swap method is atomic, this is a thread-safe way to lazily initialize values. The check that `NULL` and setting `buffer` are done atomically.\n\nObviously, you can do something similar with `dispatch_once()`.\n\n### Atomic Queues\n\nThe `OSAtomicEnqueue()` and `OSAtomicDequeue()` let you implement LIFO queues (also known as stacks) in a thread-safe, lock-free way. For code that has strict latency requirements, this can be a powerful building block.\n\nThere's also a `OSAtomicFifoEnqueue()` and `OSAtomicFifoDequeue()` for FIFO queues, but these only have documentation in the header file -- read carefully when using.\n\n### Spin Locks\n\nFinally, the `OSAtomic.h` header defines the functions to work with spin locks: `OSSpinLock`. Again, Wikipedia has [in-depth information on spin locks](https://en.wikipedia.org/wiki/Spinlock). Check out the `spinlock(3)` man page with `man 3 spinlock`. The short story is that a spin lock is very cheap when there's no lock contention.\n\nSpin locks can be useful in certain situations as a performance optimization. As always: measure first, then optimize. Don't do optimistic optimizations.\n\nHere's an example for OSSpinLock:\n\n\n```objc\n@interface MyTableViewCell : UITableViewCell\n\n@property (readonly, nonatomic, copy) NSDictionary *amountAttributes;\n\n@end\n\n\n\n@implementation MyTableViewCell\n{\n    NSDictionary *_amountAttributes;\n}\n\n- (NSDictionary *)amountAttributes;\n{\n    if (_amountAttributes == nil) {\n        static __weak NSDictionary *cachedAttributes = nil;\n        static OSSpinLock lock = OS_SPINLOCK_INIT;\n        OSSpinLockLock(&lock);\n        _amountAttributes = cachedAttributes;\n        if (_amountAttributes == nil) {\n            NSMutableDictionary *attributes = [[self subtitleAttributes] mutableCopy];\n            attributes[NSFontAttributeName] = [UIFont fontWithName:@\"ComicSans\" size:36];\n            attributes[NSParagraphStyleAttributeName] = [NSParagraphStyle defaultParagraphStyle];\n            _amountAttributes = [attributes copy];\n            cachedAttributes = _amountAttributes;\n        }\n        OSSpinLockUnlock(&lock);\n    }\n    return _amountAttributes;\n}\n```\n\nIn the above example, it's probably not worth the trouble, but it shows the concept. We're using ARC's `__weak` to make sure that the `amountAttributes` will get `dealloc`'ed once all instances of `MyTableViewCell` go away. Yet we're able to share a single instance of this dictionary among all instances.\n\nThe reason this performs well is that we're unlikely to hit the inner-most part of the method. This is quite esoteric -- don't use this in your App unless you have a real need.\n"
  },
  {
    "path": "2013-07-07-thread-safe-class-design.md",
    "content": "---\ntitle:  \"Thread-Safe Class Design\"\ncategory: \"2\"\ndate: \"2013-07-07 07:00:00\"\nauthor:\n  - name: Peter Steinberger\n    url: https://twitter.com/steipete\ntags: article\n---\n\n\n\nThis article will focus on *practical* tips, design patterns, and anti-patterns with regard to writing thread-safe classes and using Grand Central Dispatch (GCD).\n\n\n## Thread Safety\n\n### Apple's Frameworks\n\nFirst, let's have a look at Apple's frameworks. In general, unless declared otherwise, most classes are not thread-safe by default. For some this is expected; for others it's quite interesting. \n\nOne of the most common mistakes even experienced iOS/Mac developers make is accessing parts of UIKit/AppKit on background threads. It's very easy to make the mistake of setting properties like `image` from a background thread, because their content is being requested from the network in the background anyway. Apple's code is performance-optimized and will not warn you if you change properties from different threads. \n\nIn the case of an image, a common symptom is that your change is picked up with a delay. But if two threads set the image at the same time, it's likely that your app will simply crash, because the currently set image could be released twice. Since this is timing dependent, it usually will crash when used by your customers and not during development. \n\nThere are no *official* tools to find such errors, but there are some tricks that will do the job just fine. The [UIKit Main Thread Guard](https://gist.github.com/steipete/5664345) is a small source file that will patch any calls to UIView's `setNeedsLayout` and `setNeedsDisplay` and check for being executed on the main thread before forwarding the call. Since these two methods are called for a lot of UIKit setters (including image), this will catch many thread-related mistakes. Although this trick does not use private API, we don't recommend using this in production apps -- it's great during development though.\n\nIt's a conscious design decision from Apple's side to not have UIKit be thread-safe. Making it thread-safe wouldn't buy you much in terms of performance; it would in fact make many things slower. And the fact that UIKit is tied to the main thread makes it very easy to write concurrent programs and use UIKit. All you have to do is make sure that calls into UIKit are always made on the main thread.\n\n\n#### Why Isn't UIKit Thread Safe?\n\nEnsuring thread safety for a big framework like UIKit would be a major undertaking and would come at a great cost. Changing non-atomic to atomic properties would only be a tiny part of the changes required. Usually you want to change several properties at once, and only then see the changed result. For this, Apple would have to expose a method much like CoreData's `performBlock:` and `performBlockAndWait:` to synchronize changes. And if you consider that most calls to UIKit classes are about *configuration*, it's even more pointless to make them thread-safe.\n\nHowever, even calls that are not about configuration shared internal state and thus weren't thread-safe. If you already wrote apps back in the dark ages of iOS3.2 and before, you surely experienced random crashes when using NSString's `drawInRect:withFont:` while preparing background images. Thankfully, with iOS4, [Apple made most drawing methods and classes like `UIColor` and `UIFont` usable on background threads](http://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniOS/).\n\nUnfortunately, Apple's documentation is lacking on the subject of thread safety. They recommend access on the main thread only, and even for drawing methods they don't explicitly guarantee thread safety - so it's always a good idea to read the [iOS Release Notes](http://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniOS/Articles/iPhoneOS4.html) as well.\n\nFor the most part, UIKit classes should be used only from the application’s main thread. This is particularly true either for classes derived from UIResponder or those that involve manipulating your application’s user interface in any way.\n\n#### The Deallocation Problem\n\nAnother danger when using UIKit objects in the background is called \"The Deallocation Problem.\" Apple outlines the issue in [TN2109](http://developer.apple.com/library/ios/#technotes/tn2109/_index.html) and presents various solutions. The problem is that UI objects should be deallocated on the main thread, because some of them might perform changes to the view hierarchy in `dealloc`. As we know, such calls to UIKit need to happen on the main thread. \n\nSince it's common that a secondary thread, operation, or block retains the caller, this is very easy to get wrong and quite hard to find/fix. This was also [a long-standing bug in AFNetworking](https://github.com/AFNetworking/AFNetworking/issues/56), simply because not a lot of people know about this issue and -- as usual -- it manifests itself in rare, hard-to-reproduce crashes. Consistent use of \\_\\_weak and not accessing ivars in async blocks/operations helps.\n\n#### Collection Classes\n\nApple has a good overview document for both [iOS and Mac](https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html#//apple_ref/doc/uid/10000057i-CH12-SW1) listing thread safety for the most common foundation classes. In general, immutable classes like `NSArray` are thread-safe, while their mutable variants like `NSMutableArray` are not. In fact, it's fine to use them from different threads, as long as access is serialized within a queue. Remember that methods might return a mutable variant of a collection object even if they declare their return type as immutable. It's good practice to write something like `return [array copy]` to ensure the returned object is in fact immutable.\n\nUnlike in languages like [Java](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html), the Foundation framework doesn't offer thread-safe collection classes out of the box. This is actually very reasonable, because in most cases you want to apply your locks higher up anyway to avoid too many locking operations. A notable exception are caches, where a mutable dictionary might hold immutable data -- here Apple added `NSCache` in iOS4 that not only locks access, but also purges its content in low-memory situations.\n\nThat said, there might be valid cases in your application where a thread-safe, mutable dictionary can be handy. And thanks to the class cluster approach, [it's easy to write one](https://gist.github.com/steipete/5928916).\n\n\n### Atomic Properties\n\nEver wondered how Apple is handling atomic setting/getting of properties? By now you have likely heard about spinlocks, semaphores, locks, @synchronized - so what's Apple using? Thankfully, [the Objective-C runtime is public](http://www.opensource.apple.com/source/objc4/), so we can take a look behind the curtain.\n\nA nonatomic property setter might look like this:\n\n```objc\n- (void)setUserName:(NSString *)userName {\n      if (userName != _userName) {\n          [userName retain];\n          [_userName release];\n          _userName = userName;\n      }\n}\n```\n\nThis is the variant with manual retain/release; however, the ARC-generated code looks similar. When we look at this code it's obvious why this means trouble when `setUserName:` is called concurrently. We could end up releasing `_userName` twice, which can corrupt memory and lead to hard-to-find bugs.\n\nWhat's happening internally for any property that's not manually implemented is that the compiler generates a call to [`objc_setProperty_non_gc(id self, SEL _cmd, ptrdiff_t offset, id newValue, BOOL atomic, signed char shouldCopy)`](https://github.com/opensource-apple/objc4/blob/master/runtime/Accessors.subproj/objc-accessors.mm#L127). In our example, the call parameters would look like this: \n\n```objc\nobjc_setProperty_non_gc(self, _cmd, \n  (ptrdiff_t)(&_userName) - (ptrdiff_t)(self), userName, NO, NO);`\n```\n\nThe ptrdiff_t might look weird to you, but in the end it's simple pointer arithmetic, since an Objective-C class is just another C struct.\n\n`objc_setProperty` calls down to following method:\n\n```objc\nstatic inline void reallySetProperty(id self, SEL _cmd, id newValue, \n  ptrdiff_t offset, bool atomic, bool copy, bool mutableCopy) \n{\n    id oldValue;\n    id *slot = (id*) ((char*)self + offset);\n\n    if (copy) {\n        newValue = [newValue copyWithZone:NULL];\n    } else if (mutableCopy) {\n        newValue = [newValue mutableCopyWithZone:NULL];\n    } else {\n        if (*slot == newValue) return;\n        newValue = objc_retain(newValue);\n    }\n\n    if (!atomic) {\n        oldValue = *slot;\n        *slot = newValue;\n    } else {\n        spin_lock_t *slotlock = &PropertyLocks[GOODHASH(slot)];\n        _spin_lock(slotlock);\n        oldValue = *slot;\n        *slot = newValue;        \n        _spin_unlock(slotlock);\n    }\n\n    objc_release(oldValue);\n}\n```\n\nAside from the rather funny name, this method is actually fairly straightforward and uses one of the 128 available spinlocks in `PropertyLocks`. This is a pragmatic and fast approach -- the worst case scenario is that a setter might have to wait for an unrelated setter to finish because of a hash collision. \n\nWhile those methods aren't declared in any public header, it is possible to call them manually. I'm not saying this is a good idea, but it's interesting to know and could be quite useful if you want atomic properties *and* to implement the setter at the same time.\n\n```objc\n// Manually declare runtime methods.\nextern void objc_setProperty(id self, SEL _cmd, ptrdiff_t offset, \n  id newValue, BOOL atomic, BOOL shouldCopy);\nextern id objc_getProperty(id self, SEL _cmd, ptrdiff_t offset, \n  BOOL atomic);\n\n#define PSTAtomicRetainedSet(dest, src) objc_setProperty(self, _cmd, \n  (ptrdiff_t)(&dest) - (ptrdiff_t)(self), src, YES, NO) \n#define PSTAtomicAutoreleasedGet(src) objc_getProperty(self, _cmd, \n  (ptrdiff_t)(&src) - (ptrdiff_t)(self), YES)\n```\n\n[Refer to this gist](https://gist.github.com/steipete/5928690) for the full snippet including code to handle structs. But keep in mind that we don't recommend using this.\n\n#### What about @synchronized?\n\nYou might be curious why Apple isn't using `@synchronized(self)` for property locking, an already existing runtime feature. Once you [look at the source](https://github.com/opensource-apple/objc4/blob/master/runtime/objc-sync.mm#L291), you'll see that there's a lot more going on. Apple is using [up to three lock/unlock sequences](http://googlemac.blogspot.co.at/2006/10/synchronized-swimming.html), partly because they also add [exception unwinding](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html#//apple_ref/doc/uid/10000057i-CH8-SW3). This would be a slowdown compared to the much faster spinlock approach. Since setting the property usually is quite fast, spinlocks are perfect for the job. `@synchonized(self)` is good when you need to ensure that exception can be thrown without the code deadlocking.\n\n\n### Your Own Classes\n\nUsing atomic properties alone won't make your classes thread-safe. It will only protect you against [race conditions][103] in the setter, but won't protect your application logic. Consider the following snippet:\n\n```objc\nif (self.contents) {\n    CFAttributedStringRef stringRef = CFAttributedStringCreate(NULL, \n      (__bridge CFStringRef)self.contents, NULL);\n    // draw string\n}\n```\n\nI've made this mistake early on in [PSPDFKit](http://pspdfkit.com). From time to time, the application crashed with a EXC_BAD_ACCESS, when the `contents` property was set to nil after the check. A simple fix for this issue would be to capture the variable:\n\n```objc\nNSString *contents = self.contents;\nif (contents) {\n    CFAttributedStringRef stringRef = CFAttributedStringCreate(NULL, \n      (__bridge CFStringRef)contents, NULL);\n    // draw string\n}\n```\n\nThis would solve the issue here, but in most cases it's not that simple. Imagine that we also have a `textColor` property and we change both properties on one thread. Then our render thread could end up using the new content along with the old color value and we get a weird combination. This is one reason why Core Data binds model objects to one thread or queue. \n\nThere's no one-size-fits-all solution for this problem. Using [immutable models](http://www.cocoawithlove.com/2008/04/value-of-immutable-values.html) is a solution, but it has its own problems. Another way is to limit changes to existing objects to the main thread or a specific queue and to generate copies before using them on worker threads. I recommend Jonathan Sterling's article about [Lightweight Immutability in Objective-C](http://www.jonmsterling.com/posts/2012-12-27-a-pattern-for-immutability.html) for even more ideas on solving this problem.\n\nThe simple solution is to use @synchronize. Anything else is very, very likely to get you into trouble. Way smarter people have failed again and again at doing so.\n\n#### Practical Thread-Safe Design\n\nBefore trying to make something thread-safe, think hard if it's necessary. Make sure it's not premature optimization. If it's anything like a configuration class, there's no point in thinking about thread safety. A much better approach is to throw some asserts in to ensure it's used correctly:\n\n```objc\nvoid PSPDFAssertIfNotMainThread(void) {\n    NSAssert(NSThread.isMainThread, \n      @\"Error: Method needs to be called on the main thread. %@\", \n      [NSThread callStackSymbols]);\n}\n```\n\nNow there's code that definitely should be thread-safe; a good example is a caching class. A good approach is to use a concurrent dispatch_queue as read/write lock to maximize performance and try to only lock the areas that are really necessary. Once you start using multiple queues for locking different parts, things get tricky really fast.\n\nSometimes you can also rewrite your code so that special locks are not required. Consider this snippet that is a form of a multicast delegate. (In many cases, using NSNotifications would be better, but there are [valid use cases for multicast delegates.](https://code.google.com/r/riky-adsfasfasf/source/browse/Utilities/GCDMulticastDelegate.h))\n\n```objc\n// header\n@property (nonatomic, strong) NSMutableSet *delegates;\n\n// in init\n_delegateQueue = dispatch_queue_create(\"com.PSPDFKit.cacheDelegateQueue\", \n  DISPATCH_QUEUE_CONCURRENT);\n\n- (void)addDelegate:(id<PSPDFCacheDelegate>)delegate {\n    dispatch_barrier_async(_delegateQueue, ^{\n        [self.delegates addObject:delegate];\n    });\n}\n\n- (void)removeAllDelegates {\n    dispatch_barrier_async(_delegateQueue, ^{\n        self.delegates removeAllObjects];\n    });\n}\n\n- (void)callDelegateForX {\n    dispatch_sync(_delegateQueue, ^{\n        [self.delegates enumerateObjectsUsingBlock:^(id<PSPDFCacheDelegate> delegate, NSUInteger idx, BOOL *stop) {\n            // Call delegate\n        }];\n    });\n}\n```\n\nUnless `addDelegate:` or `removeDelegate:` is called thousand times per second, a simpler and cleaner approach is the following:\n\n```objc\n// header\n@property (atomic, copy) NSSet *delegates;\n\n- (void)addDelegate:(id<PSPDFCacheDelegate>)delegate {\n    @synchronized(self) {\n        self.delegates = [self.delegates setByAddingObject:delegate];\n    }\n}\n\n- (void)removeAllDelegates {\n    @synchronized(self) {\n        self.delegates = nil;\n    }\n}\n\n- (void)callDelegateForX {\n    [self.delegates enumerateObjectsUsingBlock:^(id<PSPDFCacheDelegate> delegate, NSUInteger idx, BOOL *stop) {\n        // Call delegate\n    }];\n}\n```\n\nGranted, this example is a bit constructed and one could simply confine changes to the main thread. But for many data structures, it might be worth it to create immutable copies in the modifier methods, so that the general application logic doesn't have to deal with excessive locking.\n\n\n## Pitfalls of GCD\n\nFor most of your locking needs, GCD is perfect. It's simple, it's fast, and its block-based API makes it much harder to accidentally do imbalanced locks. However, there are quite a few pitfalls, some of which we are going to explore here.\n\n### Using GCD as a Recursive Lock\n\nGCD is a queue to serialize access to shared resources. This can be used for locking, but it's quite different than `@synchronized`. GCD queues are not reentrant - this would break the queue characteristics. Many people tried working around this with using `dispatch_get_current_queue()`, which is [a bad idea](https://gist.github.com/steipete/3713233), and Apple had its reasons for deprecating this method in iOS6.\n\n```objc\n// This is a bad idea.\ninline void pst_dispatch_sync_reentrant(dispatch_queue_t queue, \n  dispatch_block_t block) \n{\n    dispatch_get_current_queue() == queue ? block() \n                                          : dispatch_sync(queue, block);\n}\n```\n\nTesting for the current queue might work for simple solutions, but it fails as soon as your code gets more complex, and you might have multiple queues locked at the same time. Once you are there, you almost certainly will get a [deadlock][104]. Sure, one could use `dispatch_get_specific()`, which will traverse the whole queue hierarchy to test for specific queues. For that you would have to write custom queue constructors that apply this metadata. Don't go that way. There are use cases where a `NSRecursiveLock` is the better solution.\n\n### Fixing Timing Issues with dispatch_async\n\nHaving some timing-issues in UIKit? Most of the time, this will be the perfect \"fix:\"\n\n```objc\ndispatch_async(dispatch_get_main_queue(), ^{\n    // Some UIKit call that had timing issues but works fine \n    // in the next runloop.\n    [self updatePopoverSize];\n});\n```\n\nDon't do this, trust me. This will haunt you later as your app gets larger. It's super hard to debug and soon things will fall apart when you need to dispatch more and more because of \"timing issues.\" Look through your code and find the proper place for the call (e.g. viewWillAppear instead of viewDidLoad). I still have some of those hacks in my codebase, but most of them are properly documented and an issue is filed.\n\nRemember that this isn't really GCD-specific, but it's a common anti-pattern and just very easy to do with GCD. You can apply the same wisdom for `performSelector:afterDelay:`, where the delay is 0.f for the next runloop.\n\n### Mixing dispatch\\_sync and dispatch\\_async in Performance Critical Code\n\nThat one took me a while to figure out. In [PSPDFKit](http://pspdfkit.com) there is a caching class that uses a LRU list to track image access. When you scroll through the pages, this is called *a lot*. The initial implementation used dispatch\\_sync for availability access, and dispatch\\_async to update the LRU position. This resulted in a frame rate far from the goal of 60 FPS. \n\nWhen other code running in your app is blocking GCD's threads, it might take a while until the dispatch manager finds a thread to perform the dispatch\\_async code -- until then, your sync call will be blocked. Even when, as in this example, the order of execution for the async case isn't important, there's no easy way to tell that to GCD. Read/Write locks won't help you there, since the async process most definitely needs to perform a barrier write and all your readers will be locked during that. Lesson: `dispatch_async` can be expensive if it's misused. Be careful when using it for locking.\n\n### Using dispatch_async to Dispatch Memory-Intensive Operations\n\nWe already talked a lot about NSOperations, and that it's usually a good idea to use the more high-level API. This is especially true if you deal with blocks of work that do memory-intensive operations.\n\nIn an old version of PSPDFKit, I used a GCD queue to dispatch writing cached JPG images to disk. When the retina iPad came out, this started causing trouble. The resolution doubled, and it took much longer to encode the image data than it took to render it. Consequently, operations piled up in the queue and when the system was busy it could crash of memory exhaustion.\n\nThere's no way to see how many operations are queued (unless you manually add code to track this), and there's also no built-in way to cancel operations in case of a low-memory notification. Switching to NSOperations made the code a lot more debuggable and allowed all this without writing manual management code. \n\nOf course there are some caveats; for example you can't set a target queue on your `NSOperationQueue` (like  `DISPATCH_QUEUE_PRIORITY_BACKGROUND` for throttled I/O). But that's a small price for debuggability, and it also prevents you from running into problem like [priority inversion][102]. I even recommend against the nice `NSBlockOperation` API and suggest real subclasses of NSOperation, including an implementation of description. It's more work, but later on, having a way to print all running/pending operations is insanely useful.\n\n\n[90]: /issues/2-concurrency/editorial/\n[100]: /issues/2-concurrency/concurrency-apis-and-pitfalls/\n[101]: /issues/2-concurrency/concurrency-apis-and-pitfalls/#challenges\n[102]: /issues/2-concurrency/concurrency-apis-and-pitfalls/#priority_inversion\n[103]: /issues/2-concurrency/concurrency-apis-and-pitfalls/#shared_resources\n[104]: /issues/2-concurrency/concurrency-apis-and-pitfalls/#dead_locks\n[200]: /issues/2-concurrency/common-background-practices/\n[300]: /issues/2-concurrency/low-level-concurrency-apis/\n[301]: /issues/2-concurrency/low-level-concurrency-apis/#async\n[302]: /issues/2-concurrency/low-level-concurrency-apis/#multiple-readers-single-writer\n[400]: /issues/2-concurrency/thread-safe-class-design/\n"
  },
  {
    "path": "2013-08-07-advanced-auto-layout-toolbox.md",
    "content": "---\ntitle: \"Advanced Auto Layout Toolbox\"\ncategory: \"3\"\ndate: \"2013-08-07 06:00:00\"\nauthor:\n  - name: Florian Kugler\n    url: http://twitter.com/floriankugler\ntags: article\n---\n\n\nAuto Layout was introduced in OS X 10.7, and one year later it made its way into iOS 6. Soon apps on iOS 7 will be expected to honor the systemwide font size setting, thus requiring even more flexibility in the user interface layout next to different screen sizes and orientations. Apple is doubling down on Auto Layout, so now is a good time to get your feet wet if you haven't done so yet.\n\nMany developers struggle with Auto Layout when first trying it, because of the often-frustrating experience of building constraint-based layouts with Xcode 4's Interface Builder. But don't let yourself be discouraged by that; Auto Layout is much better than Interface Builder's current support for it. Xcode 5 will bring some major relief in this area.\n\nThis article is not an introduction to Auto Layout. If you haven't worked with it yet, we encourage you to watch the Auto Layout sessions from WWDC 2012 ([202 -- Introduction to Auto Layout for iOS and OS X](https://developer.apple.com/videos/wwdc/2012/?id=202), [228 -- Best Practices for Mastering Auto Layout](https://developer.apple.com/videos/wwdc/2012/?id=228), [232 -- Auto Layout by Example](https://developer.apple.com/videos/wwdc/2012/?id=232)). These are excellent introductions to the topic which cover a lot of ground.\n\nInstead, we are going to focus on several advanced tips and techniques, which enhance productivity with Auto Layout and make your (development) life easier. Most of these are touched upon in the WWDC sessions mentioned above, but they are the kind of things that are easy to oversee or forget while trying to get your daily work done. \n\n\n<a name=\"layout-process\"> </a>\n\n## The Layout Process\n\nFirst we will recap the steps it takes to bring views on screen with Auto Layout enabled. When you're struggling to produce the kind of layout you want with Auto Layout, specifically with advanced use cases and animation, it helps to take a step back and to recall how the layout process works.\n\nCompared to working with springs and struts, Auto Layout introduces two additional steps to the process before views can be displayed: updating constraints and laying out views. Each step is dependent on the one before; display depends on layout, and layout depends on updating constraints.\n\nThe first step -- updating constraints -- can be considered a \"measurement pass.\" It happens bottom-up (from subview to super view) and prepares the information needed for the layout pass to actually set the views' frame. You can trigger this pass by calling `setNeedsUpdateConstraints`. Any changes you make to the system of constraints itself will automatically trigger this. However, it is useful to notify Auto Layout about changes in custom views that could affect the layout. Speaking of custom views, you can override `updateConstraints` to add the local constraints needed for your view in this phase.\n\nThe second step -- layout -- happens top-down (from super view to subview). This layout pass actually applies the solution of the constraint system to the views by setting their frames (on OS X) or their center and bounds (on iOS). You can trigger this pass by calling `setNeedsLayout`, which does not actually go ahead and apply the layout immediately, but takes note of your request for later. This way you don't have to worry about calling it too often, since all the layout requests will be coalesced into one layout pass.\n\nTo force the system to update the layout of a view tree immediately, you can call `layoutIfNeeded`/`layoutSubtreeIfNeeded` (on iOS and OS X respectively). This can be helpful if your next steps rely on the views' frame being up to date. In your custom views you can override `layoutSubviews`/`layout` to gain full control over the layout pass. We will show use cases for this later on.\n\nFinally, the display pass renders the views to screen and is independent of whether you're using Auto Layout or not. It operates top-down and can be triggered by calling `setNeedsDisplay`, which results in a deferred redraw coalescing all those calls. Overriding the familiar `drawRect:` is how you gain full control over this stage of the display process in your custom views.\n\nSince each step depends on the one before it, the display pass will trigger a layout pass if any layout changes are pending. Similarly, the layout pass will trigger updating the constraints if the constraint system has pending changes. \n\nIt's important to remember that these three steps are not a one-way street. Constraint-based layout is an iterative process. The layout pass can make changes to the constraints based on the previous layout solution, which again triggers updating the constraints following another layout pass. This can be leveraged to create advanced layouts of custom views, but you can also get stuck in an infinite loop if every call of your custom implementation of `layoutSubviews` results in another layout pass.\n\n\n## Enabling Custom Views for Auto Layout\n\nWhen writing a custom view, you need to be aware of the following things with regard to Auto Layout: specifying an appropriate intrinsic content size, distinguishing between the view's frame and alignment rect, enabling baseline-aligned layout, and how to hook into the layout process. We will go through these aspects one by one.\n\n\n### Intrinsic Content Size\n\nThe intrinsic content size is the size a view prefers to have for a specific content it displays. For example, `UILabel` has a preferred height based on the font, and a preferred width based on the font and the text it displays. A `UIProgressView` only has a preferred height based on its artwork, but no preferred width. A plain `UIView` has neither a preferred width nor a preferred height.\n\nYou have to decide, based on the content to be displayed, if your custom view has an intrinsic content size, and if so, for which dimensions.\n\nTo implement an intrinsic content size in a custom view, you have to do two things: override [`intrinsicContentSize`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/occ/instm/NSView/intrinsicContentSize) to return the appropriate size for the content, and call [`invalidateIntrinsicContentSize`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/occ/instm/NSView/invalidateIntrinsicContentSize) whenever something changes which affects the intrinsic content size. If the view only has an intrinsic size for one dimension, return `UIViewNoIntrinsicMetric`/`NSViewNoIntrinsicMetric` for the other one.\n\nNote that the intrinsic content size must be independent of the view's frame. For example, it's not possible to return an intrinsic content size with a specific aspect ratio based on the frame's height or width.\n\n\n#### Compression Resistance and Content Hugging\n\nEach view has content compression resistance priorities and content hugging priorities assigned for both dimensions. These properties only take effect for views which define an intrinsic content size, otherwise there is no content size defined that could resist compression or be hugged.\n\nBehind the scenes, the intrinsic content size and these priority values get translated into constraints. For a label with an intrinsic content size of `{ 100, 30 }`, horizontal/vertical compression resistance priority of `750`, and  horizontal/vertical content hugging priority of `250`, four constraints will be generated:\n\n```\nH:[label(<=100@250)]\nH:[label(>=100@750)]\nV:[label(<=30@250)]\nV:[label(>=30@750)]\n```\n\nIf you're not familiar with the visual format language for the constraints used above, you can read up about it in [Apple's documentation](https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage/VisualFormatLanguage.html). Keeping in mind that these additional constraints are generated implicitly helps to understand Auto Layout's behavior and to make better sense of its error messages.\n\n\n### Frame vs. Alignment Rect\n\nAuto Layout does not operate on views' frame, but on their alignment rect. It's easy to forget the subtle difference, because in many cases they are the same. But alignment rects are actually a powerful new concept that decouple a view's layout alignment edges from its visual appearance.\n\nFor example, a button in the form of a custom icon that is smaller than the touch target we want to have would normally be difficult to lay out. We would have to know about the dimensions of the artwork displayed within a larger frame and adjust the button's frame accordingly, so that the icon lines up with other interface elements. The same happens if we want to draw custom ornamentation around the content, like badges, shadows, and reflections.\n\nUsing alignment rects we can easily define the rectangle which should be used for layout. In most cases you can just override the [`alignmentRectInsets`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/occ/instm/NSView/alignmentRectInsets) method, which lets you return edge insets relative to the frame. If you need more control you can override the methods [`alignmentRectForFrame:`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/occ/instm/NSView/alignmentRectForFrame:) and [`frameForAlignmentRect:`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/occ/instm/NSView/frameForAlignmentRect:). This can be useful if you want to calculate the alignment rect based on the current frame value instead of just subtracting fixed insets. But you have to make sure that these two methods are inverses of each other.\n\nIn this context it is also good to recall that the aforementioned intrinsic content size of a view refers to its alignment rect, not to its frame. This makes sense, because Auto Layout generates the compression resistance and content hugging constraints straight from the intrinsic content size.\n\n\n### Baseline Alignment\n\nTo enable constraints using the `NSLayoutAttributeBaseline` attribute to work on a custom view, we have to do a little bit of extra work. Of course this only makes sense if the custom view in question has something like a baseline.\n\nOn iOS, baseline alignment can be enabled by implementing [`viewForBaselineLayout`](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/viewForBaselineLayout). The bottom edge of the view you return here will be used as baseline. The default implementation simply returns self, while a custom implementation can return any subview. On OS X you don't return a subview but an offset from the view's bottom edge by overriding [`baselineOffsetFromBottom`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/occ/instm/NSView/baselineOffsetFromBottom), which has the same default behavior as its iOS counterpart by returning 0 in its default implementation.\n\n\n### Taking Control of Layout\n\nIn a custom view you have full control over the layout of its subviews. You can add local constraints, you can change local constraints if a change in content requires it, you can fine-tune the result of the layout pass for subviews, or you can opt out of Auto Layout altogether.\n\nMake sure though that you use this power wisely. Most cases can be handled by simply adding local constraints for your subviews.\n\n\n#### Local Constraints\n\nIf we want to compose a custom view out of several subviews, we have to lay out these subviews somehow. In an Auto Layout environment it is most natural to add local constraints for these views. However, note that this makes your custom view dependent on Auto Layout, and it cannot be used anymore in windows without Auto Layout enabled. It's best to make this dependency explicit by implementing [`requiresConstraintBasedLayout`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/occ/clm/NSView/requiresConstraintBasedLayout) to return `YES`.\n\nThe place to add local constraints is [`updateConstraints`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/occ/instm/NSView/updateConstraints). Make sure to invoke `[super updateConstraints]` in your implementation *after* you've added whatever constraints you need to lay out the subviews. In this method, you're not allowed to invalidate any constraints, because you are already in the first step of the [layout process][110] described above. Trying to do so will generate a friendly error message informing you that you've made a \"programming error.\"\n\nIf something changes later on that invalidates one of your constraints, you should remove the constraint immediately and call [`setNeedsUpdateConstraints`](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/setNeedsUpdateConstraints). In fact, that's the only case where you should have to trigger a constraint update pass.\n\n\n#### Control Layout of Subviews\n\nIf you cannot use layout constraints to achieve the desired layout of your subviews, you can go one step further and override [`layoutSubviews`](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/layoutSubviews) on iOS or [`layout`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/occ/instm/NSView/layout) on OS X. This way, you're hooking into the second step of the [layout process][110], when the constraint system has already been solved and the results are being applied to the view. \n\nThe most drastic approach is to override `layoutSubviews`/`layout` without calling the super class's implementation. This means that you're opting out of Auto Layout for the view tree within this view. From this point on, you can position subviews manually however you like.\n\nIf you still want to use constraints to lay out subviews, you have to call `[super layoutSubviews]`/`[super layout]` and make fine-tuned adjustments to the layout afterwards. You can use this to create layouts which are not possible to define using constraints, for example layouts involving relationships between the size and the spacing between views.\n\nAnother interesting use case for this is to create a layout-dependent view tree. After Auto Layout has done its first pass and set the frames on your custom view's subviews, you can inspect the positioning and sizing of these subviews and make changes to the view hierarchy and/or to the constraints. WWDC session [228 -- Best Practices for Mastering Auto Layout](https://developer.apple.com/videos/wwdc/2012/?id=228) has a good example of this, where subviews are removed after the first layout pass if they are getting clipped. \n\nYou could also decide to change the constraints after the first layout pass. For example, switch from lining up subviews in one row to two rows, if the views are becoming too narrow. \n\n```objc\n- layoutSubviews\n{\n    [super layoutSubviews];\n    if (self.subviews[0].frame.size.width <= MINIMUM_WIDTH) {\n        [self removeSubviewConstraints];\n        self.layoutRows += 1;\n        [super layoutSubviews];\n    }\n}\n\n- updateConstraints\n{\n    // add constraints depended on self.layoutRows...\n    [super updateConstraints];\n}\n```\n\n## Intrinsic Content Size of Multi-Line Text\n\nThe intrinsic content size of `UILabel` and `NSTextField` is ambiguous for multi-line text. The height of the text depends on the width of the lines, which is yet to be determined when solving the constraints. In order to solve this problem, both classes have a new property called [`preferredMaxLayoutWidth`](http://developer.apple.com/library/ios/documentation/uikit/reference/UILabel_Class/Reference/UILabel.html#//apple_ref/occ/instp/UILabel/preferredMaxLayoutWidth), which specifies the maximum line width for calculating the intrinsic content size. \n\nSince we usually don't know this value in advance, we need to take a two-step approach to get this right. First we let Auto Layout do its work, and then we use the resulting frame in the layout pass to update the preferred maximum width and trigger layout again.\n\n```objc\n- (void)layoutSubviews\n{\n    [super layoutSubviews];\n    myLabel.preferredMaxLayoutWidth = myLabel.frame.size.width;\n    [super layoutSubviews];\n}\n```\n\nThe first call to `[super layoutSubviews]` is necessary for the label to get its frame set, while the second call is necessary to update the layout after the change. If we omit the second call we get a `NSInternalInconsistencyException` error, because we've made changes in the layout pass which require updating the constraints, but we didn't trigger layout again.\n\nWe can also do this in a label subclass itself:\n\n```objc\n@implementation MyLabel\n- (void)layoutSubviews\n{\n    self.preferredMaxLayoutWidth = self.frame.size.width;\n    [super layoutSubviews];\n}\n@end\n```\n\nIn this case, we don't need to call `[super layoutSubviews]` first, because when `layoutSubviews` gets called, we already have a frame on the label itself.\n\nTo make this adjustment from the view controller level, we hook into `viewDidLayoutSubviews`. At this point the frames of the first Auto Layout pass are already set and we can use them to set the preferred maximum width. \n\n```objc\n- (void)viewDidLayoutSubviews\n{\n    [super viewDidLayoutSubviews];\n    myLabel.preferredMaxLayoutWidth = myLabel.frame.size.width;\n    [self.view layoutIfNeeded];\n}\n```\n\nLastly, make sure that you don't have an explicit height constraint on the label that has a higher priority than the label's content compression resistance priority. Otherwise it will trump the calculated height of the content.\n\n\n## Animation\n\nWhen it comes to animating views laid out with Auto Layout, there are two fundamentally different strategies: Animating the constraints themselves, and changing the constraints to recalculate the frames and use Core Animation to interpolate between the old and the new position.\n\nThe difference between the two approaches is that animating constraints themselves results in a layout that conforms to the constraint system at all times. Meanwhile, using Core Animation to interpolate between old and new frames violates constraints temporarily.\n\nDirectly animating constraints is really only a feasible strategy on OS X, and it is limited in what you can animate, since only a constraint's constant can be changed after creating it. On iOS you would have to drive the animation manually, whereas on OS X you can use an animator proxy on the constraint's constant. Furthermore, this approach is significantly slower than the Core Animation approach, which also makes it a bad fit for mobile platforms for the time being.\n\nWhen using the Core Animation approach, animation conceptually works the same way as without Auto Layout. The difference is that you don't set the views' target frames manually, but instead you modify the constraints and trigger a layout pass to set the frames for you. On iOS, instead of:\n\n```objc\n[UIView animateWithDuration:1 animations:^{\n    myView.frame = newFrame;\n}];\n```\n\nyou now write:\n\n```objc\n// update constraints\n[UIView animateWithDuration:1 animations:^{\n    [myView layoutIfNeeded];\n}];\n```\n\nNote that with this approach, the changes you can make to the constraints are not limited to the constraints' constants. You can remove constraints, add constraints, and even use temporary animation constraints. Since the new constraints only get solved once to determine the new frames, even more complex layout changes are possible.\n\nThe most important thing to remember when animating views using Core Animation in conjunction with Auto Layout is to not touch the views' frame yourself. Once a view is laid out by Auto Layout, you've transferred the responsibility to set its frame to the layout system. Interfering with this will result in weird behavior.\n\nThis means also that view transforms don't always play nice with Auto Layout if they change the view's frame. Consider the following example:\n\n```objc\n[UIView animateWithDuration:1 animations:^{\n    myView.transform = CGAffineTransformMakeScale(.5, .5);\n}];\n```\n\nNormally we would expect this to scale the view to half its size while maintaining its center point. But the behavior with Auto Layout depends on the kind of constraints we have set up to position the view. If we have it centered within its super view, the result is as expected, because applying the transform triggers a layout pass which centers the new frame within the super view. However, if we have aligned the left edge of the view to another view, then this alignment will stick and the center point will move. \n\nAnyway, applying transforms like this to views laid out with constraints is not a good idea, even if the result matches our expectations at first. The view's frame gets out of sync with the constraints, which will lead to strange behavior down the road.\n\nIf you want to use transforms to animate a view or otherwise animate its frame directly, the cleanest technique to do this is to [embed the view into a container view](http://stackoverflow.com/a/14119154). Then you can override `layoutSubviews` on the container, either opting out of Auto Layout completely or only adjusting its result. For example, if we setup a subview in our container which is laid out within the container at its top and left edges using Auto Layout, we can correct its center after the layout happens to enable the scale transform from above:\n\n```objc\n- (void)layoutSubviews\n{\n    [super layoutSubviews];\n    static CGPoint center = {0,0};\n    if (CGPointEqualToPoint(center, CGPointZero)) {\n        // grab the view's center point after initial layout\n        center = self.animatedView.center;\n    } else {\n        // apply the previous center to the animated view\n        self.animatedView.center = center;\n    }\n}\n```\n\nIf we expose the `animatedView` property as an IBOutlet, we can even use this container within Interface Builder and position its subview with constraints, while still being able to apply the scale transform with the center staying fixed.\n\n\n## Debugging\n\nWhen it comes to debugging Auto Layout, OS X still has a significant advantage over iOS. On OS X you can make use of Instrument's Cocoa Layout template, as well as `NSWindow`'s  [`visualizeConstraints:`](http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWindow_Class/Reference/Reference.html#//apple_ref/occ/instm/NSWindow/visualizeConstraints:) method. Furthermore, `NSView` has an [`identifier`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSUserInterfaceItemIdentification_Protocol/Introduction/Introduction.html#//apple_ref/occ/intfp/NSUserInterfaceItemIdentification/identifier) property, which you can set from Interface Builder or in code, in order to get much more readable Auto Layout error messages.\n\n\n### Unsatisfiable Constraints\n\nIf we run into unsatisfiable constraints on iOS, we only see the views' memory addresses in the printout. Especially in more complex layouts, it's sometimes difficult to identify the views which are part of the problem. However, there are several ways we can help ourselves in this situation.\n\nFirst, whenever you see `NSLayoutResizingMaskConstraint`s in the unsatisfiable constraints error message, you almost certainly forgot to set `translatesAutoResizingMaskIntoConstraints` to `NO` for one of your views. While Interface Builder does this automatically, you have to do this manually for all views created in code.\n\nIf it's not obvious which views are causing the trouble, you have to identify the view by its memory address. The most straightforward option is to use the debugger console. You can print out the description of the view itself or its super view, or even the recursive description of the view tree. This mostly gives you lots of cues to identify which view you're dealing with.\n\n```\n(lldb) po 0x7731880\n$0 = 124983424 <UIView: 0x7731880; frame = (90 -50; 80 100); \nlayer = <CALayer: 0x7731450>>\n\n(lldb) po [0x7731880 superview]\n$2 = 0x07730fe0 <UIView: 0x7730fe0; frame = (32 128; 259 604); \nlayer = <CALayer: 0x7731150>>\n\n(lldb) po [[0x7731880 superview] recursiveDescription]\n$3 = 0x07117ac0 <UIView: 0x7730fe0; frame = (32 128; 259 604); layer = <CALayer: 0x7731150>>\n   | <UIView: 0x7731880; frame = (90 -50; 80 100); layer = <CALayer: 0x7731450>>\n   | <UIView: 0x7731aa0; frame = (90 101; 80 100); layer = <CALayer: 0x7731c60>>\n```\n\nA more visual approach is to modify the view in question from the console so that you can spot it on screen. For example, you can do this by changing its background color:\n\n```\n(lldb) expr ((UIView *)0x7731880).backgroundColor = [UIColor purpleColor]\n```\n\nMake sure to resume the execution of your app afterward or the changes will not show up on screen. Also note the cast of the memory address to `(UIView *)` and the extra set of round brackets so that we can use dot notation. Alternatively, you can of course also use message sending notation:\n\n```\n(lldb) expr [(UIView *)0x7731880 setBackgroundColor:[UIColor purpleColor]]\n```\n\nAnother approach is to profile the application with Instrument's allocations template. Once you've got the memory address from the error message (which you have to get out of the Console app when running Instruments), you can switch Instrument's detail view to the Objects List and search for the address with Cmd-F. This will show you the method which allocated the view object, which is often a pretty good hint of what you're dealing with (at least for views created in code).\n\nYou can also make deciphering unsatisfiable constraints errors on iOS easier by improving the error message itself. We can overwrite `NSLayoutConstraint`'s description method in a category to include the views' tags:\n\n```objc\n@implementation NSLayoutConstraint (AutoLayoutDebugging)\n#ifdef DEBUG\n- (NSString *)description\n{\n    NSString *description = super.description;\n    NSString *asciiArtDescription = self.asciiArtDescription;\n    return [description stringByAppendingFormat:@\" %@ (%@, %@)\", \n        asciiArtDescription, [self.firstItem tag], [self.secondItem tag]];\n}\n#endif\n@end\n```\n\nIf the integer property `tag` is not enough information, we can also get a bit more adventurous and add our own nametag property to the view class, which we then print out in the error message. We can even assign values to this custom property in Interface Builder using the \"User Defined Runtime Attributes\" section in the identity inspector.\n\n```objc\n@interface UIView (AutoLayoutDebugging)\n- (void)setAbc_NameTag:(NSString *)nameTag;\n- (NSString *)abc_nameTag;\n@end\n\n@implementation UIView (AutoLayoutDebugging)\n- (void)setAbc_NameTag:(NSString *)nameTag\n{\n    objc_setAssociatedObject(self, \"abc_nameTag\", nameTag, \n        OBJC_ASSOCIATION_RETAIN_NONATOMIC);\n}\n\n- (NSString *)abc_nameTag\n{\n    return objc_getAssociatedObject(self, \"abc_nameTag\");\n}\n@end\n\n@implementation NSLayoutConstraint (AutoLayoutDebugging)\n#ifdef DEBUG\n- (NSString *)description\n{\n    NSString *description = super.description;\n    NSString *asciiArtDescription = self.asciiArtDescription;\n    return [description stringByAppendingFormat:@\" %@ (%@, %@)\", \n        asciiArtDescription, [self.firstItem abc_nameTag], \n        [self.secondItem abc_nameTag]];\n}\n#endif\n@end\n```\n\nThis way the error message becomes much more readable and you don't have to find out which view belongs to which memory address. However, it requires some extra work on your part to consistently assign meaningful names to the views.\n\nAnother neat trick (via [Daniel](https://twitter.com/danielboedewadt)) that gives you better error messages without requiring extra work is to integrate call stack symbols into the error message for each layout constraint. This makes it easy to see where the constraints involved in the problem were created. To do this, you have to swizzle the `addConstraint:` and `addConstraints:` methods of `UIView` or `NSView`, as well as the layout constraint's `description` method. In the methods for adding constraints, you should then add an associated object to each constraint, which describes the first frame of the current call stack backtrace (or whatever information you would like to have from it):\n\n```objc\nstatic void AddTracebackToConstraints(NSArray *constraints)\n{\n    NSArray *a = [NSThread callStackSymbols];\n    NSString *symbol = nil;\n    if (2 < [a count]) {\n        NSString *line = a[2];\n        // Format is\n        //               1         2         3         4         5\n        //     012345678901234567890123456789012345678901234567890123456789\n        //     8   MyCoolApp                           0x0000000100029809 -[MyViewController loadView] + 99\n        //\n        // Don't add if this wasn't called from \"MyCoolApp\":\n        if (59 <= [line length]) {\n            line = [line substringFromIndex:4];\n            if ([line hasPrefix:@\"My\"]) {\n                symbol = [line substringFromIndex:59 - 4];\n            }\n        }\n    }\n    for (NSLayoutConstraint *c in constraints) {\n        if (symbol != nil) {\n            objc_setAssociatedObject(c, &ObjcioLayoutConstraintDebuggingShort, \n                symbol, OBJC_ASSOCIATION_COPY_NONATOMIC);\n        }\n        objc_setAssociatedObject(c, &ObjcioLayoutConstraintDebuggingCallStackSymbols, \n            a, OBJC_ASSOCIATION_COPY_NONATOMIC);\n    }\n}\n\n@end\n```\n\nOnce you have this information available on each constraint object, you can simply modify `UILayoutConstraint`'s description method to include it in the output.\n\n```objc\n- (NSString *)objcioOverride_description\n{\n    // call through to the original, really\n    NSString *description = [self objcioOverride_description];\n    NSString *objcioTag = objc_getAssociatedObject(self, &ObjcioLayoutConstraintDebuggingShort);\n    if (objcioTag == nil) {\n        return description;\n    }\n    return [description stringByAppendingFormat:@\" %@\", objcioTag];\n}\n```\n\nCheck [this GitHub repository](https://github.com/objcio/issue-3-auto-layout-debugging) for a full code example of this technique.\n\n\n### Ambiguous Layout\n\nAnother common problem is ambiguous layout. If we forget to add a constraint, we are often left wondering why the layout doesn't look like what we expected. `UIView` and `NSView` provide three ways to detect ambiguous layouts: [`hasAmbiguousLayout`](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/hasAmbiguousLayout), [`exerciseAmbiguityInLayout`](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/exerciseAmbiguityInLayout), and the private method `_autolayoutTrace`.\n\nAs the name indicates, `hasAmbiguousLayout` simply returns YES if the view has an ambiguous layout. Instead of traversing through the view hierarchy ourselves and logging this value, we can make use of the private `_autolayoutTrace` method. This returns a string describing the whole view tree -- similar to the printout of [`recursiveDescription`](http://developer.apple.com/library/ios/#technotes/tn2239/_index.html#//apple_ref/doc/uid/DTS40010638-CH1-SUBSECTION34) -- which tells you when a view has an ambiguous layout.\n\nSince this method is private, make sure to not ship any code which contains this call. One possible way to safeguard yourself against this is to create a method in a view category like this:\n\n```objc\n@implementation UIView (AutoLayoutDebugging)\n- (void)printAutoLayoutTrace\n{\n    #ifdef DEBUG\n    NSLog(@\"%@\", [self performSelector:@selector(_autolayoutTrace)]);\n    #endif\n}\n@end\n```\n\n`_autolayoutTrace` creates a printout like this:\n\n```\n2013-07-23 17:36:08.920 FlexibleLayout[4237:907] \n*<UIWindow:0x7269010>\n|   *<UILayoutContainerView:0x7381250>\n|   |   *<UITransitionView:0x737c4d0>\n|   |   |   *<UIViewControllerWrapperView:0x7271e20>\n|   |   |   |   *<UIView:0x7267c70>\n|   |   |   |   |   *<UIView:0x7270420> - AMBIGUOUS LAYOUT\n|   |   <UITabBar:0x726d440>\n|   |   |   <_UITabBarBackgroundView:0x7272530>\n|   |   |   <UITabBarButton:0x726e880>\n|   |   |   |   <UITabBarSwappableImageView:0x7270da0>\n|   |   |   |   <UITabBarButtonLabel:0x726dcb0>\n```\n\nAs with the unsatisfiable constraints error message, we still have to figure out which view belongs to the memory address of the printout.\n\nAnother more visual way to spot ambiguous layouts is to use `exerciseAmbiguityInLayout`. This will randomly change the view's frame between valid values. However, calling this method once will also just change the frame once. So chances are that you will not see this change at all when you start your app. It's a good idea to create a helper method which traverses through the whole view hierarchy and makes all views that have an ambiguous layout \"jiggle.\"\n\n```objc\n@implementation UIView (AutoLayoutDebugging)\n- (void)exerciseAmbiguityInLayoutRepeatedly:(BOOL)recursive\n{\n    #ifdef DEBUG\n    if (self.hasAmbiguousLayout) {\n        [NSTimer scheduledTimerWithTimeInterval:.5 \n                                         target:self \n                                       selector:@selector(exerciseAmbiguityInLayout) \n                                       userInfo:nil \n                                        repeats:YES];\n    }\n    if (recursive) {\n        for (UIView *subview in self.subviews) {\n            [subview exerciseAmbiguityInLayoutRepeatedly:YES];\n        }\n    }\n    #endif\n}\n@end\n```\n\n### NSUserDefault Options\n\nThere are a couple of helpful `NSUserDefault` options that help with debugging and testing Auto Layout. You can either set these [in code](http://stackoverflow.com/a/13044693/862060), or you can specify them as [launch arguments in the scheme editor](http://stackoverflow.com/a/13138933/862060).\n\nAs the names indicate, `UIViewShowAlignmentRects` and `NSViewShowAlignmentRects` make the alignment rects of all views visible. `NSDoubleLocalizedStrings` simply takes every localized string and doubles it in length. This is a great way to test your layout for more verbose languages. Lastly, setting `AppleTextDirection` and `NSForceRightToLeftWritingDirection` to `YES` simulates a right-to-left language.\n\n\n## Constraint Code\n\nThe first thing to remember when setting up views and their constraints in code is to always set [`translatesAutoResizingMaskIntoConstraints`](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/translatesAutoresizingMaskIntoConstraints) to NO. Forgetting this will almost inevitably result in unsatisfiable constraint errors. It's something which is easy to miss even after working with Auto Layout for a while, so watch out for this pitfall.\n\nWhen you use the [visual format language](http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/AutolayoutPG/Articles/formatLanguage.html) to set up constraints, the `constraintsWithVisualFormat:options:metrics:views:` method has a very useful `options` argument. If you're not using it already, check out the [documentation](http://developer.apple.com/library/ios/documentation/AppKit/Reference/NSLayoutConstraint_Class/NSLayoutConstraint/NSLayoutConstraint.html#//apple_ref/occ/clm/NSLayoutConstraint/constraintsWithVisualFormat:options:metrics:views:). It allows you to align the views in a dimension other than the one affected by the format string. For example, if the format specifies the horizontal layout, you can use `NSLayoutFormatAlignAllTop` to align all views included in the format string along their top edges.\n\nThere is also a [neat little trick](https://github.com/evgenyneu/center-vfl) to achieve centering of a view within its superview using the visual format language, which takes advantage of inequality constraints and the options argument. The following code aligns a view horizontally in its super view:\n\n```objc\nUIView *superview = theSuperView;\nNSDictionary *views = NSDictionaryOfVariableBindings(superview, subview);\nNSArray *c = [NSLayoutConstraint \n                constraintsWithVisualFormat:@\"V:[superview]-(<=1)-[subview]\"]\n                                    options:NSLayoutFormatAlignAllCenterX\n                                    metrics:nil\n                                      views:views];\n[superview addConstraints:c];\n```\n\nThis uses the option `NSLayoutFormatAlignAllCenterX` to create the actual centering constraint between the super view and the subview. The format string itself is merely a dummy that results in a constraint specifying that there should be less than one point of space between the super view's bottom and the subview's top edge, which is always the case as long as the subview is visible. You can reverse the dimensions in the example to achieve centering in the vertical direction.\n\nAnother convenient helper when using the visual format language is the `NSDictionaryFromVariableBindings` macro, which we already used in the example above. You pass it a variable number of variables and get back a dictionary with the variable names as keys.\n\nFor layout tasks that you have to do over and over, it's very convenient to create your own helper methods. For example, if you often have to space out a couple of sibling views vertically with a fixed distance between them while aligning all of them horizontally at the leading edge, having a method like this makes your code less verbose:\n\n```objc\n@implementation UIView (AutoLayoutHelpers)\n+ leftAlignAndVerticallySpaceOutViews:(NSArray *)views \n                             distance:(CGFloat)distance \n{\n    for (NSUInteger i = 1; i < views.count; i++) {\n        UIView *firstView = views[i - 1];\n        UIView *secondView = views[i];\n        firstView.translatesAutoResizingMaskIntoConstraints = NO;\n        secondView.translatesAutoResizingMaskIntoConstraints = NO;\n\n        NSLayoutConstraint *c1 = constraintWithItem:firstView\n                                          attribute:NSLayoutAttributeBottom\n                                          relatedBy:NSLayoutRelationEqual\n                                             toItem:secondView\n                                          attribute:NSLayoutAttributeTop\n                                         multiplier:1\n                                           constant:distance];\n                                           \n        NSLayoutConstraint *c2 = constraintWithItem:firstView\n                                          attribute:NSLayoutAttributeLeading\n                                          relatedBy:NSLayoutRelationEqual\n                                             toItem:secondView\n                                          attribute:NSLayoutAttributeLeading\n                                         multiplier:1\n                                           constant:0];\n                                               \n        [firstView.superview addConstraints:@[c1, c2]];\n    }\n}\n@end\n```\n\nIn the meantime there are also many different Auto Layout helper libraries out there taking different approaches to simplifying constraint code.\n\n\n## Performance\n\nAuto Layout is an additional step in the layout process. It takes a set of constraints and translates them into frames. Therefore it naturally comes with a performance hit. In the vast majority of cases, the time it takes to resolve the constraint system is negligible. However, if you're dealing with very performance critical view code, it's good to know about it.\n\nFor example, if you have a collection view which has to bring several new cells on screen when a new row appears, and each cell consists of several subviews laid out by Auto Layout, you may notice the effect. Luckily, we don't need to rely on our gut feeling when scrolling up and down. Instead we can fire up Instruments and actually measure how much time Auto Layout spends. Watch out for methods of the `NSISEngine` class.\n\nAnother scenario where you might run into performance issues with Auto Layout is when you are showing lots of views at once. The [constraint solving algorithm](http://www.cs.washington.edu/research/constraints/cassowary/), which translates the constraints into view frames, is of [super-linear complexity](http://en.wikipedia.org/wiki/P_%28complexity%29). This means that from a certain number of views on, performance will become pretty terrible. The exact number depends on your specific use case and view configuration. But to give you a rough idea, on current iOS devices it's in the order of a magnitude of 100. For more details, you can also read these two [blog](http://floriankugler.com/blog/2013/4/21/auto-layout-performance-on-ios) [posts](http://pilky.me/view/36).\n\nKeep in mind that these are edge cases. Don't optimize prematurely and avoid Auto Layout for its potential performance impact. It will be fine for most use cases. But if you suspect it might cost you the decisive milliseconds to get the user interface completely smooth, profile your code and only then should you decide if it makes sense to go back to setting frames manually. Furthermore, hardware will become more and more capable, and Apple will continue tweaking the performance of Auto Layout. So the edge cases where it presents a real-world performance problem will decrease over time.\n\n\n## Conclusion\n\nAuto Layout is a powerful technique to create flexible user interfaces, and it's not going away anytime soon. Getting started with Auto Layout can be a bit rough, but there is light at the end of the tunnel. Once you get the hang of it and have all the little tricks to diagnose and fix problems up your sleeve, it actually becomes very logical to work with. \n\n\n\n[100]:/issues/3-views/advanced-auto-layout-toolbox/\n[110]:/issues/3-views/advanced-auto-layout-toolbox/#layout-process\n\n[200]:/issues/3-views/moving-pixels-onto-the-screen/\n[210]:/issues/3-views/moving-pixels-onto-the-screen/#compositing\n[220]:/issues/3-views/moving-pixels-onto-the-screen/#pixels\n[230]:/issues/3-views/moving-pixels-onto-the-screen/#off-screen-rendering\n[240]:/issues/3-views/moving-pixels-onto-the-screen/#planar-data\n[250]:/issues/3-views/moving-pixels-onto-the-screen/#concurrent-drawing\n[260]:/issues/3-views/moving-pixels-onto-the-screen/#resizable-images\n[270]:/issues/3-views/moving-pixels-onto-the-screen/#core-graphics\n\n[300]:/issues/3-views/collection-view-layouts/\n[310]:/issues/3-views/collection-view-layouts/#layout-attributes-for-...-at-index-path\n\n[400]:/issues/3-views/custom-controls/\n"
  },
  {
    "path": "2013-08-07-collection-view-layouts.md",
    "content": "---\ntitle: \"Custom Collection View Layouts\"\ncategory: \"3\"\ndate: \"2013-08-07 08:00:00\"\nauthor:\n  - name: Ole Begemann\n    url: http://oleb.net\ntags: article\n---\n\n\nIntroduced in iOS 6, `UICollectionView` is [the new star among view classes](http://oleb.net/blog/2012/09/uicollectionview/) in UIKit. It shares its API design with `UITableView` but extends the latter in a few fundamental ways. The most powerful feature of `UICollectionView` and the point where it significantly exceeds `UITableView`'s capabilities is its completely flexible layout architecture. In this article, we will implement a fairly complex custom collection view layout and discuss important aspects of the class's design along the way.\n\nThe [example project](https://github.com/objcio/issue-3-collection-view-layouts) for this article is on GitHub.\n\n## Layout Objects\n\nBoth `UITableView` and `UICollectionView` are [data-source- and delegate-driven](http://developer.apple.com/library/ios/#documentation/general/conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html). They act as dumb containers for the collection of subviews they are displaying, knowing nothing about their actual contents.\n\n`UICollectionView` takes the abstraction one step further. It delegates the control over its subviews' positions, sizes, and appearances to a separate layout object. By providing a custom layout object, you can achieve pretty much any layout you can imagine. Layouts inherit from the abstract base class [`UICollectionViewLayout`](http://developer.apple.com/library/ios/#documentation/uikit/reference/UICollectionViewLayout_class/Reference/Reference.html). iOS 6 comes with one concrete layout implementation in the form of the [`UICollectionViewFlowLayout` class](http://developer.apple.com/library/ios/#documentation/uikit/reference/UICollectionViewFlowLayout_class/Reference/Reference.html#//apple_ref/occ/cl/UICollectionViewFlowLayout).\n\nA flow layout can be used to implement a standard grid view, which is probably the most common use case for a collection view. Apple was smart enough to not actually name the class `UICollectionView`**`Grid`**`Layout`, even if that is how most of us think about it. The more generic term, _flow layout_, describes the class's capabilities much better: it builds its layout by placing cell after cell, inserting line or column breaks when needed. By customizing the scroll direction, sizing, and spacing of the cells, a flow layout can also layout cells in a single row or column. In fact, `UITableView`'s layout can be thought of as a special case of flow layout.\n\nBefore you consider writing your own `UICollectionViewLayout` subclass, you should always ask yourself if you can achieve the layout you have in mind with `UICollectionViewFlowLayout`. The class is [remarkably customizable](http://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html#//apple_ref/doc/uid/TP40012334-CH3-SW2) and can also be subclassed itself for further customization. See [Knowing When to Subclass the Flow Layout](http://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html#//apple_ref/doc/uid/TP40012334-CH3-SW4) in the Collection View Programming Guide for tips.\n\n## Cells and Other Views\n\nTo accommodate arbitrary layouts, collection views set up a view hierarchy that is similar to, but more flexible than, that of a table view. As usual, your main content is displayed in **cells**, which can optionally be grouped into sections. Collection view cells must be subclasses of [`UICollectionViewCell`](http://developer.apple.com/library/ios/#documentation/uikit/reference/UICollectionViewCell_class/Reference/Reference.html). In addition to cells, collection views manage two more kinds of views: supplementary views and decoration views.\n\n**Supplementary views** in a collection view correspond to a table view's section header and footer views in that they display information about a section. Like cells, their contents are driven by the data source object. Unlike their usage in table views, however, supplementary views are not bound to being header or footer views; their number and placement are entirely controlled by the layout.\n\n**Decoration views** act as pure ornamentation. They are owned and managed entirely by the layout object and do not get their contents from the data source. When a layout object specifies that it requires a decoration view, the collection view creates it automatically and applies the layout attributes provided by the layout object. Any customization of the view's contents is not intended.\n\nSupplementary and decoration views must be subclasses of [`UICollectionReusableView`](http://developer.apple.com/library/ios/#documentation/uikit/reference/UICollectionReusableView_class/Reference/Reference.html#//apple_ref/occ/cl/UICollectionReusableView). Each view class that your layout uses must be registered with the collection view to enable it to create new instances when its data source asks it to dequeue a view from its reuse pool. If you are using Interface Builder, registering cells with a collection view can be done directly inside the visual editor by dragging a cell onto a collection view. The same method works for supplementary views, but only if you are using a `UICollectionViewFlowLayout`. If not, you have to manually register the view classes with the collection view in code by calling one of its [`registerClass:…`](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/Reference/Reference.html#//apple_ref/occ/instm/UICollectionView/registerClass:forCellWithReuseIdentifier:) or [`registerNib:…`](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/Reference/Reference.html#//apple_ref/occ/instm/UICollectionView/registerNib:forCellWithReuseIdentifier:) methods. The `viewDidLoad` method in your view controller is the correct place to do this.\n\n## Custom Layouts\n\nAs an example of a non-trivial custom collection view layout, consider a week view in a typical calendar app. The calendar displays one week at a time, with the days of the week arranged in columns. Each calendar event will be displayed by a cell in our collection view, positioned and sized so as to represent the event start date/time and duration.\n\n![Screenshot of our custom calendar collection view layout](/images/issue-3/calendar-collection-view-layout.png)\n\nThere are two general types of collection view layouts:\n\n1. **Layouts whose computations are independent of the content.** This is the “simple” case you know from `UITableView` and `UICollectionViewFlowLayout`. The position and appearance of each cell does not depend on the content it displays but on its order in the list of all cells. Consider the default flow layout as an example. Each cell is positioned next to its predecessor (or at the beginning of the next line if there is no space left). The layout object does not need to access the actual data to compute the layout.\n\n2. **Layouts that need to do content-dependent computations.** Our calendar view is an example of this type. It requires the layout object to ask the collection view's data source directly for the start and end dates of the events it is supposed to display. In many cases, the layout object not only requires data about the currently visible cells but needs some information from _all_ records in order to determine which cells are currently visible in the first place.\n\nIn our calendar example, the layout object -- if asked for the attributes of the cells inside a certain rectangle -- must iterate over all events provided by the data source to determine which ones lie in the requested time window. Contrast this with a flow layout where some relatively simple and data-source-independent math is sufficient to compute the index paths of the cells that lie in a certain rectangle (assuming that all cells in the grid have the same size).\n\nHaving a content-dependent layout is a strong indication that you will need to write your own custom layout class and won't get by with customizing `UICollectionViewFlowLayout`. So that's exactly what we are going to do.\n\nThe [documentation for `UICollectionViewLayout`](http://developer.apple.com/library/ios/#documentation/uikit/reference/UICollectionViewLayout_class/Reference/Reference.html) lists the methods that subclasses should override.\n\n### collectionViewContentSize\n  \nSince the collection view does not know anything about its content, the first piece of information the layout must provide is the size of the scroll area so that the collection view can properly manage scrolling. The layout object must compute the total size of its contents here, including all supplementary and decoration views. Note that although most “classic” collection views limit scrolling to one axis (and so does `UICollectionViewFlowLayout`), this is not a requirement.\n\nIn our calendar example, we want the view to scroll vertically. For instance, if we want one hour to take up 100 points of vertical space, the content height to display an entire day should be 2,400 points. Notice that we do not enable horizontal scrolling, which means that our collection view displays only one week. To enable paging between multiple weeks in the calendar, we could embed multiple collection views (one per week) in a separate (paged) scroll view (possibly using [`UIPageViewController`](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPageViewControllerClassReferenceClassRef/UIPageViewControllerClassReference.html) for the implementation), or stick with just one collection view and return a content width that is large enough to let the user scroll freely in both directions. This is beyond the scope of this article, though.\n\n```objc\n- (CGSize)collectionViewContentSize\n{\n    // Don't scroll horizontally\n    CGFloat contentWidth = self.collectionView.bounds.size.width;\n\n    // Scroll vertically to display a full day\n    CGFloat contentHeight = DayHeaderHeight + (HeightPerHour * HoursPerDay);\n\n    CGSize contentSize = CGSizeMake(contentWidth, contentHeight);\n    return contentSize;\n}\n```\n\nNote that for clarity reasons, I have chosen to model the layout on a very simple model that assumes a constant number of days per week and hours per day and represents days just as indices from 0 to 6. In a real calendar application, the layout would make heavy use of `NSCalendar`-based date calculations for its computations.\n\n### layoutAttributesForElementsInRect:\n\nThis is the central method in any layout class and possibly the one that is most confusing. The collection view calls this method and passes a rectangle in its own coordinate system. This rectangle will typically be the visible rectangle of the view (that is, its bounds) but that is not necessarily the case. You should be prepared to handle any rectangle that gets passed to you.\n\nYour implementation must return an array of [`UICollectionViewLayoutAttributes`](http://developer.apple.com/library/ios/#documentation/uikit/reference/UICollectionViewLayoutAttributes_class/Reference/Reference.html) objects, containing one such object for each cell, supplementary, or decoration view that is visible in the rectangle. The `UICollectionViewLayoutAttributes` class encapsulates all layout-related properties of an item in the collection view. By default, the class has properties for the `frame`, `center`, `size`, `transform3D`, `alpha`, `zIndex`, and `hidden` attributes. If your layout wants to control other attributes of a view (for example, the background color), you can subclass `UICollectionViewLayoutAttributes` and add your own properties.\n\nThe layout attributes objects are associated with their corresponding cell, supplementary view, or decoration view through an `indexPath` property. After the collection view has asked the layout object for the layout attributes of all items, it will instantiate the views and apply the respective attributes to them.\n\nNote that this one method is concerned with all types of views, that is, cell, supplementary, and decoration views. A naive implementation might opt to ignore the passed-in rectangle and just return the layout attributes for _all_ views in the collection view. This is a valid approach during prototyping and developing your layout. But note that this can have a bad impact on performance, especially if the number of total cells is much larger than those that are visible at any one time, as the collection view and the layout object will have to perform additional unnecessary work for these invisible views.\n\nYour implementation should perform these steps:\n\n1. Create an empty mutable array to contain all the layout attributes.\n\n2. Identify the index paths of all cells whose frames lie entirely or partly within the rectangle. This computation may require you to ask the collection view's data source for information about the data you want to display. Then call your implementation of [`layoutAttributesForItemAtIndexPath:`][310] in a loop to create and configure a proper layout attributes object for each index path. Add each object to the array.\n\n3. If your layout includes supplementary views, compute the index paths of the ones that are visible inside the rectangle. Call your implementation of [`layoutAttributesForSupplementaryViewOfKind:atIndexPath:`][310] in a loop and add those objects to the array. By passing different strings of your choice for the `kind` argument, you can distinguish between different types of supplementary views (such as headers and footers). The collection view will pass the `kind` string back to your data source when it needs to create the view. Remember that the number and kind of supplementary and decoration views is entirely controlled by the layout. You are not restricted to headers and footers.\n\n4. If your layout includes decoration views, compute the index paths of the ones that are visible inside the rectangle. Call your implementation of [`layoutAttributesForDecorationViewOfKind:atIndexPath:`][310] in a loop and add those objects to the array.\n\n5. Return the array.\n\nOur custom layout uses no decoration views but two kinds of supplementary views (column headers and row headers):\n\n```objc\n- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect\n{\n    NSMutableArray *layoutAttributes = [NSMutableArray array];\n\n    // Cells\n    // We call a custom helper method -indexPathsOfItemsInRect: here\n    // which computes the index paths of the cells that should be included\n    // in rect.\n    NSArray *visibleIndexPaths = [self indexPathsOfItemsInRect:rect];\n    for (NSIndexPath *indexPath in visibleIndexPaths) {\n        UICollectionViewLayoutAttributes *attributes = \n            [self layoutAttributesForItemAtIndexPath:indexPath];\n        [layoutAttributes addObject:attributes];\n    }\n\n    // Supplementary views\n    NSArray *dayHeaderViewIndexPaths = \n        [self indexPathsOfDayHeaderViewsInRect:rect];\n    for (NSIndexPath *indexPath in dayHeaderViewIndexPaths) {\n        UICollectionViewLayoutAttributes *attributes =\n            [self layoutAttributesForSupplementaryViewOfKind:@\"DayHeaderView\"\n                                                 atIndexPath:indexPath];\n        [layoutAttributes addObject:attributes];\n    }\n    NSArray *hourHeaderViewIndexPaths =\n        [self indexPathsOfHourHeaderViewsInRect:rect];\n    for (NSIndexPath *indexPath in hourHeaderViewIndexPaths) {\n        UICollectionViewLayoutAttributes *attributes =\n            [self layoutAttributesForSupplementaryViewOfKind:@\"HourHeaderView\"\n                                                 atIndexPath:indexPath];\n        [layoutAttributes addObject:attributes];\n    }\n\n    return layoutAttributes;\n}\n```\n\n<a name=\"layout-attributes-for-...-at-index-path\"> </a>\n\n### layoutAttributesFor...IndexPath\n\nSometimes, the collection view will ask the layout object for the layout attributes of one specific cell, supplementary, or decoration view rather than the list of all visible ones. This is when three other methods come into play. Your implementation of [`layoutAttributesForItemAtIndexPath:`](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewLayout_class/Reference/Reference.html#//apple_ref/occ/instm/UICollectionViewLayout/layoutAttributesForItemAtIndexPath:) should create and return a single layout attributes object that is properly formatted for the cell identified by the index path that is passed to you.\n\nYou do this by calling the [`+[UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:]`](http://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionViewLayoutAttributes_class/Reference/Reference.html#//apple_ref/occ/clm/UICollectionViewLayoutAttributes/layoutAttributesForCellWithIndexPath:) factory method. Then modify the attributes according to the index path. You may need to ask the collection view's data source for information about the data object that is displayed at this index path to get the data you need. Make sure to at least set the `frame` property here unless all your cells should sit on top of each other.\n\n```objc\n- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:\n    (NSIndexPath *)indexPath\n{\n    CalendarDataSource *dataSource = self.collectionView.dataSource;\n    id<CalendarEvent> event = [dataSource eventAtIndexPath:indexPath];\n    UICollectionViewLayoutAttributes *attributes = \n        [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];\n    attributes.frame = [self frameForEvent:event];\n    return attributes;\n}\n```\n\nIf you are using Auto Layout, you may be surprised that we are modifying the `frame` property of the layout attributes directly rather than working with constraints, but that is how `UICollectionViewLayout` works. Although you would use Auto Layout to define the collection view's frame and the internal layout of each cell, the frames of the cells have to be computed the old-fashioned way.\n\nSimilarly, the methods `layoutAttributesForSupplementaryViewOfKind:atIndexPath:` and `layoutAttributesForDecorationViewOfKind:atIndexPath:` should do the same for supplementary and decoration views, respectively. Implementing these two methods is only required if your layout includes such views. `UICollectionViewLayoutAttributes` contains two more factory methods, [`+layoutAttributesForSupplementaryViewOfKind:withIndexPath:`](http://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionViewLayoutAttributes_class/Reference/Reference.html#//apple_ref/occ/clm/UICollectionViewLayoutAttributes/layoutAttributesForSupplementaryViewOfKind:withIndexPath:) and [`+layoutAttributesForDecorationViewOfKind:withIndexPath:`](http://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionViewLayoutAttributes_class/Reference/Reference.html#//apple_ref/occ/clm/UICollectionViewLayoutAttributes/layoutAttributesForDecorationViewOfKind:withIndexPath:), to create the correct layout attributes object.\n\n### shouldInvalidateLayoutForBoundsChange:\n\nLastly, the layout must tell the collection view if it needs to recompute the layout when the collection view's bounds change. My guess is that most layouts need to be invalidated when the collection view resizes, for example during device rotation. Hence, a naive implementation of this method would simply return `YES`. It is important to realize, however, that a scroll view's bounds also change during scrolling, which means your layout could be invalidated several times per second. Depending on the complexity of the computations, this could have a sizable performance impact.\n\nOur custom layout must be invalidated when the collection view's width changes but is not affected by scrolling. Fortunately, the collection view passes its new bounds to the `shouldInvalidateLayoutForBoundsChange:` method. This enables us to compare the view's current bounds to the new value and only return `YES` if we have to:\n\n```objc\n- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds\n{\n    CGRect oldBounds = self.collectionView.bounds;\n    if (CGRectGetWidth(newBounds) != CGRectGetWidth(oldBounds)) {\n        return YES;\n    }\n    return NO;\n}\n```\n\n## Animation\n\n### Insertions and Deletions\n\n`UITableView` comes with a set of very nice pre-defined animations for cell insertions and deletions. When adding animation functionality for adding and removing cells to `UICollectionView`, UIKit engineers were faced with a problem: if the collection view's layout is entirely flexible, there is no way some pre-defined animations will play well with developers' custom layouts. The solution they came up with is very elegant: when a cell (or supplementary or decoration view) gets inserted into a collection view, the view asks its layout object not only for the cell's “normal” layout attributes but also for its _initial_ layout attributes, i.e., the attributes the cell should have at the beginning of the insertion animation. The collection view then simply creates an animation block in which it changes all cell's attributes from their _initial_ to their “normal” state.\n\nBy supplying different _initial_ layout attributes, you can completely customize the insertion animation. For example, setting the _initial_ `alpha` to `0` will create a fade-in animation. Setting a translation and scale transform at the same time will move and zoom the cell into place.\n\nThe same principle is applied to deletions, this time animating from the “normal” state to a set of _final_ layout attributes you provide. These are the methods you have to implement in your layout class to provide the initial/final layout attributes:\n\n* `initialLayoutAttributesForAppearingItemAtIndexPath:`\n* `initialLayoutAttributesForAppearingSupplementaryElementOfKind:atIndexPath:`\n* `initialLayoutAttributesForAppearingDecorationElementOfKind:atIndexPath:`\n* `finalLayoutAttributesForDisappearingItemAtIndexPath:`\n* `finalLayoutAttributesForDisappearingSupplementaryElementOfKind:atIndexPath:`\n* `finalLayoutAttributesForDisappearingDecorationElementOfKind:atIndexPath:`\n\n### Switching Between Layouts\n\nChanges from one collection view layout to another can be animated in a similar manner. When sent a `setCollectionViewLayout:animated:` method, the collection view will query the new layout for the new layout attributes of the cells and then animate each cell (identified by the same index path in the old and the new layout) from its old to its new attributes. You don't have to do a thing.\n\n## Conclusion\n\nDepending on the complexity of a custom collection view layout, writing one is often not easy. In fact, it is essentially just as difficult as writing a totally custom view class that implements the same layout from scratch, since the computations that are involved to determine which subviews are currently visible and where they are positioned are identical. Nevertheless, using `UICollectionView` gives you some nice benefits such as cell reuse and automatic support for animations, not to mention the clean separation of layout, subview management, and data preparation its architecture prescribes.\n\nA custom collection view layout is also a nice step toward a [lighter view controller](/issues/1-view-controllers/lighter-view-controllers/) as your view controller does not contain any layout code. Combine this with a separate datasource class as explained in Chris' article and the view controller for a collection view will hardly contain any code at all.\n\nWhenever I use `UICollectionView`, I feel a certain admiration for its clean design. `NSTableView` and `UITableView` probably needed to come first in order for an experienced Apple engineer to come up with such a flexible class.\n\n### Further Reading\n\n* [Collection View Programming Guide](http://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40012334-CH1-SW1).\n* [NSHipster on `UICollectionView`](http://nshipster.com/uicollectionview/).\n* [`UICollectionView`: The Complete Guide](http://ashfurrow.com/uicollectionview-the-complete-guide/), e-book by Ash Furrow.\n* [`MSCollectionViewCalendarLayout`](https://github.com/monospacecollective/MSCollectionViewCalendarLayout) by Eric Horacek is an excellent and more complete implementation of a custom layout for a week calendar view.\n\n\n\n[100]:/issues/3-views/advanced-auto-layout-toolbox/\n[110]:/issues/3-views/advanced-auto-layout-toolbox/#layout-process\n\n[200]:/issues/3-views/moving-pixels-onto-the-screen/\n[210]:/issues/3-views/moving-pixels-onto-the-screen/#compositing\n[220]:/issues/3-views/moving-pixels-onto-the-screen/#pixels\n[230]:/issues/3-views/moving-pixels-onto-the-screen/#off-screen-rendering\n[240]:/issues/3-views/moving-pixels-onto-the-screen/#planar-data\n[250]:/issues/3-views/moving-pixels-onto-the-screen/#concurrent-drawing\n[260]:/issues/3-views/moving-pixels-onto-the-screen/#resizable-images\n[270]:/issues/3-views/moving-pixels-onto-the-screen/#core-graphics\n\n[300]:/issues/3-views/collection-view-layouts/\n[310]:/issues/3-views/collection-view-layouts/#layout-attributes-for-...-at-index-path\n\n[400]:/issues/3-views/custom-controls/\n"
  },
  {
    "path": "2013-08-07-custom-controls.markdown",
    "content": "---\ntitle:  \"Custom Controls\"\ncategory: \"3\"\ndate: \"2013-08-07 07:00:00\"\nauthor:\n  - name: Chris Eidhof\n    url: http://twitter.com/chriseidhof\ntags: article\n---\n\nIn this article we will look at tips and tricks to write custom views and\ncontrols.\nWe will start with an overview of what UIKit provides us already, and\nsee some tricks for rendering. We will dive into communication\nstrategies between views and their owners, and very briefly look at\naccessibility, localization and testing.\n\n\n## Overview of the View Hierarchy\n\nIf you look at any UIView subclass, you will see three base classes:\nresponders, views, and controls. We'll quickly go over all three to see what is going on.\n\n### UIResponder\n\nThe `UIResponder` class is the superclass of `UIView`. A responder can\nhandle events such as touches, motion, and remote control events. \nThe reason that this is a separate class, and not merged into `UIView`,\nis that there are more subclasses of `UIResponder`, most notably\n`UIApplication` and `UIViewController`. By overriding the\nmethods in `UIResponder`, a class can determine whether it can become a\nfirst responder (i.e. the currently focused element for input).\n\nWhen interface events happen, such as touches or motion, they get sent\nto the first responder (often, this is a view). When the event\ndoes not get handled by the first responder, it goes up the responder chain to the view\ncontroller, and if it still doesn't get handled, it continues to the application. If\nyou want to detect a shake gesture, you could do this in all three of these levels,\ndepending on your needs.\n\nThe `UIResponder` also lets you customize the input methods, from adding\nan accessory view to the keyboard with `inputAccessoryView` to providing\na completely custom keyboard by using `inputView`.\n\n### UIView\n\nThe `UIView` subclass handles everything related to drawing \ncontent and handling touches. Anybody who has built a \"Hello, World\" app\nknows about views, but let's reiterate some of the tricky bits:\n\nA common misconception is that this area is defined by the view's frame.\nIn reality, the frame is actually a property that is derived, most notably from the\ncombination of center and bounds. When not doing Auto Layout, most\npeople use the frame to position and size the view. Be warned, because [the\ndocumentation](https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/frame) spells out a caveat:\n\n> If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.\n\nOne of the other things that lets you add interactivity to views is\ngesture recognizers. Note that they don't work on responders, but instead only on\nviews and their subclasses.\n\n### UIControl\n\nBuilding on views, the `UIControl` class adds more support for\ninteractivity. Most importantly, it adds the target/action pattern.\nLooking at the concrete subclasses, we can see buttons, date pickers,\ntext fields, and more. When creating interactive controls, you often\nwant to subclass a descendant of `UIControl`.\nSome notable classes that are not controls are bar buttons (although\nthey do support target/action) and text views (here, getting notified\nrequires you to use a delegate).\n  \n## Rendering\n\nNow, let's move on to the visual bit: custom rendering. As Daniel mentioned\nin [his article](/issue-3/moving-pixels-onto-the-screen.html), you probably want to avoid doing rendering on the CPU, but\ninstead offload it to the GPU. There is one rule of thumb to achieve this:\ntry to avoid `drawRect:`, and instead compose your custom views out of\nexisting views.\n\nOften, the quickest way to render something\nis just by using image views. For example, let's suppose that you want\nto draw round avatars and a border, such as in the picture below:\n\n![Rounded Image View](/images/issue-3/issue-3-rounded-corners@2x.png)\n\nTo achieve this, we created an image view subclass with the following\ncode:\n\n```objc\n// called from initializer\n- (void)setupView\n{\n    self.clipsToBounds = YES;\n    self.layer.cornerRadius = self.bounds.size.width / 2;\n    self.layer.borderWidth = 3;\n    self.layer.borderColor = [UIColor darkGrayColor].CGColor;\n}\n```\n\nI would like to encourage you to dive into `CALayer` and its properties,\nbecause most of what you can achieve with that will be faster than\ndrawing your own things using Core Graphics. Nonetheless, as always, it is\nimportant to profile your code.\n\nBy using stretchable images together with your image views, you can also\ngreatly improve performance. In a post called [Taming\nUIButton](http://robots.thoughtbot.com/post/33427366406/designing-for-ios-taming-uibutton),\nReda Lemeden explores different ways of drawing. At the end of\nthe article there's a nugget of gold: a link to [a comment by Andy\nMatuschak](https://news.ycombinator.com/item?id=4645585) on Hacker News,\nexplaining which is the fastest of these techniques: a resizable image.\nThe reason is because a resizable image takes a minimum amount of\ndata transfer between the CPU and GPU, and the drawing of these images is\nhighly optimized.\n\nIf you are processing images, you can also often get away with letting the\nGPU do that for you, instead of doing it with Core Graphics. Using Core\nImage, you can create complicated effects on images without having to\ndo any rendering on the CPU. You can render directly to an OpenGL\ncontext, and everything will happen on the GPU.\n\n### Custom Drawing\n\nIf you do decide to do custom drawing, there are several different\noptions you can choose from. If possible, see if you can generate an image, and\nthen cache that, either on disk or in memory. If your content\nis very dynamic, you can maybe use Core Animation, or if it doesn't\nwork, go for Core Graphics. If you really want to get close to the\nmetal, it is not that hard to use GLKit and raw OpenGL, but it does require a lot of\nwork.\n\nIf you do choose to override `drawRect:`, make sure to take a\nlook at content modes. The default mode scales the content to fill the\nview's bounds, and does not get redrawn when the frame changes.\n\n## Custom Interaction\n\nAs said, when writing custom controls, you almost always want to\nextend a subclass of UIControl. In your subclass, you can fire events\nusing the target action mechanism, as shown in this example:\n\n```objc\n[self sendActionsForControlEvents:UIControlEventValueChanged];\n```\n\nTo respond to touches, you almost always want to use gesture\nrecognizers. However, if you want to go low-level, you can still\noverride the methods `touchesBegan`, `touchesMoved`, and `touchesEnded`\nto get access to the raw touches. That said, to separate the gesture\nhandling from your custom view or view controller, it is almost always\nmore appropriate to create a gesture recognizer subclass.\n\nOne common design problem you face when creating custom controls is\ncommunicating back the value to the classes that own them. For example,\nsuppose you want to create a custom control for drawing interactive pie\ncharts, and you want to know when the user selected a sector. You can\nsolve this in a lot of different ways, by using target-action, delegates,\nblocks or key-value observing, or even notifications.\n\n### Use Target-Action\n\nThe old-school way, and often the most convenient, is to use\ntarget-action. After the selection, you would do something like this in\nyour custom view:\n\n```objc\n[self sendActionsForControlEvents:UIControlEventValueChanged];\n```\n\nIf you have a view controller that manages this view, it would do\nsomething like this:\n\n```objc\n - (void)setupPieChart\n {\n    [self.pieChart addTarget:self \n                      action:@selector(updateSelection:)\n            forControlEvents:UIControlEventValueChanged];\n }\n \n - (void)updateSelection:(id)sender\n {\n    NSLog(@\"%@\", self.pieChart.selectedSector);\n }\n```\n\nThe advantage is that you need to do very little in your custom view\nsubclass, and you automatically get support for multiple targets.\n\n### Use Delegates\n\nIf you need more control over the kind of messages being sent from\nthe view to the controller, it is often handy to\nuse the delegate pattern. In case of our pie chart, it would look\nsomething like this:\n\n```objc\n[self.delegate pieChart:self didSelectSector:self.selectedSector];\n```\n\nAnd in the view controller, you would write code like so:\n\n\n```objc\n @interface MyViewController <PieChartDelegate>\n\n ...\n\n - (void)setupPieChart\n {\n     self.pieChart.delegate = self;\n }\n \n - (void)pieChart:(PieChart*)pieChart didSelectSector:(PieChartSector*)sector\n {\n     // Handle the sector\n }\n```\n\nThis is nice when you want to do more complicated things than just\nletting the owner know that the value changed. Even though most\nprogrammers can write custom delegates very quickly, there are also some\ndrawbacks: you might need to check if your delegate implements the\nmethod you want to call (using `respondsToSelector:`), and most\nimportantly, you can typically only have one delegate (or you need to create an\narray of delegates). That said, once the communication between a view's\nowner and the view gets a bit more complicated, this is the pattern we\nalmost always resort to.\n\n### Use Blocks\n\nAnother option you have is to use blocks. Again, in case of the pie\nchart, it would look something like this:\n\n```objc\n@interface PieChart : UIControl\n\n@property (nonatomic,copy) void(^selectionHandler)(PieChartSection* selectedSection);\n\n@end\n```\n\nThen, in the selection code, you would just call it. It is important to\ncheck if the block is set, because calling a block that is not set will\ncrash.\n\n```objc\nif (self.selectionHandler != NULL) {\n  self.selectionHandler(self.selectedSection);\n}\n```\n\nThe advantage of setting things up this way is that you can group\nrelated code together in the view controller:\n\n```objc\n- (void)setupPieChart\n{\n   self.pieChart.selectionHandler = ^(PieChartSection* section) {\n      // Do something with the section\n   }\n}\n```\n\nJust like with delegates, you typically have only one block per\naction. Another more important limitation is that you don't want to\ncreate retain cycles. If your view controller holds a strong reference\nto the pie chart, and the pie chart to the block, and the block to the\nview controller, you've created a retain cycle. To make this mistake, you only\nneed to reference self in the block. So you often end up with code like\nthis:\n\n```objc\n__weak id weakSelf = self;\nself.pieChart.selectionHandler = ^(PieChartSection* section) {\n   MyViewController* strongSelf = weakSelf;\n   [strongSelf handleSectionChange:section];\n}\n```\n\nOnce the block bodies get out of hand, you will also probably extract\nthem to methods of their own, and then you might as well have used\ndelegates.\n\n### Use KVO\n\nIf you like KVO, you can also use this for observing. It's a bit more\nmagical and less direct, but when you are already using it in your\napplication, it's a nice pattern to decouple things. In your pie chart\nclass, you would do this:\n\n```objc\nself.selectedSegment = theNewSelectedSegment.\n```\n\nWhen you use synthesized properties, KVO will pick up this change and\nsend notifications. In your view controller, you would do something like\nthis:\n\n```objc\n- (void)setupPieChart\n{\n    [self.pieChart addObserver:self forKeyPath:@\"selectedSegment\" options:0 context:NULL];\n}\n\n- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context \n{\n  if(object == self.pieChart && [keyPath isEqualToString:@\"selectedSegment\"]) {\n     // Handle change\n  }\n}\n```\n\nYou also need to remove the observer, for example, in `viewWillDisappear:` or `dealloc`, depending on your use case. Observing multiple children from the same object quickly gets messy. There are some techniques for dealing with this, such as [ReactiveCocoa](https://github.com/ReactiveCocoa/ReactiveCocoa) or the more lightweight [`THObserversAndBinders`](https://github.com/th-in-gs/THObserversAndBinders).\n\n### Use Notifications\n\nAs a final option, if you want a very loose coupling, you can use\nnotifications for letting other objects know about changes. In case of the pie chart\nyou almost certainly wouldn't want this, but for completeness, here is\nhow you would do it. In the pie chart's header file:\n\n```objc\nextern NSString* const SelectedSegmentChangedNotification;\n```\n\nAnd in the implementation:\n\n```objc\nNSString* const SelectedSegmentChangedNotification = @\"selectedSegmentChangedNotification\";\n\n...\n\n- (void)notifyAboutChanges\n{\n   [[NSNotificationCenter defaultCenter] postNotificationName:SelectedSegmentChangedNotification object:self];\n}\n```\n\nNow, to subscribe to notifications, you do the following in your view\ncontroller:\n\n```objc\n- (void)setupPieChart\n{\n  [[NSNotificationCenter defaultCenter] addObserver:self \n                                           selector:@selector(segmentChanged:) \n                                               name:SelectedSegmentChangedNotification\n                                             object:self.pieChart];\n\n}\n```\n\n \n```objc\n...\n\n- (void)segmentChanged:(NSNotification*)note\n{\n}\n```\n\nWhen you add the observer, instead of passing in the pie chart as the `object`, you could\nalso pass in `nil` and receive notifications from all pie chart objects.\nJust like with KVO notifications, you also need to unsubscribe from\nthese notifications in the appropriate place.\n\nThe advantage of this technique is that it's quite decoupled. On the other hand,\nyou lose type safety, because you get a notification object in your\ncallback, and unlike with delegation, the compiler can't check if the\ntypes between the notification sender and the notification receiver\nmatch.\n\n## Accessibility\n\nThe standard iOS controls provided by Apple are all accessible. This is another reason to create your custom controls out of standard controls.\n\nThis is probably the topic of an entire issue, but \nif you write a custom view, the [Accessibility Programming Guide](http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/iPhoneAccessibility/Accessibility_on_iPhone/Accessibility_on_iPhone.html#//apple_ref/doc/uid/TP40008785-CH100-SW3) explains how to make the controls accessible. Most notably, if you have a view that has multiple elements inside it that should be accessible, but are not subviews, then you can implement the `UIAccessibilityContainer` protocol for your view. For each element, you then return a `UIAccessibilityElement` object that describes it.\n\n## Localization\n\nWhen creating custom views, it is also important to think about localization. Like accessibility, this could be the topic of an entire issue. The most straightforward thing to localize in your custom views is string contents. If you use `NSString`, you don't have to worry about encoding. If you display dates or numbers in your custom views, use the date and number formatter classes to display them. To localize strings, use `NSLocalizedString`.\n\nAnother really helpful tool in localization is Auto Layout. For example, some words that are short in English might be very long in German. If you hard-code the size of your view based on the word size in English, you will almost certainly run into trouble when translating to German. By using Auto Layout, you can make this quite easy by letting labels adjust to the size of their contents, and adding other constraints on dependent elements to make sure they resize too. Apple provides a good [introduction](http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/RoadMapiOS/chapters/InternationalizeYourApp/InternationalizeYourApp/InternationalizeYourApp.html) for this. Also, with right-to-left languages such as Hebrew, your entire view will display right-to-left automatically if you use leading and trailing attributes, instead of hardcoding left and right.\n\n## Testing\n\nFinally, let's consider testing your views. For unit testing, you can use the default tools provided in Xcode or any of the third-party frameworks. In addition, you can use UIAutomation or any of the tools built on top of that. For this, it is essential that your views are fully accessible. An underused feature of UIAutomation is taking screenshots; you can do this to make sure your views are really pixel-perfect by [automatically comparing them](http://jeffkreeftmeijer.com/2011/comparing-images-and-creating-image-diffs/) with the design specification. (And on an unrelated note: you can also use this to [automatically generate the screenshots](http://www.smallte.ch/blog-read_en_29001.html) for the App Store, which is especially useful if you have multiple apps in multiple languages).\n"
  },
  {
    "path": "2013-08-07-editorial.md",
    "content": "---\r\ntitle:  \"Editorial\"\r\ncategory: \"3\"\r\ndate: \"2013-08-07 11:00:00\"\r\ntags: editorial\r\n---\r\n\r\nWelcome to objc.io issue #3!\r\n\r\nThis issue is all about the view layer. Of course the view layer has so many aspects that we had to narrow it down to a few topics -- which are hopefully of interest to many developers -- while leaving tons of other interesting stuff aside (perhaps to be addressed in a later issue?).\r\n\r\nFirst, we cover some internals of how pixels actually get drawn onto the screen. Aside from it simply being fascinating to take a sneak peak at the full stack under the covers of the frameworks we use everyday, it will help application developers to make smart choices when it comes to graphic performance. Similarly, [Joe Conway](http://stablekernel.com) -- author of the [Big Nerd Ranch's iOS Programming Guide](http://www.bignerdranch.com/book/ios_programming_the_big_nerd_ranch_guide_rd_edition_) -- takes a closer look at how scroll views take advantage of `UIView`'s architecture to get their work done.  \r\n\r\nBalancing these topics of internals, we then dive into a series of very practical view layer details: creating custom collection view layouts (many thanks to [Ole Begemann](http://oleb.net)!), creating custom controls, and advanced Auto Layout tips and tricks.\r\n\r\nAs always, we are happy to [hear from you](mailto:mail@objc.io) with feedback, suggestions for future topics, or if you would like to contribute an article to objc.io yourself (you don't need to be a native speaker, thanks to our copy editor [Natalye](https://twitter.com/deutschbitte)).\r\n\r\nWe wish you happy reading from a very summery Berlin,\r\n\r\nChris, Daniel, and Florian.\r\n"
  },
  {
    "path": "2013-08-07-moving-pixels-onto-the-screen.md",
    "content": "---\ntitle: \"Getting Pixels onto the Screen\"\ncategory: \"3\"\ndate: \"2013-08-07 10:00:00\"\nauthor:\n  - name: Daniel Eggert\n    url: http://twitter.com/danielboedewadt\ntags: article\n---\n\n\n\nHow does a pixel get onto the screen? There are many ways to get something onto the display and they involve many different frameworks and many different combinations of functions and methods. Here we'll walk through some of the things that happen behind the scenes. We hope this will help you understand which API works best when you need to determine when and how to debug and fix performance problems. We'll focus on iOS, however most of what is discussed will apply to OS X as well.\n\n\n## Graphics Stack\n\n\nThere's a lot going on under the hood when pixels have to get onto the screen. But once they're on the screen, each pixel consists of three color components: red, green, and blue. Three individual color cells light up with a particular intensity to give the impression of a single pixel with a specific color. On your iPhone 5, the [liquid-crystal display](https://en.wikipedia.org/wiki/IPS_LCD) has 1,136×640 = 727,040 pixels and hence 2,181,120 color cells. On a 15\" MacBook Pro with Retina Display this number is just above 15.5 million. The entire graphics stack works together to make sure each one lights up with the correct intensity. And when you scroll in full screen, all those million intensities have to update 60 times per second. That's a lot of work.\n\n### The Software Components\n\nIn a simplified view, the software stack looks somewhat like this:\n\n![Software Stack](/images/issue-3/pixels-software-stack@2x.png)\n\n\nJust above the display sits the GPU, the *graphics processing unit*. The GPU is a highly concurrent processing unit, which is tailored specifically for parallel computation of graphics. That's how it's possible to update all those pixels and push the result onto the display. Its parallel nature also allows it to do compositing of textures onto each other very efficiently. We'll talk about [compositing][210] in more detail in a bit. The key point is that the GPU is extremely specialized and therefore it is efficient at some kinds of work, i.e. it's very fast and uses less power than the CPU for this work. The 'normal' CPU has a very general purpose; it can do many different things, but compositing, for example, would be way slower on the CPU.\n\nThe GPU driver is the piece of code that talks directly to the GPU. Different GPUs are different beasts, and the driver makes them appear more uniform to the next layer, which is typically OpenGL / OpenGL ES.\n\nOpenGL ([Open Graphics Library](http://en.wikipedia.org/wiki/OpenGL)) is an API for rendering 2D and 3D graphics. Since the GPU is a very specialized piece of hardware, OpenGL works very closely with the GPU to facilitate the GPU's capabilities and to achieve hardware-accelerated rendering. To many, OpenGL may seem very low-level, but when it was first released in 1992 (more than twenty years ago) it was the first major standardized way to talk to graphics hardware (the GPU), and hence a major leap forward since programmers no longer had to rewrite their apps for each GPU.\n\nAbove OpenGL things split out a bit. On iOS nearly everything goes through Core Animation at this point, while on OS X, it's not uncommon for Core Graphics to bypass Core Animation. For some specialized applications, particularly games, the app might talk directly to OpenGL / OpenGL ES. And things get even more confusing, because Core Animation uses Core Graphics for some of its rendering. Frameworks like AVFoundation, Core Image, and others access a mix of everything. \n\nOne thing to keep in mind, though, is this: The GPU is a very powerful piece of graphics hardware and it plays a central role in displaying your pixels. It is connected to the CPU. In hardware there's some sort of [bus](https://en.wikipedia.org/wiki/Bus_%28computing%29) between the two -- and there are frameworks such as OpenGL, Core Animation, and Core Graphics that orchestrate the transfer of data between the GPU and the CPU. In order for your pixels to make it onto the screen, some processing will be done on the CPU. Then data will be transferred to the GPU, which, in turn, will also do processing, and finally your pixels show up on screen.\n\nEach part of this \"journey\" has its own challenges, and there are tradeoffs to be made along the way.\n\n### The Hardware Players\n\n&nbsp;\n\n![Graphics Hardware](/images/issue-3/pixels%2C%20hardware%402x.png)\n\nA very simplified view of the challenges looks like this: The GPU has textures (bitmaps) that it composites together for each frame (i.e. 60 times a second). Each texture takes up VRAM (video RAM) and therefore there's a limit to how many textures the GPU can hold onto. The GPU is super efficient at compositing, but certain compositing tasks are more complex than others, and there's a limit to how much work the GPU can do in 16.7 ms (1/60 s).\n\nThe next challenge is getting your data to the GPU. In order for the GPU to access data, it needs to be moved from RAM into VRAM. This is referred to as *uploading to the GPU*. This may seem trivial, but for large textures this can be very time-consuming.\n\nFinally, the CPU runs your program. You may be telling the CPU to load a PNG from your bundle and decompress it. All that happens on the CPU. When you then want to display that decompressed image, it somehow needs to get uploaded to the GPU. Something as mundane as displaying text is a tremendously complex task for the CPU that facilitates a tight integration between the Core Text and Core Graphics frameworks to generate a bitmap from the text. Once ready, it gets uploaded to the GPU as a texture, ready to be displayed. When you scroll or otherwise move that text on screen, however, the very same texture can be reused, and the CPU will simply tell the GPU what the new position is, so the GPU can reuse the existing texture. The CPU doesn't have to re-render the text, and the bitmap doesn't have to be re-uploaded.\n\nThis illustrates some of the complexities involved. With this overview out of the way, we'll dive into some of the technologies involved.\n\n\n<a name=\"compositing\"> </a>\n\n## Compositing\n\nCompositing in the graphics world is a term that describes how different bitmaps are put together to create the final image you see on the screen. It is, in many ways, so obvious that it's easy to forget the complexity and computations involved.\n\nLet's ignore some of the more esoteric cases and assume that everything on the screen is a texture. A texture is a rectangular area of RGBA values, i.e. for each pixel we have red, green, and blue values, and an alpha value. In the Core Animation world this is basically what a `CALayer` is.\n\nIn this slightly simplified setup, each layer is a texture, and all these textures are in some way stacked on top of each other. For each pixel on screen, the GPU needs to to figure out how to blend / mix these textures to get the RGB value of that pixel. That's what compositing is about.\n\nIf all we have is a single texture that is the size of the screen and aligned with the screen pixels, each pixel on the screen corresponds to a single pixel in that texture. The texture's pixels end up being the screen's pixels.\n\nIf we have a second texture that's placed on top of the first texture, the GPU will then have to composite this texture onto the first. There are different blend modes, but if we assume that both textures are pixel-aligned and we're using the normal blend mode, the resulting color is calculated with this formula for each pixel:\n\n```\nR = S + D * (1 - Sa)\n```\n\nThe resulting color is the source color (top texture) plus the destination color (lower texture) times one minus the source color's alpha. All colors in this formula are assumed to be pre-multiplied with their alpha.\n\nObviously there's quite a bit going on here. Let's for a second assume that all textures are fully opaque, i.e. alpha = 1. If the destination (lower) texture is blue (RGB = 0, 0, 1), and the source (top) texture is red (RGB = 1, 0, 0), and because `Sa` is `1`, the result is\n\n```\nR = S\n```\n\nand the result is the source's red color. That's what you'd expect.\n\nIf the source (top) layer was 50% transparent, i.e. alpha = 0.5, the RGB values for S would be (0.5, 0, 0) since the alpha component is pre-multiplied into the RGB-values. The formula would then look like this:\n\n    \n```\n                       0.5   0               0.5\nR = S + D * (1 - Sa) = 0   + 0 * (1 - 0.5) = 0\n                       0     1               0.5\n```\n\nWe'd end up getting an RGB value of (0.5, 0, 0.5) which is a saturated 'plum' or purple color. That's hopefully what you'd intuitively expect when mixing a transparent red onto a blue background.\n\nRemember that what we just did is compositing one single pixel of a texture into another pixel from another texture. The GPU needs to do this for all pixels where the two textures overlay. And as you know, most apps have a multitude of layers and hence textures that need to be composited together. This keeps the GPU busy, even though it's a piece of hardware that's highly optimized to do things like this.\n\n### Opaque vs. Transparent\n\nWhen the source texture is fully opaque, the resulting pixels are identical to the source texture. This could save the GPU a lot of work, since it can simply copy the source texture in place of blending all pixel values. But there's no way for the GPU to tell if all pixels in a texture are opaque or not. Only you as a programmer know what you're putting into your `CALayer`. And that's why `CALayer` has a property called `opaque`. If this is `YES`, then the GPU will not do any blending and simply copy from this layer, disregarding anything that's below it. It saves the GPU quite a bit work. This is what the Instruments option **color blended layers** is all about (which is also available in the Simulator's Debug menu). It allows you to see which layers (textures) are marked as non-opaque, i.e. for which layers the GPU is doing blending. Compositing opaque layers is cheaper because there's less math involved. \n\nSo if you know your layer is opaque, be sure to set `opaque` to `YES`. If you're loading an image that doesn't have an alpha channel and displaying it in a `UIImageView` this will happen automatically. But note that there's a big difference between an image without an alpha channel, and an image that has alpha at 100% everywhere. In the latter case, Core Animation has to assume that there might be pixels for which alpha is not 100%. In the Finder, you can use *Get Info* and check the *More Info* section. It'll say if the image has an alpha channel or not.\n\n### Pixel Alignment and Misalignment\n\nSo far we've looked at layers that have pixels that are perfectly aligned with the display. When everything is pixel-aligned we get the relatively simple math we've looked at so far. Whenever the GPU has to figure out what color a pixel on screen should be, it only needs to look at a single pixel in the layers that are above this screen pixel and composite those together. Or, if the top texture is opaque, the GPU can simply copy the pixel of that top texture.\n\nA layer is pixel-aligned when all its pixels line up perfectly with the screen's pixels. There are mainly two reasons why this may not be the case. The first one is scaling; when a texture is scaled up or down, the pixels of the texture won't line up with the screen's. Another reason is when the texture's origin is not at a pixel boundary.\n\nIn both of these cases the GPU again has to do extra math. It has to blend together multiple pixels from the source texture to create a value that's used for compositing. When everything is pixel-aligned, the GPU has less work to do.\n\nAgain, the Core Animation Instrument and the Simulator have an option called **color misaligned images** that will show you when this happens for your CALayer instances.\n\n### Masks\n\nA layer can have a mask associated with it. The mask is a bitmap of alpha values which are to be applied to the layer's pixels before they're composited onto the content below it. When you're setting a layer's corner radius, you're effectively setting a mask on that layer. But it's also possible to specify an arbitrary mask, e.g. have a mask that's the shape of the letter *A*. Only the part of the layer's content that's part of that mask would be rendered then.\n\n\n<a name=\"off-screen-rendering\"> </a>\n\n### Offscreen Rendering\n\nOffscreen rendering can be triggered automatically by Core Animation or be forced by the application. Offscreen rendering composites / renders a part of the layer tree into a new buffer (which is offscreen, i.e. not on the screen), and then that buffer is rendered onto the screen.\n\nYou may want to force offscreen rendering when compositing is computationally expensive. It's a way to cache composited texture / layers. If your render tree (all the textures and how they fit together) is complex, you can force offscreen rendering to cache those layers and then use that cache for compositing onto the screen.\n\nIf your app combines lots of layers and wants to animate them together, the GPU normally has to re-composite all of these layers onto what's below them for each frame (1/60 s). When using offscreen rendering, the GPU first combines those layers into a bitmap cache based on a new texture and then uses that texture to draw onto the screen. Now when those layers move together, the GPU can re-use this bitmap cache and has to do less work. The caveat is that this only works if those layers don't change. If they do, the GPU has to re-create the bitmap cache. You trigger this behavior by setting `shouldRasterize` to `YES`.\n\nIt's a trade-off, though. For one, it may cause things to get slower. Creating the extra offscreen buffer is an additional step that the GPU has to perform, and particularly if it can never reuse this bitmap, it's a wasted effort. If however, the bitmap can be reused, the GPU may be offloaded. You have to measure the GPU utilization and frame rate to see if it helps.\n\nOffscreen rendering can also happen as a side effect. If you're directly or indirectly applying a mask to a layer, Core Animation is forced to do offscreen rendering in order to apply that mask. This puts a burden on the GPU. Normally it would just be able to render directly onto the frame buffer (the screen).\n\nInstruments' Core Animation Tool has an option called **Color Offscreen-Rendered Yellow** that will color regions yellow that have been rendered with an offscreen buffer (this option is also available in the Simulator's Debug menu). Be sure to also check **Color Hits Green and Misses Red**. Green is for whenever an offscreen buffer is reused, while red is for when it had to be re-created.\n\nGenerally, you want to avoid offscreen rendering, because it's expensive. Compositing layers straight onto the frame buffer (onto the display) is way cheaper than first creating an offscreen buffer, rendering into that, and then rendering the result back into the frame buffer. There are two expensive context switches involved (switching the context to the offscreen buffer, then switching the context back to the frame buffer).\n\nSo when you see yellow after turning on **Color Offscreen-Rendered Yellow**, that should be a warning sign. But it isn't necessarily bad. If Core Animation is able to reuse the result of the offscreen rendering, it may improve performance if Core Animation can reuse the buffer. It can reuse when the layers that were used for the offscreen buffer didn't change.\n\nNote also that there's limited space for rasterized layers. Apple hinted that there's roughly two times the screen size of space for rasterized layers / offscreen buffers.\n\nAnd if the way you're using layers causes an offscreen rendering pass, you're probably better off trying to get rid of that offscreen rendering altogether. Using masks or setting a corner radius on a layer causes offscreen rendering, so does applying shadows.\n\nAs for masks, with corner radius (which is just a special mask), and `clipsToBounds` / `masksToBounds`, you may be able to simply create content that has masks already burned in, e.g. by using an image with the right mask already applied. As always, it's a trade-off. If you want to apply a rectangular mask to a layer with its `contents` set, you can probably use `contentsRect` instead of the mask.\n\nIf you end up setting `shouldRasterize` to `YES`, remember to set the `rasterizationScale` to the `contentsScale`.\n\n### More about Compositing\n\nAs always, Wikipedia has more background on the math of [alpha compositing](https://en.wikipedia.org/wiki/Alpha_compositing). We'll dive a bit more into how red, green, blue, and alpha are represented in memory later on when talking about [pixels][220].\n\n### OS X\n\nIf you're working on OS X, you'll find most of those debugging options in a separate app called \"Quartz Debug,\" and not inside Instruments. Quartz Debug is a part of the \"Graphics Tools\" which is a separate [download at the developer portal](https://developer.apple.com/downloads/).\n\n\n## Core Animation & OpenGL ES\n\nAs the name suggests, Core Animation lets you animate things on the screen. We will mostly skip talking about animations, though, and focus on drawing. The thing to note, however, is that Core Animation allows you to do extremely efficient rendering. And that's why you can do animations at 60 frames per second when you're using Core Animation.\n\nCore Animation, at its core, is an abstraction on top of OpenGL ES. Simply put, it lets you use the power of OpenGL ES without having to deal with all of its complexities. When we talked about [compositing][210] above we were using the terms layer and texture interchangeably. They're not the same thing, but quite analogous.\n\nCore Animation layers can have sublayers, so what you end up with is a layer tree. The heavy lifting that Core Animation does is figuring out what layers need to be (re-)drawn, and which OpenGL ES calls need to be made to composite the layers onto the screen.\n\nFor example, Core Animation creates an OpenGL texture when you set a layer's contents to a `CGImageRef`, making sure that the bitmap in that image gets uploaded to the corresponding texture, etc. Or if you override `-drawInContext`, Core Animation will allocate a texture and make sure that the Core Graphics calls you make will turn into that texture's bitmap data. A layer's properties and the `CALayer` subclasses affect how OpenGL rendering is performed, and many lower-level OpenGL ES behaviors are nicely encapsulated in easy-to-understand `CALayer` concepts.\n\nCore Animation orchestrates CPU-based bitmap drawing through Core Graphics on one end with OpenGL ES on the other end. And because Core Animation sits at this crucial place in the rendering pipeline, how you use Core Animation can dramatically impact performance.\n\n### CPU bound vs. GPU bound\n\nThere are many components at play when you're displaying something on the screen. The two main hardware players are the CPU and the GPU. The P and U in their names stand for processing unit, and both of these will do processing when things have to be drawn on the screen. Both also have limited resources.\n\nIn order to achieve 60 frames per second, you have to make sure that neither the CPU nor the GPU are overloaded with work. In addition to that, even when you're at 60 fps, you want to put as much of the work as possible onto the GPU. You want to CPU to be free to run your applications code instead of being busy drawing. And quite often, the GPU is more efficient than the CPU at rendering, which turns into a lower overall load and power consumption of the system.\n\nSince drawing performance depends on both CPU and GPU, you need to figure out which one is limiting your drawing performance. If you're using all GPU resources, i.e. the GPU is what's limiting your performance, your drawing is said to be *GPU bound*. Likewise if you're maxing out the CPU, you're said to be *CPU bound*.\n\nIf you're GPU bound, you need to offload the GPU (and perhaps do more of the work on the CPU). If you're CPU bound, you need to offload the CPU.\n\nTo tell if you're GPU bound, use the *OpenGL ES Driver* instrument. Click on the little *i* button, then *configure*, and make sure *Device Utilization %* is checked. Now, when you run your app, you'll see how loaded the GPU is. If this number is close to 100%, you're trying to do much work on the GPU.\n\nBeing CPU bound is the more *traditional* aspect of your app doing to much work. The *Time Profiler* instrument helps you with that.\n\n\n<a name=\"core-graphics\"> </a>\n\n## Core Graphics / Quartz 2D\n\nQuartz 2D is more commonly known by the name of the framework that contains it: Core Graphics. \n\nQuartz 2D has more tricks up its sleeve than we'd possibly be able to cover here. We're not going to talk about the huge part that's related to PDF creating, rendering, parsing, or printing. Just note that printing and PDF creation is largely identical to drawing bitmaps on the screen, since it is all based on Quartz 2D.\n\nLet's just very briefly touch upon the main concepts of Quartz 2D. For details, make sure to check Apple's [Quartz 2D Programming Guide](https://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/Introduction/Introduction.html).\n\nRest assured that Quartz 2D is very powerful when it comes to 2D drawing. There's path-based drawing, anti-aliased rendering, transparency layers, and resolution- and device-independency, to name a few features. It can be quite daunting, more so because it's a low-level and C-based API.\n\nThe main concepts are relatively simple, though. Both UIKit and AppKit wrap some Quartz 2D in simply to use API, and even the plain C API is accessible once you've gotten used to it. You end up with a drawing engine that can do most of what you'd be able to do in Photoshop and Illustrator. Apple [mentioned the stocks app on iOS](https://developer.apple.com/videos/wwdc/2011/?id=129) as an example of Quartz 2D usage, as the graph is a simple example of a graph that's dynamically rendered in code using Quartz 2D.\n\nWhen your app does bitmap drawing it will -- in one way or another -- be based on Quartz 2D. That is, the CPU part of your drawing will be performed by Quartz 2D. And although Quartz can do other things, we'll be focusing on bitmap drawing here, i.e. drawing that results on a buffer (a piece of memory) that contains RGBA data.\n\nLet's say we want to draw an [Octagon](https://en.wikipedia.org/wiki/Octagon). We could do that using UIKit\n\n```objc\nUIBezierPath *path = [UIBezierPath bezierPath];\n[path moveToPoint:CGPointMake(16.72, 7.22)];\n[path addLineToPoint:CGPointMake(3.29, 20.83)];\n[path addLineToPoint:CGPointMake(0.4, 18.05)];\n[path addLineToPoint:CGPointMake(18.8, -0.47)];\n[path addLineToPoint:CGPointMake(37.21, 18.05)];\n[path addLineToPoint:CGPointMake(34.31, 20.83)];\n[path addLineToPoint:CGPointMake(20.88, 7.22)];\n[path addLineToPoint:CGPointMake(20.88, 42.18)];\n[path addLineToPoint:CGPointMake(16.72, 42.18)];\n[path addLineToPoint:CGPointMake(16.72, 7.22)];\n[path closePath];\npath.lineWidth = 1;\n[[UIColor redColor] setStroke];\n[path stroke];\n```\n\nThis corresponds more or less to this Core Graphics code:\n\n```objc\nCGContextBeginPath(ctx);\nCGContextMoveToPoint(ctx, 16.72, 7.22);\nCGContextAddLineToPoint(ctx, 3.29, 20.83);\nCGContextAddLineToPoint(ctx, 0.4, 18.05);\nCGContextAddLineToPoint(ctx, 18.8, -0.47);\nCGContextAddLineToPoint(ctx, 37.21, 18.05);\nCGContextAddLineToPoint(ctx, 34.31, 20.83);\nCGContextAddLineToPoint(ctx, 20.88, 7.22);\nCGContextAddLineToPoint(ctx, 20.88, 42.18);\nCGContextAddLineToPoint(ctx, 16.72, 42.18);\nCGContextAddLineToPoint(ctx, 16.72, 7.22);\nCGContextClosePath(ctx);\nCGContextSetLineWidth(ctx, 1);\nCGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);\nCGContextStrokePath(ctx);\n```\n\nThe question to ask is: Where is this drawing to? This is where the so-called `CGContext` comes into play. The `ctx` argument we were passing is in that context. And the context defines where we're drawing to. If we're implementing `CALayer`'s `-drawInContext:` we're being passed a context. Drawing to that context will draw into the layer's backing store (its buffer). But we can also create our own context, namely a bitmap-based context with e.g. `CGBitmapContextCreate()`. This function returns a context that we can then pass to the `CGContext` functions to draw into that context, etc.\n\nNote how the `UIKit` version of the code doesn't pass a context into the methods. That's because when using `UIKit` or `AppKit` the context is implicit. `UIKit` maintains a stack of contexts and the UIKit methods always draw into the top context. You can use `UIGraphicsGetCurrentContext()` to get that context. You'd use `UIGraphicsPushContext()` and `UIGraphicsPopContext()` to push and pop context onto UIKit's stack.\n\nMost notably, UIKit has the convenience methods `UIGraphicsBeginImageContextWithOptions()` and `UIGraphicsEndImageContext()` to create a bitmap context analogous to `CGBitmapContextCreate()`. Mixing UIKit and Core Graphics calls is quite simple:\n\n```objc\nUIGraphicsBeginImageContextWithOptions(CGSizeMake(45, 45), YES, 2);\nCGContextRef ctx = UIGraphicsGetCurrentContext();\nCGContextBeginPath(ctx);\nCGContextMoveToPoint(ctx, 16.72, 7.22);\nCGContextAddLineToPoint(ctx, 3.29, 20.83);\n...\nCGContextStrokePath(ctx);\nUIGraphicsEndImageContext();\n```\n\nor the other way around:\n\n```objc\nCGContextRef ctx = CGBitmapContextCreate(NULL, 90, 90, 8, 90 * 4, space, bitmapInfo);\nCGContextScaleCTM(ctx, 0.5, 0.5);\nUIGraphicsPushContext(ctx);\nUIBezierPath *path = [UIBezierPath bezierPath];\n[path moveToPoint:CGPointMake(16.72, 7.22)];\n[path addLineToPoint:CGPointMake(3.29, 20.83)];\n...\n[path stroke];\nUIGraphicsPopContext(ctx);\nCGContextRelease(ctx);\n```\n\nThere's a huge amount of really cool stuff you can do with Core Graphics. For a good reason, the Apple documents call out its *unmatched output fidelity*. We can't get into all the details, but: Core Graphics has a graphics model that (for historic reasons) is very close to how [Adobe Illustrator](https://en.wikipedia.org/wiki/Adobe_Illustrator) and [Adobe Photoshop](https://en.wikipedia.org/wiki/Adobe_Photoshop) work. And most of the tools' concepts translate to Core Graphics. After all, its origins are in [NeXTSTEP](https://en.wikipedia.org/wiki/NextStep), which used [Display PostScript](https://en.wikipedia.org/wiki/Display_PostScript).\n\n### CGLayer\n\nWe originally indicated that `CGLayer` could be used to speed up repeated drawing of identical elements. As pointed out by [Dave Hayden](https://twitter.com/davehayden), [word in the street](http://iosptl.com/posts/cglayer-no-longer-recommended/) has it that this is no longer true.\n\n\n<a name=\"pixels\"> </a>\n\n## Pixels\n\nPixels on screen are composed of three color components: red, green, blue. Hence bitmap data is also sometimes referred to as RGB data. You may wonder how this data is organized in memory. But the fact is, there are many, many different ways RGB bitmap data can be represented in memory.\n\nIn a bit we'll talk about compressed data, which is entirely different again. For now, let us look at RGB bitmap data, where we have a value for each color component: red, green, and blue. And quite often we'll have a fourth component: alpha. We end up with four individual values for each and every pixel.\n\n### Default Pixel Layouts\n\nA very common format on iOS and OS X is what is known amongst friends as *32 bits-per-pixel (bpp), 8 bits-per-component (bpc), alpha premultiplied first*. In memory this looks like\n\n```\n  A   R   G   B   A   R   G   B   A   R   G   B  \n| pixel 0       | pixel 1       | pixel 2   \n  0   1   2   3   4   5   6   7   8   9   10  11 ...\n```\n\nThis format is often (ambiguously) referred to as ARGB. Each pixel uses four bytes (32 bpp). Each color component is one byte (8 bpc). Each pixel has an alpha value, which comes first (before the RGB values). And finally the red-green-blue values are *pre-multiplied* with the alpha. Pre-multiplied means that the alpha value is baked into the red, green and blue component. If we have an orange color its RGB values at 8 bpc would be something like 240, 99 and 24 respectively. An orange pixel that's fully opaque would have ARGB values of 255, 240, 99, 24 in memory with the above layout. If we had a pixel of the same color, but an alpha value of 33%, the pixel values would be 84, 80, 33, 8.\n\nAnother common format is *32 bpp, 8 bpc, alpha-none-skip-first* which looks like this:\n\n```\n  x   R   G   B   x   R   G   B   x   R   G   B  \n| pixel 0       | pixel 1       | pixel 2   \n  0   1   2   3   4   5   6   7   8   9   10  11 ...\n```\n\nThis is also referred to as xRGB. The pixels don't have any alpha value (they're assumed to be 100% opaque), but the memory layout is the same. You may wonder why this format is popular, as, if we didn't have that unused byte for each pixel, we would save 25% space. It turns out, though, that this format is much easier to *digest* by modern CPUs and imaging algorithms, because the individual pixels are aligned to 32-bit boundaries. Modern CPUs don't like loading (reading) unaligned data. The algorithms would have to do a lot of shifting and masking, particularly when mixing this format with the above format that does have alpha.\n\nWhen dealing with RGB data, Core Graphics also supports putting the alpha value last (and additionally skipping). These are sometimes referred to as RGBA and RGBx respectively, implicitly assuming 8 bpc and pre-multiplied alpha.\n\n### Esoteric Layouts\n\nMost of the time, when dealing with bitmap data, we'll be dealing with Core Graphics / Quartz 2D. It has a very specific list of format combinations that it supports. But let's first look at the remaining RGB formats:\n\nAnother option is *16 bpp, 5 bpc without alpha*. This layout takes up only 50% of memory (2 bytes per pixel) compared to the previous ones. That can come in handy if you need to store (uncompressed) RGB data in memory or on disk. But since this format only has 5 bits per pixel, images (particularly smooth gradients) may get [banding artifacts](https://en.wikipedia.org/wiki/Posterization).\n\nGoing the other way, there's *64 bpp, 16 bpc* and finally *128 bpp, 32 bpc, float-components* (both with or without alpha). These use eight bytes and sixteen bytes per pixel respectively and allow for much higher fidelity, at the cost of higher memory usage and being more computationally expensive.\n\nTo round things off, Core Graphics also supports a few grayscale and [CMYK](https://en.wikipedia.org/wiki/CMYK) formats, as well as an alpha-only format (for masks).\n\n<a name=\"planar-data\"> </a>\n\n### Planar Data\n\nMost frameworks (including Core Graphics) use pixel data where the components (red, green, blue, alpha) are intermixed. There are situation where we'll run into something called *planar components*, or *component planes*. What this means is that each color component is in its own region of memory, i.e. *plane*. For RGB data, we'd have three independent memory regions, with one large region containing the red values for all pixels, one containing the green values for all pixels, and one containing the blue values for all pixels.\n\nSome of the video frameworks will use planar data under some circumstances. \n\n### YCbCr\n\n[YCbCr](https://en.wikipedia.org/wiki/YCbCr) is a format relatively common when working with video data. It also consists of threecomponents (Y, Cb and Cr) and can represent color data. But (briefly put) it is more similar to the way human vision perceives color. Human vision is less sensitive to fidelity of the two chroma components Cb and Cr, but quite sensitive to fidelity of the luma signal Y. When data is in YCbCr format, the Cb and Cr components can be compressed harder than the Y component with the same perceived quality.\n\nJPEG images are also sometimes converting pixel data from RGB to YCbCr for the same reason. JPEG compresses each color plane independently. When compressing YCbCr-based planes, the Cb and Cr can be compressed more than the Y plane.\n\n\n\n<a name=\"ImageFormats\"></a>\n## Image Formats\n\nImages on disk are mostly JPEG and PNG when you're dealing with iOS or OS X. Let's take a closer look.\n\n<a name=\"JPEG\"></a>\n### JPEG\n\nEverybody knows [JPEG](https://en.wikipedia.org/wiki/JPEG). It's the stuff that comes out of cameras. It's how photos are stored on computers. Even your mom has heard of JPEG.\n\nFor good reasons, many people think that a JPEG file is just another way to format pixel data, meaning the RGB pixel layouts we [just talked about][220]. That's very far from the truth, though.\n\nTurning JPEG data into pixel data is a very complex process, certainly nothing you'd be able to pull off as a weekend project, even on a very long weekend. For each [color plane][240], JPEG compression uses an algorithm based on the [discrete cosine transform](https://en.wikipedia.org/wiki/Discrete_cosine_transform) to convert spatial information into the frequency domain. This information is then quantized, sequenced, and packed using a variant of [Huffman encoding](https://en.wikipedia.org/wiki/Huffman_encoding). And quite often, initially, the data is converted from RGB to YCbCr planes. When decoding a JPEG all of this has to run in reverse.\n\nThat's why when you create a `UIImage` from a JPEG file and draw that onto the screen, there'll be a delay, because the CPU is busy decompressing that JPEG. If you have to decompress a JPEG for each table view cell, your scrolling won't be smooth.\n\nSo why would you use JPEG at all? The answer ist that JPEG can compress photos very, very well. An uncompressed photo from your iPhone 5 would take up almost 24MB. With the default compression setting, your photos in your camera roll are usually around 2MB to 3MB. JPEG compression works so well, because it is lossy. It throws away information that is less perceptible to the human eye, and in doing so, it can push the limits far beyond what \"normal\" compression algorithms such as gzip do. But this only works well on photos, because JPEG relies on the fact that there's a lot in photos that's not very perceptible to the human vision. If you take a screen shot of a web page that's mostly displaying text, JPEG is not going to do very well. Compression will be lower, and you'll most likely see that the JPEG compression has altered the image.\n\n<a name=\"PNG\"></a>\n### PNG\n\n[PNG](https://en.wikipedia.org/wiki/Portable_Network_Graphics) is pronounced \"ping\". As opposed to JPEG, it's a lossless compression format. When you save an image as PNG, and later open it (and decompress it), all pixel data is exactly what it was originally. Because of this restriction, PNG can't compress photos as well as JPEG, but for app artwork such as buttons, icons etc. it actually works very well. And what's more, decoding PNG data is a lot less complex than decoding JPEG.\n\nIn the real world, things are never quite as simple, and there are actually a slew of different PNG formats. Check Wikipedia for the details. But simply put, PNG supports compressing color (RGB) pixels with or without an alpha channel. That's another reason why it works well for app artwork.\n\n### Picking a Format\n\nWhen you're using photos in your app, you should stick to one of these two: JPEG or PNG. The decompressors and compressors for reading and writing these formats are highly optimized for performance and, to some extent, support parallization. And you'll get the constant additional improvements that Apple is doing to these decompressors for free with future updates of the OS. If you're tempted to use another format, be aware that this is likely to impact performance of your app, and also likely to open security holes, as image decompressors are a favorite target of attackers.\n\nQuite a bit has been written about [optimizing PNGs](https://duckduckgo.com/?q=%22optimizing%20PNG%22). Search the web yourself, if you feel so inclined. It is very important, though, to note that Xcode's *optimized PNG* option is something quite different that most other optimization engines.\n\nWhen Xcode *optimizes* PNG files, it turns these PNG files into something that's, technically speaking, [no longer a valid PNG](https://developer.apple.com/library/ios/#qa/qa1681/_index.html). But iOS can read these files, and in fact can decompress these files way faster that normal PNGs. Xcode changes them in such a way that lets iOS use a more efficient decompression algorithm that doesn't work on regular PNGs. The main point worth noting is that it changes the pixel layout. As we mentioned under [Pixels][220], there are many ways to represent RGB data, and if the format is not what the iOS graphics system needs, it needs to shift the data around for each pixel. Not having to do so speeds things up.\n\nAnd, let us stress again: If you can, you should use so-called [resizable images][260] for artwork. Your files will be smaller, and hence there's less data that needs to be loaded from the file system and then decompressed. \n\n\n## UIKit and Pixels\n\nEach view in UIKit has its own `CALayer`. In turn, this layer (normally) has a backing store, which is pixel bitmap, a bit like an image. This backing store is what actually gets rendered onto the display.\n\n### With -drawRect:\n\nIf your view class implements `-drawRect:` things work like this:\n\nWhen you call `-setNeedsDisplay`, UIKit will call `-setNeedsDisplay` on the views layer. This sets a flag on that layer, marking it as *dirty*, as needing display. It's not actually doing any work, so it's totally acceptable to call `-setNeedsDisplay` multiple times in a row.\n\nNext, when the rendering system is ready, it calls `-display` on that view's layer. At this point, the layer sets up its backing store. Then it sets up a Core Graphics context (`CGContextRef`) that is backed by the memory region of that backing store. Drawing using that `CGContextRef` will then go into that memory region.\n\nWhen you use UIKit's drawing methods such as `UIRectFill()` or `-[UIBezierPath fill]` inside your `-drawRect:` method, they will use this context. The way this works is that UIKit pushes the `CGContextRef` for that backing store onto its *graphics context stack*, i.e. it makes that context the *current* one. Thus `UIGraphicsGetCurrent()` will return that very context. And since the UIKit drawing methods use `UIGraphicsGetCurrent()`, the drawing will go into the layer's backing store. If you want to use Core Graphics methods directly, you can get to that same context by calling `UIGraphicsGetCurrent()` yourself and passing the context into the Core Graphics functions as the context.\n\nFrom now on, the layers backing store will be rendered onto the display repeatedly, until something calls the view's `-setNeedsDisplay` again, and that in turn causes the layers backing store to be updated.\n\n### Without -drawRect:\n\nWhen you're using a `UIImageView` things work slightly different. The view still has a `CALayer`, but the layer doesn't allocate a backing store. Instead it uses a `CGImageRef` as its contents and the render server will draw that image's bits into the frame buffer, i.e. onto the display.\n\nIn this case, there's no drawing going on. We're simply passing bitmap data in form of an image to the `UIImageView`, which forwards it to Core Animation, which, in turn, forwards it to the render server.\n\n\n### To -drawRect: or Not to -drawRect:\n\nIt may sound cheesy, but: The fastest drawing is the drawing you don't do.\n\nMost of the time, you can get away with compositing your custom view from other views or compositing it from layers or a combination of the two. Check Chris' article about [custom controls][400] for more info. This is recommended, because the view classes of `UIKit` are extremely optimized.\n\nA good example of when you might need custom drawing code is the \"finger painting\" app that Apple shows in [WWDC 2012's session 506](https://developer.apple.com/videos/wwdc/2012/?id=506): *Optimizing 2D Graphics and Animation Performance*.\n\nAnother place that uses custom drawing is the iOS stocks app. The stock graph is drawn on the device with Core Graphics. Note, however that just because you do custom drawing, you don't necessarily need to have a `-drawRect:` method. At times, it may make more sense to create a bitmap with `UIGraphicsBeginImageContextWithOptions()` or `CGBitmapContextCreate()`, grab the resulting image from that, and set it as a `CALayer`'s `contents`. Test and measure. We'll give an example of this [below][250].\n\n\n### Solid Colors\n\nIf we look at this example:\n\n```objc\n// Don't do this\n- (void)drawRect:(CGRect)rect\n{\n    [[UIColor redColor] setFill];\n    UIRectFill([self bounds]);\n}\n```\n\nwe now know why this is bad: We're causing Core Animation to create a backing store for us, and we're asking Core Graphics to fill the backing store with a solid color. And then that has to get uploaded to the GPU.\n\nWe can save all this work by not implementing `-drawRect:` at all, and simply setting the view's layer's `backgroundColor`. If the view has a `CAGradientLayer` as its layer, the same technique would work for gradients.\n\n<a name=\"resizable-images\"> </a>\n\n### Resizable Images\n\nSimilarly, you can use resizable images to lower pressure on the graphics system. Let's say you want a 300 x 50 points button for which you have artwork. That's 600 x 100 = 60k pixels or 60k x 4 = 240kB of memory that has to get uploaded to the GPU, and that then takes up VRAM. If we were to use a so-called resizable image, we might get away with e.g. a 54 x 12 points image which would be just below 2.6k pixels or 10kB of memory. Things are faster.\n\nCore Animation can resize images with the [`contentsCenter`](https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instp/CALayer/contentsCenter) property on `CALayer` but in most cases you'd want to use [`-[UIImage resizableImageWithCapInsets:resizingMode:]`](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/instm/UIImage/resizableImageWithCapInsets:resizingMode:).\n\nNote also, that before this button can be rendered for the first time, instead of having to read a 60k pixel PNG from the file system and decode that 60k pixel PNG, decoding the much smaller PNG is faster. This way, your app has to do way less work in all steps involved and your views will load a lot quicker.\n\n\n<a name=\"concurrent-drawing\"> </a>\n\n### Concurrent Drawing\n\nThe [last objc.io issue](/issues/2-concurrency/) was about concurrency. And as you'll know, UIKit's threading model is very simple: You can only use UIKit classes (views etc.) from the main queue (i.e. main thread). So what's this thing about concurrent drawing?\n\nIf you have to implement `-drawRect:` and you have to draw something non-trivial, this is going to take time. And since you want animations to be smooth, you'll be tempted to do things on a queue other than the main queue. Concurrency is complex, but with a few caveats, drawing concurrently is easily achievable.\n\nWe can't draw into a `CALayer`'s backing store from anything but the main queue. Bad things would happen. But what we can do is to draw into a totally disconnected bitmap context.\n\nAs we mentioned above under [Core Graphics][270], all Core Graphics drawing methods take a *context* argument that specifies which context drawing goes into. And UIKit in turn has a concept of a *current* context that its drawing goes into. This *current* context is per-thread.\n\nIn order to concurrently draw, we'll do the following. We'll create an image on another queue, and once we have that image, we'll switch back to the main queue and set that resulting image as the `UIImageView`'s image. This technique is discussed in [WWDC 2012 session 211](https://developer.apple.com/videos/wwdc/2012/?id=211).\n\nAdd a new method in which you'll do the drawing:\n\n```objc\n- (UIImage *)renderInImageOfSize:(CGSize)size;\n{\n    UIGraphicsBeginImageContextWithOptions(size, NO, 0);\n    \n    // do drawing here\n    \n    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();\n    UIGraphicsEndImageContext();\n    return result;\n}\n```\n\nThis method creates a new bitmap `CGContextRef` for the given size through the `UIGraphicsBeginImageContextWithOptions()` function. That function also makes that new context the *current* UIKit context. You can now do your drawing just as you would normally do in `-drawRect:`. Then we get the bitmap data of that context as an `UIImage` with `UIGraphicsGetImageFromCurrentImageContext()`, and finally tear down the context.\n\nIt is important that any calls your drawing code in this method makes are threadsafe, i.e. if you access properties etc., they need to be threadsafe. The reason is that you'll call this method from another queue. If this method is in your view class, that can be a bit tricky. Another option (which may be easier) is to create a separate renderer class that you set all needed properties on and only then trigger to render the image. If so, you might be able to use a plain `UIImageView` or `UITableViewCell`.\n\nNote that all the UIKit drawing APIs are safe to use on another queue. Just make sure to call them inside the same operation that starts with `UIGraphicsBeginImageContextWithOptions()` and ends with `UIGraphicsEndIamgeContext()`.\n\nYou'd trigger the rendering code with something like this:\n\n```objc\nUIImageView *view; // assume we have this\nNSOperationQueue *renderQueue; // assume we have this\nCGSize size = view.bounds.size;\n[renderQueue addOperationWithBlock:^(){\n    UIImage *image = [renderer renderInImageOfSize:size];\n    [[NSOperationQueue mainQueue] addOperationWithBlock:^(){\n        view.image = image;\n    }];\n}];\n```\n\nNote that we're calling `view.image = image` on the main queue. This is a very important detail. You can *not* call this on any other queue.\n\nAs always, with concurrency comes a lot of complexity. You may now have to implement canceling of background rendering. And you'll most likely have to set a reasonable maximum concurrent operation count on the render queue.\n\nIn order to support all this, it's most likely easiest to implement the `-renderInImageOfSize:` inside an `NSOperation` subclass.\n\nFinally, it's important to point out that setting `UITableViewCell` content asynchronously is tricky. The cell may have been reused at the point in time when the asynchronous rendering is done, and you'll be setting content on it although the cell now is being used for something else. \n\n\n## CALayer Odds and Ends\n\nBy now you should know that a `CALayer` is somehow related and similar to a texture on the GPU. The layer has a backing store, which is the bitmap that gets drawn onto the display.\n\nQuite often, when you use a `CALayer`, you'll set its `contents` property to an image. What this does, is it tells Core Animation to use that image's bitmap data for the texture. Core Animation will cause the image to be decoded if it's compressed (JPEG or PNG) and then upload the pixel data to the GPU.\n\nThere are other kinds of layers, though. If you use a plain `CALayer`, don't set the `contents`, and set a background color, Core Animation doesn't have to upload any data to the GPU, but will be able to do all the work on the GPU without any pixel data. Similarly for gradient layers, the GPU can create the gradient and the CPU doesn't need to do any work, and nothing needs to be uploaded to the GPU.\n\n### Layers with Custom Drawing\n\nIf a `CALayer` subclass implements `-drawInContext:` or its delegate, the corresponding `-drawLayer:inContext:`, Core Animation will allocate a backing store for that layer to hold the bitmap that these methods will draw into. The code inside these methods runs on the CPU, and the result is then uploaded to the GPU.\n\n### Shape and Text Layers\n\nThings are somewhat different for shape and text layers. First off, Core Animation allocates a backing store for these layers to hold the bitmap data that needs to be generated for the contents. Core Animation will then draw the shape or the text into that backing store. This is conceptually very similar to the situation where you'd implement `-drawInContext:` and would draw the shape or text inside that method. And performance is going to be very similar, too.\n\nWhen you change a shape or text layer in a way that it needs to update its backing store, Core Animation will re-render the backing store. E.g. when animating the size of a shape layer, Core Animation has to re-draw the shape for each frame in the animation.\n\n\n### Asynchronous Drawing\n\n`CALayer` has a property called `drawsAsynchronously`, and this may seems like a silver bullet to solve all problems. Beware, though, that it may improve performance, but it might just as well make things slower.\n\nWhat happens, when you set `drawsAsynchronously` to `YES`, is that your `-drawRect:` / `-drawInContext:` method will still get called on the main thread. But all calls to Core Graphics (and hence also UIKit's graphics API, which in turn call Core Graphics) don't do any drawing. Instead, the drawing commands are deferred and processed asynchronously in a background thread.\n\nOne way to look at it is that the drawing commands are recorded first, and then later replayed on a background thread. In order for this to work, more work has to be done, and more memory needs to be allocated. But some work is shifted off the main queue. Test and measure.\n\nIt is most likely to improve performance for expensive drawing methods, and less likely for those that are cheap.\n\n\n\n\n[100]:/issues/3-views/advanced-auto-layout-toolbox/\n[110]:/issues/3-views/advanced-auto-layout-toolbox/#layout-process\n\n[200]:/issues/3-views/moving-pixels-onto-the-screen/\n[210]:/issues/3-views/moving-pixels-onto-the-screen/#compositing\n[220]:/issues/3-views/moving-pixels-onto-the-screen/#pixels\n[230]:/issues/3-views/moving-pixels-onto-the-screen/#off-screen-rendering\n[240]:/issues/3-views/moving-pixels-onto-the-screen/#planar-data\n[250]:/issues/3-views/moving-pixels-onto-the-screen/#concurrent-drawing\n[260]:/issues/3-views/moving-pixels-onto-the-screen/#resizable-images\n[270]:/issues/3-views/moving-pixels-onto-the-screen/#core-graphics\n\n[300]:/issues/3-views/collection-view-layouts/\n[310]:/issues/3-views/collection-view-layouts/#layout-attributes-for-...-at-index-path\n\n[400]:/issues/3-views/custom-controls/\n"
  },
  {
    "path": "2013-08-07-scroll-view.md",
    "content": "---\ntitle: \"Understanding Scroll Views\"\ncategory: \"3\"\ndate: \"2013-08-07 09:00:00\"\nauthor:\n  - name: Joe Conway\n    url: http://stablekernel.com\ntags: article\n---\n\n\nIt may be hard to believe, but a [`UIScrollView`](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html) isn't much different than a standard [`UIView`](http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/). Sure, the scroll view has a few more methods, but those methods are really just facades of existing `UIView` properties. Thus, most of the understanding of how a UIScrollView works comes from understanding `UIView` - specifically, the details of the two-step view rendering process.\n\n## Rasterization and Composition\n\nThe first part of the rendering process is known as *rasterization*. Rasterization simply means to take a set of drawing instructions and produce an image. `UIButton`s, for example, draw an image with a rounded rectangle and a title in the center. These images aren't drawn to the screen; instead, they are held onto by their view to be used during the next step.\n\nOnce each view has its rasterized image, these images are drawn on top of each other to produce one screen-sized image in a step called *composition*. The view hierarchy plays a big role in how composition occurs: a view's image is composited on top of its superview's image. Then, that composited image is composited on top of the super-superview's image, and so on. The view at the top of the hierarchy is the window and its composited image (which is a composite of every image in the view hierarchy) is what the user sees.\n\nConceptually, this idea of layering independent images on top of each other to produce a final, flat image should make sense, especially if you have used a tool like Photoshop before. We also have another article in this issue explaining in detail [how pixels get onto the screen][200].\n\nNow, recall that every view has a [`bounds`](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/bounds) and [`frame`](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/frame) rectangle. When laying out an interface, we deal with the frame rectangle of a view. This allows us to position and size the view. The frame and bounds of a view will commonly have the same size ([though transforms can alter this](http://stackoverflow.com/a/11282765/2020445)), but their origin will usually differ. Understanding how these two rectangles work is the key to understanding how UIScrollView works.\n\nDuring the rasterization step, a view doesn't care about what is going to happen in the upcoming composition step. That is to say, it doesn't care about its frame (which will be used to position the view's image) or its place in the view hierarchy (which will determine the order in which it is composited). The only thing a view cares about at this time is drawing its own content. This drawing occurs in each view's [`drawRect:`](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/drawRect:) method.\n\nBefore `drawRect:` is called, a blank image is created for the view to draw its content in. This image's coordinate system is the bounds rectangle of the view. For nearly every view, the bounds rectangle's origin is `{0, 0}`. Thus, to draw something in the top-left corner of the rasterized image, you would draw at the origin of the bounds, the point `{x:0, y:0}`. To draw something in the bottom right corner of an image, you would draw at point `{x:width, y:height}`. If you draw outside of a view's bounds, that drawing is not part of the rasterized image and is discarded.\n\n![](/images/issue-3/SV2@2x.png)\n\nDuring the composition step, each view composites its rasterized image on top of its superview's image (and so on). A view's frame rectangle determines where the view's image is drawn on its superview's image - the origin of the frame indicates the offset between the top-left corner of the view's image and its superview's image. So, a frame origin of `{x:20, y:15}` will create a composited image where the view's image is drawn on top of its superview's image, shifted to the right 20 points and down 15 points. Because the frame and bounds rectangle of a view are always the same size, the image is composited pixel for pixel to its superview's image. This ensures there is no stretching or shrinking of the rasterized image.\n\n![](/images/issue-3/SV1@2x.png) \n\nRemember, we're talking about just one composite operation between a view and its superview. Once those two views are composited together, the resulting composite image is composited with the super-superview's image and so on: a snowball effect.\n\nThink about the math behind compositing an image onto another. The top-left corner of a view's image is offset by its frame's origin and then drawn onto its superview's image:\n\n```\nCompositedPosition.x = View.frame.origin.x - Superview.bounds.origin.x;\nCompositedPosition.y = View.frame.origin.y - Superview.bounds.origin.y;\n```\n\nNow, as we have said before, the origin of a view's bounds rectangle is typically just `{0, 0}`. Thus, when doing the math, we just drop out one of the values and we get:\n\n```\nCompositedPosition.x = View.frame.origin.x;\nCompositedPosition.y = View.frame.origin.y;\n```\n\nSo, we can look at a few different frames and see how they would look:\n\n![](/images/issue-3/SV3@2x.png)\n\nAnd this should make sense. We change the frame's origin of the button, and it changes its position relative to its lovely purple superview. Notice that if we move the button so that parts of it are outside of the bounds of the purple superview, those parts are clipped in the same way drawing during rasterization would be clipped. However, technically, because of how iOS handles compositing under the hood, you can have a subview render outside of its superview's bounds, but drawing during rasterization cannot occur outside of a view's bounds.\n\n## Scroll View's Content Offset\n\nNow, what does all of this have to do with UIScrollView? *Everything*. Think about a way we could accomplish scrolling: we could have a view whose frame we change when we drag it. It accomplishes the same thing, right? If I drag my finger to the right, I increase the `origin.x` of the view I'm dragging and voila, scroll view!\n\nThe problem with that, of course, is that there are typically many views in a scroll view. To implement this panning feature, you would have to change the frames of every view every time the user moved his or her finger. But we're missing something. Remember that equation that we came up with to determine where a view composited its image onto its superview?\n\n```\nCompositedPosition.x = View.frame.origin.x - Superview.bounds.origin.x;\nCompositedPosition.y = View.frame.origin.y - Superview.bounds.origin.y;\n```\n\nWe dropped the `Superview.bounds.origin` values because they were always 0. But what if they weren't? What if, say, we used the same frames from the previous diagram, but we changed the purple view's `bounds` origin to something like {-30, -30}. We'd get this:\n\n![](/images/issue-3/SV4@2x.png)\n\nNow, the beauty of this is that every single subview of this purple view is shifted by the change to its bounds. This is, in fact, exactly how a scroll view works when you set its [`contentOffset`](http://developer.apple.com/library/ios/documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html#//apple_ref/occ/instp/UIScrollView/contentOffset) property: it changes the origin of the scroll view's bounds. In fact, `contentOffset` isn't even real! Its code probably looks like this:\n\n```\n- (void)setContentOffset:(CGPoint)offset\n{\n    CGRect bounds = [self bounds];\n    bounds.origin = offset;\n    [self setBounds:bounds];\n}\n```\n\nNotice that in the previous diagram, changing the bounds' origin enough moved the button outside of the composited image produced by the purple view and the button. This is just what happens when you scroll a scroll view enough so that a view disappears!\n\n## A Window into the World: Content Size\n\nNow that the hard part is out of the way, let's look at another property of `UIScrollView`, [`contentSize`](http://developer.apple.com/library/ios/documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html#//apple_ref/occ/instp/UIScrollView/contentSize).\n\nThe content size of a scroll view doesn't change anything about the bounds of a scroll view and therefore does not impact how a scroll view composites its subviews. Instead, the content size defines the scrollable area. By default, a scroll view's content size is a big, fat `{w:0, h:0}`. Since there is no scrollable area, the user can't scroll, but the scroll view will still display all of the subviews that fit inside the scroll view's bounds. \n\nWhen the content size is set to be larger than the bounds of the scroll view, the user is allowed to scroll. You can think of the bounds of a scroll view as a window into the scrollable area defined by the content size:\n\n![](/images/issue-3/SV5@2x.png)\n\nWhen the content offset is `{x:0, y:0}`, the viewing window's top-left corner is in the top-left corner of the scrollable area. This is also the minimum value of the content offset; the user can't scroll to the left or above the scrollable area. There's nothing there!\n\nThe maximum value for the content offset is the difference between the content size and the scroll view's bounds' size. This makes sense; scrolling all the way to the bottom right, the user is stopped so that the bottom-right edge of the scrolling area is flush with the bottom-right edge of the scroll view's bounds. You could write the maximum content offset like this:\n\n```objc\ncontentOffset.x = contentSize.width - bounds.size.width;\ncontentOffset.y = contentSize.height - bounds.size.height;\n```\n\n## Tweaking the Window with Content Insets\n\nThe property [`contentInset`](http://developer.apple.com/library/ios/documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html#//apple_ref/occ/instp/UIScrollView/contentInset) can change the maximum and minimum values of the content offset to allow scrolling outside of the scrollable area. Its type is [`UIEdgeInsets`](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitDataTypesReference/Reference/reference.html#//apple_ref/doc/c_ref/UIEdgeInsets), which consists of 4 numbers: `{top, left, bottom, right}`. When you introduce an inset, you change the range of the content offset. For example, setting the content inset to have a value of 10 for its top value allows the content offset's y value to reach -10. This introduces padding around the scrollable area. \n\n![](/images/issue-3/SV6@2x.png)\n\nThis may not seem very useful at first. In fact, why not just increase the content size? Well, you should avoid changing the content size of a scroll view unless you have to. To understand why, consider a table view (`UITableView` is a subclass of `UIScrollView`, so it has all of the same properties). The table view's scrollable area has been carefully calculated to fit each one of its cells snugly. When you scroll past the boundaries of the table view's first or last cells, the table view snaps the content offset back into place, so that the cells once again fit snugly in the scroll view's bounds.\n\nNow, what happens when you want to implement pull to refresh using a [`UIRefreshControl`](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIRefreshControl_class/Reference/Reference.html)? You can't put the `UIRefreshControl` within the scrollable area of the table view, otherwise, the table view would allow the user to stop scrolling halfway through the refresh control, and the top would snap to the top of the refresh control. Thus, you must put refresh control just above the scrollable area. This allows the content offset to snap back to the first row, not the refresh control.\n\nBut wait, if you initiate the pull-to-refresh mechanism by scrolling far enough, the table view *does* allow the content offset to snap refresh control into the scrollable area, and this is because of the table view's content inset. When the refresh action is initiated, the content inset is adjusted so that the minimum content offset includes the entirety of the refresh control. When the refresh completes, the content inset is returned to normalcy, the content offset follows suit, and none of the math required for determining the content size needs to be re-computed.\n\nHow can you use the content inset in your own code? Well, there is one great use for the it: when the keyboard is on the screen. Typically, you try to design a user interface that fits the screen snugly. When the keyboard appears on the screen, you lose a few hundred pixels of that space. All of the stuff underneath the keyboard is obscured.\n\nNow, the scroll view's bounds haven't changed, and neither has its content size (nor should it). But the user can't scroll the scroll view. Think about the equation from earlier: the maximum content offset is the difference between the content size and the bounds' size. If they are equal, which they are in your snug interface that now has a keyboard messing up your day, the maximum content offset is `{x:0, y:0}`.\n\nThe trick, then, is to put the interface in a scroll view. The content size of the scroll view remains fixed at the same size as the scroll view's bounds. When the keyboard appears on the screen, you set the bottom of the content inset equal to the height of the keyboard. \n\n![](/images/issue-3/SV7@2x.png)\n\nThis allows the maximum value of the content offset to show the area beyond the scrollable area. The top of the visible area is outside the bounds of the scroll view, and is therefore clipped (although it is also off the screen itself, so that doesn't matter too much).\n\nHopefully, this gives you some insight into the inner workings of scroll views. Are you wondering about zooming? Well, we won't talk about it today, but here's a fun tip: check the [`transform`](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/transform) property of the view you return from [`viewForZoomingInScrollView:`](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollViewDelegate_Protocol/Reference/UIScrollViewDelegate.html#//apple_ref/doc/uid/TP40006923-CH3-SW7). Once again, you'll find that a scroll view is just cleverly using already-existing properties of `UIView`.\n\n\n\n\n\n[100]:/issues/3-views/advanced-auto-layout-toolbox/\n[110]:/issues/3-views/advanced-auto-layout-toolbox/#layout-process\n\n[200]:/issues/3-views/moving-pixels-onto-the-screen/\n[210]:/issues/3-views/moving-pixels-onto-the-screen/#compositing\n[220]:/issues/3-views/moving-pixels-onto-the-screen/#pixels\n[230]:/issues/3-views/moving-pixels-onto-the-screen/#off-screen-rendering\n[240]:/issues/3-views/moving-pixels-onto-the-screen/#planar-data\n[250]:/issues/3-views/moving-pixels-onto-the-screen/#concurrent-drawing\n[260]:/issues/3-views/moving-pixels-onto-the-screen/#resizable-images\n[270]:/issues/3-views/moving-pixels-onto-the-screen/#core-graphics\n\n[300]:/issues/3-views/collection-view-layouts/\n[310]:/issues/3-views/collection-view-layouts/#layout-attributes-for-...-at-index-path\n\n[400]:/issues/3-views/custom-controls/\n"
  },
  {
    "path": "2013-09-09-SQLite-instead-of-core-data.markdown",
    "content": "---\ntitle: On Using SQLite and FMDB Instead of Core Data\ncategory: \"4\"\ndate: \"2013-09-09 09:00:00\"\nauthor:\n  - name: Brent Simmons\n    url: http://inessential.com/\ntags: article\n---\n\n\nI can’t in good conscience tell you not to use Core Data. It’s good and getting better, and it’s understood by many other Cocoa developers, which is important when you add people to your team or when someone else takes over your app.\n\nMore importantly, it’s simply not worth the time and effort to write your own system instead. Use Core Data. Really.\n\n## Why I Don’t Use Core Data\n\n[Mike Ash writes](http://www.mikeash.com/pyblog/friday-qa-2013-08-30-model-serialization-with-property-lists.html):\n\n>Personally, I'm not a big fan. I find the API to be unwieldy and the framework itself to be painfully slow for anything more than a small amount of data.\n\n### A Real-Life Example: 10,000 Items\n\nPicture an RSS reader. A user can right-click on a feed and choose Mark All As Read.\n\nUnder the hood, there’s an Article entity with a `read` attribute. To mark all items as read, the app has to load all of the articles for the feed (probably via a to-many relationship) and then set the `read` attribute to YES.\n\nMost of the time that’s okay. But suppose there are 200 articles in that feed, and you might consider doing this work in a background thread so you don’t block the main thread (especially if the app is an iPhone app). As soon as you start working with multi-threaded Core Data, things start to get tricky.\n\nThat’s probably not so bad, or at least not worth switching away from Core Data.\n\nBut then add syncing.\n\nI worked with two different RSS sync APIs that returned arrays of uniqueIDs of articles that had been read. One of those returned up to 10,000 IDs.\n\nYou’re not going to load 10,000 articles on the main thread and set `read` to NO. You don’t even want to load 10,000 articles on a background thread, even with careful memory management. It’s just too much work. (Think of the effect on battery life if this is done frequently.)\n\nWhat you really want to do, conceptually, is this: tell the database to set `read` to YES for each article in an array of unique IDs.\n\nWith SQLite you can do that. With one call. And, assuming an index on `uniqueID`, it’s fast. And you can do it on a background thread as easily as on the main thread.\n\n### Another Example: Fast Startup\n\nWith another app of mine I wanted to reduce the start-up time — not just the time for the app to launch, but the amount of time before data is displayed.\n\nIt was kind of like a Twitter app (though it wasn’t): it displayed a timeline of messages. To display that timeline meant fetching the messages and loading the associated users. It was pretty fast, but still, at start-up, the UI would fill in and *then* the data would fill in.\n\nMy theory about iPhone apps (or any app, really) is that start-up time matters more than most developers think. Apps where start-up time is slower are less likely to get launched, because people remember subconsciously and develop a resistance to launching that app. Reducing start-up time reduces friction and makes it more likely people will continue to use your app, as well as recommend it to other people. It’s part of how you make your app successful.\n\nSince I wasn’t using Core Data I had an easy, old-school solution at hand. I saved the timeline (messages and people objects) to a plist file (via `NSCoding`). At start-up it read the file, created the message and people objects, and displayed the timeline as soon as the UI appeared.\n\nThis noticeably reduced latency.\n\nHad the messages and people objects been instances of `NSManagedObject`, this wouldn’t have been possible. (I suppose I could have encoded and stored the object IDs, but that would have meant reading the plist and *then* hitting the database. This way I avoided the database entirely.)\n\n(Later on I ended up removing that code after newer, faster devices came out. In retrospect, I wish I’d left it in.)\n\n### How I Think About It\n\nWhen deciding whether or not to use Core Data, I consider a few things:\n\n#### Could There Be an Incredible Amount of Data?\n\nWith an RSS reader or Twitter app, the answer is obviously yes. Some people follow hundreds of people. A person might subscribe to thousands of feeds.\n\nEven if your app doesn’t grab data from the web, it’s still possible that a person might automate adding data. If you do a Mac version with AppleScript support, somebody will write a script that loads crazy amounts of data. This is the same if it has a web API for adding data.\n\n#### Could There Be a Web API that Includes Database-Like Endpoints (as Opposed to Object-Like Endpoints)?\n\nAn RSS sync API could return a list of the uniqueIDs of read articles. A sync API for a note-taking app might return lists of the uniqueIDs of archived and deleted notes.\n\n#### Could a User Take Actions that Cut Across Large Numbers of Objects?\n\nUnder the hood, it’s the same issue as the previous consideration. How well does your recipes app perform when someone deletes all 5,000 pasta recipes the app has downloaded? (On an iPhone?)\n\nIf I do decide to use Core Data - (and I have: I’ve shipped Core Data apps) - I pay careful attention to how I’m using it. If, in order to get decent performance, I find that I’m using it as a weird interface to a SQL database, then I know I should drop Core Data and use SQLite more directly.\n\n## How I Use SQLite\n\nI use SQLite with the excellent [FMDB wrapper](https://github.com/ccgus/fmdb) from Flying Meat Software, by Gus Mueller.\n\n### Basic Operation\n\nI’ve been using SQLite since before iPhones, since before Core Data. Here’s the gist of how it works:\n\n- All database access — reading and writing — happens in a serial queue, in a background thread. Hitting the database on the main thread is *never* allowed. Using a serial queue ensures that everything happens in order.\n- I use blocks extensively to make async programming simpler.\n- Model objects exist on the main thread only (with two important exceptions). Changes trigger a background save.\n- Model objects list their database-stored attributes. It might be in code or might in a plist file.\n- Some model objects are uniqued and some aren’t. It depends on the needs of the app. (They’re usually unique.)\n- For relationships I avoid creating lookup tables as much as possible.\n- Some object types are read entirely into memory at start-up. For other object types I may create and maintain an NSMutableSet of just their uniqueIDs, so I know what exists and what doesn’t without having to hit the database.\n- Web API calls happen in background threads, and they get to use “detached” model objects.\n\nI'll elaborate, using code from my [current app](http://vesperapp.co/).\n\n### Database Updating\n\nI have a single database controller — `VSDatabaseController` in my latest app — that talks to SQLite via FMDB.\n\nFMDB differentiates between updates and queries. To update the database the app calls: \n\n```objc\n-[VSDatabaseController runDatabaseBlockInTransaction:(VSDatabaseUpdateBlock)databaseBlock]\n```\n\n`VSDatabaseUpdateBlock` is simple:\n\n```objc\ntypedef void (^VSDatabaseUpdateBlock)(FMDatabase *database);\n```\n\n`runDatabaseBlockInTransaction` is also simple:\n\n```objc\n- (void)runDatabaseBlockInTransaction:(VSDatabaseUpdateBlock)databaseBlock {\n    dispatch_async(self.serialDispatchQueue, ^{\n        @autoreleasepool {\n            [self beginTransaction];\n            databaseBlock(self.database);\n            [self endTransaction];\n        }\n    });\n}\n```\n\n(Note that I’m using my own serial dispatch queue. Gus recommends checking out `FMDatabaseQueue`, which is also a serial dispatch queue. I just haven’t gotten around to checking it out yet, since it’s newer than much of the rest of FMDB.)\n\nCalls to `beginTransaction` and `endTransaction` are nestable (in my database controller). At the appropriate time they call `-[FMDatabase beginTransaction]` and `-[FMDatabase commit]`. (Using transactions is a big key to making SQLite fast.) Tip: I store the current transaction count in `-[NSThread threadDictionary]`. That’s a handy spot for per-thread data, which I almost never use for anything else. Almost.\n\nHere’s a simple example of a call to update the database:\n\n```objc\n- (void)emptyTagsLookupTableForNote:(VSNote *)note {\n    NSString *uniqueID = note.uniqueID;\n    [self runDatabaseBlockInTransaction:^(FMDatabase *database) {\n        [database executeUpdate:\n            @\"delete from tagsNotesLookup where noteUniqueID = ?;\", uniqueID];\n    }];\n}\n```\n\nThis illustrates a few things. The first is that SQL isn’t that scary. Even if you’ve never seen it before, you know what’s going on in that line.\n\n`emptyTagsLookupTableForNote`, like every other public interface for `VSDatabaseController`, should be called from the main thread. Model objects may only be referenced on the main thread, and so the block references `uniqueID` but not the `VSNote` object.\n\nNote that in this case I’m updating a lookup table. Notes and tags have a many-to-many relationship, and one way to represent that is with a database table that maps note uniqueIDs and tag uniqueIDs. These tables aren’t hard to maintain, but I do try to avoid their use when possible.\n\nNote the ? in the update string. `-[FMDatabase executeUpdate:]` is a variadic function. SQLite supports using placeholders — ? characters — so you don’t have to put the actual value in the string. This is a security issue: it helps guard against SQL injection. It also saves you the trouble of having to escape values.\n\nAnd, finally, note that there is an index on noteUniqueID in the tagsNotesLookup table. (Indexes are another key to SQLite performance.) This line of code runs at each launch:\n\n```objc\n[self.database executeUpdate:\n    @\"CREATE INDEX if not exists noteUniqueIDIndex on tagsNotesLookup (noteUniqueID);\"];\n```\n\n### Database Fetching\n\nTo fetch objects, the app calls: \n\n```objc\n-[VSDatabaseController runFetchForClass:(Class)databaseObjectClass \n                             fetchBlock:(VSDatabaseFetchBlock)fetchBlock \n                      fetchResultsBlock:(VSDatabaseFetchResultsBlock)fetchResultsBlock];\n```\n\nThese two lines do much of the work:\n\n```objc\nFMResultSet *resultSet = fetchBlock(self.database);\nNSArray *fetchedObjects = [self databaseObjectsWithResultSet:resultSet \n                                                       class:databaseObjectClass];\n```\n\nA database fetch using FMDB returns an `FMResultSet`. With that resultSet you can step through and create model objects.\n\nI recommend writing general code for turning database rows into objects. One way I’ve used is to include a plist with the app that maps column names to model object properties. It also includes types, so you know whether or not to call `-[FMResultSet dateForColumn:]` versus `-[FMResultSet stringForColumn:]` versus something else.\n\nIn my latest app I did something simpler. The database rows map exactly to model object property names. All the properties are strings, except for those properties whose names end in \"Date.\" Simple, but you can see how an explicit map might be needed.\n\n#### Uniquing Objects\n\nCreation of model objects happens in the same background thread that fetches from the database. Once fetched, the app turns these over to the main thread.\n\nUsually I have the objects *uniqued*. The same database row will always result in the same object.\n\nTo do the uniquing, I create an object cache, an NSMapTable, in the init method: `_objectCache = [NSMapTable weakToWeakObjectsMapTable]`. I’ll explain:\n\nWhen, for instance, you do a database fetch and turn the objects over to a view controller, you want those objects to disappear after the view controller is finished with them, or once a different view controller is displayed.\n\nIf your object cache is an `NSMutableDictionary`, you’ll have to do some extra work to empty objects from the object cache. It becomes a pain to be sure that it references only objects that have a reference somewhere else. NSMapTable with weak references handles this automatically.\n\nSo: we unique the objects on the main thread. If an object already exists in the object cache, we use that existing object. (Main thread wins, since it might have newer changes.) If it doesn’t exist in the object cache, it’s added.\n\n#### Keeping Objects in Memory\n\nThere are times when it makes sense to keep an entire object type in memory. My latest app has a `VSTag` object. While there may be many hundreds or thousands of notes, the number of tags is small, often less than 10. And a tag has just six properties: three BOOLs, two very small NSStrings, and one NSDate.\n\nAt start-up, the app fetches all the tags and stores them in two dictionaries: one keyed by tag uniqueID, and another keyed by the lowercase tag name.\n\nThis simplifies a bunch of things, not least is the tag auto-completion system, which can operate entirely in memory and doesn’t require a database fetch.\n\nHowever, there are times when keeping all objects in memory is impractical. We don’t keep all notes in memory, for instance.\n\nThere are times, though, when for an object type that you can’t keep in memory, you will want to keep all the uniqueIDs in memory. You’d do a fetch like this:\n\n```objc\nFMResultSet *resultSet = [self.database executeQuery:@\"select uniqueID from some_table\"];\n```\n\nThe resultSet would contain just uniqueIDs, which you’d then store in an NSMutableSet.\n\nI’ve found this useful sometimes with web APIs. Picture an API call that returns a list of the uniqueIDs of notes created since a certain date and time. If I had an NSMutableSet containing all the uniqueIDs of notes known locally, I could check quickly (via `-[NSMutableSet minusSet]`) to see if there are any missing notes, and then make another API call to download any missing notes. All without hitting the database at all.\n\nBut, again, things like this should be done carefully. Can the app afford the memory? Does it really simplify programming *and* help performance?\n\nUsing SQLite and FMDB instead of Core Data allows for a ton of flexibility and makes room for clever solutions. The thing to remember is that sometimes clever is good — and sometimes clever is a big mistake.\n\n### Web APIs\n\nMy API calls all happen in a background thread (usually with an `NSOperationQueue`, so I can cancel operations). Model objects are main-thread only — and yet I pass model objects to my API calls.\n\nHere’s how: a database object has a `detachedCopy` method which copies the database object. That copy is *not* referenced in the object cache I use for uniquing. The only thing that references that object is the API call. When the API call is finished, that object, the detached copy, goes away.\n\nThis is a nice system, because it means I can still use model objects with the API calls. A method might look like this:\n\n```objc\n- (void)uploadNote:(VSNote *)note {\n    VSNoteAPICall *apiCall = [[VSNoteAPICall alloc] initWithNote:[note detachedCopy]];\n    [self enqueueAPICall:apiCall];\n}\n```\n\nAnd VSNoteAPICall would pull values from the detached `VSNote` and create the HTTP request, rather than having a dictionary or some other representation of the note.\n\n#### Handling Web API Return Values\n\nI do something similar with values returned from the web. I’ll create a model object with the returned JSON or XML or whatever, and that model object is also detached. That is, it’s not stored in the object cache used for uniquing.\n\nHere’s where things get dicey. It is sometimes necessary to use that model object to make local changes in two places: the in-memory cache *and* in the database.\n\nThe database is generally the easy part. For instance: there’s already a method in my app which saves a note object. It uses a SQL `insert or replace into` string. I just call that with a note object generated from a web API return value and the database is updated.\n\nBut there might also be an in-memory version of that same object. Luckily this is easy to find:\n\n```objc\nVSNote *cachedNote = [self.mapTable objectForKey:downloadedNote.uniqueID];\n```\n\nIf the cachedNote exists, rather than replace it (which would violate uniquing), I have it pull values from the `downloadedNote`. (This can share code with the `detachedCopy` method.)\n\nOnce that cachedNote is updated, observers will note the change via KVO, or I’ll have it post an `NSNotification` of some kind. Or both.\n\nThere are other return values from web API calls; I mentioned the big list of read items that an RSS reader might get. In this case, I’d create an `NSSet` out of that list, update the `read` property for each article cached in memory, then call `-[FMDatabase executeUpdate:]`.\n\nThe key to making this work is that an NSMapTable lookup is fast. If you find yourself looking for objects inside an NSArray, it’s time to re-think.\n\n## Database Migration\n\nCore Data’s database migration is pretty cool, [when it works](http://openradar.appspot.com/search?query=migration).\n\nBut it is, inescapably, a layer between the code and the database. If you’re using SQLite more directly, you update the database directly.\n\nYou can do this safely and easily.\n\nTo add a table, for instance:\n\n```objc\n[self.database executeUpdate:@\"CREATE TABLE if not exists tags \"\n    \"(uniqueID TEXT UNIQUE, name TEXT, deleted INTEGER, deletedModificationDate DATE);\"];\n```\n\nOr add an index:\n\n```objc\n[self.database executeUpdate:@\"CREATE INDEX if not exists \"\n    \"archivedSortDateIndex on notes (archived, sortDate);\"];\n```\n\nOr add a column:\n\n```objc\n[self.database executeUpdate:@\"ALTER TABLE tags ADD deletedDate DATE\"];\n```\n\nThe app should set up the database in code in the first place using lines like the above. Any changes added later are just added executeUpdate calls — I leave them all in and have them run in order. Since it’s my database that I designed, this isn’t a problem. (And I’ve never seen a performance issue here. It’s fast.)\n\nBigger changes take more code, of course. But if your data is available via the web, sometimes you can start with a fresh database model and re-download what you need. Sometimes.\n\n## Performance Tips\n\nSQLite can be very, very fast. It can be very slow, too. It’s all in how you use it.\n\n### Transactions\n\nWrap updates in transactions. Use `-[FMDatabase beginTransaction]` before your updates and `-[FMDatabase commit]` after the updates.\n\n### Denormalize If You Have To\n\n[Denormalization](http://en.wikipedia.org/wiki/Denormalization) is a bummer. The idea is that you add redundant data in order to speed up queries, but, of course, it also means maintaining redundant data.\n\nI avoid it like crazy, right up until it makes a serious performance difference. And then I do it as minimally as possible.\n\n### Use Indexes\n\nThe create table statement for my app’s tags table looks like this: \n\n```sql\nCREATE TABLE if not exists tags \n  (uniqueID TEXT UNIQUE, name TEXT, deleted INTEGER, deletedModificationDate DATE);\n```\n\nThe uniqueID column is automatically indexed, since it’s defined as unique. But if I wanted to query that table by name, I might make an index on the name, like this: \n\n```sql\nCREATE INDEX if not exists tagNameIndex on tags (name);\n```\n\nYou can do indexes on multiple columns at once, like this: \n\n```sql\nCREATE INDEX if not exists archivedSortDateIndex on notes (archived, sortDate);\n```\n\nBut note that too many indexes can slow down your inserts. You need just enough amount and just the right ones.\n\n### Use the Command Line App\n\nI have an `NSLog` that runs when my app launches in the simulator. It prints the path to the database, so I can open it using the command-line sqlite3 app. (Do a man sqlite3 for info about the app.)\n\nTo open the database: `sqlite3 path/to/database`.\n\nOnce open, you can look at the schema: type `.schema`.\n\nYou can do updates and run queries; it’s a great way to get your SQL correct before using it in your app.\n\nOne of the coolest parts is the [SQLite Explain Query Plan command](http://www.sqlite.org/eqp.html). You want to make sure your queries run as quickly as possible.\n\n### Real-Life Example\n\nMy app displays a table listing all the tags of non-archived notes. This query is re-run whenever a note or tag changes, and it needs to be super fast.\n\nI was able to do the query with a [SQL join](http://en.wikipedia.org/wiki/Join_%28SQL%29), but it was slow. (Joins are slow.)\n\nSo I fired up sqlite3 and started experimenting. I looked again at my schema and realized I could denormalize. While the archived status of a note is stored in the notes table, it could also be stored in the tagsNotesLookup table.\n\nThen I could do a query like this: \n\n```sql\nselect distinct tagUniqueID from tagsNotesLookup where archived=0;\n```\n\nI already had an index on tagUniqueID. So I used explain query plan to tell me what would happen when I ran that query.\n\n```\nsqlite> explain query plan select distinct tagUniqueID from tagsNotesLookup where archived=0;\n0|0|0|SCAN TABLE tagsNotesLookup USING INDEX tagUniqueIDIndex (~100000 rows)\n```\n\nIt’s nice that it’s using an index, but SCAN TABLE sounds ominous. Better yet would be a SEARCH TABLE and a [covering index](http://www.sqlite.org/queryplanner.html#covidx).\n\nI added an index on tagUniqueID and archive: \n\n```sql\nCREATE INDEX archivedTagUniqueID on tagsNotesLookup(archived, tagUniqueID);\n```\n\nI ran explain query plan again:\n\n```\nsqlite> explain query plan select distinct tagUniqueID from tagsNotesLookup where archived=0;\n0|0|0|SEARCH TABLE tagsNotesLookup USING COVERING INDEX archivedTagUniqueID (archived=?) (~10 rows)\n```\n\n*Way* better.\n\n### More Performance Tips\n\nSomewhere along the line FMDB, added the ability to cache statements, so I always call `[self.database setShouldCacheStatements:YES]` when creating/opening a database. This means you don’t have to re-compile each statement for every call.\n\nI’ve never found good guidance on using `vacuum`. If the database isn’t compacted periodically, it gets slower and slower. I have my app run a vacuum about once a week. (It stores the last vacuum date in NSUserDefaults, and checks at start if it’s been a week.)\n\nIt’s possible that auto_vacuum would be better — see the list of [pragma statements supported by SQLite](http://www.sqlite.org/pragma.html#pragma_auto_vacuum).\n\n## Bonus Cool Thing\n\nGus Mueller asked me to cover custom SQLite functions. This isn’t something I’ve actually used, but now that he’s pointed it out, it’s a safe bet I’ll find a use for it. Because it’s cool.\n\n[Gus posted a Gist](https://gist.github.com/ccgus/6324222) where a query looks like this:\n\n```sql\nselect displayName, key from items where UTTypeConformsTo(uti, ?) order by 2;\n```\n\nSQLite doesn’t know anything about UTTypes. But you can add Core functions as a block — see `-[FMDatabase makeFunctionNamed:maximumArguments:withBlock:]`.\n\nYou could instead do a larger query, and then evaluate each object — but that’s a bunch more work. Better to do the filtering at the SQL level instead of after turning table rows into objects.\n\n## Finally\n\nYou really should use Core Data. I’m not kidding.\n\nI’ve been using SQLite and FMDB for a long time, and I get a particular thrill out of going the extra mile (or two or ten) and getting exceptional performance.\n\nBut remember that devices are getting faster. And also remember that anybody else who looks at your code is going to expect Core Data, which he or she already knows — someone else isn't going to know how your database code works.\n\nSo please treat this entire article as a madman’s yelling about the detailed and crazy world he’s created for himself — and locked himself into.\n\nJust shake your head a little sadly, and please enjoy the awesome Core Data articles in this issue.\n\nUp next for me, after checking out the custom SQLite functions feature Gus pointed out, is investigating SQLite’s [full-text search extension](http://www.sqlite.org/fts3.html). There’s always more to learn.\n"
  },
  {
    "path": "2013-09-09-core-data-fetch-requests.md",
    "content": "---\ntitle: Fetch Requests\ncategory: \"4\"\ndate: \"2013-09-09 06:00:00\"\nauthor:\n  - name: Daniel Eggert\n    url: http://twitter.com/danielboedewadt\ntags: article\n---\n\n\nA way to get objects out of the store is to use an `NSFetchRequest`. Note, though, that one of the most common mistakes is to fetch data when you don't need to. Make sure you read and understand [Getting to Objects][320]. Most of the time, traversing relationships is more efficient, and using an `NSFetchRequest` is often expensive.\n\nThere are usually two reasons to perform a fetch with an `NSFetchRequest`: (1) You need to search your entire object graph for objects that match specific predicates. Or (2), you want to display all your objects, e.g. in a table view. There's a third, less-common scenario, where you're traversing relationships but want to pre-fetch more efficiently. We'll briefly dive into that, too. But let us first look at the main two reasons, which are more common and each have their own set of complexities.\n\n## The Basics\n\nWe won't cover the basics here, since the Xcode Documentation on Core Data called [Fetching Managed Objects](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html) covers a lot of ground already. We'll dive right into some more specialized aspects.\n\n## Searching the Object Graph\n\nIn our [sample with transportation data](https://github.com/objcio/issue-4-importing-and-fetching), we have 12,800 stops and almost 3,000,000 stop times that are interrelated. If we want to find stop times with a departure time between 8:00 and 8:30 for stops close to 52° 29' 57.30\" North, +13° 25' 5.40\" East, we don't want to load all 12,800 *stop* objects and all three million *stop time* objects into the context and then loop through them. If we did, we'd have to spend a huge amount of time to simply load all objects into memory and then a fairly large amount of memory to hold all of these in memory. Instead what we want to do is have SQLite narrow down the set of objects that we're pulling into memory.\n\n\n### Geo-Location Predicate\n\nLet's start out small and create a fetch request for stops close to 52° 29' 57.30\" North, +13° 25' 5.40\" East. First we create the fetch request:\n\n```objc\nNSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:[Stop entityName]]\n```\n\nWe're using the `+entityName` method that we mention in [Florian's data model article][250]. Next, we need to limit the results to just those close to our point.\n\nWe'll simply use a (not quite) square region around our point of interest. The actual math is [a bit complex](https://en.wikipedia.org/wiki/Geographical_distance), because the Earth happens to be somewhat similar to an ellipsoid. If we cheat a bit and assume the earth is spherical, we get away with this formula:\n\n```\nD = R * sqrt( (deltaLatitude * deltaLatitude) +\n              (cos(meanLatitidue) * deltaLongitude) * (cos(meanLatitidue) * deltaLongitude))\n```\n\nWe end up with something like this (all approximate):\n\n```objc\nstatic double const R = 6371009000; // Earth readius in meters\ndouble deltaLatitude = D / R * 180 / M_PI;\ndouble deltaLongitude = D / (R * cos(meanLatitidue)) * 180 / M_PI;\n```\n\nOur point of interest is:\n\n```objc\nCLLocation *pointOfInterest = [[CLLocation alloc] initWithLatitude:52.4992490 \n                                                         longitude:13.4181670];\n```\n\nWe want to search within ±263 feet (80 meters):\n    \n```objc\nstatic double const D = 80. * 1.1;\ndouble const R = 6371009.; // Earth readius in meters\ndouble meanLatitidue = pointOfInterest.latitude * M_PI / 180.;\ndouble deltaLatitude = D / R * 180. / M_PI;\ndouble deltaLongitude = D / (R * cos(meanLatitidue)) * 180. / M_PI;\ndouble minLatitude = pointOfInterest.latitude - deltaLatitude;\ndouble maxLatitude = pointOfInterest.latitude + deltaLatitude;\ndouble minLongitude = pointOfInterest.longitude - deltaLongitude;\ndouble maxLongitude = pointOfInterest.longitude + deltaLongitude;\n```\n\n(This math is broken when we're close to the 180° meridian. We'll ignore that since our traffic data is for Berlin which is far, far away.)\n\n```objc\nrequest.result = [NSPredicate predicateWithFormat:\n                  @\"(%@ <= longitude) AND (longitude <= %@)\"\n                  @\"AND (%@ <= latitude) AND (latitude <= %@)\",\n                  @(minLongitude), @(maxLongitude), @(minLatitude), @(maxLatitude)];\n```\n\nThere's no point in specifying a sort descriptor. Since we're going to be doing a second in-memory pass over all objects, we will, however, ask Core Data to fill in all values for all returned objects:\n\n```objc\nrequest.returnsObjectsAsFaults = NO;\n```\n\nWithout this, Core Data will fetch all values into the persistent store coordinator's row cache, but it will not populate the actual objects. Often that makes sense, but since we'll immediately be accessing all of the objects, we don't want that behavior.\n\nAs a safe-guard, it's good to add:\n\n```objc\nrequest.fetchLimit = 200;\n```\n\nWe execute this fetch request:\n\n```objc\nNSError *error = nil;\nNSArray *stops = [moc executeFetchRequest:request error:&error];\nNSAssert(stops != nil, @\"Failed to execute %@: %@\", request, error);\n```\n\nThe only (likely) reasons the fetch would fail is if the store went corrupt (file was deleted, etc.) or if there's a syntax error in the fetch request. So it's safe to use `NSAssert()` here.\n\nWe'll now do the second pass over the in-memory data using Core Locations advance distance math:\n\n```objc\nNSPredicate *exactPredicate = [self exactLatitudeAndLongitudePredicateForCoordinate:self.location.coordinate];\nstops = [stops filteredArrayUsingPredicate:exactPredicate];\n```\n\nand:\n\n```objc\n- (NSPredicate *)exactLatitudeAndLongitudePredicateForCoordinate:(CLLocationCoordinate2D)pointOfInterest;\n{\n    return [NSPredicate predicateWithBlock:^BOOL(Stop *evaluatedStop, NSDictionary *bindings) {\n        CLLocation *evaluatedLocation = [[CLLocation alloc] initWithLatitude:evaluatedStop.latitude longitude:evaluatedStop.longitude];\n        CLLocationDistance distance = [self.location distanceFromLocation:evaluatedLocation];\n        return (distance < self.distance);\n    }];\n}\n```\n\nAnd we're all set.\n\n#### Geo-Location Performance\n\nThese fetches take around 360µs on average on a recent MacBook Pro with SSD. That is, you can do approximately 2,800 of these requests per second. On an iPhone 5 we'd be getting around 1.67ms on average, or some 600 requests per second.\n\nIf we add `-com.apple.CoreData.SQLDebug 1` as launch arguments to the app, we get this output:\n\n```\nsql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZIDENTIFIER, t0.ZLATITUDE, t0.ZLONGITUDE, t0.ZNAME FROM ZSTOP t0 WHERE (? <=  t0.ZLONGITUDE AND  t0.ZLONGITUDE <= ? AND ? <=  t0.ZLATITUDE AND  t0.ZLATITUDE <= ?)  LIMIT 100\nannotation: sql connection fetch time: 0.0008s\nannotation: total fetch execution time: 0.0013s for 15 rows.\n```\n\nIn addition to some statistics (for the store itself), this shows us the generated SQL for these fetches:\n\n```\nSELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZIDENTIFIER, t0.ZLATITUDE, t0.ZLONGITUDE, t0.ZNAME FROM ZSTOP t0\nWHERE (? <=  t0.ZLONGITUDE AND  t0.ZLONGITUDE <= ? AND ? <=  t0.ZLATITUDE AND  t0.ZLATITUDE <= ?)\nLIMIT 200\n```\n\nwhich is what we'd expect. If we'd want to investigate the performance, we can use the SQL [`EXPLAIN` command](https://www.sqlite.org/eqp.html). For this, we'd open the database with the command line `sqlite3` command like this:\n\n```\n% cd TrafficSearch\n% sqlite3 transit-data.sqlite\nSQLite version 3.7.13 2012-07-17 17:46:21\nEnter \".help\" for instructions\nEnter SQL statements terminated with a \";\"\nsqlite> EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZIDENTIFIER, t0.ZLATITUDE, t0.ZLONGITUDE, t0.ZNAME FROM ZSTOP t0\n   ...> WHERE (13.30845219672199 <=  t0.ZLONGITUDE AND  t0.ZLONGITUDE <= 13.33441458422844 AND 52.42769566863058 <=  t0.ZLATITUDE AND  t0.ZLATITUDE <= 52.44352370653525)\n   ...> LIMIT 100;\n0|0|0|SEARCH TABLE ZSTOP AS t0 USING INDEX ZSTOP_ZLONGITUDE_INDEX (ZLONGITUDE>? AND ZLONGITUDE<?) (~6944 rows)\n```\n\nThis tell us that SQLite was using the `ZSTOP_ZLONGITUDE_INDEX` for the `(ZLONGITUDE>? AND ZLONGITUDE<?)` condition. We could do better by using a *compound index* as described in the [model article][260]. Since we'd always search for a combination of longitude and latitude that is more efficient, and we can remove the individual indexes on longitude and latitude.\n\nThis would make the output look like this:\n\n```\n0|0|0|SEARCH TABLE ZSTOP AS t0 USING INDEX ZSTOP_ZLONGITUDE_ZLATITUDE (ZLONGITUDE>? AND ZLONGITUDE<?) (~6944 rows)\n```\n\nIn our simple case, adding a compound index hardly affects performance.\n\nAs explained in the [SQLite Documentation](https://www.sqlite.org/eqp.html), the warning sign is a `SCAN TABLE` in the output. That basically means that SQLite needs to go through *all* entries to see which ones are matching. Unless you store just a few objects, you'd probably want an index.\n\n### Subqueries\n\nLet's say we only want those stops near us that are serviced within the next twenty minutes.\n\nWe can create a predicate for the *StopTimes* entity like this:\n\n```objc\nNSPredicate *timePredicate = [NSPredicate predicateWithFormat:@\"(%@ <= departureTime) && (departureTime <= %@)\",\n                              startDate, endDate];\n```\n\nBut what if what we want is a predicate that we can use to filter *Stop* objects based on the relationship to *StopTime* objects, not *StopTime* objects themselves? We can do that with a `SUBQUERY` like this:\n\n```objc\nNSPredicate *predicate = [NSPredicate predicateWithFormat:\n                          @\"(SUBQUERY(stopTimes, $x, (%@ <= $x.departureTime) && ($x.departureTime <= %@)).@count != 0)\",\n                          startDate, endDate];\n```\n\nNote that this logic is slightly flawed if we're close to midnight, since we ought to wrap by splitting the predicate up into two. But it'll work for this example.\n\nSubqueries are very powerful for limiting data across relationship. The Xcode documentation for [`-[NSExpression expressionForSubquery:usingIteratorVariable:predicate:]`](https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSExpression_Class/Reference/NSExpression.html#//apple_ref/occ/clm/NSExpression/expressionForSubquery:usingIteratorVariable:predicate:) has more info. \n\nWe can combine two predicates simply using `AND` or `&&`, i.e.\n\n```objc\n[NSPredicate predicateWithFormat:@\"(%@ <= departureTime) && (SUBQUERY(stopTimes ....\n```\n\nor in code using [`+[NSCompoundPredicate andPredicateWithSubpredicates:]`](https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSCompoundPredicate_Class/Reference/Reference.html#//apple_ref/occ/clm/NSCompoundPredicate/andPredicateWithSubpredicates:).\n\nWe end up with a predicate that looks like this:\n\n```\n(lldb) po predicate\n(13.39657778010461 <= longitude AND longitude <= 13.42266155792719\n    AND 52.63249629924865 <= latitude AND latitude <= 52.64832433715332)\n    AND SUBQUERY(\n        stopTimes, $x, CAST(-978250148.000000, \"NSDate\") <= $x.departureTime \n        AND $x.departureTime <= CAST(-978306000.000000, \"NSDate\")\n    ).@count != 0\n```\n\n#### Subquery Performance\n\nIf we look at the generated SQL it looks like this:\n\n```\nsql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZIDENTIFIER, t0.ZLATITUDE, t0.ZLONGITUDE, t0.ZNAME FROM ZSTOP t0\n     WHERE ((? <=  t0.ZLONGITUDE AND  t0.ZLONGITUDE <= ? AND ? <=  t0.ZLATITUDE AND  t0.ZLATITUDE <= ?)\n            AND (SELECT COUNT(t1.Z_PK) FROM ZSTOPTIME t1 WHERE (t0.Z_PK = t1.ZSTOP AND ((? <=  t1.ZDEPARTURETIME AND  t1.ZDEPARTURETIME <= ?))) ) <> ?)\n     LIMIT 200\n```\n\nThis fetch request now takes around 12.3 ms to run on a recent MacBook Pro. On an iPhone 5, it'll take about 110 ms. Note that we have three million stop times and almost 13,000 stops.\n\nThe query plan explanation looks like this:\n\n```\nsqlite> EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZIDENTIFIER, t0.ZLATITUDE, t0.ZLONGITUDE, t0.ZNAME FROM ZSTOP t0\n   ...> WHERE ((13.37190946378911 <=  t0.ZLONGITUDE AND  t0.ZLONGITUDE <= 13.3978625285315 AND 52.41186440524024 <=  t0.ZLATITUDE AND  t0.ZLATITUDE <= 52.42769244314491) AND\n   ...> (SELECT COUNT(t1.Z_PK) FROM ZSTOPTIME t1 WHERE (t0.Z_PK = t1.ZSTOP AND ((-978291733.000000 <=  t1.ZDEPARTURETIME AND  t1.ZDEPARTURETIME <= -978290533.000000))) ) <> ?)\n   ...> LIMIT 200;\n0|0|0|SEARCH TABLE ZSTOP AS t0 USING INDEX ZSTOP_ZLONGITUDE_ZLATITUDE (ZLONGITUDE>? AND ZLONGITUDE<?) (~3472 rows)\n0|0|0|EXECUTE CORRELATED SCALAR SUBQUERY 1\n1|0|0|SEARCH TABLE ZSTOPTIME AS t1 USING INDEX ZSTOPTIME_ZSTOP_INDEX (ZSTOP=?) (~2 rows)\n```\n\nNote that it is important how we order the predicate. We want to put the longitude and latitude stuff first, since it's cheap, and the subquery last, since it's expensive.\n\n\n### Text Search\n\nA common scenario is searching for text. In our case, let's look at searching for *Stop* entities by their name.\n\nBerlin has a stop called \"U Görlitzer Bahnhof (Berlin)\". A naïve way to search for that would be:\n\n```objc\nNSString *searchString = @\"U Görli\";\npredicate = [NSPredicate predicateWithFormat:@\"name BEGINSWITH %@\", searchString];\n```\n\nThings get even worse if you want to be able to do:\n\n```\nname BEGINSWITH[cd] 'u gorli'\n```\n\ni.e. do a case and / or diacritic insensitive lookup.\n\nThings are not that simple, though. Unicode is very complicated and there are quite a few gotchas. First and foremost ís that many characters can be represented in multiple ways. Both [U+00F6](http://unicode.org/charts/PDF/U0080.pdf) and [U+006F](http://www.unicode.org/charts/PDF/U0000.pdf) [U+0308](http://unicode.org/charts/PDF/U0300.pdf) represent \"ö.\" And concepts such as uppercase / lowercase are very complicated once you're outside the ASCII code points.\n\nSQLite will do the heavy lifting for you, but it comes at a price. It may seem straightforward, but it's really not. What we want to do for string searches is to have a *normalized* version of the field that you can search on. We'll remove diacritics and make the string lowercase and then put that into a`normalizedName` field. We'll then do the same to our search string. SQLite then won't have to consider diacritics and case, and the search will effectively still be case and diacritics insensitive. But we have to do the heavy lifting up front.\n\nSearching with `BEGINSWITH[cd]` takes around 7.6 ms on a recent MacBook Pro with the sample strings in our sample code (130 searches / second). On an iPhone 5 those numbers are 47 ms per search and 21 searches per second.\n\nTo make a string lowercase and remove diacritics, we can use `CFStringTransform()`:\n\n```objc\n@implementation NSString (SearchNormalization)\n\n- (NSString *)normalizedSearchString;\n{\n    // C.f. <http://userguide.icu-project.org/transforms>\n    NSString *mutableName = [self mutableCopy];\n    CFStringTransform((__bridge CFMutableStringRef) mutableName, NULL, \n                      (__bridge CFStringRef)@\"NFD; [:Nonspacing Mark:] Remove; Lower(); NFC\", NO);\n    return mutableName;\n}\n\n@end\n```\n\nWe'll update the `Stop` class to automatically update the `normalizedName`:\n\n```objc\n@interface Stop (CoreDataForward)\n\n@property (nonatomic, strong) NSString *primitiveName;\n@property (nonatomic, strong) NSString *primitiveNormalizedName;\n\n@end\n\n\n@implementation Stop\n\n@dynamic name;\n- (void)setName:(NSString *)name;\n{\n    [self willAccessValueForKey:@\"name\"];\n    [self willAccessValueForKey:@\"normalizedName\"];\n    self.primitiveName = name;\n    self.primitiveNormalizedName = [name normalizedSearchString];\n    [self didAccessValueForKey:@\"normalizedName\"];\n    [self didAccessValueForKey:@\"name\"];\n}\n\n// ...\n\n@end\n```\n\nWith this, we can search with `BEGINSWITH` instead of `BEGINSWITH[cd]`:\n\n```objc\npredicate = [NSPredicate predicateWithFormat:@\"normalizedName BEGINSWITH %@\", [searchString normalizedSearchString]];\n```\n\nSearching with `BEGINSWITH` takes around 6.2 ms on a recent MacBook Pro with the sample strings in our sample code (160 searches / second). On an iPhone 5 it takes 40ms corresponding to 25 searches / second.\n\n\n#### Free Text Search\n\nOur search still only works if the beginning of the string matches the search string. The way to fix that is to create another *Entity* that we search on. Let's call this Entity `SearchTerm`, and give it a single attribute `normalizedWord` and a relationship to a `Stop`. For each `Stop` we would then normalize the name and split it into words, e.g.:\n\n```\n    \"Gedenkstätte Dt. Widerstand (Berlin)\"\n->  \"gedenkstatte dt. widerstand (berlin)\"\n->  \"gedenkstatte\", \"dt\", \"widerstand\", \"berlin\"\n```\n\nFor each word, we create a `SearchTerm` and a relationship from the `Stop` to all its `SearchTerm` objects. When the user enters a string, we search on the `SearchTerm` objects' `normalizedWord` with:\n\n```objc\npredicate = [NSPredicate predicateWithFormat:@\"normalizedWord BEGINSWITH %@\", [searchString normalizedSearchString]]\n```\n\nThis can also be done in a subquery directly on the `Stop` objects.\n\n## Fetching All Objects\n\nIf we don't set a predicate on our fetch request, we'll retrieve all objects for the given *Entity*. If we did that for the `StopTimes` entity, we'll be pulling in three million objects. That would be slow, and use up a lot of memory. Sometimes, however, we need to get all objects. The common example is that we want to show all objects inside a table view.\n\nWhat we would do in this case, is to set a batch size:\n  \n```objc\nrequest.fetchBatchSize = 50;\n```\n\nWhen we run `-[NSManagedObjectContext executeFetchRequest:error:]` with a batch size set, we still get an array back. We can ask it for its count (which will be close to three million for the `StopTime` entity), but Core Data will only populate it with objects as we iterate through the array. And Core Data will get rid of objects again, as they're no longer accessed. Simply put, the array has batches of size 50 (in this case). Core Data will pull in 50 objects at a time. Once more than a certain number of these batches are around, Core Data will release the oldest batch. That way you can loop through all objects in such an array, without having to have all three million objects in memory at the same time.\n\nOn iOS, when you use an `NSFetchedResultsController` and you have a lot of objects, make sure that the `fetchBatchSize` is set on your fetch request. You'll have to experiment with what size works well for you. Twice the amount of objects that you'll be displaying at any given point in time is a good starting point.\n\n\n\n[100]:/issues/4-core-data/importing-large-data-sets-into-core-data/\n[110]:/issues/4-core-data/importing-large-data-sets-into-core-data/#efficient-importing\n[120]:/issues/4-core-data/importing-large-data-sets-into-core-data/#user-generated-data\n\n[200]:/issues/4-core-data/core-data-models-and-model-objects/\n[210]:/issues/4-core-data/core-data-models-and-model-objects/#managed-objects\n[220]:/issues/4-core-data/core-data-models-and-model-objects/#validation\n[230]:/issues/4-core-data/core-data-models-and-model-objects/#ivars-in-managed-object-classes\n[240]:/issues/4-core-data/core-data-models-and-model-objects/#entity-vs-class-hierarchy\n[250]:/issues/4-core-data/core-data-models-and-model-objects/#creating-objects\n[260]:/issues/4-core-data/core-data-models-and-model-objects/#indexes\n\n[300]:/issues/4-core-data/core-data-overview/\n[310]:/issues/4-core-data/core-data-overview/#complicated-stacks\n[320]:/issues/4-core-data/core-data-overview/#getting-to-objects\n\n[400]:/issues/4-core-data/full-core-data-application/\n\n[500]:/issues/4-core-data/SQLite-instead-of-core-data/\n\n[600]:/issues/4-core-data/core-data-fetch-requests/\n\n[700]:/issues/4-core-data/core-data-migration/\n"
  },
  {
    "path": "2013-09-09-core-data-migration.md",
    "content": "---\ntitle: Custom Core Data Migrations\ncategory: \"4\"\ndate: \"2013-09-06 05:00:00\"\nauthor:\n  - name: Martin Hwasser\n    url: http://github.com/hwaxxer\ntags: article\n---\n\n\nCustom Core Data migrations are somewhat of an obscure topic. Apple provides little documentation on the subject, and it can be a frightening experience when first ventured into. Given the nature of client side programs, there’s no way to test all the possible permutations of datasets that your users will have. Moreover, it’s very hard to fix any issues that might occur during migration and rollback isn’t an option since you most likely have code that depends on the latest data model. \n\nIn this article we’ll go through the process of setting up custom Core Data migrations, with a focus on refactoring the data model. We’ll look into extracting data from the previous model and using that data to populate the destination model with new entities and relationships. In addition, there’s an [example project](https://github.com/objcio/issue-4-core-data-migration) including unit tests that demonstrate two custom migrations. \n\nNote that for simple changes to the data model, like adding an entity or optional attribute, lightweight migrations are great. They're extremely easy to set up which is why the topic won’t be brought up in this article. To find out what kind of changes are supported by lightweight migrations and which are not, take a look at the [official documentation](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreDataVersioning/Articles/vmLightweightMigration.html). \n\nThat said, if you work fast and need to make relatively complex changes to your data model, custom migrations are for you.\n\n## Mapping Models\n\nWhen you add a new version of your data model, you are asked to select the model on which it should be based. For lightweight migrations, the persistent store will infer a *mapping model* automatically for you. However, if you want to make a change to the new model that’s not supported by lightweight migrations, you need to create a mapping model. A mapping model needs a source and a destination data model. `NSMigrationManager` can infer the mapping model between two models. This makes it tempting to create a mapping model between each previous model all the way up to your latest one, but this quickly gets messy. For each new model version, the amount of mapping models you need to create increases linearly. This may not seem like a big deal, but along with it comes the added complexity of testing each mapping model.\n\nImagine that you just shipped an update containing version 3 of your data model. One of your users hasn’t updated your app in some time. This user is currently on version 1 of your data model. Now you’d need a mapping model from version 1 to version 3. You also need a mapping model from version 2 to version 3. As you add version 4, you need to create three new mapping models. Clearly this doesn’t scale very well. Enter progressive migrations.\n\n## Progressive Migrations\n\nRather than creating one mapping model between each previous data model to your new one, you create one mapping model for each consecutive data model. Given the previous example, you would need one mapping model between version 1 and version 2, and one mapping model between version 2 and version 3. This way you migrate from versions 1 > 2 > 3. Granted, this kind of migration will be slower for users on an older data model, but it will save development time and ensure robustness, since you only need to make sure that the migration from your previous model to your new model works, as the previous mapping models have already been tested.\n\nThe general idea is to manually figure out the mapping model between the current version `v` and `v+1`, migrate between those, and continue recursively until the persistent store is compatible with the current data model.\n\nThis looks something like this (the full version can be found in the [example project](https://github.com/objcio/issue-4-core-data-migration)):\n\n```objc\n- (BOOL)progressivelyMigrateURL:(NSURL *)sourceStoreURL\n                         ofType:(NSString *)type\n                        toModel:(NSManagedObjectModel *)finalModel\n                          error:(NSError **)error\n{\n    NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:type\n                                                                                              URL:sourceStoreURL\n                                                                                            error:error];\n    if (!sourceMetadata) {\n        return NO;\n    }\n    if ([finalModel isConfiguration:nil\n        compatibleWithStoreMetadata:sourceMetadata]) {\n        if (NULL != error) {\n            *error = nil;\n        }\n        return YES;\n    }\n    NSManagedObjectModel *sourceModel = [self sourceModelForSourceMetadata:sourceMetadata];\n    NSManagedObjectModel *destinationModel = nil;\n    NSMappingModel *mappingModel = nil;\n    NSString *modelName = nil;\n    if (![self getDestinationModel:&destinationModel\n                      mappingModel:&mappingModel\n                         modelName:&modelName\n                    forSourceModel:sourceModel\n                             error:error]) {\n        return NO;\n    }\n    // We have a mapping model, time to migrate\n    NSURL *destinationStoreURL = [self destinationStoreURLWithSourceStoreURL:sourceStoreURL\n                                                                   modelName:modelName];\n    NSMigrationManager *manager = [[NSMigrationManager alloc] initWithSourceModel:sourceModel\n                                                                 destinationModel:destinationModel];\n    if (![manager migrateStoreFromURL:sourceStoreURL\n                                 type:type\n                              options:nil\n                     withMappingModel:mappingModel\n                     toDestinationURL:destinationStoreURL\n                      destinationType:type\n                   destinationOptions:nil\n                                error:error]) {\n        return NO;\n    }\n    // Migration was successful, move the files around to preserve the source in case things go bad\n    if (![self backupSourceStoreAtURL:sourceStoreURL\n          movingDestinationStoreAtURL:destinationStoreURL\n                                error:error]) {\n        return NO;\n    }\n    // We may not be at the \"current\" model yet, so recurse\n    return [self progressivelyMigrateURL:sourceStoreURL\n                                  ofType:type\n                                 toModel:finalModel\n                                   error:error];\n}\n```\n\nCredit for the major chunk of this code goes to [Marcus Zarra](https://twitter.com/mzarra), who wrote a great book on Core Data. [Check it out here](http://pragprog.com/book/mzcd2/core-data). \n\n_Since iOS 7 and OS X Mavericks, Apple changed the journaling mode for SQLite stores to Write-Ahead Logging (WAL), which means transactions are appended to a -wal file. This causes copies of the store to likely result in data loss and inconsistencies. To safely back up our store, we’ll change the journaling mode to rollback mode. This is easily done by passing a dictionary to `-addPersistentStoreWithType:configuration:URL:options:error:` if we are about to migrate (and subsequently back up the store)._\n\n```objc\n@{ NSSQLitePragmasOption: @{ @\"journal_mode\": @\"DELETE” } }\n```\n\n_The relevant code to create the `NSPersistentStoreCoordinator` can be found [here](https://github.com/objcio/issue-4-core-data-migration/blob/master/BookMigration/MHWCoreDataController.m#L73-L84)._\n\n## Migration Policies\n\n`NSEntityMigrationPolicy` is the essence of the custom migration process. [From the documentation](https://developer.apple.com/library/ios/documentation/cocoa/Reference/NSEntityMigrationPolicy_class/NSEntityMigrationPolicy.html): \n> Instances of `NSEntityMigrationPolicy` customize the migration process for an entity mapping. \nSimply put, this class allows us not only to modify the attributes and relationships of an entity, but do any other processing we might need as each instance of that entity is migrated. \n\n### A Migration Example\n\nLet’s say we have [a book app with a simple data model](https://github.com/objcio/issue-4-core-data-migration). There are two entities: `User` and `Book`. The `Book` entity has an attribute called `authorName`. We want to improve this model and add a new entity: `Author`. We also want to create a many-to-many relationship between `Book` and `Author`, as a book can have multiple authors, and an author can write multiple books. We will extract the `authorName` from the `Book` object, and use that to populate a new entity and establish the relationship.\n\nThe very first thing we need to do is to add a new model version based on the first data model. For this example, we added an `Author` entity with a many-to-many relationship with `Book`. \n\n![](/images/issue-4/cdm-model-2.png)\n\nNow the data model suits our purposes, but we’ll need to migrate any existing data. This is where `NSEntityMigrationPolicy` comes in. We create a subclass of `NSEntityMigrationPolicy` called [`MHWBookToBookPolicy`](https://github.com/objcio/issue-4-core-data-migration/blob/master/BookMigration/MHWBookToBookPolicy.m). In the mapping model, we select the `Book` entity and set it as the custom policy in the Utilities section.\n\n![Custom NSEntityMigrationPolicy subclass](/images/issue-4/cdm-book-to-book-policy.png)\n\nWe also use the user info dictionary to set a `modelVersion` which will come in handy in future migrations.\n\nIn [`MHWBookToBookPolicy`](https://github.com/objcio/issue-4-core-data-migration/blob/master/BookMigration/MHWBookToBookPolicy.m) we’ll override `-createDestinationInstancesForSourceInstance:entityMapping:manager:error:` which lets us customize how to migrate each `Book` instance. If the value of `modelVersion` isn’t 2, we’ll just call the super implementation, otherwise we need to do a custom migration. We’ll start off by inserting a new `NSManagedObject` based on the mapping’s destination entity into the destination context. Then we iterate through the attribute keys of the destination instance and populate them with the values from the source instance. This ensures that we preserve the existing data and avoid setting any values that have been removed in the destination instance:\n\n```objc\nNSNumber *modelVersion = [mapping.userInfo valueForKey:@\"modelVersion\"];\nif (modelVersion.integerValue == 2) {\n    NSMutableArray *sourceKeys = [sourceInstance.entity.attributesByName.allKeys mutableCopy];\n    NSDictionary *sourceValues = [sourceInstance dictionaryWithValuesForKeys:sourceKeys];\n    NSManagedObject *destinationInstance = [NSEntityDescription insertNewObjectForEntityForName:mapping.destinationEntityName\n                                                                         inManagedObjectContext:manager.destinationContext];\n    NSArray *destinationKeys = destinationInstance.entity.attributesByName.allKeys;\n    for (NSString *key in destinationKeys) {\n        id value = [sourceValues valueForKey:key];\n        // Avoid NULL values\n        if (value && ![value isEqual:[NSNull null]]) {\n            [destinationInstance setValue:value forKey:key];\n        }\n    }\n}\n```\n\nThen we’ll create the `Author` entity, based on the values from the source instance. But what happens now if there are multiple books with the same author? We’ll make use of a category method on `NSMigrationManager` to create a lookup dictionary, making sure we only create one `Author` entity for each unique `Author` name:\n\n```objc\nNSMutableDictionary *authorLookup = [manager lookupWithKey:@\"authors\"];\n// Check if we’ve already created this author\nNSString *authorName = [sourceInstance valueForKey:@\"author\"];\nNSManagedObject *author = [authorLookup valueForKey:authorName];\nif (!author) {\n    // Create the author\n    // Populate lookup for reuse\n    [authorLookup setValue:author forKey:authorName];\n}\n[destinationInstance performSelector:@selector(addAuthorsObject:) withObject:author];\n```\n\nFinally, we need to tell the migration manager to associate data between the source and destination stores:\n\n```objc\n[manager associateSourceInstance:sourceInstance\n         withDestinationInstance:destinationInstance\n                forEntityMapping:mapping];\nreturn YES;\n```\n\nIn a category on `NSMigrationManager`:\n\n```objc\n@implementation NSMigrationManager (Lookup)\n\n- (NSMutableDictionary *)lookupWithKey:(NSString *)lookupKey\n{\n    NSMutableDictionary *userInfo = (NSMutableDictionary *)self.userInfo;\n    // Check if we’ve already created a userInfo dictionary\n    if (!userInfo) {\n        userInfo = [@{} mutableCopy];\n        self.userInfo = userInfo;\n    }\n    NSMutableDictionary *lookup = [userInfo valueForKey:lookupKey];\n    if (!lookup) {\n        lookup = [@{} mutableCopy];\n        [userInfo setValue:lookup forKey:lookupKey];\n    }\n    return lookup;\n}\n\n@end\n```\n\n### A More Complex Migration\n\nLater on, we want to move the `fileURL` from the `Book` entity into a new entity called `File`.\nWe want to rearrange the relationships so that a `User` has a one-to-many relationship with `File`, which in turn has a many-to-one relationship with `Book`. \n\n![Our 3rd model](/images/issue-4/cdm-model-3.png)\n\nIn the previous migration, we were only migrating one entity. When we add `File`, things become a bit more tricky. We can’t simply insert a `File` entity when migrating a `Book` and set its relationship with `User`, because the `User` entity hasn’t yet been migrated and has no files-relationship. *We have to think about the order in which the migration is executed*. In the mapping model, it’s possible to rearrange the order of the entity mappings. For this case, we want to put the `UserToUser` mapping above the `BookToBook` mapping. This guarantees that the `User` entity will be migrated before the `Book` entity.\n\n![Mapping model orders are important](/images/issue-4/cdm-mapping-order.png)\n\nThe approach for adding a `File` entity is similar to when we created the `Author` entity. We’ll create `File` objects when we migrate the `Book` entity in `MHWBookToBookPolicy`. We’ll look at the source instance’s users, create a new `File` object for each user, and establish the relationship:\n\n```objc\nNSArray *users = [sourceInstance valueForKey:@\"users\"];\nfor (NSManagedObject *user in users) {\n\n    NSManagedObject *file = [NSEntityDescription insertNewObjectForEntityForName:@\"File\"\n                                                          inManagedObjectContext:manager.destinationContext];\n    [file setValue:[sourceInstance valueForKey:@\"fileURL\"] forKey:@\"fileURL\"];\n    [file setValue:destinationInstance forKey:@\"book\"];\n    \n    NSInteger userId = [[user valueForKey:@\"userId\"] integerValue];\n    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@\"User\"];\n    request.predicate = [NSPredicate predicateWithFormat:@\"userId = %d\", userId];\n    NSManagedObject *user = [[manager.destinationContext executeFetchRequest:request error:nil] lastObject];\n    [file setValue:user forKey:@\"user\"];\n}\n```\n\n### Large Datasets\n\nIf your store contains a lot of data, to a point where the migration will consume too much memory, Core Data provides a way to migrate in chunks. Apple briefly mentions it [here](https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/CoreDataVersioning/Articles/vmCustomizing.html#//apple_ref/doc/uid/TP40004399-CH8-SW9). The approach is to separate your migration using multiple mapping models and migrating once with each of the mapping models. This requires that you have an object graph in which migration can be divided into two or more parts. The code we need to add to support this is actually very little. \n\nFirst, let’s update our migration method to support migrating using multiple mapping models. Since the order of mapping models is important, we’ll ask for them in a delegate method:\n\n```objc\nNSArray *mappingModels = @[mappingModel]; // The one we found previously\nif ([self.delegate respondsToSelector:@selector(migrationManager:mappingModelsForSourceModel:)]) {\n    NSArray *explicitMappingModels = [self.delegate migrationManager:self\n                                         mappingModelsForSourceModel:sourceModel];\n    if (0 < explicitMappingModels.count) {\n        mappingModels = explicitMappingModels;\n    }\n}\nfor (NSMappingModel *mappingModel in mappingModels) {\n    didMigrate = [manager migrateStoreFromURL:sourceStoreURL\n                                         type:type\n                                      options:nil\n                             withMappingModel:mappingModel\n                             toDestinationURL:destinationStoreURL\n                              destinationType:type\n                           destinationOptions:nil\n                                        error:error];\n}\n```\n\nNow, how do we know which mapping models to use for a particular source model? The API here makes this a bit clumsy but the following solution does the job. In the delegate method, we figure out the name of the source model and return the relevant mapping models:\n\n```objc\n- (NSArray *)migrationManager:(MHWMigrationManager *)migrationManager \n  mappingModelsForSourceModel:(NSManagedObjectModel *)sourceModel\n{\n    NSMutableArray *mappingModels = [@[] mutableCopy];\n    NSString *modelName = [sourceModel mhw_modelName];\n    if ([modelName isEqual:@\"Model2\"]) {\n        // Add mapping models to mappingModels \n    }\n    return mappingModels;\n}\n```\n\nWe’ll add a category on `NSManagedObjectModel` that helps us figure out its filename:\n\n```objc\n- (NSString *)mhw_modelName\n{\n    NSString *modelName = nil;\n    NSArray *modelPaths = // get paths to all the mom files in the bundle\n    for (NSString *modelPath in modelPaths) {\n        NSURL *modelURL = [NSURL fileURLWithPath:modelPath];\n        NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];\n        if ([model isEqual:self]) {\n            modelName = modelURL.lastPathComponent.stringByDeletingPathExtension;\n            break;\n        }\n    }\n    return modelName;\n}\n```\n\nSince the `User` was isolated from the rest of the object graph in the previous example (no source relationship mapping), we could take advantage of this approach and migrate `User` by itself. We’ll remove the `UserToUser` mapping from our first mapping model, and create another mapping model with only a `UserToUser` mapping. Remember to return the new `User` mapping model in the list of mapping models, since we’re setting the new relationship in the other mapping.\n\n## Unit Tests\n\nSetting up unit tests for this is surprisingly simple:\n\n1. Populate your old store with relevant data\\*.\n2. Copy the resulting persistent store file to your *test target*.\n3. Write tests that assert compliance with your latest data model.\n4. Run tests that migrate data to your new data model.\n\n*\\*This can easily be done by running the latest production version of your app in the simulator*\n\nSteps 1 and 2 are simple. Step 3 is left to the reader as an exercise, and I will guide you through step 4. \n\nWhen the persistent store file has been added to the unit testing target, we need to tell the migration manager to migrate from that store to our destination store. This is demonstrated in the example project like this:\n\n```objc\n- (void)setUpCoreDataStackMigratingFromStoreWithName:(NSString *)name\n{\n    NSURL *storeURL = [self temporaryRandomURL];\n    [self copyStoreWithName:name toURL:storeURL];\n\n    NSURL *momURL = [[NSBundle mainBundle] URLForResource:@\"Model\" withExtension:@\"momd\"];\n    self.managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];\n\n    NSString *storeType = NSSQLiteStoreType;\n\n    MHWMigrationManager *migrationManager = [MHWMigrationManager new];\n    [migrationManager progressivelyMigrateURL:storeURL\n                                       ofType:storeType\n                                      toModel:self.managedObjectModel\n                                        error:nil];\n\n    self.persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];\n    [self.persistentStoreCoordinator addPersistentStoreWithType:storeType\n                                                  configuration:nil\n                                                            URL:storeURL\n                                                        options:nil\n                                                          error:nil];\n\n    self.managedObjectContext = [[NSManagedObjectContext alloc] init];\n    self.managedObjectContext.persistentStoreCoordinator = self.persistentStoreCoordinator;\n}\n\n- (NSURL *)temporaryRandomURL\n{\n    NSString *uniqueName = [NSProcessInfo processInfo].globallyUniqueString;\n    return [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:uniqueName]];\n}\n\n- (void)copyStoreWithName:(NSString *)name toURL:(NSURL *)url\n{\n    // Create a unique url every test so migration always runs\n    NSBundle *bundle = [NSBundle bundleForClass:[self class]];\n    NSFileManager *fileManager = [NSFileManager new];\n    NSString *path = [bundle pathForResource:[name stringByDeletingPathExtension] ofType:name.pathExtension];\n    [fileManager copyItemAtPath:path\n                         toPath:url.path error:nil];\n}\n```\n\nPut this code in a superclass, and reuse it in test classes that test migration:\n\n```objc\n- (void)setUp\n{\n    [super setUp];\n    [self setUpCoreDataStackMigratingFromStoreWithName:@\"Model1.sqlite\"];\n}\n```\n\n## Conclusion\n\nWhen doing lightweight migrations, the migration occurs directly inside the SQLite store. This is very fast and efficient compared to custom migrations, where the source objects need to be fetched into memory, their values copied onto the destination objects, their relationships re-established, and finally inserted into the new store. Not only is this much slower, it can also impose problems when migrating larger datasets due to memory constraints.\n\n### Add More Data Than You Think You Might Need\n\nOne of the most important things when working with any type of data persisting is to think through your model carefully. You’ll want it to be sustainable. Put more things into your data model than you think you might need. Empty attributes or entities is better than having to migrate and create them later on. Migrations are prone to mistakes. Unused data is not.\n\n### Debugging Migrations\n\nA useful launch argument when testing migrations is `-com.apple.CoreData.MigrationDebug`. When set to 1, you will receive information in the console about exceptional cases as it migrates data. If you’re used to SQL but new to Core Data, set `-com.apple.CoreData.SQLDebug` to 1 to see actual SQL commands.\n\n"
  },
  {
    "path": "2013-09-09-core-data-models-and-model-objects.md",
    "content": "---\ntitle: Data Models and Model Objects\ncategory: \"4\"\ndate: \"2013-09-09 08:00:00\"\nauthor:\n  - name: Florian Kugler\n    url: http://twitter.com/floriankugler\ntags: article\n---\n\n\n\nIn this article we are going to have a closer look at Core Data models and managed object classes. It's not meant to be an introduction to the topic, but rather a collection of some lesser-known or often-forgotten aspects which can come in handy when working with Core Data. If you're looking for a more extensive and step-by-step overview, we recommend you read through [Apple's Core Data Programming Guide](https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/CoreData/cdProgrammingGuide.html#//apple_ref/doc/uid/TP30001200-SW1). \n\n\n\n## Data Model\n\nThe Core Data data model (stored in the `*.xcdatamodel` file) is where the data types (\"Entities\" in Core Data) are defined. Mostly we will define a data model using Xcode's graphical interface, but it's equally possible to create the whole thing in code. You would start by creating an [`NSManagedObjectModel`](https://developer.apple.com/library/ios/documentation/cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectModel_Class/Reference/Reference.html) object, then create entities represented by [`NSEntitiyDescription`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSEntityDescription_Class/NSEntityDescription.html) objects, which in turn contain relationships and attributes represented by [`NSAttributeDescription`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSAttributeDescription_Class/reference.html) and [`NSRelationshipDescription`](https://developer.apple.com/library/ios/documentation/cocoa/Reference/CoreDataFramework/Classes/NSRelationshipDescription_Class/NSRelationshipDescription.html) objects. You'll hardly ever need to do this, but it's good to know about these classes.\n\n\n### Attributes\n\nOnce we have created an entity, we now have to define some attributes on it. Attribute definition is very straightforward, but there are some properties which we are going to look at in detail.\n\n\n#### Default/Optional\n\nEach attribute can be defined as optional or non-optional. A non-optional attribute will cause saving to fail if it's not set on one of the modified objects. At the same time, we can define a default value for each attribute. Nobody is stopping us from declaring an attribute as optional and defining a default value on it. But when you think about it, this doesn't make much sense and can be confusing. So we recommend to always uncheck *optional* for attributes with default values.\n\n\n#### Transient\n\nAnother often-overlooked property of attributes is their *transient* option. Attributes declared as transient are never saved to the persistent store, but they otherwise behave just like normal attributes. This means they will participate in [validation][220], undo management, faulting, etc. Transient attributes can be very useful once you start moving more model logic into managed object subclasses. We'll talk more about this below and how it's better to [use transient attributes instead of ivars][230].\n\n<a name=\"indexes\"> </a>\n\n#### Indexes\n\nIf you've worked with relational databases before, you will be familiar with indexes. If you haven't, you can think of an [index](http://en.wikipedia.org/wiki/Database_index) on an attribute as a way to speed up lookups on it dramatically. It's a trade-off though; indexes speed up reads and slow down writes, because the index has to be updated when the data changes. \n\nSetting the `indexed` property on an attribute will translate into an index on the underlying table column in SQLite. We can create indexes on any attribute we like, but be aware of write performance implications. Core Data also offers the possibility to create compound indexes (look for the Indexes section in the entity inspector), i.e. indexes which span more than one attribute. This can help performance when you typically use a predicate with conditions on multiple attributes to fetch data. Daniel has an example for this in his article about [fetching data][600].\n\n\n#### Scalar Types\n\nCore Data has support for many common data types like integers, floats, booleans, and so on. However, by default, the data model editor generates these attributes as `NSNumber` properties in the managed object subclasses. This often results in endless `floatValue`, `boolValue`, `integerValue`, or similar calls on these `NSNumber` objects in the application code. \n\nBut we can also just specify those properties with their correct scalar type, e.g. as `int64_t`, `float_t`, or `BOOL`, and it will work with Core Data. Xcode even has a little checkbox in the save dialogue of the `NSManagedObject` generator (\"Use scalar properties for primitive data types\") which does this for you. Anyway, instead of:\n\n```objc\n@property (nonatomic, strong) NSNumber *myInteger;\n```\n\nthe property would be declared as:\n\n```objc\n@property (nonatomic) int64_t myInteger;\n```\n\nThat's all we have to do to retrieve and store scalar types in Core Data. The documentation still states that Core Data cannot automatically generate accessor methods for scalar values, but that seems to be outdated. \n\n\n#### Storing Other Objects\n\nCore Data doesn't limit us to only storing values of the predefined types. In fact we can easily store any object which conforms to [`NSCoding`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSCoding_Protocol/Reference/Reference.html) and pretty much anything else including structs with some more work.\n\nIn order to store `NSCoding` compliant objects we use [transformable attributes](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreData/Articles/cdNSAttributes.html#//apple_ref/doc/uid/TP40001919-SW7). All we have to do is select the \"Transformable\" type in the drop-down menu and we are ready to go. If you generate the managed object subclasses you will see a property declared like this:\n\n```objc\n@property (nonatomic, retain) id anObject;\n```\n\nWe can manually change the type from `id` to whatever we want to store in this attribute to get type checking from the compiler. However, there is one pitfall when using transformable attributes: we must not specify the name of the transformer if we want to use the default transformer (which we mostly want). Even specifying the default transformer's name, `NSKeyedUnarchiveFromDataTransformerName`, will result in [bad things](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreData/Articles/cdNSAttributes.html#//apple_ref/doc/uid/TP40001919-SW7).\n\nBut it doesn't stop there. We can also create our custom value transformers and use them to store arbitrary object types. We can store anything as long as we can transform it into one of the supported basic types. In order to store not supported non-object types like structs, the basic approach is to create a transient attribute of undefined type and a persistent \"shadow attribute\" of one of the supported types. Then the accessor methods of the transient attribute are overridden to transform the value from and to the persisted type. This is not trivial, because these accessor methods have to be KVC and KVO compliant and make correct use of Core Data's primitive accessor methods. Please read the [custom code](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreData/Articles/cdNSAttributes.html#//apple_ref/doc/uid/TP40001919-SW8) section in Apple's guide to [non-standard persistent attributes](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreData/Articles/cdNSAttributes.html#//apple_ref/doc/uid/TP40001919-SW1).\n\n\n### Fetched Properties\n\nFetched properties are mostly used to create relationships across multiple persistent stores. Since having multiple persistent stores already is a very uncommon and advanced use case, fetched properties are rarely used either. \n\nBehind the scenes, Core Data executes a fetch request and caches the results when we access a fetched property. This fetch request can be configured directly in Xcode's data model editor by specifying a target entity type and a predicate. This predicate is not static though, but can be configured at runtime via the `$FETCH_SOURCE` and `$FETCHED_PROPERTY` variables. Please refer to Apple's [documentation](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreData/Articles/cdRelationships.html#//apple_ref/doc/uid/TP40001857-SW7) for more details.\n\n\n### Relationships\n\nRelationships between entities should always be defined in both directions. This gives Core Data enough information to fully manage the object graph for us. However, defining relationships bidirectionally is not a strict requirement, although it is strongly recommended. \n\nIf you know what you're doing, you can define unidirectional relationships and Core Data will not complain. However, you just have taken on a lot of responsibilities normally managed by Core Data, including ensuring the consistency of the object graph, change tracking, and undo management. To take a simple example, if we have `Book` and `Author` entities and define a to-one relationship from book to author, deleting an author will not propagate the deletion to the affected books. We still can access the book's author relationship, but we'll get a fault pointing to nowhere.\n\nIt should become clear that unidirectional relationships are basically never what you want. Always define relationships in both ways to stay out of trouble. \n\n\n### Data Model Design\n\nWhen designing a data model for Core Data, we have to remember that Core Data is not a relational database. Therefore, we shouldn't design the model like we would design a database schema, but think much more from the perspective of how the data should be used and presented. \n\nOften it makes sense to [denormalize](http://en.wikipedia.org/wiki/Denormalization) the data model, in order to avoid extensive fetching of related objects when displaying this data. For example, if we have an `Authors` entity with a to-many relationship to a  `Books` entity, it might make sense to store the number of books associated with one author in the Author entity, if that's something we want to show later on. \n\nLet's say we want to show a table view listing all authors and the number of books for each author. If the number of books per author can only be retrieved by counting the number of associated book objects, one fetch request per author cell is necessary to get this data. This is not going to perform very well. We could pre-fetch the book objects using [`relationshipKeyPathsForPrefetching`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSFetchRequest_Class/NSFetchRequest.html#//apple_ref/occ/instm/NSFetchRequest/relationshipKeyPathsForPrefetching), but this might not be ideal either if we have large amounts of books stored. However, if we manage a book count attribute for each author, the author's fetch request gets all the data we need.\n\nNaturally denormalization comes with the cost of being responsible to ensure that the duplicated data stays in sync. We have to weigh the benefits and drawbacks on a case-by-case basis, because sometimes it is trivial to do, and other times it can create major headaches. This very much depends on the specifics of the data model, if the app has to interact with a backend, or if the data even has to be synched between multiple clients either via a central authority or peer-to-peer. \n\nOften the data model will already be defined by some kind of backend service and we might just duplicate this model for client applications. However, even in this case we have the freedom to make some modifications to the model on the client side, as long as we can define an unambiguous mapping to the data model of the backend. For the simple example of books and authors, it would be trivial to add a book count property to the `Author` Core Data entity, which lives only on the client side to help with performance, but never gets sent to the server. If we make a change locally or receive new data from the server, we update this attribute and keep it in sync with the rest of the data.\n\nIt's not always as simple as that, but often small changes like this can alleviate serious performance bottlenecks that arise from dealing with a too normalized, relational, database-ish data model.\n\n\n<a name=\"entity-vs-class-hierarchy\"> </a>\n\n### Entity Hierarchy vs. Class Hierarchy\n\nManaged object models offer the possibility to create entity hierarchies, i.e. we can specify an entity as the parent of another entity. This might sound good, for example, if our entities share some common attributes. In practice, however, it's rarely what you would want to do.\n\nWhat happens behind the scenes is that Core Data stores all entities with the same parent entity in the same table. This quickly creates tables with a large number of attributes, slowing down performance. Often the purpose of creating an entity hierarchy is solely to create a class hierarchy, so that we can put code shared between multiple entities into the base class. There is a much better way to achieve this though. \n\nThe entity hierarchy is *independent* of the `NSManagedObject` subclass hierarchy. Or put differently, we don't need to have a hierarchy within our entities to create a class hierarchy.\n\nLet's have a look at the `Author` and `Book` example again. Both of these entities have common fields, like an identifier, a `createdAt` date field, and a `changedAt` date field. For this case we could create the following structure:\n\n```\nEntity hierarchy            Class hierarchy\n----------------            ---------------\n\n   BaseEntity                 BaseEntity\n    |      |                   |      |\n Authors  Books             Authors  Books\n```\n\nHowever, we can equally maintain this class hierarchy while flattening the entity hierarchy:\n \n```\n Entity hierarchy            Class hierarchy\n ----------------            ---------------\n```\n\n \n```objc\n  Authors  Books               BaseEntity\n                                |      |\n                             Authors  Books\n```\n\n \nThe classes would be declared like this:\n \n```objc\n @interface BaseEntity : NSManagedObject\n @property (nonatomic) int64_t identifier;\n @property (nonatomic, strong) NSDate *createdAt;\n @property (nonatomic, strong) NSDate *changedAt;\n @end\n\n @interface Author : BaseEntity\n // Author specific code...\n @end\n\n @interface Book : BaseEntity\n // Book specific code...\n @end\n```\n\nThis gives us the benefit of being able to move common code into the base class without the performance overhead of storing all entities in a single table. We cannot create class hierarchies deviating from the entity hierarchy with Xcode's managed object generator though. But that's a small price to pay, since there are more benefits to not auto-generating managed object classes, as we will discuss [below][210].\n\n\n### Configurations and Fetch Request Templates\n\nEverybody who has used Core Data has worked with the entity-modeling aspect of data models. But data models also have two lesser-known or lesser-used aspects: configurations and fetch request templates.\n\nConfigurations are used to define which entity should be stored in which persistent store. Persistent stores are added to the persistent store coordinator using [`addPersistentStoreWithType:configuration:URL:options:error:`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSPersistentStoreCoordinator_Class/NSPersistentStoreCoordinator.html#//apple_ref/occ/instm/NSPersistentStoreCoordinator/addPersistentStoreWithType:configuration:URL:options:error:), where the configuration argument defines this mapping. Now, in almost all cases, we will only use one persistent store, and therefore never deal with multiple configurations. The one default configuration we need for a single store setup is created for us. There are some rare use cases for multiple stores though, and one of those is outlined in the article, [Importing Large Data Sets][120], in this issue.\n\nFetch request templates are just what the name suggests: predefined fetch requests stored with the managed object model, which can be used later on using [`fetchRequestFromTemplateWithName:substitutionVariables`](https://developer.apple.com/library/ios/documentation/cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectModel_Class/Reference/Reference.html#//apple_ref/occ/instm/NSManagedObjectModel/fetchRequestFromTemplateWithName:substitutionVariables:). We can define those templates in Xcode's data model editor or in code. Xcode's editor doesn't support all the features of `NSFetchRequest` though.\n\nTo be honest, I have a hard time coming up with convincing use cases of fetch request templates. One advantage is that the predicate of the fetch request will be pre-parsed, so this step doesn't have to happen each time you're performing a new fetch request. This will hardly ever be relevant though, and we are in trouble anyway if we have to fetch that frequently. But if you're looking for a place to define your fetch requests (and you should not define them in view controllers...), check if storing them with the managed object model might be a viable alternative.\n\n\n<a name=\"managed-objects\"> </a>\n\n## Managed Objects\n\nManaged objects are at the heart of any Core Data application. Managed objects live in a managed object context and represent our data. Managed objects are supposed to be passed around in the application, crossing at least the model-controller barrier, and potentially even the controller-view barrier. The latter is somewhat more controversial though, and can be [abstracted in a better way](/issues/1-view-controllers/table-views/) by e.g. defining a protocol to which an object must conform in order to be consumed by a certain view, or by implementing configuration methods in a view category that bridge the gap from the model object to the specifics of the view.\n\nAnyway, we shouldn't limit managed objects to the model layer and pull out their data into different structures as soon as we want to pass them around. Managed objects are first-class citizens in a Core Data app and we should use them accordingly. For example, managed objects should be passed between view controllers to provide them with the data they need. \n\nIn order to access the managed object context we often see code like this in view controllers:\n\n```objc\nNSManagedObjectContext *context = \n  [(MyApplicationDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];\n```\n\nIf you already pass a model object to the view controller, it's much better to access the context directly via this object:\n\n```objc\nNSManagedObjectContext *context = self.myObject.managedObjectContext;\n```\n\nThis removes the hidden dependency on the application delegate, and makes it much more readable and also easier to test. \n\n\n### Working with Managed Object Subclasses\n\nSimilarly, managed object subclasses are meant to be used. We can and should implement custom model logic, validation logic, and helper methods in these classes and create class hierarchies in order to pull out common code into super classes. The latter is easy to do because of the decoupling of the class and the entity hierarchy as [mentioned above][240]. \n\nYou may wonder how to implement custom code in managed object subclasses if Xcode keeps overwriting these files when regenerating them. Well, the answer is pretty simple: don't generate them with Xcode. If you think about it, the generated code in these classes is trivial and very easy to write yourself, or generate once and then keep up to date manually. It's really just a bunch of property declarations. \n\nThere are other solutions like putting the custom code in a category, or using tools like [mogenerator](https://github.com/rentzsch/mogenerator), which creates a base class for each entity and a subclass of it where the user-written code is supposed to go. But none of these solutions allow for a flexible class hierarchy independent of the entity hierarchy. So at the cost of writing a few lines of trivial code, our advice is to just write those classes manually.\n\n\n<a name=\"ivars-in-managed-object-classes\"> </a>\n\n#### Instance Variables in Managed Object Subclasses\n\nOnce we start using our managed object subclasses to implement model logic, we might want to create some instance variables to cache computed values or something similar. It's much more convenient though to use transient properties for this purpose. The reason is that the lifecycle of managed objects is somewhat different from normal objects. Core Data often [faults](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreData/Articles/cdFaultingUniquing.html#//apple_ref/doc/uid/TP30001202-CJBDBHCB) objects that are no longer needed. If we would use instance variables, we would have to manually participate in this process and release our ivars. If we use transient properties instead, all this is done for us.\n\n<a name=\"creating-objects\"> </a>\n\n#### Creating New Objects\n\nOne good example of implementing useful helper methods on our model classes is a class method to insert a new object into a managed object context. Core Data's API for creating new objects is not very intuitive: \n\n```objc\nBook *newBook = [NSEntityDescription insertNewObjectForEntityForName:@\"Book\"\n                                              inManagedObjectContext:context];\n```\n\nLuckily, we can easily solve this task in a much more elegant manner in our own subclass:\n\n```objc\n@implementation Book\n// ...\n\n+ (NSString *)entityName\n{\n    return @\"Book\"\n}\n\n+ (instancetype)insertNewObjectIntoContext:(NSManagedObjectContext *)context\n{\n    return [NSEntityDescription insertNewObjectForEntityForName:[self entityName]\n                                         inManagedObjectContext:context];\n}\n@end\n```\n\nNow, creating a new book object is much easier:\n\n```objc\nBook *book = [Book insertNewObjectIntoContext:context];\n```\n\nOf course, if we subclass our actual model classes from a common base class, we should move the `insertNewObjectIntoContext:` and `entityName` class methods into the common super class. Then each subclass only needs to overwrite `entityName`.\n\n\n#### To-Many Relationship Mutators\n\nIf you generate a managed object subclass with Xcode which has a to-many relationship, it will create methods like this to add and remove objects to and from this relationship:\n\n```objc\n- (void)addBooksObject:(Book *)value;\n- (void)removeBooksObject:(Book *)value;\n- (void)addBooks:(NSSet *)values;\n- (void)removeBooks:(NSSet *)values;\n```\n\nInstead of using those four mutator methods, there is a much more elegant way of doing this, especially if we don't generate the managed object subclasses. We can simply use the [`mutableSetValueForKey:`](https://developer.apple.com/library/mac/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html#//apple_ref/occ/instm/NSObject/mutableSetValueForKey:) method to retrieve a mutable set of related objects (or [`mutableOrderedSetValueForKey:`](https://developer.apple.com/library/mac/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html#//apple_ref/occ/instm/NSObject/mutableOrderedSetValueForKey:) for ordered relationships). This can be encapsulated into a simple accessor method:\n\n```objc\n- (NSMutableSet *)mutableBooks\n{\n    return [self mutableSetValueForKey:@\"books\"];\n}\n```\n\nThen we can use this mutable set like any other set. Core Data will pick up the changes and do the rest for us:\n\n```objc\nBook *newBook = [Book insertNewObjectIntoContext:context];\n[author.mutableBooks addObject:newBook];\n```\n\n&nbsp;\n\n\n<a name=\"validation\"> </a>\n\n#### Validation\n\nCore Data supports various ways of data validation. Xcode's data model editor lets us specify some basic requirements on our attribute, like a string's minimum and maximum length or a minimum and maximum number of objects in a to-many relationship. But beyond that, there's much we can do in code.\n\nThe section [\"Managed Object Validation\"](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreData/Articles/cdValidation.html#//apple_ref/doc/uid/TP40004807-SW1) is the go-to place for in-depth information on this topic. Core Data supports property-level validation by implementing `validate<Key>:error:` methods, as well as inter-property validation via [`validateForInsert:`](https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html#//apple_ref/occ/instm/NSManagedObject/validateForInsert:), [`validateForUpdate:`](https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html#//apple_ref/occ/instm/NSManagedObject/validateForUpdate:), and [`validateForDelete:`](https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html#//apple_ref/occ/instm/NSManagedObject/validateForDelete:). Validation happens automatically before saving, but we can also trigger it manually on the property level with [`validateValue:forKey:error:`](https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html#//apple_ref/occ/instm/NSManagedObject/validateValue:forKey:error:).\n\n\n## Conclusion\n\nData models and model objects are the bread and butter of any Core Data application. We would like to encourage you not to jump to convenience wrappers right away, but to embrace working with managed object subclasses and objects. With something like Core Data, it is very important to understand what's happening, otherwise it can backfire very easily once your application grows and becomes more complex. \n\nWe hope to have demonstrated some simple techniques to make working with managed objects easier without introducing any magic. Additionally, we peeked into some very advanced stuff we can do with data models in order to give you an idea of what's possible. Use these techniques sparingly though, as at the end of the day, simplicity mostly wins.\n\n\n\n[100]:/issues/4-core-data/importing-large-data-sets-into-core-data/\n[110]:/issues/4-core-data/importing-large-data-sets-into-core-data/#efficient-importing\n[120]:/issues/4-core-data/importing-large-data-sets-into-core-data/#user-generated-data\n\n[200]:/issues/4-core-data/core-data-models-and-model-objects/\n[210]:/issues/4-core-data/core-data-models-and-model-objects/#managed-objects\n[220]:/issues/4-core-data/core-data-models-and-model-objects/#validation\n[230]:/issues/4-core-data/core-data-models-and-model-objects/#ivars-in-managed-object-classes\n[240]:/issues/4-core-data/core-data-models-and-model-objects/#entity-vs-class-hierarchy\n[250]:/issues/4-core-data/core-data-models-and-model-objects/#creating-objects\n[260]:/issues/4-core-data/core-data-models-and-model-objects/#indexes\n\n[300]:/issues/4-core-data/core-data-overview/\n[310]:/issues/4-core-data/core-data-overview/#complicated-stacks\n[320]:/issues/4-core-data/core-data-overview/#getting-to-objects\n\n[400]:/issues/4-core-data/full-core-data-application/\n\n[500]:/issues/4-core-data/SQLite-instead-of-core-data/\n\n[600]:/issues/4-core-data/core-data-fetch-requests/\n\n[700]:/issues/4-core-data/core-data-migration/\n"
  },
  {
    "path": "2013-09-09-core-data-overview.md",
    "content": "---\ntitle: Core Data Overview\ncategory: \"4\"\ndate: \"2013-09-09 11:00:00\"\nauthor:\n  - name: Daniel Eggert\n    url: http://twitter.com/danielboedewadt\ntags: article\n---\n\n\n\nCore Data is probably one of the most misunderstood Frameworks on OS X and iOS. To help with that, we'll quickly go through Core Data to give you an overview of what it is all about, as understanding Core Data concepts is essential to using Core Data the right way. Just about all frustrations with Core Data originate in misunderstanding what it does and how it works. Let's dive in...\n\n## What is Core Data?\n\nMore than eight years ago, in April 2005, Apple released OS X version 10.4, which was the first to sport the Core Data framework. That was back when YouTube launched.\n\nCore Data is a model layer technology. Core Data helps you build the model layer that represents the state of your app. Core Data is also a persistent technology, in that it can persist the state of the model objects to disk. But the important takeaway is that Core Data is much more than just a framework to load and save data. It's also about working with the data while it's in memory.\n\nIf you've worked with [Object-relational mapping (O/RM)](https://en.wikipedia.org/wiki/Object-relational_mapping) before: Core Data is **not** an O/RM. It's much more. If you've been working with [SQL](https://en.wikipedia.org/wiki/Sql) wrappers before: Core Data is **not** an SQL wrapper. It does by default use SQL, but again, it's a way higher level of abstraction. If you want an O/RM or SQL wrapper, Core Data is not for you.\n\nOne of the very powerful things that Core Data provides is its object graph management. This is one of the pieces of Core Data you need to understand and learn in order to bring the powers of Core Data into play.\n\nOn a side note: Core Data is entirely independent from any UI-level frameworks. It's, by design, purely a model layer framework. And on OS X it may make a lot of sense to use it even in background daemons and the like.\n\n## The Stack\n\nThere are quite a few components to Core Data. It's a very flexible technology. For most uses cases, the setup will be relatively simple. \n\nWhen all components are tied together, we refer to them as the *Core Data Stack*. There are two main parts to this stack. One part is about object graph management, and this should be the part that you know well, and know how to work with. The second part is about persistence, i.e. saving the state of your model objects and retrieving the state again.\n\nIn between the two parts, in the middle of the stack, sits the Persistent Store Coordinator (PSC), also known to friends as the *central scrutinizer*. It ties together the object graph management part with the persistence part. When one of the two needs to talk to the other, this is coordinated by the PSC.\n\n![Complex core data stack](/images/issue-4/stack-complex.png)\n\nThe *object graph management* is where your application's model layer logic will live. Model layer objects live inside a context. In most setups, there's one context and all objects live in that context. Core Data supports multiple contexts, though, for more advanced use cases. Note that contexts are distinct from one another, as we'll see in a bit. The important thing to remember is that objects are tied to their context. Each *managed* object knows which context it's in, and each context knows which objects it is managing.\n\nThe other part of the stack is where persistency happens, i.e. where Core Data reads and writes from / to the file system. In just about all cases, the persistent store coordinator (PSC) has one so-called *persistent store* attached to it, and this store interacts with a [SQLite](https://www.sqlite.org) database in the file system. For more advanced setups, Core Data supports using multiple stores that are attached to the same persistent store coordinator, and there are a few store types than just SQL to choose from.\n\nThe most common scenario, however, looks like this:\n\n![Simple core data stack](/images/issue-4/stack-simple.png)\n\n## How the Components Play Together\n\nLet's quickly walk through an example to illustrate how these components play together. In our article about a [full application using Core Data][400], we have exactly one *entity*, i.e. one kind of object: We have an *Item* entity that holds on to a title. Each item can have sub-items, hence we have a *parent* and a *child* relationship. \n\nThis is our data model. As we mention in the article about [Data Models and Model Objects][200], a particular *kind* of object is called an *Entity* in Core Data. In this case we have just one entity: an *Item* entity. And likewise, we have a subclass of `NSManagedObject` which is called `Item`. The *Item* entity maps to the `Item` class. The [data models article][200] goes into more detail about this.\n\nOur app has a single *root* item. There's nothing magical to it. It's simply an item we use to show the bottom of the item hierarchy. It's an item that we'll never set a parent on.\n\nWhen the app launches, we set up our stack as depicted above, with one store, one managed object context, and a persistent store coordinator to tie the two together.\n\nOn first launch, we don't have any items. The first thing we need to do is to create the *root* item. You add managed objects by inserting them into the context.\n\n### Creating Objects\n\nIt may seem cumbersome. The way to insert objects is with the\n\n```objc\n+ (id)insertNewObjectForEntityForName:(NSString *)entityName \n               inManagedObjectContext:(NSManagedObjectContext *)context\n```\n\nmethod on `NSEntityDescription`. We suggest that you add two convenience methods to your model class:\n\n```objc\n+ (NSString *)entityName\n{\n   return @“Item”;\n}\n\n+ (instancetype)insertNewObjectInManagedObjectContext:(NSManagedObjectContext *)moc;\n{\n   return [NSEntityDescription insertNewObjectForEntityForName:[self entityName] \n                                        inManagedObjectContext:moc];\n}\n```\n\nNow, we can insert our root object like so:\n\n```objc\nItem *rootItem = [Item insertNewObjectInManagedObjectContext:managedObjectContext];\n```\n\nNow there's a single item in our managed object context (MOC). The context knows about this newly inserted *managed object* and the managed object `rootItem` knows about the context (it has a `-managedObjectContext` method).\n\n### Saving Changes\n\nAt this point, though, we have not touched the persistent store coordinator or the persistent store, yet. The new model object, `rootItem`, is just in memory. If we want to save the state of our model objects (in this case just that one object), we need to *save* the context:\n\n```objc\nNSError *error = nil;\nif (! [managedObjectContext save:&error]) {\n    // Uh, oh. An error happened. :(\n}\n```\n\nAt this point, a lot is going to happen. First, the managed object context figures out what has changed. It is the context's responsibility to track any and all changes you make to any managed objects inside that context. In our case, the only change we've made thus far is inserting one object, our `rootItem`.\n\nThe managed object context then passes these changes on to the persistent store coordinator and asks it to propagate the changes through to the store. The persistent store coordinator coordinates with the store (in our case, an SQL store) to write our inserted object into the SQL database on disk. The `NSPersistentStore` class manages the actual interaction with SQLite and generates the SQL code that needs to be executed. The persistent store coordinator's role is to simply coordinate the interaction between the store and the context. In our case, that role is relatively simple, but complex setups can have multiple stores and multiple contexts.\n\n### Updating Relationships\n\nThe power of Core Data is managing relationships. Let's look at the simple case of adding our second item and making it a child item of the `rootItem`:\n\n```objc\nItem *item = [Item insertNewObjectInManagedObjectContext:managedObjectContext];\nitem.parent = rootItem;\nitem.title = @\"foo\";\n```\n\nThat's it. Again, these changes are only inside the managed object context. Once we save the context, however, the managed object context will tell the persistent store coordinator to add that newly created object to the database file just like for our first object. But it will also update the relationship from our second item to the first and the other way around, from the first object to the second. Remember how the *Item* entity has a *parent* and a *children* relationship. These are reverse relationships of one another. Because we set the first item to be the parent of the second, the second will be a child of the first. The managed object context tracks these relationships and the persistent store coordinator and the store persist (i.e. save) these relationships to disk.\n\n<a name=\"getting-to-objects\"> </a>\n\n### Getting to Objects\n\nLet's say we've been using our app for a while and have added a few sub-items to the root item, and even sub-items to the sub-items. Then we launch our app again. Core Data has saved the relationships of the items in the database file. The object graph is persisted. We now need to get to our *root* item, so we can show the bottom-level list of items. There are two ways for us to do that. We'll look at the simpler one first.\n\nWhen we created our `rootItem` object, and once we've saved it, we can ask it for its `NSManagedObjectID`. This is an opaque object that uniquely represents that object. We can store this into e.g. `NSUSerDefaults`, like this:\n\n```objc\nNSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];\n[defaults setURL:rootItem.managedObjectID.URIRepresentation forKey:@\"rootItem\"];\n```\n\nNow when the app is relaunched, we can get back to the object like so:\n\n```objc\nNSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];\nNSURL *uri = [defaults URLForKey:@\"rootItem\"];\nNSManagedObjectID *moid = [managedObjectContext.persistentStoreCoordinator managedObjectIDForURIRepresentation:uri];\nNSError *error = nil;\nItem *rootItem = (id) [managedObjectContext existingObjectWithID:moid error:&error];\n```\n\nObviously, in a real app, we'd have to check if `NSUserDefaults` actually returns a valid value.\n\nWhat just happened is that the managed object context asked the persistent store coordinator to get that particular object from the database. The root object is now back inside the context. However, all the other items are not in memory, yet.\n\nThe `rootItem` has a relationship called `children`. But there's nothing there, yet. We want to display the sub-items of our `rootItem`, and hence we'll call:\n\n```objc\nNSOrderedSet *children = rootItem.children;\n```\n\nWhat happens now, is that the context notes that the relationship `children` from that `rootItem` is a so-called *fault*. Core Data has marked the relationship as something it still has to resolve. And since we're accessing it at this point, the context will now automatically coordinate with the persistent store coordinator to bring those child items into the context.\n\nThis may sound very trivial, but there's actually a lot going on at this point. If any of the child objects happen to already be in memory, Core Data guarantees that it will reuse those objects. That's what is called *uniquing*. Inside the context, there's never going to be more than a single object representing a given item.\n\nSecondly, the persistent store coordinator has its own internal cache of object values. If the context needs a particular object (e.g. a child item), and the persistent store coordinator already has the needed values in its cache, the object (i.e. the item) can be added to the context without talking to the store. That's important, because accessing the store means running SQL code, which is much slower than using values already in memory.\n\nAs we continue to traverse from item to sub-item to sub-item, we're slowly bringing the entire object graph into the managed object context. Once it's all in memory, operating on objects and traversing the relationships is super fast, since we're just working inside the managed object context. We don't need to talk to the persistent store coordinator at all. Accessing the `title`, `parent`, and `children` properties on our `Item` objects is super fast and efficient at this point.\n\nIt's important to understand how data is fetched in these cases, since it affects performance. In our particular case, it doesn't matter too much, since we're not touching a lot of data. But as soon as you do, you'll need to understand what goes on under the hood.\n\nWhen you traverse a relationship (such as `parent` or `children` in our case) one of three things can happen: (1) the object is already in the context and traversing is basically for free. (2) The object is not in the context, but the persistent store coordinator has its values cached, because you've recently retrieved the object from the store. This is reasonably cheap (some locking has to occur, though). The expensive case is (3) when the object is accessed for the first time by both the context and the persistent store coordinator, such that is has to be retrieved by store from the SQLite database. This last case is much more expensive than (1) and (2).\n\nIf you know you have to fetch objects from the store (because you don't have them), it makes a huge difference when you can limit the number of fetches by getting multiple objects at once. In our example, we might want to fetch all child items in one go instead of one-by-one. This can be done by crafting a special `NSFetchRequest`. But we must take care to only to run a fetch request when we need to, because a fetch request will also cause option (3) to happen; it will always access the SQLite database. Hence, when performance matters, it makes sense to check if objects are already around. You can use [`-[NSManagedObjectContext objectRegisteredForID:]`](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/NSManagedObjectContext.html#//apple_ref/occ/instm/NSManagedObjectContext/objectRegisteredForID:) for that.\n\n\n### Changing Object Values\n\nNow, let's say we are changing the `title` of one of our `Item` objects:\n\n```objc\nitem.title = @\"New title\";\n```\n\nWhen we do this, the items title changes. But additionally, the managed object context marks the specific managed object (`item`) as changed, such that it will be saved through the persistent store coordinator and attached store when we call `-save:` on the context. One of the key responsibilities of the context is *change tracking*.\n\nThe context knows which objects have been inserted, changed, and deleted since the last save. You can get to those with the `-insertedObjects`, `-updatedObjects`, and `-deletedObjects` methods. Likewise, you can ask a managed object which of its values have changed by using the `-changedValues` method. You will probably never have to. But this is what Core Data uses to be able to push changes you make to the backing database.\n\nWhen we inserted new `Item` objects above, this is how Core Data knew it had to push those to the store. And now, when we changed the `title`, the same thing happened.\n\nSaving values needs to coordinate with both the persistent store coordinator and the persistent store, which, in turn, accesses the SQLite database. As when retrieving objects and values, accessing the store and database is relatively expensive when compared to simply operating on objects in memory. There's a fixed cost for a save, regardless of how many changes you're saving. And there's a per-change cost. This is simply how SQLite works. When you're changing a lot of things, you should therefore try to batch changes into reasonably sized batches. If you save for each change, you'd pay a high price, because you have to save very often. If you save too rarely, you'd have a huge batch of changes that SQLite would have to process.\n\nIt is also important to note that saves are atomic. They're transactional. Either all changes will be committed to the store / SQLite database or none of the changes will be saved. This is important to keep in mind when implementing custom [`NSIncrementalStore`](https://developer.apple.com/library/ios/DOCUMENTATION/CoreData/Reference/NSIncrementalStore_Class/Reference/NSIncrementalStore.html) subclasses. You have to either guarantee that a save will never fail (e.g. due to conflicts), or your store subclass has to revert all changes when the save fails. Otherwise, the object graph in memory will end up being inconsistent with the one in the store.\n\nSaves will normally never fail if you use a simple setup. But Core Data allows multiple contexts per persistent store coordinator, so you can run into conflicts at the persistent store coordinator level. Changes are per-context, and another context may have introduced conflicting changes. And Core Data even allows for completely separate stacks both accessing the same SQLite database file on disk. That can obviously also lead to conflicts (i.e. one context trying to update a value on an object that was deleted by another context). Another reason why a save can fail is validation. Core Data supports complex validation policies for objects. It's an advanced topic. A simple validation rule could be that the `title` of an `Item` must not be longer than 300 characters. But Core Data also supports complex validation policies across properties.\n\n## Final Words\n\nIf Core Data seems daunting, that's most likely because its flexibility allows you to use it in very complex ways. As always: try to keep things as simple as possible. It will make development easier and save you and your user from trouble. Only use the more complex things such as background contexts if you're certain they will actually help.\n\nWhen you're using a simple Core Data stack, and you use managed objects the way we've tried to outline in this issue, you'll quickly learn to appreciate what Core Data can do for you, and how it speeds up your development cycle.\n\n\n\n[100]:/issues/4-core-data/importing-large-data-sets-into-core-data/\n[110]:/issues/4-core-data/importing-large-data-sets-into-core-data/#efficient-importing\n[120]:/issues/4-core-data/importing-large-data-sets-into-core-data/#user-generated-data\n\n[200]:/issues/4-core-data/core-data-models-and-model-objects/\n[210]:/issues/4-core-data/core-data-models-and-model-objects/#managed-objects\n[220]:/issues/4-core-data/core-data-models-and-model-objects/#validation\n[230]:/issues/4-core-data/core-data-models-and-model-objects/#ivars-in-managed-object-classes\n[240]:/issues/4-core-data/core-data-models-and-model-objects/#entity-vs-class-hierarchy\n[250]:/issues/4-core-data/core-data-models-and-model-objects/#creating-objects\n[260]:/issues/4-core-data/core-data-models-and-model-objects/#indexes\n\n[300]:/issues/4-core-data/core-data-overview/\n[310]:/issues/4-core-data/core-data-overview/#complicated-stacks\n[320]:/issues/4-core-data/core-data-overview/#getting-to-objects\n\n[400]:/issues/4-core-data/full-core-data-application/\n\n[500]:/issues/4-core-data/SQLite-instead-of-core-data/\n\n[600]:/issues/4-core-data/core-data-fetch-requests/\n\n[700]:/issues/4-core-data/core-data-migration/\n"
  },
  {
    "path": "2013-09-09-editorial.md",
    "content": "---\r\ntitle:  \"Editorial\"\r\ncategory: \"4\"\r\ndate: \"2013-09-09 12:00:00\"\r\ntags: editorial\r\n---\r\n\r\n[100]:/issues/4-core-data/importing-large-data-sets-into-core-data/\r\n[200]:/issues/4-core-data/core-data-models-and-model-objects/\r\n[300]:/issues/4-core-data/core-data-overview/\r\n[400]:/issues/4-core-data/full-core-data-application/\r\n[500]:/issues/4-core-data/SQLite-instead-of-core-data/\r\n[600]:/issues/4-core-data/core-data-fetch-requests/\r\n[700]:/issues/4-core-data/core-data-migration/\r\n\r\n\r\nWelcome to objc.io issue #4!\r\n\r\nIn this issue we are taking an in-depth look into Apple's model layer framework, Core Data.\r\n\r\nCore Data is a powerful object graph management and persistence framework, which has been available on both iOS and OS X for many years. Nevertheless, even experienced developers often don't use it. Additionally, there is a lot of misleading information about Core Data on the Internet, which can confuse newcomers.\r\n\r\nThe articles in this issue provide a deeper overview of what Core Data is, how it works, and how you should use it. If you haven't worked with Core Data before, you will find the [overview][300] and the example of building a [full Core Data application][400] useful. If you're familiar with Core Data, we have several articles which cover the topics of [managed objects][200], [fetching data][600], [importing large data sets][100], and [migrations][700] (the latter is thanks to our guest writer Martin Hwasser).\r\n\r\nLast but not least, special guest writer [Brent Simmons](http://inessential.com) gives you an overview of [how he uses SQLite directly][500] in his applications, including the recently released [Vesper](http://vesperapp.co), but why you probably shouldn't. It's an interesting peek behind the scenes that shows what it takes to manually recreate what Core Data does for us.  \r\n\r\nWe wish you a successful iOS 7 launch month!\r\n\r\nAll the best from Berlin,\r\n\r\nChris, Daniel, and Florian.\r\n\r\n\r\n[100]:/issues/4-core-data/importing-large-data-sets-into-core-data/\r\n[110]:/issues/4-core-data/importing-large-data-sets-into-core-data/#efficient-importing\r\n[120]:/issues/4-core-data/importing-large-data-sets-into-core-data/#user-generated-data\r\n\r\n[200]:/issues/4-core-data/core-data-models-and-model-objects/\r\n[210]:/issues/4-core-data/core-data-models-and-model-objects/#managed-objects\r\n[220]:/issues/4-core-data/core-data-models-and-model-objects/#validation\r\n[230]:/issues/4-core-data/core-data-models-and-model-objects/#ivars-in-managed-object-classes\r\n[240]:/issues/4-core-data/core-data-models-and-model-objects/#entity-vs-class-hierarchy\r\n[250]:/issues/4-core-data/core-data-models-and-model-objects/#creating-objects\r\n[260]:/issues/4-core-data/core-data-models-and-model-objects/#indexes\r\n\r\n[300]:/issues/4-core-data/core-data-overview/\r\n[310]:/issues/4-core-data/core-data-overview/#complicated-stacks\r\n[320]:/issues/4-core-data/core-data-overview/#getting-to-objects\r\n\r\n[400]:/issues/4-core-data/full-core-data-application/\r\n\r\n[500]:/issues/4-core-data/SQLite-instead-of-core-data/\r\n\r\n[600]:/issue-4/core-data-fetch-requests.html\r\n\r\n[700]:/issue-4/core-data-migration.html"
  },
  {
    "path": "2013-09-09-full-core-data-application.md",
    "content": "---\ntitle:  A Complete Core Data Application\ncategory: \"4\"\ndate: \"2013-09-09 10:00:00\"\nauthor:\n  - name: Chris Eidhof\n    url: http://twitter.com/chriseidhof\ntags: article\n---\n\n\nIn this article, we will build a small but complete Core Data backed application. \nThe application allows you to create nested lists; each list item can have a sub-list, allowing you to create very deep hierarchies of items.\nInstead of using the Xcode template for Core Data, we will build our stack by hand, in order to fully understand what's going on.\nThe example code for this application is on [GitHub](https://github.com/objcio/issue-4-full-core-data-application).\n\n### How Will We Build It?\n\nFirst, we will create a `PersistentStack` object that, given a Core Data Model and a filename, returns a managed object context. Then we will build our Core Data Model. Next, we will create a simple table view controller that shows the root list of items using a fetched results controller, and add interaction step-by-step, by adding items, navigating to sub-items, deleting items, and adding undo support.\n\n## Set Up the Stack\n\nWe will create a managed object context for the main queue. In older\ncode, you might see `[[NSManagedObjectContext alloc] init]`. These days,\nyou should use the `initWithConcurrencyType:` initializer to make it\nexplicit that you're using the queue-based concurrency model:\n\n```objc\n- (void)setupManagedObjectContext\n{\n    self.managedObjectContext = \n         [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];\n    self.managedObjectContext.persistentStoreCoordinator = \n        [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];\n    NSError* error;\n    [self.managedObjectContext.persistentStoreCoordinator \n         addPersistentStoreWithType:NSSQLiteStoreType\n                      configuration:nil\n                                URL:self.storeURL \n                            options:nil \n                              error:&error];\n    if (error) {\n        NSLog(@\"error: %@\", error);\n    }\n    self.managedObjectContext.undoManager = [[NSUndoManager alloc] init];\n}\n```\n\nIt's important to check the error, because this will probably fail a\nlot during development. When you change your data model, Core Data detects this and will\nnot continue. You can also pass in options to instruct Core Data about\nwhat to do in this case, which Martin explains thoroughly in his article\nabout [migrations][700]. Note that the\nlast line adds an undo manager; we will need this later. On iOS, you\nneed to explicitly add an undo manager, whereas on Mac it is there by\ndefault.\n\nThis code creates a really simple Core Data Stack: one managed object\ncontext, which has a persistent store coordinator, which has one\npersistent store. [More complicated setups][310] are possible; the most\ncommon is to have multiple managed object contexts (each on a\nseparate queue).\n\n## Creating a Model\n\nCreating a model is simple, as we just add a new file to our project, choosing the\nData Model template (under Core Data). This model file will get compiled to a\nfile with extension `.momd`, which we will load at runtime to\ncreate a `NSManagedObjectModel`, which is needed for the persistent store. The\nsource of the model is simple XML, and in our experience, you typically won't\nhave any merge problems when checking it into source control. \nIt is also possible to create a managed object model in code, if you\nprefer that.\n\nOnce you create the model, you can add an `Item` entity with two\nattributes: `title`, which is a string, and `order`, which is an integer.\nThen, you add two relationships: `parent`, which\nrelates an item to its parent, and `children`, which is a to-many\nrelationship. Set the relationships as the inverse of one another, which means\nthat if you set `a`'s parent to be `b`, then `b` will have `a` in its children\nautomatically. \n\nNormally, you could even use ordered relationships, and leave out the\n`order` property entirely. However, they don't play together nicely with\nfetched results controllers (which we will use later on). We would\neither need to reimplement part of fetched results controllers, or\nreimplement the ordering, and we chose the latter.\n\nNow, choose *Editor > Create NSManagedObject subclass...* from the menu, and\ncreate a subclass of `NSManagedObject` that is tied to this entity. This\ncreates two files: `Item.h` and `Item.m`. There is an extra category in the header\nfile, which we will delete immediately (it is there for legacy reasons).\n\n\n### Create a `Store` Class\n\nFor our model, we will create a root node that is the start of our item\ntree. We need a place to create this root node and to find it later.\nTherefore, we create a simple `Store` class, that does exactly this. It\nhas a managed object context, and one method `rootItem`.\nIn our app delegate, we will find this root item at launch\nand pass it to our root view controller. As an optimization, you can\nstore the object id of the item in the user defaults, in order to look it up even\nfaster:\n\n```objc\n- (Item*)rootItem\n{\n    NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@\"Item\"];\n    request.predicate = [NSPredicate predicateWithFormat:@\"parent = %@\", nil];\n    NSArray* objects = [self.managedObjectContext executeFetchRequest:request error:NULL];\n    Item* rootItem = [objects lastObject];\n    if (rootItem == nil) {\n        rootItem = [Item insertItemWithTitle:nil \n                                      parent:nil \n                      inManagedObjectContext:self.managedObjectContext];\n    }\n    return rootItem;\n}\n```\n\nAdding an item is mostly\nstraightforward. \nHowever, we have to set the `order` property to be\nlarger than any of the existing items with that parent. The invariant we\nwill use is\nthat the first child has an `order` of 0, and every subsequent child has an\n`order` value that is 1 higher.\nWe create a custom method on the `Item` class where we\nput the logic:\n\n```objc\n+ (instancetype)insertItemWithTitle:(NSString*)title\n                             parent:(Item*)parent\n             inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext\n{\n    NSUInteger order = parent.numberOfChildren;\n    Item* item = [NSEntityDescription insertNewObjectForEntityForName:self.entityName\n                                               inManagedObjectContext:managedObjectContext];\n    item.title = title;\n    item.parent = parent;\n    item.order = @(order);\n    return item;\n}\n```\n\nThe number of children is a very simple method:\n\n```objc\n- (NSUInteger)numberOfChildren\n{\n    return self.children.count;\n}\n```\n\nTo support automatic updates to our table view, we will use a fetched\nresults controller. A fetched results controller is an object that can\nmanage a fetch request with a big number of items and is the perfect\nCore Data companion to a table view, as we will see in the next section:\n    \n```objc\n- (NSFetchedResultsController*)childrenFetchedResultsController\n{\n    NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:[self.class entityName]];\n    request.predicate = [NSPredicate predicateWithFormat:@\"parent = %@\", self];\n    request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@\"order\" ascending:YES]];\n    return [[NSFetchedResultsController alloc] initWithFetchRequest:request \n                                               managedObjectContext:self.managedObjectContext \n                                                 sectionNameKeyPath:nil \n                                                          cacheName:nil];\n}\n```\n\n## Add a Table-View Backed by Fetched Results Controller\n\nOur next step is to create the root view controller: a table view, which\ngets its data from an `NSFetchedResultsController`. The fetched results\ncontroller manages your fetch request, and if you assign a delegate, it\nalso notifies you of any changes in the managed object context.  In\npractice, this means that if you implement the delegate methods, you can\nautomatically update your table view when relevant changes happen in the\ndata model. For example, if you synchronize in a background thread,\nand then you store the changes in the database, your table view will\nupdate automatically.\n\n\n### Creating the Table View's Data Source\n\nIn our article on [lighter view controllers](/issues/1-view-controllers/lighter-view-controllers/), we demonstrated how to separate out the data source of a table view. We will do exactly the same for a fetched results controller; we create a separate class `FetchedResultsControllerDataSource` that acts as a table view's data source, and by listening to the fetched results controller, updates the table view automatically.\n\nWe initialize the object with a table view, and the initializer looks like this:\n\n```objc\n- (id)initWithTableView:(UITableView*)tableView\n{\n    self = [super init];\n    if (self) {\n        self.tableView = tableView;\n        self.tableView.dataSource = self;\n    }\n    return self;\n}\n```\n\nWhen we set the fetch results controller, we have to make ourselves the\ndelegate, and perform the initial fetch. It is easy to forget the\n`performFetch:` call, and you will get no results (and no errors):\n\n```objc\n- (void)setFetchedResultsController:(NSFetchedResultsController*)fetchedResultsController\n{\n    _fetchedResultsController = fetchedResultsController;\n    fetchedResultsController.delegate = self;\n    [fetchedResultsController performFetch:NULL];\n}\n```\n\nBecause our class implements the `UITableViewDataSource` protocol, we need to implement some methods for that. In these two methods we just ask the fetched results controller for the required information:\n\n```objc\n- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView\n{\n    return self.fetchedResultsController.sections.count;\n}\n\n- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)sectionIndex\n{\n    id<NSFetchedResultsSectionInfo> section = self.fetchedResultsController.sections[sectionIndex];\n    return section.numberOfObjects;\n}\n```\n\nHowever, when we need to create cells, it requires some simple steps: we\nask the fetched results controller for the right object, we dequeue a\ncell from the table view, and then we tell our delegate (which will be a\nview controller) to configure that cell with the object. Now, we have a\nnice separation of concerns, as the view controller only has to care about\nupdating the cell with the model object:\n    \n```objc\n- (UITableViewCell*)tableView:(UITableView*)tableView \n        cellForRowAtIndexPath:(NSIndexPath*)indexPath\n{\n    id object = [self.fetchedResultsController objectAtIndexPath:indexPath];\n    id cell = [tableView dequeueReusableCellWithIdentifier:self.reuseIdentifier\n                                             forIndexPath:indexPath];\n    [self.delegate configureCell:cell withObject:object];\n    return cell;\n}\n```\n\n### Creating the Table View Controller\n\nNow, we can create a view controller that displays a list of items using\nthe class we just created. In the example app, we created a Storyboard,\nand added a navigation controller with a table view controller. This\nautomatically sets the view controller as the data source, which is not\nwhat we want. Therefore, in our `viewDidLoad`, we do the following:\n\n```objc\nfetchedResultsControllerDataSource =\n    [[FetchedResultsControllerDataSource alloc] initWithTableView:self.tableView];\nself.fetchedResultsControllerDataSource.fetchedResultsController = \n    self.parent.childrenFetchedResultsController;\nfetchedResultsControllerDataSource.delegate = self;\nfetchedResultsControllerDataSource.reuseIdentifier = @\"Cell\";\n```\n\nIn the initializer of the fetched results controller data source, the\ntable view's data source gets set. The reuse identifier matches the one\nin the Storyboard. Now, we have to implement the delegate method:\n\n```objc\n- (void)configureCell:(id)theCell withObject:(id)object\n{\n    UITableViewCell* cell = theCell;\n    Item* item = object;\n    cell.textLabel.text = item.title;\n}\n```\n\nOf course, you could do a lot more than just setting the text label, but\nyou get the point. Now we have pretty much everything in place for\nshowing data, but as there is no way to add anything yet, it looks\npretty empty.\n\n## Adding Interactivity\n\nWe will add a couple of ways of interacting with the data. First, we\nwill make it possible to add items. Then we will implement the fetched\nresults controller's delegate methods to update the table view, and add\nsupport for deletion and undo.\n\n### Adding Items\n\nTo add items, we steal the interaction design from\n[Clear](http://www.realmacsoftware.com/clear/), which is high on my list\nof most beautiful apps. We add a text field as the table view's header,\nand modify the content inset of the table view to make sure it stays\nhidden by default, as explained in Joe's [scroll view\narticle](/issues/3-views/scroll-view/). As always, the full code is on github, but\nhere's the relevant call to inserting the item, in `textFieldShouldReturn`:\n\n```objc\n[Item insertItemWithTitle:title \n                   parent:self.parent\n   inManagedObjectContext:self.parent.managedObjectContext];\ntextField.text = @\"\";\n[textField resignFirstResponder];\n```\n\n### Listening to Changes\n\nThe next step is making sure that your table view inserts a row for the\nnewly created item. There are several ways to go about this, but we'll use\nthe fetched results controller's delegate method:\n\n```objc\n- (void)controller:(NSFetchedResultsController*)controller\n   didChangeObject:(id)anObject\n       atIndexPath:(NSIndexPath*)indexPath\n     forChangeType:(NSFetchedResultsChangeType)type\n      newIndexPath:(NSIndexPath*)newIndexPath\n{\n    if (type == NSFetchedResultsChangeInsert) {\n        [self.tableView insertRowsAtIndexPaths:@[newIndexPath]\n                              withRowAnimation:UITableViewRowAnimationAutomatic];\n    }\n}\n```\n\nThe fetched results controller also calls these methods for deletions,\nchanges, and moves (we'll implement that later). If you have multiple\nchanges happening at the same time, you can implement two more methods\nso that the table view will animate everything at the same time. For\nsimple single-item insertions and deletions, it doesn't make a difference,\nbut if you choose to implement syncing at some time, it makes everything\na lot prettier:\n\n```objc\n- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller\n{\n    [self.tableView beginUpdates];\n}\n\n- (void)controllerDidChangeContent:(NSFetchedResultsController*)controller\n{\n    [self.tableView endUpdates];\n}\n```\n\n#### Using a Collection View\n\nIt's worth noting that fetched results controllers are not at all\nlimited to table views; you can use them with any kind of view. Because\nthey are index-path-based, they also work really well with\ncollection views, although you unfortunately have to jump through some\nminor hoops in order to make it work perfectly, as a collection view\ndoesn't have a `beginUpdates` and `endUpdates` method, but rather a single\nmethod `performBatchUpdates`. To deal with this, you can collect all the\nupdates you get, and then in the `controllerDidChangeContent`, perform\nthem all inside the block. Ash Furrow wrote an example of [how you could\ndo this](https://github.com/AshFurrow/UICollectionView-NSFetchedResultsController).\n\n#### Implementing Your Own Fetched Results Controller\n\nYou don't have to use `NSFetchedResultsController`. In fact, in a lot of\ncases it might make sense to create a similar class that works\nspecifically for your application.  What you can do is subscribe to the \n`NSManagedObjectContextObjectsDidChangeNotification`. You then get a\nnotification, and the `userInfo` dictionary will contain a list of the\nchanged objects, inserted objects, and deleted objects. Then you can\nprocess them in any way you want.\n\n### Passing Model Objects Around\n\nNow that we can add and list items, it's time to make sure we can make\nsub-lists. In the Storyboard, you can create a segue by dragging from a\ncell to the view controller. It's wise to give the segue a name, so that\nit can be identified if we ever have multiple segues originating from\nthe same view controller.\n\nMy pattern for dealing with segues looks like this: first, you try to\nidentify which segue it is, and for each segue you pull out a separate\nmethod that prepares the destination view controller:\n\n```objc\n- (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender\n{\n    [super prepareForSegue:segue sender:sender];\n    if ([segue.identifier isEqualToString:selectItemSegue]) {\n        [self presentSubItemViewController:segue.destinationViewController];\n    }\n}\n\n- (void)presentSubItemViewController:(ItemViewController*)subItemViewController\n{\n    Item* item = [self.fetchedResultsControllerDataSource selectedItem];\n    subItemViewController.parent = item;\n}\n```\n\nThe only thing the child view controller needs is the item. From the\nitem, it can also get to the managed object context.\nWe get the selected item from our data source (which looks up\nthe table view's selected item index and fetches the correct item from the\nfetched results controller). It's as simple as that.\n\nOne pattern that's unfortunately very common is having the managed\nobject context as a property on the app delegate, and then always accessing\nit from everywhere. This is a bad idea. If you ever want to use a\ndifferent managed object context for a certain part of your view\ncontroller hierarchy, this will be very hard to refactor,\nand additionally, your code will be a lot more difficult to test.\n\nNow, try adding an item in the sub-list, and you will probably get a\nnice crash. This is because we now have two fetched results controllers,\none for the topmost view controller, but also one for the root view\ncontroller. The latter one tries to update its table view, which is\noffscreen, and everything crashes. The solution is to tell our data\nsource to stop listening to the fetched results controller delegate methods:\n\n```objc\n- (void)viewWillAppear:(BOOL)animated\n{\n    [super viewWillAppear:animated];\n    self.fetchedResultsControllerDataSource.paused = NO;\n}\n\n- (void)viewWillDisappear:(BOOL)animated\n{\n    [super viewWillDisappear:animated];\n    self.fetchedResultsControllerDataSource.paused = YES;\n}\n```\n\nOne way to implement this inside the data source is setting the fetched\nresults controller's delegate to nil, so that no updates are received\nany longer. We then need to add it after we come out of `paused` state:\n\n```objc\n- (void)setPaused:(BOOL)paused\n{\n    _paused = paused;\n    if (paused) {\n        self.fetchedResultsController.delegate = nil;\n    } else {\n        self.fetchedResultsController.delegate = self;\n        [self.fetchedResultsController performFetch:NULL];\n        [self.tableView reloadData];\n    }\n}\n```\n\nThe `performFetch` will then make sure your data source is up to date.\nOf course, a nicer implementation would be to not set the delegate to nil, but\ninstead keep a list of the changes that happened while in paused state,\nand update the table view accordingly after you get out of paused state.\n\n### Deletion\n\nTo support deletion, we need to take a few steps. First, we need to\nconvince the table view that we support deletion, and second, we need to\ndelete the object from core data and make sure our order invariant stays\ncorrect.\n\nTo allow for swipe to delete, we need to implement two methods in the\ndata source:\n\n```objc\n     - (BOOL)tableView:(UITableView*)tableView\n canEditRowAtIndexPath:(NSIndexPath*)indexPath\n {\n     return YES;\n }\n \n  - (void)tableView:(UITableView *)tableView \n commitEditingStyle:(UITableViewCellEditingStyle)editingStyle\n  forRowAtIndexPath:(NSIndexPath *)indexPath {\n     if (editingStyle == UITableViewCellEditingStyleDelete) {\n         id object = [self.fetchedResultsController objectAtIndexPath:indexPath]\n         [self.delegate deleteObject:object];\n     }\n }\n```\n\nRather than deleting immediately, we tell our delegate (the view\ncontroller) to delete the object. That way, we don't have to share the\nstore object with our data source (the data source should be reusable\nacross projects), and we keep the flexibility to do any custom actions.\nThe view controller simply calls `deleteObject:` on the managed object\ncontext.\n\nHowever, there are two important problems to solve: what do we do with\nthe children of the item that we delete, and how do we enforce our order\nvariant? Luckily, propagating deletion is easy: in our data model, we\ncan choose *Cascade* as the delete rule for the children relationship.\n\nFor enforcing our order variant, we can override the\n`prepareForDeletion` method, and update all the siblings with a higher\n`order`:\n\n```objc\n- (void)prepareForDeletion\n{\n    NSSet* siblings = self.parent.children;\n    NSPredicate* predicate = [NSPredicate predicateWithFormat:@\"order > %@\", self.order];\n    NSSet* siblingsAfterSelf = [siblings filteredSetUsingPredicate:predicate];\n    [siblingsAfterSelf enumerateObjectsUsingBlock:^(Item* sibling, BOOL* stop)\n    {\n        sibling.order = @(sibling.order.integerValue - 1);\n    }];\n}\n```\n\nNow we're almost there. We can interact with table view cells and delete\nthe model object. The final step is to implement the necessary code to\ndelete the table view cells once the model objects get deleted. In our\ndata source's `controller:didChangeObject:...` method we add another if\nclause:\n\n```objc\n...\nelse if (type == NSFetchedResultsChangeDelete) {\n    [self.tableView deleteRowsAtIndexPaths:@[indexPath]\n                          withRowAnimation:UITableViewRowAnimationAutomatic];\n}\n```\n\n### Add Undo Support\n\nOne of the nice things about Core Data is that it comes with integrated\nundo support. We will add *the shake to undo* feature, and a first step\nis telling the application that we can do this:\n\n```objc\napplication.applicationSupportsShakeToEdit = YES;\n```\n\nNow, whenever a shake is triggered, the application will ask the first\nresponder for its undo manager, and perform an undo. In [last month's\narticle](/issues/3-views/custom-controls/), we saw that a\nview controller is also in the responder chain, and this is exactly\nwhat we'll use. In our view controller, we override the following two\nmethods from the `UIResponder` class:\n\n```objc\n- (BOOL)canBecomeFirstResponder {\n    return YES;\n}\n\n- (NSUndoManager*)undoManager\n{\n    return self.managedObjectContext.undoManager;\n}\n```\n\nNow, when a shake gesture happens, the managed object context's undo manager will get an\nundo message, and undo the last change.  Remember, on iOS, a managed object context doesn't have an undo manager by default, (whereas on Mac, a newly created managed object context does have an undo manager), so we created that in the setup of the persistent\nstack:\n\n```objc\nself.managedObjectContext.undoManager = [[NSUndoManager alloc] init];\n```\n\nAnd that's almost all there is to it. Now, when you shake, you get the\ndefault iOS alert view with two buttons: one for undoing, and one for\ncanceling. One nice feature of Core Data is that it automatically groups\nchanges. For example, the `addItem:parent` will record as one undo\naction. For the deletion, it's the same. \n\nTo make managing the undos a bit easier for the user, we can also name\nthe actions, and change the first lines of `textFieldShouldReturn:` to\nthis:\n\n```objc\nNSString* title = textField.text;\nNSString* actionName = [NSString stringWithFormat:\n    NSLocalizedString(@\"add item \\\"%@\\\"\", @\"Undo action name of add item\"), title];\n[self.undoManager setActionName:actionName];\n[self.store addItem:title parent:nil];\n```\n\nNow, when the user shakes, he or she gets a bit more context than just the\ngeneric label \"Undo\".\n\n### Editing\n\nEditing is currently not supported in the example application, but is a\nmatter of just changing properties on the objects. For example, to\nchange the title of an item, just set the `title` property and you're\ndone. To change the parent of an item `foo`, just set the `parent` property\nto a new value `bar`, and everything gets updated: `bar` now has `foo`\nin its `children`, and because we use fetched results controllers the\nuser interface also updates automatically.\n\n### Reordering\n\nReordering cells is also not possible in the sample application, but\nis mostly straightforward to implement. Yet, there is one caveat: if you\nallow user-driven reordering, you will update the `order` property in\nthe model, and then get a delegate call from the fetched results\ncontroller (which you should ignore, because the cells have already\nmoved). This is explained in the\n[`NSFetchedResultsControllerDelegate` documentation](https://developer.apple.com/library/ios/documentation/CoreData/Reference/NSFetchedResultsControllerDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40008228-CH1-SW14)\n\n## Saving\n\nSaving is as easy as calling `save` on the managed object context.\nBecause we don't access that directly, we do it in the store. The only\nhard part is when to save. Apple's sample code does it in\n`applicationWillTerminate:`, but depending on your use case it could\nalso be in `applicationDidEnterBackground:` or even while your app is\nrunning.\n\n## Discussion\n\nIn writing this article and the example application, I made an initial\nmistake: I chose to not have an empty root item, but instead let all\nthe user-created items at root level have a `nil` parent. This caused a\nlot of trouble: because the `parent` item in the view controller could\nbe `nil`, we needed to pass the store (or the managed object context)\naround to each child view controller. Also, enforcing the order\ninvariant was harder, as we needed a fetch request to find an item's\nsiblings, thus forcing Core Data to go back to disk. Unfortunately,\nthese problems were not immediately clear when writing the code, and\nsome only became clear when writing the tests. When rewriting the code,\nI was able to move almost all code from the `Store` class into the\n`Item` class, and everything became a lot cleaner.\n\n\n[100]:/issues/4-core-data/importing-large-data-sets-into-core-data/\n[110]:/issues/4-core-data/importing-large-data-sets-into-core-data/#efficient-importing\n[120]:/issues/4-core-data/importing-large-data-sets-into-core-data/#user-generated-data\n\n[200]:/issues/4-core-data/core-data-models-and-model-objects/\n[210]:/issues/4-core-data/core-data-models-and-model-objects/#managed-objects\n[220]:/issues/4-core-data/core-data-models-and-model-objects/#validation\n[230]:/issues/4-core-data/core-data-models-and-model-objects/#ivars-in-managed-object-classes\n[240]:/issues/4-core-data/core-data-models-and-model-objects/#entity-vs-class-hierarchy\n[250]:/issues/4-core-data/core-data-models-and-model-objects/#creating-objects\n[260]:/issues/4-core-data/core-data-models-and-model-objects/#indexes\n\n[300]:/issues/4-core-data/core-data-overview/\n[310]:/issues/4-core-data/core-data-overview/#complicated-stacks\n[320]:/issues/4-core-data/core-data-overview/#getting-to-objects\n\n[400]:/issues/4-core-data/full-core-data-application/\n\n[500]:/issues/4-core-data/SQLite-instead-of-core-data/\n\n[600]:/issues/4-core-data/core-data-fetch-requests/\n\n[700]:/issues/4-core-data/core-data-migration/\n"
  },
  {
    "path": "2013-09-09-importing-large-data-sets-into-core-data.md",
    "content": "---\ntitle: Importing Large Data Sets\ncategory: \"4\"\ndate: \"2013-09-09 07:00:00\"\nauthor:\n  - name: Florian Kugler\n    url: http://twitter.com/floriankugler\ntags: article\n---\n\n\nImporting large data sets into a Core Data application is a common problem. There are several approaches you can take dependent on the nature of the data:\n\n1. Downloading the data from a web server (for example as JSON) and inserting it into Core Data.\n2. Downloading a pre-populated Core Data SQLite file from a web server.\n3. Shipping a pre-populated Core Data SQLite file in the app bundle.\n\nThe latter two options especially are often overlooked as viable options for some use cases. Therefore, we are going to have a closer look at them in this article, but we will also outline how to efficiently import data from a web service into a live application.\n\n\n## Shipping Pre-Populated SQLite files\n\nShipping or downloading pre-populated SQLite files is a viable option to seed Core Data with big amounts of data and is much more efficient then creating the database on the client side. If the seed data set consists of static data and can live relatively independently next to potential user-generated data, it might be a use case for this technique.\n\nThe Core Data framework is shared between iOS and OS X, therefore we can create an OS X command line tool to generate the SQLite file, even if it should be used in an iOS application.\n\nIn our example, (which you can [find on GitHub](https://github.com/objcio/issue-4-importing-and-fetching)), we created a command line tool which takes two files of a [transit data set](http://stg.daten.berlin.de/datensaetze/vbb-fahrplan-2013) for the city of Berlin as input and inserts them into a Core Data SQLite database. The data set consists of roughly 13,000 stop records and 3 million stop-time records.\n\nThe most important thing with this technique is to use exactly the same data model for the command line tool as for the client application. If the data model changes over time and you're shipping new seed data sets with application updates, you have to be very careful about managing data model versions. It's usually a good idea to not duplicate the .xcdatamodel file, but to link it into the client applications project from the command line tool project.\n\nAnother useful step is to perform a `VACUUM` command on the resulting SQLite file. This will bring down the file size and therefore the size of the app bundle or the database download, dependent on how you ship the file.\n\nOther than that, there is really no magic to the process; as you can see in our [example project](), it's all just simple standard Core Data code. And since generating the SQLite file is not a performance-critical task, you don't even need to go to great lengths of optimizing its performance. If you want to make it fast anyway, the same rules apply as outlined below for [efficiently importing large data sets in a live application][110].\n\n\n<a name=\"user-generated-data\"> </a>\n\n### User-Generated Data\n\nOften we will have the case where we want to have a large seed data set available, but we also want to store and modify some user-generated data next to it. Again, there are several approaches to this problem.\n\nThe first thing to consider is if the user-generated data is really a candidate to be stored with Core Data. If we can store this data just as well e.g. in a plist file, then it's not going to interfere with the seeded Core Data database anyway. \n\nIf we want to store it with Core Data, the next question is whether the use case might require updating the seed data set in the future by shipping an updated pre-populated SQLite file. If this will never happen, we can safely include the user-generated data in the same data model and configuration. However, if we might ever want to ship a new seed database, we have to separate the seed data from the user-generated data.\n\nThis can be done by either setting up a second, completely independent Core Data stack with its own data model, or by distributing the data of the same data model between two persistent stores. For this, we would have to create a second [configuration](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreData/Articles/cdMOM.html#//apple_ref/doc/uid/TP40002328-SW3) within the same data model that holds the entities of the user-generated data. When setting up the Core Data stack, we would then instantiate two persistent stores, one with the URL and configuration of the seed database, and the other one with the URL and configuration of the database for the user-generated data.\n\nUsing two independent Core Data stacks is the more simple and straightforward solution. If you can get away with it, we strongly recommend this approach. However, if you want to establish relationships between the user-generated data and the seed data, Core Data cannot help you with that. If you include everything in one data model spread out across two persistent stores, you still cannot define relationships between those entities as you would normally do, but you can use Core Data's [fetched properties](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreData/Articles/cdRelationships.html#//apple_ref/doc/uid/TP40001857-SW7) in order to automatically fetch objects from a different store when accessing a certain property.\n\n\n### SQLite Files in the App Bundle\n\nIf we want to ship a pre-populated SQLite file in the application bundle, we have to detect the first launch of a newly updated application and copy the database file out of the bundle into its target directory:\n\n```objc\nNSFileManager* fileManager = [NSFileManager defaultManager];\nNSError *error;\n\nif([fileManager fileExistsAtPath:self.storeURL.path]) {\n    NSURL *storeDirectory = [self.storeURL URLByDeletingLastPathComponent];\n    NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtURL:storeDirectory\n                                          includingPropertiesForKeys:nil\n                                                             options:0\n                                                        errorHandler:NULL];\n    NSString *storeName = [self.storeURL.lastPathComponent stringByDeletingPathExtension];\n    for (NSURL *url in enumerator) {\n        if (![url.lastPathComponent hasPrefix:storeName]) continue;\n        [fileManager removeItemAtURL:url error:&error];\n    }\n    // handle error\n}\n\nNSString* bundleDbPath = [[NSBundle mainBundle] pathForResource:@\"seed\" ofType:@\"sqlite\"];\n[fileManager copyItemAtPath:bundleDbPath toPath:self.storeURL.path error:&error];\n```\n\nNotice that we're first deleting the previous database files. This is not as straightforward as one may think though, because there can be different auxiliary files (e.g. journaling or write-ahead logging files) next to the main `.sqlite` file. Therefore we have to enumerate over the items in the directory and delete all files that match the store file name without its extension.\n\nHowever, we also need a way to make sure that we only do this once. An obvious solution would be to delete the seed database from the bundle. However, while this works on the simulator, it will fail as soon as you try this on a real device because of restricted permissions. There are many options to solve this problem though, like setting a key in the user defaults which contains information about the latest seed data version imported:\n\n```objc\nNSDictionary *infoDictionary = [NSBundle mainBundle].infoDictionary;\nNSString* bundleVersion = [infoDictionary objectForKey:(NSString *)kCFBundleVersionKey];\nNSString *seedVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@\"SeedVersion\"];\nif (![seedVersion isEqualToString:bundleVersion]) {\n    // Copy the seed database\n}\n\n// ... after the import succeeded\n[[NSUserDefaults standardUserDefaults] setObject:bundleVersion forKey:@\"SeedVersion\"];\n```\n\nAlternatively for example, we could also copy the existing database file to a path including the seed version and detect its presence to avoid doing the same import twice. There are many practicable solutions which you can choose from, dependent on what makes the most sense for your case.\n\n\n## Downloading Pre-Populated SQLite Files\n\nIf for some reason we don't want to include a seed database in the app bundle (e.g. because it would push the bundle size beyond the cellular download threshold), we can also download it from a web server. The process is exactly the same as soon as we have the database file locally on the device. We need to make sure though, that the server sends a version of the database which is compatible with the data model of the client, if the data model might change across different app versions.\n\nBeyond replacing a file in the app bundle with a download though, this option also opens up possibilities to deal with seeding more dynamic datasets without incurring the performance and energy cost of importing the data dynamically on the client side.\n\nWe can run a similar command line importer as we used before on a (OS X) server in order to generate the SQLite file on the fly. Admittedly, the computing resources required for this operation might not permit doing this fully on demand for each request, depending on the size of the data set and the number of request we would have to serve. A viable alternative though is to generate the SQLite file in regular intervals and to serve these readily available files to the clients.\n\nThis of course requires some additional logic on the server and the client, in order to provide an API next to the SQLite download which can provide the data to the clients which has changed since the last seed file generation. The whole setup becomes a bit more complex, but it enables easily seeding Core Data with dynamic data sets of arbitrary size without performance problems (other than bandwidth limitations).\n\n\n## Importing Data from Web Services\n\nFinally, let's have a look at what we have to do in order to live import large amounts of data from a web server that provides the data, for example, in JSON format.\n\nIf we are importing different object types with relationships between them, then we will need to import all objects independently first before attempting to resolve the relationships between them. If we could guarantee on the server side that the client receives the objects in the correct order in order to resolve all relationships immediately, we wouldn't have to worry about this. But mostly this will not be the case.\n\nIn order to perform the import without affecting user-interface responsiveness, we have to perform the import on a background thread. In a previous issue, Chris wrote about a simple way of [using Core Data in the background](/issues/2-concurrency/common-background-practices/). If we do this right, devices with multiple cores can perform the import in the background without affecting  the responsiveness of the user interface. Be aware though that using Core Data concurrently also creates the possibility of conflicts between different managed object contexts. You need to come up with a [policy](http://thermal-core.com/2013/09/07/in-defense-of-core-data-part-I.html) of how to prevent or handle these situations.\n\nIn this context, it is important to understand how concurrency works with Core Data. Just because we have set up two managed object contexts on two different threads does not mean that they both get to access the database at the same time. Each request issued from a managed object context will cause a lock on the objects from the context all the way down to the SQLite file. For example, if you trigger a fetch request in a child context of the main context, the main context, the persistent store coordinator, the persistent store, and ultimately the SQLite file will be locked in order to execute this request (although the [lock on the SQLite file will come and go faster](https://developer.apple.com/wwdc/videos/?id=211) than on the stack above). During this time, everybody else in the Core Data stack is stuck waiting for this request to finish.\n\nIn the example of mass-importing data on a background context, this means that the save requests of the import will repeatedly lock the persistent store coordinator. During this time, the main context cannot execute, for example, a fetch request to update the user interface, but has to wait for the save request to finish. Since Core Data's API is synchronous, the main thread will be stuck waiting and the responsiveness of the user interface might be affected.\n\nIf this is an issue in your use case, you should consider using a separate Core Data stack with its own persistent store coordinator for the background context. Since, in this case, the only shared resource between the background context and the main context is the SQLite file, lock contention will likely be lower than before. Especially when SQLite is operating in [write-ahead logging](http://www.sqlite.org/draft/wal.html) mode, (which it is by default on iOS 7 and OS X 10.9), you get true concurrency even on the SQLite file level. Multiple readers and a single writer are able to access the database at the same time (See [WWDC 2013 session \"What's New in Core Data and iCloud\"](https://developer.apple.com/wwdc/videos/?id=207)).\n\nLastly, it's mostly not a good idea to merge every change notification into the main context immediately while mass-importing data. If the user interface reacts to these changes automatically, (e.g. by using a `NSFetchedResultsController`), it will come to a grinding halt quickly. Instead, we can send a custom notification once the whole import is done to give the user interface a chance to reload its data. \n\nIf the use case warrants putting additional effort into this aspect of live-updating the UI during the import, then we can consider filtering the save notifications for certain entity types, grouping them together in batches, or other means of reducing the frequency of interface updates, in order to keep it responsive. However, in most cases, it's not worth the effort, because very frequent updates to the user interface are more confusing then helpful to the user. \n\nAfter laying out the general setup and modus operandi of a live import, we'll now have a look at some specific aspects to make it as efficient as possible.\n\n\n<a name=\"efficient-importing\"> </a>\n\n### Importing Efficiently\n\nOur first recommendation for importing data efficiently is to read [Apple's guide on this subject](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreData/Articles/cdImporting.html). We would also like to highlight a few aspects described in this document, which are often forgotten.\n\nFirst of all, you should always set the `undoManager` to `nil` on the context that you use for importing. This applies only to OS X though, because on iOS, contexts come without an undo manager by default. Nilling out the `undoManager` property will give a significant performance boost.\n\nNext, accessing relationships between objects in *both directions* creates retain cycles. If you see growing memory usage during the import despite well-placed auto-release pools, watch out for this pitfall in the importer code.  [Here, Apple describes](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreData/Articles/cdMemory.html#//apple_ref/doc/uid/TP40001860-SW3) how to break these cycles using [`refreshObject:mergeChanges:`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/NSManagedObjectContext.html#//apple_ref/occ/instm/NSManagedObjectContext/refreshObject:mergeChanges:).\n\nWhen you're importing data which might already exist in the database, you have to implement some kind of find-or-create algorithm to prevent creating duplicates. Executing a fetch request for every single object is vastly inefficient, because every fetch request will force Core Data to go to disk and fetch the data from the store file. However, it's easy to avoid this by importing the data in batches and using the efficient find-or-create algorithm Apple outlines in the above-mentioned guide.\n\nA similar problem often arises when establishing relationships between the newly imported objects. Using a fetch request to get each related object independently is vastly inefficient. There are two possible ways out of this: either we resolve relationships in batches similar to how we imported the objects in the first place, or we cache the objectIDs of the already-imported objects. \n\nResolving relationships in batches allows us to greatly reduce the number of fetch requests required by fetching many related objects at once. Don't worry about potentially long predicates like:\n\n```objc\n[NSPredicate predicateWithFormat:@\"identifier IN %@\", identifiersOfRelatedObjects];\n```\n\nResolving a predicate with many identifiers in the `IN (...)` clause is always way more efficient than going to disk for each object independently. \n\nHowever, there is also a way to avoid fetch requests altogether (at least if you only need to establish relationships between newly imported objects). If you cache the objectIDs of all imported objects (which is not a lot of data in most cases really), you can use them later to retrieve faults for related objects using `objectWithID:`.\n\n```objc\n// after a batch of objects has been imported and saved\nfor (MyManagedObject *object in importedObjects) {\n    objectIDCache[object.identifier] = object.objectID;\n}\n\n// ... later during resolving relationships \nNSManagedObjectID objectID = objectIDCache[object.foreignKey];\nMyManagedObject *relatedObject = [context objectWithID:objectId];\nobject.toOneRelation = relatedObject;\n```\n\nNote that this example assumes that the `identifier` property is unique across all entity types, otherwise we would have to account for duplicate identifiers for different types in the way we cache the object IDs. \n\n\n## Conclusion\n\nWhen you face the challenge of having to import large data sets into Core Data, try to think out of the box first, before doing a live-import of massive amounts of JSON data. Especially if you're in control of the client and the server side, there are often much more efficient ways of solving this problem. But if you have to bite the bullet and do large background imports, make sure to operate as efficiently and as independently from the main thread as possible. \n\n\n\n[100]:/issues/4-core-data/importing-large-data-sets-into-core-data/\n[110]:/issues/4-core-data/importing-large-data-sets-into-core-data/#efficient-importing\n[120]:/issues/4-core-data/importing-large-data-sets-into-core-data/#user-generated-data\n\n[200]:/issues/4-core-data/core-data-models-and-model-objects/\n[210]:/issues/4-core-data/core-data-models-and-model-objects/#managed-objects\n[220]:/issues/4-core-data/core-data-models-and-model-objects/#validation\n[230]:/issues/4-core-data/core-data-models-and-model-objects/#ivars-in-managed-object-classes\n[240]:/issues/4-core-data/core-data-models-and-model-objects/#entity-vs-class-hierarchy\n[250]:/issues/4-core-data/core-data-models-and-model-objects/#creating-objects\n[260]:/issues/4-core-data/core-data-models-and-model-objects/#indexes\n\n[300]:/issues/4-core-data/core-data-overview/\n[310]:/issues/4-core-data/core-data-overview/#complicated-stacks\n[320]:/issues/4-core-data/core-data-overview/#getting-to-objects\n\n[400]:/issues/4-core-data/full-core-data-application/\n\n[500]:/issues/4-core-data/SQLite-instead-of-core-data/\n\n[600]:/issues/4-core-data/core-data-fetch-requests/\n\n[700]:/issues/4-core-data/core-data-migration/\n"
  },
  {
    "path": "2013-10-08-collection-views-and-uidynamics.md",
    "content": "---\ntitle:  \"UICollectionView + UIKit Dynamics\"\ncategory: \"5\"\ndate: \"2013-10-07 10:00:00\"\ntags: article\nauthor:\n  - name: Ash Furrow\n    url: https://twitter.com/ashfurrow\n---\n\nUIKit Dynamics is the new physics-based animation engine in iOS 7 – it has been specifically designed to work well with collection views, which were first introduced in iOS 6. We're going to take a tour of how you put these two together. \n\nThis article is going to discuss two examples of using collection views with UIKit Dynamics. The first example demonstrates how to reproduce the springy effect in the iOS 7 Messages app and is then amended to incorporate a tiling mechanism that makes the layout scalable. The second example shows how to use UIKit Dynamics to simulate a Newton's Cradle where items can be added to the collection view one at a time, interacting with one another. \n\nBefore we get started, I'm going to assume that you have a baseline understanding of how `UICollectionView` works – see [this objc.io post](/issues/3-views/collection-view-layouts/) for all the details you'll need. I'll also assume that you understand how UIKit Dynamics works – see [this post](http://www.teehanlax.com/blog/introduction-to-uikit-dynamics/) for more. \n\nThe two example projects for this article are on GitHub: \n\n- [`ASHSpringyCollectionView`](https://github.com/objcio/issue-5-springy-collection-view) (based on [`UICollectionView` Spring Demo](https://github.com/TeehanLax/UICollectionView-Spring-Demo)\n- [Newtownian `UICollectionView`](https://github.com/objcio/issue-5-newtonian-collection-view)\n\n## The Dynamic Animator\n\nThe key component backing a `UICollectionView` that employes UIKit Dynamics is the `UIDynamicAnimator`. This class belongs inside a `UICollectionViewFlowLayout` object and should be strongly referenced by it (someone needs to retain the animator, after all). \n\nWhen we create our dynamic animator, we're not going to be giving it a reference view like we normally would. Instead, we'll use a different initializer that requires a collection view layout as a parameter. This is critical, as the dynamic animator needs to be able to invalidate the collection view layout when the attributes of its behaviors' items should be updated. In other words, the dynamic animator is going to be invalidating the layout a lot. \n\nWe'll see how things are hooked up shortly, but it's important to understand at a conceptual level how a collection view interacts with a dynamic animator. The collection view layout is going to add behaviors for each `UICollectionViewLayoutAttributes` object in the collection view (later, we'll talk about tiling these). After adding these behaviors to the dynamic animator, the collection view layout is going to be queried by UIKit about the state of its collection view layout attributes. Instead of doing any calculations ourselves, we're going to return the items provided by our dynamic animator. The animator is going to invalidate the layout whenever its simulation state changes. This will prompt UIKit to requery the layout, and the cycle continues until the simulation comes to a rest. \n\nSo to recap, the layout creates the dynamic animator and adds behaviors corresponding to the layout attributes for each of its items. When asked about layout information, it provides the information supplied by the dynamic animator. \n\n## Subclassing UICollectionViewFlowLayout\n\nWe're going to build a simple example of how to use UIKit Dynamics with a collection view layout. The first thing we need is, of course, a data source to drive our collection view. I know that you're smart enough to provide your own data source, but for the sake of completeness, I've provided one for you: \n\n```objc\n@implementation ASHCollectionViewController\n\nstatic NSString * CellIdentifier = @\"CellIdentifier\";\n\n-(void)viewDidLoad \n{\n    [super viewDidLoad];\n    [self.collectionView registerClass:[UICollectionViewCell class] \n            forCellWithReuseIdentifier:CellIdentifier];\n}\n\n-(UIStatusBarStyle)preferredStatusBarStyle \n{\n    return UIStatusBarStyleLightContent;\n}\n\n-(void)viewDidAppear:(BOOL)animated \n{\n    [super viewDidAppear:animated];\n    [self.collectionViewLayout invalidateLayout];\n}\n\n#pragma mark - UICollectionView Methods\n\n-(NSInteger)collectionView:(UICollectionView *)collectionView \n    numberOfItemsInSection:(NSInteger)section \n{\n    return 120;\n}\n\n-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView \n                 cellForItemAtIndexPath:(NSIndexPath *)indexPath \n{\n    UICollectionViewCell *cell = [collectionView \n        dequeueReusableCellWithReuseIdentifier:CellIdentifier \n                                  forIndexPath:indexPath];\n    \n    cell.backgroundColor = [UIColor orangeColor];\n    return cell;\n}\n\n@end\n```\n\nNotice that it's invalidating the layout when the view first appears. That's a consequence of not using Storyboards (the timing of the first invocation of the prepareLayout method is different when using Storyboards – or not – something they didn't tell you in the WWDC video). As a result, we need to manually invalidate the collection view layout once the view appears. When we use tiling, this isn't necessary.\n\nLet's create our collection view layout. We need to have a strong reference to a dynamic animator that will drive the attributes of our collection view layout. We'll have a private property declared in the implementation file:\n\n```objc\n@interface ASHSpringyCollectionViewFlowLayout ()\n\n@property (nonatomic, strong) UIDynamicAnimator *dynamicAnimator;\n\n@end\n```\n\nWe'll initialize our dynamic animator in the init method of the layout. We'll also set up some of our properties belonging to `UICollectionViewFlowLayout`, our superclass:\n\n```objc\n- (id)init \n{\n    if (!(self = [super init])) return nil;\n    \n    self.minimumInteritemSpacing = 10;\n    self.minimumLineSpacing = 10;\n    self.itemSize = CGSizeMake(44, 44);\n    self.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);\n    \n    self.dynamicAnimator = [[UIDynamicAnimator alloc] initWithCollectionViewLayout:self];\n    \n    return self;\n}\n```\n\nThe next method we'll implement is prepareLayout. We'll need to call our superclass's implementation first. Since we're subclassing `UICollectionViewFlowLayout`, calling super's prepareLayout method will position the collection view layout attributes for us. We can now rely on them being laid out and can ask for all of the attributes in a given rect. Let's load *all* of them.\n\n```objc\n[super prepareLayout];\n\nCGSize contentSize = self.collectionView.contentSize;\nNSArray *items = [super layoutAttributesForElementsInRect:\n    CGRectMake(0.0f, 0.0f, contentSize.width, contentSize.height)];\n```\n    \nThis is *really* inefficient code. Since our collection view could have tens of thousands of cells, loading all of them at once is potentially an incredibly memory-intensive operation. We're going to iterate over those elements in a moment, making this a time-intensive operation as well. An efficiency double-whammy! Don't worry – we're responsible developers so we'll solve this problem shortly. For now, we'll just continue on with a simple, naïve implementation. \n\nAfter loading all of our collection view layout attributes, we need to check and see if they've already been added to our animator. If a behavior for an item already exists in the animator, then we can't re-add it or we'll get a very obscure runtime exception:\n\n```\n<UIDynamicAnimator: 0xa5ba280> (0.004987s) in \n<ASHSpringyCollectionViewFlowLayout: 0xa5b9e60> \\{\\{0, 0}, \\{0, 0\\}\\}: \nbody <PKPhysicsBody> type:<Rectangle> representedObject:\n[<UICollectionViewLayoutAttributes: 0xa281880> \nindex path: (<NSIndexPath: 0xa281850> {length = 2, path = 0 - 0}); \nframe = (10 10; 300 44); ] 0xa2877c0  \nPO:(159.999985,32.000000) AN:(0.000000) VE:(0.000000,0.000000) AV:(0.000000) \ndy:(1) cc:(0) ar:(1) rs:(0) fr:(0.200000) re:(0.200000) de:(1.054650) gr:(0) \nwithout representedObject for item <UICollectionViewLayoutAttributes: 0xa3833e0> \nindex path: (<NSIndexPath: 0xa382410> {length = 2, path = 0 - 0}); \nframe = (10 10; 300 44);\n```\n\nIf you see this error, then it basically means that you're adding two behaviors for identical `UICollectionViewLayoutAttributes`, which the system doesn't know how to handle. \n\nAt any rate, once we've checked that we haven't already added behaviors to our dynamic animator, we'll need to iterate over each of our collection view layout attributes to create and add a new dynamic behavior:\n\n```objc\nif (self.dynamicAnimator.behaviors.count == 0) {\n    [items enumerateObjectsUsingBlock:^(id<UIDynamicItem> obj, NSUInteger idx, BOOL *stop) {\n        UIAttachmentBehavior *behaviour = [[UIAttachmentBehavior alloc] initWithItem:obj \n                                                                    attachedToAnchor:[obj center]];\n        \n        behaviour.length = 0.0f;\n        behaviour.damping = 0.8f;\n        behaviour.frequency = 1.0f;\n        \n        [self.dynamicAnimator addBehavior:behaviour];\n    }];\n}\n```\n\nThe code is very straightforward. For each of our items, we create a new `UIAttachmentBehavior` with the center of the item as the attachment point. We then set the length of our attachment behavior to zero so that it requires the cell to be centered under the behavior's attachment point at all times. We then set the damping and frequency to some values that I determined experimentally to be visually pleasing and not over-the-top. \n\nThat's it for prepareLayout. We now need to respond to two methods that UIKit will call to query us about the layout of collection view layout attributes, `layoutAttributesForElementsInRect:` and `layoutAttributesForItemAtIndexPath:`. Our implementations will forward these queries onto the dynamic animator, which has methods specifically designed to respond to these queries: \n\n```objc\n-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect \n{\n    return [self.dynamicAnimator itemsInRect:rect];\n}\n\n-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath \n{\n    return [self.dynamicAnimator layoutAttributesForCellAtIndexPath:indexPath];\n}\n```\n\n## Responding to Scroll Events\n\nWhat we've implemented so far will provide a static-feeling `UICollectionView` that scrolls normally; there is nothing special about the way that it works. That's fine, but it's not really *dynamic*, is it? \n\nIn order to behave dynamically, we need our layout and dynamic animator to react to changes in the scroll position of the collection view. Luckily there is a method perfectly suited for our task called `shouldInvalidateLayoutForBoundsChange:`. This method is called when the bounds of the collection view change and it provides us with an opportunity to adjust the behaviors' items in our dynamic animator to the new [content offset](/issue-3/scroll-view.html#scroll_views_content_offset). After adjusting the behaviors' items, we're going to return NO from this method; since the dynamic animator will take care of invalidating our layout, there's no need to invalidate it in this case:\n\n```objc\n-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds \n{\n    UIScrollView *scrollView = self.collectionView;\n    CGFloat delta = newBounds.origin.y - scrollView.bounds.origin.y;\n    \n    CGPoint touchLocation = [self.collectionView.panGestureRecognizer locationInView:self.collectionView];\n    \n    [self.dynamicAnimator.behaviors enumerateObjectsUsingBlock:^(UIAttachmentBehavior *springBehaviour, NSUInteger idx, BOOL *stop) {\n        CGFloat yDistanceFromTouch = fabsf(touchLocation.y - springBehaviour.anchorPoint.y);\n        CGFloat xDistanceFromTouch = fabsf(touchLocation.x - springBehaviour.anchorPoint.x);\n        CGFloat scrollResistance = (yDistanceFromTouch + xDistanceFromTouch) / 1500.0f;\n        \n        UICollectionViewLayoutAttributes *item = springBehaviour.items.firstObject;\n        CGPoint center = item.center;\n        if (delta < 0) {\n            center.y += MAX(delta, delta*scrollResistance);\n        }\n        else {\n            center.y += MIN(delta, delta*scrollResistance);\n        }\n        item.center = center;\n        \n        [self.dynamicAnimator updateItemUsingCurrentState:item];\n    }];\n    \n    return NO;\n}\n```\n\nLet's go through this implementation in detail. First, we grab the scroll view (that's our collection view) and calculate the change in the content offset's y component (our collection view scrolls vertically in this example). Once we have the delta, we need to grab the location of the user's touch. This is important because we want items closer to the touch to move more immediately while items further from the touch should lag behind.\n\nFor each behavior in our dynamic animator, we divide the sum of the x and y distances from the touch to the behavior's item by a denominator of 1500, a value determined experimentally. Use a smaller denominator to make the collection view react with more spring. Once we have this \"scroll resistance,\" we move the behavior's item's center.y component by that delta, multiplied by the scrollResistance variable. Finally, note that we clamp the product of the delta and scroll resistance by the delta in case the scroll resistance exceeds the delta (meaning the item might begin to move in the wrong direction). This is unlikely since we're using such a high denominator, but it's something to watch out for in more bouncy collection view layouts. \n\nThat's really all there is to it. In my experience, this naïve approach is effective for collection views with up to a few hundred items. Beyond that, the burden of loading all the items into memory at once becomes too great and you'll begin to drop frames when scrolling. \n\n![Springy Collection View](/images/issue-5/springyCollectionView.gif)\n\n## Adding New Rows\n\nNow let's say that you want to add new rows. For that, we will need to add behaviors to our dynamic animator for each new row. Let's start off by creating a public instance method for our flow layout.\n\n```objc\n@interface ASHSpringyCollectionViewFlowLayout : UICollectionViewFlowLayout\n- (void)resetLayout;\n@end\n```\n\nAnd in its implementation we remove the current behaviors and prepare for layout with the new items.\n\n```objc\n- (void)resetLayout {\n    [self.dynamicAnimator removeAllBehaviors];\n    [self prepareLayout];\n}\n```\n\nAnd in your collection view, call the new method after you reload your data.\n\n```objc\n[self.collectionView reloadData];\n[(ASHSpringyCollectionViewFlowLayout *)[self collectionViewLayout] resetLayout];\n```\n    \nPlease note that the code above for adding rows is a naïve implementation that wouldn't work with the tiling mechanism outlined below to improve performance.\n\n\n## Tiling your Dynamic Behaviors for Performance\n\nA few hundred cells is all well and good, but what happens when your collection view data source exceeds that size? Or what if you can't predict exactly how large your data source will grow at runtime? Our naïve approach breaks down.\n\nInstead of loading *all* of our items in prepareLayout, it would be nice if we could be *smarter* about which items we load. Say, just the items that are visible or are about to become visible. That's exactly the approach that we're going to take. \n\nThe first thing we need to do is keep track of all of the index paths that are currently represented by behaviors' items in the dynamic animator. We'll add a property to our collection view layout to do this:\n\n```objc\n@property (nonatomic, strong) NSMutableSet *visibleIndexPathsSet;\n```\n\nWe're using a set because it features constant-time lookup for testing inclusion, and we'll be testing for inclusion *a lot*. \n\nBefore we dive into a whole new prepareLayout method – one that tiles behaviors – it's important to understand what tiling means. When we tile behaviors, we're removing behaviors as their items leave the visible bounds of the collection view and adding behaviors as their items enter the visible bounds. There's a big challenge though: when we create new behaviors, we need to create them *in flight*. That means creating them as though they were already in the dynamic animator and being modified by the `shouldInvalidateLayoutForBoundsChange:` method. \n\nSince we're creating these new behaviors in flight, we need to maintain some state of our current collection view. In particular, we need to keep track of the latest delta in our bounds change. This state will be used to create our behaviors in flight:\n\n```objc\n@property (nonatomic, assign) CGFloat latestDelta;\n```\n\nAfter adding this property, we'll add the following line to our `shouldInvalidateLayoutForBoundsChange:` method:\n\n```objc\nself.latestDelta = delta;\n```\n\nThat's all we need to modify to our method that responds to scrolling events. Our two methods for relaying queries about the layout of items in the collection view to the dynamic animator remain completely unchanged. Actually, most of the time, when backing your collection view with a dynamic animator, you'll have `layoutAttributesForElementsInRect:` and `layoutAttributesForItemAtIndexPath:` implemented the way we have them above. \n\nThe most complicated bit is now the tiling mechanism. We're going to completely rewrite our prepareLayout. \n\nThe first step of this method is going to be to remove certain behaviors from the dynamic animator where those behaviors represent items whose index paths are no longer on screen. The second step is to add new behaviors for items that are *becoming* visible. Let's take a look at the first step. \n\nLike before, we're going to call `[super prepareLayout]` so that we can rely on the layout information provided by `UICollectionViewFlowLayout`, our superclass. Also like before, we're going to be querying our superclass for the layout attributes for the elements in a rect. The difference is that instead of asking about attributes for elements in the *entire collection view*, we're going to only query about elements in the *visible rect*. \n\nSo we need to calculate the visible rect. But not so fast! There's one thing to keep in mind. Our user might scroll the collection view too fast for the dynamic animator to keep up, so we need to expand the visible rect slightly so that we're including items that are *about* to become visible. Otherwise, flickering could appear when scrolling quickly. Let's calculate our visible rect: \n\n```objc\nCGRect originalRect = (CGRect){.origin = self.collectionView.bounds.origin, .size = self.collectionView.frame.size};\nCGRect visibleRect = CGRectInset(originalRect, -100, -100);\n```\n\nI determined that insetting the actual visible rect by -100 points in both directions works for my demo. Double-check these values for your collection view, especially if your cells are really small. \n\nNext we need to collect the collection view layout attributes which lie within the visible rect. Let's also collect their index paths: \n\n```objc\nNSArray *itemsInVisibleRectArray = [super layoutAttributesForElementsInRect:visibleRect];\n\nNSSet *itemsIndexPathsInVisibleRectSet = [NSSet setWithArray:[itemsInVisibleRectArray valueForKey:@\"indexPath\"]];\n```\n\nNotice that we're using an NSSet. That's because we're going to be testing for inclusion within that set and we want constant-time lookup: \n\nWhat we're going to do is iterate over our dynamic animator's behaviors and filter out the ones that represent items that are in our `itemsIndexPathsInVisibleRectSet`. Once we've filtered our behaviors, we'll iterate over the ones that are no longer visible and remove those behaviors from the animator (along with the index paths from the `visibleIndexPathsSet` property):\n\n```objc\nNSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(UIAttachmentBehavior *behaviour, NSDictionary *bindings) {\n    BOOL currentlyVisible = [itemsIndexPathsInVisibleRectSet member:[[[behaviour items] firstObject] indexPath]] != nil;\n    return !currentlyVisible;\n}]\nNSArray *noLongerVisibleBehaviours = [self.dynamicAnimator.behaviors filteredArrayUsingPredicate:predicate];\n\n[noLongerVisibleBehaviours enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {\n    [self.dynamicAnimator removeBehavior:obj];\n    [self.visibleIndexPathsSet removeObject:[[[obj items] firstObject] indexPath]];\n}];\n```\n\nThe next step is to calculate a list of `UICollectionViewLayoutAttributes` that are *newly* visible – that is, ones whose index paths are in `itemsIndexPathsInVisibleRectSet` but not in our property `visibleIndexPathsSet`:\n\n```objc\nNSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *item, NSDictionary *bindings) {\n    BOOL currentlyVisible = [self.visibleIndexPathsSet member:item.indexPath] != nil;\n    return !currentlyVisible;\n}];\nNSArray *newlyVisibleItems = [itemsInVisibleRectArray filteredArrayUsingPredicate:predicate];\n```\n\nOnce we have our newly visible layout attributes, we can iterate over them to create our new behaviors and add their index paths to our `visibleIndexPathsSet` property. First, however, we'll need to grab the touch location of our user's finger. If it's CGPointZero, then we know that the user isn't scrolling the collection view and we can *assume* that we don't have to create new behaviors in flight: \n\n```objc\nCGPoint touchLocation = [self.collectionView.panGestureRecognizer locationInView:self.collectionView];\n```\n\nThis is a potentially dangerous assumption. What if the user has scrolled the collection view quickly and released his or her finger? The collection view would still be scrolling but our method wouldn't create the new behaviors in flight. Luckily, that also means that the scroll view is scrolling too fast to notice! Huzzah! This might become a problem, however, for collection views using large cells. In this case, increase the bounds of your visible rect so you're loading more items. \n\nNow we need to enumerate our newly visible items and create new behaviors for them and add their index paths to our `visibleIndexPathsSet` property. We'll also need to do some [math](http://www.youtube.com/watch?v=gENVB6tjq_M) to create the behavior in flight:\n\n```objc\n[newlyVisibleItems enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes *item, NSUInteger idx, BOOL *stop) {\n    CGPoint center = item.center;\n    UIAttachmentBehavior *springBehaviour = [[UIAttachmentBehavior alloc] initWithItem:item attachedToAnchor:center];\n    \n    springBehaviour.length = 0.0f;\n    springBehaviour.damping = 0.8f;\n    springBehaviour.frequency = 1.0f;\n    \n    if (!CGPointEqualToPoint(CGPointZero, touchLocation)) {\n        CGFloat yDistanceFromTouch = fabsf(touchLocation.y - springBehaviour.anchorPoint.y);\n        CGFloat xDistanceFromTouch = fabsf(touchLocation.x - springBehaviour.anchorPoint.x);\n        CGFloat scrollResistance = (yDistanceFromTouch + xDistanceFromTouch) / 1500.0f;\n        \n        if (self.latestDelta < 0) {\n            center.y += MAX(self.latestDelta, self.latestDelta*scrollResistance);\n        }\n        else {\n            center.y += MIN(self.latestDelta, self.latestDelta*scrollResistance);\n        }\n        item.center = center;\n    }\n    \n    [self.dynamicAnimator addBehavior:springBehaviour];\n    [self.visibleIndexPathsSet addObject:item.indexPath];\n}];\n```\n\nA lot of this code should look familiar. About half of it is from our naïve implementation of prepareLayout without tiling. The other half is from our `shouldInvalidateLayoutForBoundsChange:` method. We use our latestDelta property in lieu of a calculated delta from a bounds change and adjust the center point of our `UICollectionViewLayoutAttributes` appropriately so that the cell it represents will be \"pulled\" by the attachment behavior. \n\nAnd that's it. Really! I've tested this on a device displaying ten thousand cells and it works perfectly. Go [give it a shot](https://github.com/objcio/issue-5-springy-collection-view). \n\n## Beyond Flow Layouts\n\nAs usual, when working with `UICollectionView`, it's easier to subclass `UICollectionViewFlowLayout` rather than `UICollectionViewLayout` itself. This is because *flow* layouts will do a lot of the work for us. However, flow layouts are restricted to line-based, breaking layouts. What if you have a layout that doesn't fit that criteria? Well, if you've already tried fitting it into a `UICollectionViewFlowLayout` and you're sure that won't work, then it's time to break out the heavy-duty `UICollectionViewLayout` subclass. \n\nThis is true when dealing with UIKit Dynamics as well. \n\nLet's subclass `UICollectionViewLayout`. It's very important to implement `collectionViewContentSize` when subclassing `UICollectionViewLayout`. Otherwise the collection view won't have any idea how to display itself and nothing will be displayed at all. Since we want our collection view not to scroll at all, we'll return our collection view's frame's size, minus its contentInset.top component:\n\n```objc\n-(CGSize)collectionViewContentSize \n{\n    return CGSizeMake(self.collectionView.frame.size.width, \n        self.collectionView.frame.size.height - self.collectionView.contentInset.top);\n}\n```\n\nIn this (somewhat pedagogical) example, our collection view *always begins* with zero cells and items are added via `performBatchUpdates:`. That means that we have to use the `-[UICollectionViewLayout prepareForCollectionViewUpdates:]` method to add our behaviors (i.e. the collection view data source always starts at zero).\n\nInstead of just adding an attachment behavior for each individual item, we'll also maintain two other behaviors: gravity and collision. For each item we add to the collection view, we'll have to add these items to our collision and attachment behaviors. The final step is to set the item's initial position to somewhere offscreen so that it's pulled onscreen by the attachment behavior: \n\n```objc\n-(void)prepareForCollectionViewUpdates:(NSArray *)updateItems\n{\n    [super prepareForCollectionViewUpdates:updateItems];\n\n    [updateItems enumerateObjectsUsingBlock:^(UICollectionViewUpdateItem *updateItem, NSUInteger idx, BOOL *stop) {\n        if (updateItem.updateAction == UICollectionUpdateActionInsert) {\n            UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes \n                layoutAttributesForCellWithIndexPath:updateItem.indexPathAfterUpdate];\n        \n            attributes.frame = CGRectMake(CGRectGetMaxX(self.collectionView.frame) + kItemSize, 300, kItemSize, kItemSize);\n\n            UIAttachmentBehavior *attachmentBehaviour = [[UIAttachmentBehavior alloc] initWithItem:attributes \n                                                                                  attachedToAnchor:attachmentPoint];\n            attachmentBehaviour.length = 300.0f;\n            attachmentBehaviour.damping = 0.4f;\n            attachmentBehaviour.frequency = 1.0f;\n            [self.dynamicAnimator addBehavior:attachmentBehaviour];\n        \n            [self.gravityBehaviour addItem:attributes];\n            [self.collisionBehaviour addItem:attributes];\n        }\n    }];\n}\n```\n\n![Demo](/images/issue-5/newtonianCollectionView@2x.gif)\n\nDeletion is far more complicated. We want the item to \"fall off\" instead of simply disappearing. This involves more than just removing the cell from the collection view, as we want it to remain in the collection view until it moves offscreen. I've implemented something to that effect in the code, but it is a bit of a cheat. \n\nWhat we basically do is provide a method in the layout that removes the attachment behavior, then, after two seconds, removes the cell from the collection view. We're hoping that in that time, the cell can fall offscreen, but that's not necessarily going to happen. If it doesn't, that's OK. It'll just fade away. However, we also have to prevent new cells from being added and old cells from being deleted during this two-second interval. (I said it was a cheat.) \n\nPull requests welcome. \n\nThis approach is somewhat limited. I've capped the number of cells at ten, but even then the animation is slow on older hardware like the second-generation iPad. However, this example is supposed to be demonstrative of the approach you can take for interesting dynamics simulations – it's not meant to be a turn-key solution for any data set. The individual aspects of your simulation, including its performance, are up to you. \n\n\n\n\n\n"
  },
  {
    "path": "2013-10-08-editorial.md",
    "content": "---\r\ntitle:  \"Editorial\"\r\ncategory: \"5\"\r\ndate: \"2013-10-07 12:00:00\"\r\ntags: editorial\r\n---\r\n\r\nWelcome to objc.io issue #5!\r\n\r\nWe hope you're as excited by the launch of iOS 7 as we are. Choosing\r\nthis as our theme was a no-brainer; there's a lot of stuff we wanted to\r\ntalk about. As always, the list of potential topics was much greater\r\nthan what we could possibly write, so this is by no means a definitive\r\nguide to iOS 7. Instead, we picked some highlights and wrote about those.\r\n\r\nThis time, we have more guest writers than ever. Holger Riegel and Tobias Kreß explain the process they went through as they upgraded their app to iOS 7. One of the things that's worth considering when redesigning is the addition of UIDynamics, and Ash Furrow shows us how to use this in collection views. Chris talks about the new view controller transitions API, Mattt Thompson writes about moving from NSURLConnection to NSURLSession, and David shows us the new possibilities in multitasking. Max Seelemann covers one of the most game-changing features on the new iOS: TextKit. Finally, Peter Steinberger will guide us through his favorite new things in iOS 7.\r\n\r\nWe are still looking for more guest writers. We have a great number of\r\nideas for upcoming topics, and would be very happy to have you on board.\r\n\r\nAll the best from Berlin,\r\n\r\nChris, Daniel, and Florian.\r\n"
  },
  {
    "path": "2013-10-08-from-nsurlconnection-to-nsurlsession.md",
    "content": "---\ntitle:  \"From NSURLConnection to NSURLSession\"\ncategory: \"5\"\ndate: \"2013-10-07 08:00:00\"\ntags: article\nauthor:\n  - name: Mattt Thompson\n    url: http://twitter.com/mattt\n---\n\nOne of the more significant changes in iOS 7 and Mac OS X 10.9 Mavericks was the overhaul of the Foundation URL Loading System.\n\nAs someone [steeped in Apple's networking infrastructure](http://afnetworking.com), I thought it would be useful to share my thoughts and impressions of these new APIs, how they will change the way we build apps, and what they signify in terms of the evolution of API design philosophies.\n\n`NSURLConnection` got its start a decade ago, with the original release of Safari in 2003, as an abstraction on top of the Core Foundation / CFNetwork APIs. The name `NSURLConnection` actually refers to a group of the interrelated components that form the Foundation URL Loading System: `NSURLRequest`, `NSURLResponse`, `NSURLProtocol`, `NSURLCache`, `NSHTTPCookieStorage`, `NSURLCredentialStorage`, and its namesake, `NSURLConnection`.\n\n`NSURLRequest` objects are passed to an `NSURLConnection` object. The delegate (conforming to the erstwhile informal `<NSURLConnectionDelegate>` and `<NSURLConnectionDataDelegate>` protocols) responds asynchronously as an `NSURLResponse`, and any associated `NSData` are sent from the server. \n\nBefore a request is sent to the server, the shared cache is consulted, and depending on the policy and availability, a cached response may be returned immediately and transparently. If no cached response is available, the request is made with the option to cache its response for any subsequent requests.\n\nIn the process of negotiating a request to a server, that server may issue an authentication challenge, which is either handled automatically by the shared cookie or credential storage, or by the connection delegate. Outgoing requests could also be intercepted by a registered `NSURLProtocol` object to seamlessly change loading behavior as necessary.\n\nFor better or worse, `NSURLConnection` has served as the networking\ninfrastructure for hundreds of thousands of Cocoa and Cocoa Touch\napplications, and has held up rather well, considering. But over the\nyears, emerging use cases--on the iPhone and iPad, especially--have challenged several core assumptions, and created cause for refactoring.\n\nAt WWDC 2013, Apple unveiled the successor to `NSURLConnection`: `NSURLSession`.\n\n---\n\nLike `NSURLConnection`, `NSURLSession` refers to a group of interdependent classes, in addition to the eponymous class `NSURLSession`. `NSURLSession` is comprised of the same pieces as before, with `NSURLRequest`, `NSURLCache`, and the like, but replaces `NSURLConnection` with `NSURLSession`, `NSURLSessionConfiguration`, and three subclasses of `NSURLSessionTask`: `NSURLSessionDataTask`, `NSURLSessionUploadTask`, and `NSURLSessionDownloadTask`.\n\nThe most immediate improvement `NSURLSession` provides over `NSURLConnection` is the ability to configure per-session cache, protocol, cookie, and credential policies, rather than sharing them across the app. This allows the networking infrastructure of frameworks and parts of the app to operate independently, without interfering with one another. Each `NSURLSession` object is initialized with an `NSURLSessionConfiguration`, which specifies these policies, as well a number of new options specifically added to improve performance on mobile devices.\n\nThe other big part of `NSURLSession` is session tasks, which handle the loading of data, as well as uploading and downloading files and data between the client and server. `NSURLSessionTask` is most analogous to `NSURLConnection` in that it is responsible for loading data, with the main difference being that tasks share the common delegate of their parent `NSURLSession`.\n\nWe'll dive into tasks first, and then talk more about session configuration later on.\n\n## NSURLSessionTask\n\n`NSURLSessionTask` is an abstract subclass, with three concrete subclasses that are used directly: `NSURLSessionDataTask`, `NSURLSessionUploadTask`, and `NSURLSessionDownloadTask`. These three classes encapsulate the three essential networking tasks of modern applications: fetching data, such as JSON or XML, and uploading and downloading files.\n\n![NSURLSessionTask class diagram](/images/issue-5/NSURLSession.png)\n\nWhen an `NSURLSessionDataTask` finishes, it has associated data, whereas an `NSURLSessionDownloadTask` finishes with a temporary file path for the downloaded file. `NSURLSessionUploadTask` inherits from `NSURLSessionDataTask`, since the server response of an upload often has associated data. \n￼￼￼\nAll tasks are cancelable, and can be paused and resumed. When a download task is canceled, it has the option to create _resume data_, which can then be passed when creating a new download task to pick up where it left off.\n\nRather than being `alloc-init`'d directly, tasks are created by an `NSURLSession`. Each task constructor method has a version with and without a `completionHandler` property, for example `–dataTaskWithRequest:`\nand `–dataTaskWithRequest:completionHandler:`. Similar to `NSURLConnection -sendAsynchronousRequest:queue:completionHandler:`, specifying a `completionHandler` creates an implicit delegate to be used instead of the task's session. For any cases where a session task delegate's default behavior needs to be overridden, the less convenient non-`completionHandler` variant would need to be used.\n\n### Constructors\n\nIn iOS 5, `NSURLConnection` added the method `sendAsynchronousRequest:queue:completionHandler:`, which greatly simplified its use for one-off requests, and offered an asynchronous alternative to `-sendSynchronousRequest:returningResponse:error:`:\n\n```objc\n NSURL *URL = [NSURL URLWithString:@\"http://example.com\"];\n NSURLRequest *request = [NSURLRequest requestWithURL:URL];\n \n [NSURLConnection sendAsynchronousRequest:request\n                                    queue:[NSOperationQueue mainQueue]\n                        completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {\n     // ...\n }];\n```\n\n`NSURLSession` iterates on this pattern with its task constructor methods. Rather than running immediately, the task object is returned to allow for further configuration before being kicked off with `-resume`.\n\nData tasks can be created with either an `NSURL` or `NSURLRequest` (the former being a shortcut for a standard `GET` request to that URL):\n\n```objc\n NSURL *URL = [NSURL URLWithString:@\"http://example.com\"];\n NSURLRequest *request = [NSURLRequest requestWithURL:URL];\n \n NSURLSession *session = [NSURLSession sharedSession];\n NSURLSessionDataTask *task = [session dataTaskWithRequest:request\n                                         completionHandler:\n     ^(NSData *data, NSURLResponse *response, NSError *error) {\n         // ...\n     }];\n     \n [task resume];\n```\n\nUpload tasks can also be created with a request and either an `NSData` object or a URL to a local file to upload:\n\n```objc\n NSURL *URL = [NSURL URLWithString:@\"http://example.com/upload\"];\n NSURLRequest *request = [NSURLRequest requestWithURL:URL];\n NSData *data = ...;\n \n NSURLSession *session = [NSURLSession sharedSession];\n NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request\n                                                            fromData:data\n                                                   completionHandler:\n     ^(NSData *data, NSURLResponse *response, NSError *error) {\n         // ...\n     }];\n     \n [uploadTask resume];\n```\n\nDownload requests take a request as well, but differ in their `completionHandler`. Rather than being returned all at once upon completion, as data and upload tasks, download tasks have their data written to a local temp file. It's the responsibility of the completion handler to move the file from its temporary location to a permanent location.\n\n```objc\n NSURL *URL = [NSURL URLWithString:@\"http://example.com/file.zip\"];\n NSURLRequest *request = [NSURLRequest requestWithURL:URL];\n \n NSURLSession *session = [NSURLSession sharedSession];\n NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request\n                                                         completionHandler:\n    ^(NSURL *location, NSURLResponse *response, NSError *error) {\n        NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];\n        NSURL *documentsDirectoryURL = [NSURL fileURLWithPath:documentsPath];\n        NSURL *documentURL = [documentsDirectoryURL URLByAppendingPathComponent:[response \nsuggestedFilename]];\n        [[NSFileManager defaultManager] moveItemAtURL:location\n                                                toURL:documentURL\n                                                error:nil];\n }];\n\n [downloadTask resume];\n```\n\n### NSURLSession & NSURLConnection Delegate Methods\n\nOverall, the delegate methods of `NSURLSession` are a marked improvement over the rather ad-hoc pattern that emerged over the decade of `NSURLConnection`'s evolution. For a complete overview, check out this [mapping table](https://gist.github.com/floriankugler/6870499).\n\nA few specific observations:\n\nNSURLSession has both session and task delegate methods for handling authentication challenge. The session delegate method handles connection level concerns, such as Server Trust and Client Certificate evaluation, NTLM, and Kerberos, while the task delegate handles request-based challenges, such as Basic, Digest, or Proxy authentication.\n\nWhereas `NSURLConnection` has two methods that signal that a request has finished (`NSURLConnectionDataDelegate -connectionDidFinishLoading:` and `NSURLConnectionDelegate -connection:didFailWithError:`), there is a single delegate method for `NSURLSession` (`NSURLSessionTaskDelegate -URLSession:task:didCompleteWithError:`)\n\nDelegate methods signaling the transfer of a certain number of bytes use parameters with the `int64_t` type in `NSURLSession`, as compared to `long long` used by `NSURLConnection`.\n\nNSURLSession introduces a new pattern to Foundation delegate methods with its use of `completionHandler:` parameters. This allows delegate methods to safely be run on the main thread without blocking; a delegate can simply `dispatch_async` to the background, and call the `completionHandler` when finished. It also effectively allows for multiple return values, without awkward argument pointers. In the case of `NSURLSessionTaskDelegate -URLSession:task:didReceiveChallenge:completionHandler:`, for example, `completionHandler` takes two arguments: the authentication challenge disposition, and the credential to be used, if applicable.\n\n> For more information about session tasks, check out [WWDC Session 705: \"What’s New in Foundation Networking\"](http://asciiwwdc.com/2013/sessions/705)\n\n## NSURLSessionConfiguration\n\n`NSURLSessionConfiguration` objects are used to initialize `NSURLSession` objects. Expanding on the options available on the request level with `NSMutableURLRequest`, `NSURLSessionConfiguration` provides a considerable amount of control and flexibility on how a session makes requests. From network access properties, to cookie, security, and caching policies, as well as custom protocols, launch event settings, and several new properties for mobile optimization, you'll find what you're looking for with `NSURLSessionConfiguration`.\n\nSessions copy their configuration on initialization, and though `NSURLSession` has a `readonly` `configuration` property, changes made on that object have no effect on the policies of the session. Configuration is read once on initialization, and set in stone after that.\n\n### Constructors\n\nThere are three class constructors for `NSURLSessionConfiguration`, which do well to illustrate the different use cases for which `NSURLSession` is designed.\n\n`+defaultSessionConfiguration` returns the standard configuration, which is effectively the same as the `NSURLConnection` networking stack, with the same shared `NSHTTPCookieStorage`, shared `NSURLCache`, and shared `NSURLCredentialStorage`.\n\n`+ephemeralSessionConfiguration` returns a configuration preset with no persistent storage for caches, cookies, or credentials. This would be ideal for a feature like private browsing.\n\n`+backgroundSessionConfiguration:` is unique in that it creates a _background session_. Background sessions differ from regular, run-of-the-mill sessions in that they can run upload and download tasks even when the app is suspended, exits, or crashes. The identifier specified during initialization is used to provide context to any daemons that may resume background transfers out of process.\n\n> For more information about background sessions, check out [WWDC Session 204: \"What's New with Multitasking\"](http://asciiwwdc.com/2013/sessions/204)\n\n### Properties\n\nThere are 20 properties on `NSURLSessionConfiguration`. Having a working knowledge of what they are will allow apps to make the most of its networking environments.\n\n#### General\n\n`HTTPAdditionalHeaders` specifies a set of default headers to be set on outbound requests. This is useful for information that is shared across a session, such as content type, language, user agent, and authentication:\n\n```objc\nNSString *userPasswordString = [NSString stringWithFormat:@\"%@:%@\", user, password];\nNSData * userPasswordData = [userPasswordString dataUsingEncoding:NSUTF8StringEncoding];\nNSString *base64EncodedCredential = [userPasswordData base64EncodedStringWithOptions:0];\nNSString *authString = [NSString stringWithFormat:@\"Basic %@\", base64EncodedCredential];\nNSString *userAgentString = @\"AppName/com.example.app (iPhone 5s; iOS 7.0.2; Scale/2.0)\";\n\nconfiguration.HTTPAdditionalHeaders = @{@\"Accept\": @\"application/json\",\n                                        @\"Accept-Language\": @\"en\",\n                                        @\"Authorization\": authString,\n                                        @\"User-Agent\": userAgentString};\n```\n\n`networkServiceType` distinguishes between standard network traffic, VOIP, voice, video, and traffic used by a background process. Most applications won't need to set this.\n\n `allowsCellularAccess` and `discretionary` are used to save bandwidth over cellular connections. It is recommended that the `discretionary` property is used instead of `allowsCellularAccess` for background transfers, as it takes WiFi and power availability into account.\n\n`timeoutIntervalForRequest` and `timeoutIntervalForResource` specify the timeout interval for the request as well as the resource. Many developers have used the `timeoutInterval` in an attempt to limit the total amount of time spent making the request, rather than what it actually represents: the amount of time between packets. `timeoutIntervalForResource` actually provides that overall timeout, which should only really be used for background transfers, rather than anything a user might actually want to wait for.\n\n`HTTPMaximumConnectionsPerHost` is a new configuration option for the Foundation URL Loading System. It used to be that `NSURLConnection` would manage a private connection pool. Now with `NSURLSession`, developers can limit that number of concurrent connections to a particular host, should the need arise. \n\n`HTTPShouldUsePipelining` can be found on `NSMutableURLRequest` as well, and can be used to turn on [HTTP pipelining](http://en.wikipedia.org/wiki/HTTP_pipelining), which can dramatically reduce loading times of requests, but is not widely supported by servers, and is disabled by default.\n\n`sessionSendsLaunchEvents` is another new property that specifies whether the session should be launched from the background.\n\n`connectionProxyDictionary` specifies the proxies used by connections in the sessions. Again, most consumer-facing applications don't deal with proxies, so it's unlikely that this property would need to be configured.\n\n> Additional information about connection proxies can be found in the [`CFProxySupport` Reference](https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFProxySupport/Reference/reference.html).\n\n#### Cookie Policies\n\n`HTTPCookieStorage` is the cookie storage used by the session. By default, `NSHTTPCookieStorage +sharedHTTPCookieStorage` is used, which is the same as `NSURLConnection`.\n\n`HTTPCookieAcceptPolicy` determines the conditions in which the session should accept cookies sent from the server.\n\n`HTTPShouldSetCookies` specifies whether requests should use cookies from the session `HTTPCookieStorage`.\n\n#### Security Policies\n\n`URLCredentialStorage` is the credential storage used by the session. By default, `NSURLCredentialStorage +sharedCredentialStorage` is used, which is the same as `NSURLConnection`.\n\n`TLSMaximumSupportedProtocol` and `TLSMinimumSupportedProtocol` determine the supported `SSLProtocol` versions for the session. \n\n#### Caching Policies\n\n`URLCache` is the cache used by the session. By default, `NSURLCache +sharedURLCache` is used, which is the same as `NSURLConnection`.\n\n`requestCachePolicy` specifies when a cached response should be returned for a request. This is equivalent to `NSURLRequest -cachePolicy`.\n\n#### Custom Protocols\n\n`protocolClasses` is a session-specific array of registered `NSURLProtocol` classes.\n\n## Conclusion\n\nThe changes to the URL Loading System in iOS 7 and Mac OS X 10.9 Mavericks are a thoughtful and natural evolution of `NSURLConnection`. Overall, the Foundation Team did an amazing job of identifying and anticipating the existing and emerging use cases of mobile developers, by creating genuinely useful APIs that lend themselves well to everyday tasks.\n\nWhile certain decisions in the architecture of session tasks are a step backward in terms of composability and extensibility, `NSURLSession` nonetheless serves as a great foundation for higher-level networking functionality.\n"
  },
  {
    "path": "2013-10-08-getting-to-know-textkit.md",
    "content": "---\ntitle:  \"Getting to Know TextKit\"\ncategory: \"5\"\ndate: \"2013-10-07 11:00:00\"\ntags: article\nauthor:\n  - name: Max Seelemann\n    url: http://twitter.com/macguru17\n---\n\n\nThe release of iOS 7 brings a lot of new tools to the table for developers. One of these is *TextKit*. TextKit consists of a bunch of new classes in UIKit that, as the name suggests, somehow deal with text. Here, we will cover how TextKit came to be, what it’s all about, and — by means of a couple of examples — how developers can put it to great use.\n\nBut let’s have some perspective first: TextKit is probably *the* most significant recent addition to UIKit. iOS 7’s new interface replaces a lot of icons and bezels with plain-text buttons. Overall, text and text layout play a much more significant role in all visual aspects of the OS. It is perhaps no overstatement to say that iOS 7’s redesign is driven by text — text that is all handled by TextKit.\n\nTo give an idea of how big this change really is: in every version of iOS prior to 7, (almost) all text was handled by WebKit. That’s right: WebKit, the web browser engine. All `UILabel`s, `UITextField`s, and `UITextView`s were using web views in the background in some way to lay out and render text. For the new interface style, they have all been reengineered to take advantage of TextKit.\n\n\n## A Short History of Text on iOS\n\nThese new classes are no replacement for something that was previously available to developers. What TextKit does is absolutely new to the SDK. Before iOS 7, all the things TextKit does now would have to be done manually. It is the missing link between already existing functionalities.\n\nFor a long time, there was a framework for bare bones text layout and rendering: *CoreText*. There also was a way to directly grab a user’s input from the keyboard: the `UITextInput` protocol. In iOS 6, there even was a way of getting the system’s text selection for almost free: by subclassing `UITextView`.\n\n(This is probably the point where I should disclose my 10 years of experience in shipping text editors.) There is a huge (read: HUGE) gap between rendering text and grabbing keyboard input. This gap is probably also the reason why there always were so few rich-text or syntax-highlighting editors — getting a text editor right was, without doubt, a couple of months' worth of work.\n\nSo here it goes - a short rundown of the (not so) short history of text on iOS:\n\n**iOS 2**: The first public SDK includes a simple text display component (`UILabel`), a simple text input component (`UITextField`), and a simple, scrollable, editable component for larger amounts of text: `UITextView`. These are all plain text only, have no selection support (just insertion points), and allow almost no customization beyond setting a font and a text color.\n\n**iOS 3**: New features are copy and paste and — as a requirement for these — also text selection. Data detectors introduce a way to highlight phone numbers and URLs in text views. However, there is still essentially nothing a developer could influence beyond turning these features off or on.\n\n**iOS 3.2**: The introduction of the iPad brings CoreText, the\naforementioned low-level text layout and rendering engine (which was ported from Mac OS X 10.5), and `UITextInput`, the also-mentioned keyboard access. Apple demos Pages as the new light tower of text editing on mobile devices[^1]. However, due to the framework gap I talked about earlier, only very few apps follow suit.\n\n**iOS 4**: Announced only months after iOS 3.2, there is nothing new to text.\n_(Anecdote: Me at WWDC, I walk up to engineers, tell them I want the fully fledged text layout system on iOS. The answer: “Yeah… File a radar.” Not unexpected…)_\n    \n**iOS 5**: No news regarding text.\n_(Anecdote: Me at WWDC, I tell engineers about a text system on iOS. The answer: “We don’t see a lot of requests for that…” Doh!)_\n    \n**iOS 6**: Some movement: Attributed text editing comes to `UITextView`. It is, unfortunately, hardly customizable. The default UI does bold, italic, and underline. Users can set font sizes and colors. While this is great at first sight, there is still no control over layout or a convenient way to customize text attributes. For (text-editing) developers however, there is a big new feature: it’s now possible to subclass `UITextView` to get, in addition to the keyboard input that was previously available, text selection “for free.” Having to implement a completely custom text selection has probably put most previous attempts of non-plain-text tools to a halt.\n_(Anecdote: Me, WWDC, engineers. I want a text system on iOS. Answer: “Uhhhm. Ehm. Yes. Maybe? See, it just doesn’t perform…” So there is hope after all, isn’t there?)_\n    \n**iOS 7**: Finally. TextKit.\n\n\n## Features\n\nSo here we are. iOS 7-land with TextKit. Let’s see what it can do! Before we dive into it I still want to mention that, strictly speaking, most of these things were *possible* before. If you had plenty of resources and time to build a text engine on top of CoreText, these were all doable. But if you wanted to build a fully fledged rich text editor before, this could mean *months* of work. Now it’s as easy as opening an interface file in Xcode and dropping a `UITextView` into your view controller to get all these features:\n\n**Kerning**: Drop the idea that all characters have simple quadratic shapes and that these shapes must be placed exactly adjacent to each other. Modern text layout takes into account that, for example, a capital letter “T” does have a lot of free space under its “wings” and moves the following lowercase letters closer. This results in significantly improved legibility of text, especially in longer pieces of writing.\n\n![Kerning: the bounding box of the letter “a” (blue rect) clearly overlapp the capital “T” when kerning is enabled.](/images/issue-5/kerning.png)\n    \n**Ligatures**: I consider this mostly an artistic feature, but some texts do look nicer (more artistic) when certain character combinations (like an “f” followed by an “l”) are drawn using combined symbols (so-called glyphs). \n    \n![Ligatures: the “Futura” font family contains special symbols for character combinations like “fl”.](/images/issue-5/ligature.png)\n    \n**Image Attachments**: It is now possible to have images inside a text view.\n\n**Hyphenation**: Not so important for editing text, but for presenting text in a nice and readable way. Hyphenation means splitting longer words at line boundaries, creating a more homogeneous flow and look of the overall text.\n_Anecdote:_ Before iOS 7, developers had to employ CoreText directly. Like so: Start by detecting the text language on a per-sentence basis, then get the possible hyphenation point for each word in the sentence, then insert a custom hyphenation placeholder character at each suggested point. After preparation is done, run CoreText’s layout and manually insert a hyphen into wrapped lines. If you want great results, check afterward if the text with the hyphen still fits into the line’s boundaries and if it doesn’t, re-run the lines’s layout without the previously used hyphenation point.\nWith TextKit, enabling hyphenation is as easy as setting the `hyphenationFactor` property.\n    \n![The text in this view would have looked much more compartmentalized without hyphenation.](/images/issue-5/Screen%20Shot%202013-09-29%20at%2022.19.58.png)\n\n**Customizability**: For me, even more than the improved typesetting, this is *the* new feature. Before, developers had the choice between using what was there or rewriting it all from scratch on their own. Now there is a set of classes that have delegate protocols or can be overwritten to change *part* of their behavior. For example, you can now influence the line break behavior of certain words without having to re-write the complete text component. I consider this a win.\n\n**More Rich Text Attributes**: It is now possible to set different underline styles (double, thick, dashed, dotted, or a combination thereof). It is very easy to shift the text baseline, for example, for doing superscript numbers. Also, developers no longer have to draw background colors for custom rendered text on their own (CoreText does not have support for these).\n\n**Serialization**: Previously, there was no built-in way to read strings with text attributes from disk. Or to write it out again. Now there is.\n\n**Text Styles**: iOS 7’s interface introduces a new concept of globally predefined types of text. These types of text are assigned a globally predefined look. Ideally this will result in headings and continuous text looking the same all over the system. Users can define their reading habits (like text size) from the Preferences app, and apps that use text styles will automatically have the right text size and look.\n\n**Text Effects**: Last and least. In iOS 7 there is exactly one text effect: Letterpress. Text with this effect will look like it was physically stamped into a sheet of paper. Inner shadows, etcetera.\n_Opinion: Really? What the…? In an OS that completely, radically, and unforgivably kills useless [skeuomorphism][1], who needs the look of text-stamped into paper?_\n\n## Structure\n\nThe best way to get an overview of a system is probably to draw an image. Here is a schematic of UIKit’s text system, TextKit:\n\n![The structure of all essential TextKit classes. Highlighted with a “New” badge are classes introduced in iOS 7](/images/issue-5/TextKit.png)\n\nAs can be seen from the picture, putting a text engine to work requires a couple of actors. We will cover them starting from outside:\n\n**String**: Where there is text to be drawn, there must somewhere be a string to hold it. In the default configuration, the string is contained within and managed by the `NSTextStorage`, and in these cases, it may be left out from the drawing.\nBut that need not necessarily be the case. With TextKit, the text can originate from whatever source suits the use case. For a code editor, for example, the string could actually be an annotated syntax tree (AST) that contains all information about the structure of the displayed code. With a custom-built text storage, this text is then only later and dynamically enriched with text attributes like font or color highlights. For the first time, developers are able to directly use their own model for a text component. All that is needed is a specially engineered text storage. Which leads us to:\n    \n`NSTextStorage`: If you see the text system as model-view-controller (MVC) architecture, this class represents the model. A text storage is the central object that knows everything about the text and its attributes. It provides access to them through a mere two accessor methods and allows changing text and attributes through just two more methods. We will give them a closer look below.\nFor now, it’s important to understand that `NSTextStorage` inherits these methods from its superclass, `NSAttributedString`. This makes it clear that a text storage — as seen by the text system – is just a string with attributes, albeit with a few extensions. The only significant difference between the two is that a text storage contains a way to post notifications about all changes made to its contents. We will also cover that in a moment.\n    \n`UITextView`: On the opposite end of the stack is the actual view. In TextKit, the text view serves two purposes: For one, it is the view that is drawn into by the text system. The text view itself does *not* do any drawing on its own; it just provides a region that others draw to.\nAs the only component that is contained in the view hierarchy, the second purpose is to deal with all user interaction. Specifically, the text view implements the `UITextInput` protocol to handle keyboard events, and it provides a way for the user to set an insertion point or select text. It does not do any actual change to the text but merely forwards these changes to the just-discussed text storage.\n    \n`NSTextContainer`: Every text view defines a region that text can be drawn to. For this, each text view has a *text container* that precisely describes the area available. In simple cases, this is a vertical infinitely sizable rectangular area. Text is then filled into the region and the text view enables the user to scroll through it.\nIn more advanced cases however, this region may be an finitely large rectangle. For example, when rendering a book, each page has a maximum height and width. The text container would then define this size and not accept any text beyond. In the same case, an image might cover parts of the page and text should re-flow around its edges. This is also handled by the text container, as we will see in a example a little later.\n    \n`NSLayoutManager`: The layout manager is the central component that brings it all together:\n\n1. The manager listens to text or attribute change notifications in the text storage and, upon reception, triggers the layout process.\n2. Starting with the text provided by the text storage, it translates all characters into glyphs[^2].\n3. Once the glyphs have been generated, the manager consults its text container(s) for the available regions for text.\n4. These regions are then filled stepwise with lines, which again are filled stepwise with glyphs. Once a line has been filled, the next one is started.\n5. For each line, the layout manager must consider line-breaking behavior (the word not fitting must be moved to the next line), hyphenation, inline image attachments, and so forth.\n6. When layout is finished, the text view’s current display state is invalidated and the layout manager draws the previously set text *into* the text view.\n    \n**CoreText**: Not directly contained within TextKit, CoreText is the library that does the actual typesetting. For each of the layout manager’s steps, CoreText is consulted in one way or another. It provides the translation from characters to glyphs, fills line segments with them, and suggest hyphenation points.\n\n\n### Cocoa Text System\n\nBuilding a system as big and as complex as TextKit is certainly nothing done easily or quickly and definitely requires *a lot* of experience and expertise to be successful. The fact that a “real” text component has been missing from iOS for six subsequent major releases is also quite telling. Apple is definitely correct in selling it as a big new feature. But is it really new?\n\nHere’s a number: out of the [131 public classes in UIKit][3], all but nine have the `UI` prefix in their name. These nine classes carry the legacy, old-world (read: Mac OS) prefix `NS`. And out of these nine classes, seven deal with text. Coincidence? Well…\n\nHere’s a schematic of the Cocoa Text System. Feel free to compare it to the one for TextKit above.\n\n![The structure of all essential classes of the Cocoa Text System as present on Mac OS today.](/images/issue-5/CocoaTextSystem.png)\n\nThe similarity is staggering. It’s clear that, at least in huge parts, the two are the same. Obviously – with the exception of the right side with `NSTextView` and `UITextView` — the primary classes are all the same. TextKit is (at least a partial) port of Cocoa’s text system to iOS. _(The one I asked for in my anecdotes, yay!)_\n\nWhen giving it a closer look, there are some differences. Most notable of these are:\n\n- There are no `NSTypesetter` and no `NSGlyphGenerator` classes on iOS. While on Mac OS there is every imaginable way of customizing typesetting, these possibilities have been dramatically simplified. This allowed for the ridding of some abstractions and allowed the whole process to be merged into `NSLayoutManager`. What remains is a handful of delegate methods to alter text layout and line break behavior.\n- There are a couple of new, nice conveniences in the iOS variants of the classes. Excluding certain regions from a text container (see above) must be done by hand in Cocoa. The UIKit class, however, provides a simple `exclusionPaths` property.\n- Some examples of left-out functionality are support for inline tables and different attachments than images.\n\nAll-in-all the system is still the same, though. `NSTextStorage` is exactly the same class on both platforms and `NSLayoutManager`and `NSTextContainer` do not differ so significantly. The changes that were made seem to (in some cases dramatically) ease the use of the text system while not cutting too many special cases. I consider this a good thing.\n\n_Reviewing the answers I got from Apple engineers regarding porting the Cocoa Text System to iOS in hindsight reveals quite a bit of background information. The reason for the delay and the reduction in functionality is simple: performance, performance, performance. Text layout can be an extremely expensive task — memory-wise, power-wise, and time-wise — especially on a mobile device. Apple had to opt for a simpler solution and to wait for more processing power to be able to at least partially support a fully fledged text layout engine._\n\n\n## Examples\n\nTo illustrate what is possible with TextKit, I created a little demo project that [can be found on GitHub][4]. In this demo I did only things that were not easily possible before. I must admit that coding it up took only a Sunday morning; something similar would probably have taken me days or weeks before.\n\nTextKit consists of well over 100 methods, and is just to big to be covered exhaustively in one article. Besides the fact that, most of the time, all you need is just the one right method, TextKit’s usage and customizability are also still to be explored. So instead of doing one big demonstration that covers it all, I decided to do four smaller ones. In each one, I tried to show a different aspect and a different class for customization.\n\n\n### Demo 1: Configurations\n\nLet’s get started with an easy one: configuring the text system. As you may have seen from the TextKit figure above, the arrows between `NSTextStorage`, `NSLayoutManager`, and `NSTextContainer` are double-headed. With that, I tried to indicate these relationships are 1-to-N. That’s right: a text storage can hold multiple layout managers, and a layout manager can hold multiple text containers. These multiplicities enable great features:\n\n- Adding multiple layout managers on a single text storage results in two (or more) *visual representations of the same text* that can be shown side by side. Each of these representations can be placed and sized independently. If the corresponding text views are editable, all changes performed in either view are then immediately mapped to the other.\n- Adding multiple text containers to a single layout manager results in one representation of the text being *spread across multiple views*. One example where this is useful is a page-based layout: each page would contain a separate text view. The text containers of those views would, however, be referenced with a single layout manager which then spreads text across them.\n\nWhen instantiating a `UITextView` from a storyboard or an interface file, it will come with a text system preconfigured: one text storage, referencing one layout manager, referencing one text container. In the same way, a text system stack can be built directly from code:\n\n```objc\nNSTextStorage *textStorage = [NSTextStorage new];\n\nNSLayoutManager *layoutManager = [NSLayoutManager new];\n[textStorage addLayoutManager: layoutManager];\n\nNSTextContainer *textContainer = [NSTextContainer new];\n[layoutManager addTextContainer: textContainer];\n\nUITextView *textView = [[UITextView alloc] initWithFrame:someFrame \n                                           textContainer:textContainer];\n```\n\nThis is as straightforward as it can be. The only thing to remember when building a text system by hand is that your view controller must retain the text storage. The text view being at the end of the stack only weakly references the text storage and the layout manager. When the text storage is released, the layout manager is as well, leaving the text view with a disconnected container.\n\nThere is one exception to this rule. Only when instantiating a text view from an interface or a storyboard, the text view *does* retain the text storage. The framework applies some black magic to ensure all objects are retained without forming a retain cycle.\n\nWith that in mind, creating a more advanced setup is also pretty straightforward. Assume in a view that there is already a text view as instantiated from the nib, called `originalTextView`. Adding a second text view for the same text essentially means just copying the above code and reusing the text storage from the original view:\n\n```objc\nNSTextStorage *sharedTextStorage = originalTextView.textStorage;\n\nNSLayoutManager *otherLayoutManager = [NSLayoutManager new];\n[sharedTextStorage addLayoutManager: otherLayoutManager];\n\nNSTextContainer *otherTextContainer = [NSTextContainer new];\n[otherLayoutManager addTextContainer: otherTextContainer];\n\nUITextView *otherTextView = [[UITextView alloc] initWithFrame:someFrame \n                                                textContainer:otherTextContainer];\n```\n\nAdding a second text container to a layout manager works almost the same. Let’s say we wanted the text in the above example to fill *two* text views instead of just one. Easy:\n\n```objc\nNSTextContainer *thirdTextContainer = [NSTextContainer new];\n[otherLayoutManager addTextContainer: thirdTextContainer];\n\nUITextView *thirdTextView = [[UITextView alloc] initWithFrame:someFrame \n                                                textContainer:thirdTextContainer];\n```\n\nBut there is one caveat: Since the text container in the other text view resizes infinitely, the third one will never get any text. We must thus indicate that instead of resizing and scrolling, the text should reflow from one view to the other:\n\n```objc\notherTextView.scrollEnabled = NO;\n```\n\nUnfortunately, as it seems, adding multiple text containers to a single layout manager disables editing. If text should remain editable, only a single text container may be used per layout manager.\n\nFor a working example of this configuration see the “Configuration” tab in the aforementioned [TextKitDemo][4].\n\n\n### Demo 2: Syntax Highlighting\n\nWhile configuring text views is not *that* exciting, here comes something much more interesting: syntax highlighting!\n\nLooking at the distribution of responsiveness across the TextKit components, it’s clear syntax highlighting should be implemented in the text storage. Since `NSTextStorage` is a class cluster[^3], subclassing requires a little bit of work. The idea here is to build a composite object: Implement all methods by just forwarding them to a concrete instance, modifying `inout` parameters or results as wished.\n\n`NSTextStorage` inherits from `NSMutableAttributedString` and must implement the following four methods — two getters and two setters:\n\n```objc\n- (NSString *)string;\n- (NSDictionary *)attributesAtIndex:(NSUInteger)location \n                     effectiveRange:(NSRangePointer)range;\n- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str;\n- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;\n```\n\nThe implementation of a composite object of a class cluster subclass is also pretty straightforward. First, find the *simplest* class that fulfills all requirements. In our case this is `NSMutableAttributedString`, which we use as implementation of the custom storage:\n\n```objc\n@implementation TKDHighlightingTextStorage \n{\n    NSMutableAttributedString *_imp;\n}\n\n- (id)init\n{\n    self = [super init];\n    if (self) {\n        _imp = [NSMutableAttributedString new];\n    }\n    return self;\n}\n```\n\nWith the object in place, responding to the getters requires just a one-liner:\n\n```objc\n- (NSString *)string \n{\n    return _imp.string;\n}\n\n- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range \n{\n    return [_imp attributesAtIndex:location effectiveRange:range];\n}\n```\n\nResponding to the setters is also almost as simple. There is one catch, though: The text storage needs to notify its layout managers that a change happened. The setters must thus also invoke `-edited:range:changeInLegth:` and pass along a change description. Sounds worse that it turns out to be:\n\n```objc\n- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str \n{\n    [_imp replaceCharactersInRange:range withString:str];\n    [self edited:NSTextStorageEditedCharacters range:range \n                                      changeInLength:(NSInteger)str.length - (NSInteger)range.length];\n}\n\n- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range \n{\n    [_imp setAttributes:attrs range:range];\n    [self edited:NSTextStorageEditedAttributes range:range changeInLength:0];\n}\n```\n\nWith that, we have a fully functional replacement for the text storage in the text system stack. Plugging it into a text view as loaded from a interface file goes like this — but remember to reference the text storage from an instance variable:\n\n```objc\n_textStorage = [TKDHighlightingTextStorage new];\n[_textStorage addLayoutManager: self.textView.layoutManager];\n```\n\nSo far so good. We managed to plug in a custom text storage, and next we need to actually highlight some parts of the text. For now, a simple highlighting should suffice: We want to color all iWords red — words that start with a lowercase “i” followed by an uppercase letter.\n\nA convenient place to implement highlighting is to overwrite `-processEditing`. This method is automatically called after every change to the text storage. `NSTextStorage` uses this method to sanitize the string after an edit. When, for example, some characters would be invisible with the chosen font, the storage replaces them with a font that is capable of showing them.\n\nAs with everything else, adding the simple highlighting of iWords is straightforward. We override `-processEditing`, call `super`’s  implementation, and set up a regular expression for finding words:\n\n```objc\n- (void)processEditing \n{\n    [super processEditing];\n    \n    static NSRegularExpression *iExpression;\n    NSString *pattern = @\"i[\\\\p{Alphabetic}&&\\\\p{Uppercase}][\\\\p{Alphabetic}]+\";\n    iExpression = iExpression ?: [NSRegularExpression regularExpressionWithPattern:pattern \n                                                                           options:0 \n                                                                             error:NULL];\n```\n\nThen first, clean all previously assigned highlights:\n\n```objc\n    NSRange paragaphRange = [self.string paragraphRangeForRange: self.editedRange];\n    [self removeAttribute:NSForegroundColorAttributeName range:paragaphRange];\n```\n\nAnd second, iterate all pattern matches and assign new highlights:\n\n```objc\n    [iExpression enumerateMatchesInString:self.string \n                                  options:0 range:paragaphRange \n                               usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) \n    {\n        [self addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:result.range];\n    }];\n}\n```\n\nThat’s it. We built a dynamic text view with syntax highlighting. The highlighting will be applied *live* as the user types. And it only took a couple lines of code. How cool is that?\n\n![A screenshot from the TextKitDemo project showing the text view with iWords highlighted.](/images/issue-5/SyntaxHighlighting.png)\n\nPlease note that just using the *edited range* will not be sufficient. For example, when manually typing in iWords, the regexp would only match *after* the third character of a word has been entered. But then the `editedRange` covers just the third character and thus all processing would inspect this character only. By re-processing the complete paragraph, we’re on the safe side without giving up too much performance.\n\nFor a working example, see the “Highlighting” tab in the aforementioned [TextKitDemo][4].\n\n\n### Demo 3: Layout Modifications\n\nAs previously mentioned, the layout manager is the central layout workhorse. What is highly customizable in `NSTypesetter` on Mac OS has been merged into `NSLayoutManager` on iOS. While the complete customizability of the Cocoa Text System is not available in TextKit, a bunch of delegate methods allow some adjustments. As mentioned, TextKit is more tightly integrated with CoreText mostly due to performance reasons. But the philosophy is to some extent different between the two text systems:\n\n**Cocoa Text System**: On Mac OS, where performance is not a problem, the design is all about flexibility. Probably so: “This is the thing that does it. You can override if you want. You can adjust about everything. Performance does not matter. You may also supply your *completely* own character to glyph translation, just go ahead…”\n\n**TextKit**: Here is where performance seems to be a real issue. The philosophy (at least for now) is more in along the lines of: “We did it in a simple but performant way. Here is the result, but we give you the chance to alter some of it. You’re asked only at points that don’t hurt performance too hard, though.”\n\nEnough philosophy, let’s customize something. For example, how about adjusting line heights? Sounds crazy, but adjusting line heights has been [at least hacky or required using private API][8] on previous releases of iOS. Fortunately, this is — again — now a no-brainer. Set the layout manager’s delegate and implement a single method:\n\n```objc\n- (CGFloat)      layoutManager:(NSLayoutManager *)layoutManager \n  lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex \n  withProposedLineFragmentRect:(CGRect)rect \n{\n    return floorf(glyphIndex / 100);\n}\n```\n\nIn the above code, I changed the line spacing to increase with the text length. This results in lines on top being closer together than those at the bottom. Not particularly useful I admit, but it’s *possible* (and there surely are more practical use cases).\n\nOK, let’s have a more realistic scenario. Say you have links in a text and do not want these to be wrapped around lines. If possible, a URL should always appear as a whole in a single piece of text. Nothing could be simpler that that.\n\nWe start by using a custom text storage just like the one previously discussed. But instead of detecting iWords, it finds links and marks them as such:\n \n```objc\nstatic NSDataDetector *linkDetector;\nlinkDetector = linkDetector ?: [[NSDataDetector alloc] initWithTypes:NSTextCheckingTypeLink error:NULL];\n\nNSRange paragaphRange = [self.string paragraphRangeForRange: NSMakeRange(range.location, str.length)];\n[self removeAttribute:NSLinkAttributeName range:paragaphRange];\n\n[linkDetector enumerateMatchesInString:self.string \n                               options:0 \n                                 range:paragaphRange \n                            usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) \n{\n    [self addAttribute:NSLinkAttributeName value:result.URL range:result.range];\n}];\n```\n\nWith this at hand, changing the line break behavior is as easy as implementing a single layout manager delegate method:\n\n```objc\n- (BOOL)layoutManager:(NSLayoutManager *)layoutManager shouldBreakLineByWordBeforeCharacterAtIndex:(NSUInteger)charIndex \n{\n    NSRange range;\n    NSURL *linkURL = [layoutManager.textStorage attribute:NSLinkAttributeName \n                                                  atIndex:charIndex \n                                           effectiveRange:&range];\n    \n    return !(linkURL && charIndex > range.location && charIndex <= NSMaxRange(range));\n}\n```\n\nFor a working example, see the “Layout” tab in the aforementioned [TextKitDemo][4]. Here’s a screenshot:\n\n![A screenshot from the TextKitDemo project showing altered line break behavior for link URLs.](/images/issue-5/LineBreaking.png)\n\nBy the way, the green outline in the shot above is something usually not possible with TextKit. In the same demo, I’ve added a little trick for drawing an outline around text right from within a layout manager subclass. Extending TextKit’s drawing in special ways is also done quite easily. Be sure to check it out!\n\n\n### Demo 4: Text Interaction\n\nHaving covered `NSTextStorage` and `NSLayoutManager`, the last demo will play a little with `NSTextContainer`. This class is not very complex and it doesn’t do anything other than specifying where text may or may not be placed.\n\nNot placing text in some regions of a view is a common requirement, for example, in magazine apps. For this case, `NSTextContainer` on iOS provides a property Mac developers have long sought after: `exclusionPaths` allows developers to set an array of `NSBezierPath`s that should not be filled with text. To get an idea of what this is, take a look at the following screenshot:\n\n![A screenshot from the TextKitDemo project showing text revolving around an excluded oval view.](/images/issue-5/ReflowingTextAndClippy.png)\n\nAs you can see, all text is placed outside the blue shape. Getting this behavior into a text view is simple, but it has a small catch: The coordinates of the bezier path must be specified in container coordinates. Here is the conversion:\n\n```objc\n- (void)updateExclusionPaths \n{\n    CGRect ovalFrame = [self.textView convertRect:self.circleView.bounds \n                                         fromView:self.circleView];\n    \n    ovalFrame.origin.x -= self.textView.textContainerInset.left;\n    ovalFrame.origin.y -= self.textView.textContainerInset.top;\n    \n    UIBezierPath *ovalPath = [UIBezierPath bezierPathWithOvalInRect:ovalFrame];\n    self.textView.textContainer.exclusionPaths = @[ovalPath];\n}\n```\n\nIn this example, I am using a user-positionable view that can then be moved around freely while the text reflows live around the shape. We start by converting its bounds (`self.circleView.bounds`) to the coordinate system of the text view.\n\nBecause without an inset, text would glitch too close to the view borders, `UITextView` insets its text container by a few points. Thus, to get the exclusion path in container coordinates, the text container inset must be subtracted from the origin. \n\nAfter that, getting the exclusion to be applied is as easy as setting a bezier path on the text container. Everything else is transparently and automatically handled by TextKit for you.\n\nFor a working example, see the “Interaction” tab in the [TextKitDemo][4]. As a little gimmick, it also includes a view that follows the current text selection. Because, you know, what would a good text editor demo be without a little ugly annoying paper clip getting in your way?\n\n[^1]:   Pages did — according to Apple — use absolutely no private API. \\*cough\\*<br>\nMy theory: it either used an extremely early version of TextKit or copied half of UIKit’s private sources. Or a mix thereof.\n\n[^2]:   _Glyphs_: While characters are the “semantical” representation of a letter, glyphs are the visual representation thereof. Depending on the font used, glyphs are either bezier paths or bitmap images defining the shape that should be drawn. See also the excellent [Wikipedia article][2] about glyphs.\n\n[^3]:   In a class cluster, only an abstract superclass is public. Allocating an instance actually creates an object of a private subclass. As such, subclassing always happens on an abstract class and requires all methods to be implemented. See also the [class cluster documentation][6].\n\n[1]:    http://en.wikipedia.org/wiki/Skeuomorph \"Skeuomorphism on Wikipedia\"\n[2]:    http://en.wikipedia.org/wiki/Glyph \"Glyphs on Wikipedia\"\n[3]:    https://developer.apple.com/library/ios/documentation/uikit/reference/uikit_framework/index.html\n[4]:    https://github.com/objcio/issue-5-textkit \"TextKitDemo by macguru on GitHub\"\n[6]:    https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/ClassClusters/ClassClusters.html\n[8]:    http://stackoverflow.com/questions/3760924/set-line-height-in-uitextview/3914228\n\n[image-1]:  /images/issue-5/kerning.png\n[image-2]:  /images/issue-5/ligature.png\n[image-3]:  /images/issue-5/Screen%20Shot%202013-09-29%20at%2022.19.58.png\n[image-4]:  /images/issue-5/TextKit.png\n[image-5]:  /images/issue-5/CocoaTextSystem.png\n[image-6]:  /images/issue-5/SyntaxHighlighting.png\n[image-7]:  /images/issue-5/LineBreaking.png\n[image-8]:  /images/issue-5/ReflowingTextAndClippy.png\n"
  },
  {
    "path": "2013-10-08-iOS7-hidden-gems-and-workarounds.md",
    "content": "---\ntitle:  \"iOS 7: Hidden Gems and Workarounds\"\ncategory: \"5\"\ndate: \"2013-10-07 06:00:00\"\ntags: article\nauthor:\n  - name: Peter Steinberger\n    url: https://twitter.com/steipete\n---\n\nWhen iOS 7 was first announced, Apple developers all over the world tried to compile their apps, and spent the next few months fixing whatever was broken, or even rebuilding the app from scratch. As a result, there wasn't much time to take a closer look at what's new in iOS 7. Apart from the obvious great small tweaks like NSArray's `firstObject`, which has been retroactively made public all the way back to iOS 4, there are a lot more hidden gems waiting to be discovered.\n\n## Smooth Fade Animations\n\nI'm not talking about the new spring animation APIs or UIDynamics, but something more subtle. CALayer gained two new methods: `allowsGroupOpacity` and `allowsEdgeAntialiasing`. Now, group opacity isn't something particularly new. iOS was evaluating the `UIViewGroupOpacity` key in the Info.plist for quite some time and enabled/disabled this application-wide. For most apps, this was unwanted, as it decreases global performance. In iOS 7, this now is enabled by default for applications that link against SDK 7 - and since certain animations will be slower when this is enabled, it can also be controlled on the layer. \n\nAn interesting detail is that `_UIBackdropView` (which is used as the background view inside `UIToolbar` or `UIPopoverView`) can't animate its blur if `allowsGroupOpacity` is enabled, so you might want to temporarily disable this when you do an alpha transform. Since this degrades the animation experience, you can fall back to the old way and temporarily enable `shouldRasterize` during the animation. Don't forget setting the appropriate `rasterizationScale` or the view will look pixelerated on retina devices.\n\nThe edge antialiasing property can be useful if you want to replicate the animation that Safari does when showing all tabs.\n\n## Blocking Animations\n\nA small but very useful addition is `[UIView performWithoutAnimation:]`. It's a simple wrapper that checks if animations are currently enabled, disables them, executes the block, and re-enables animations. One caveat is that this will *not* block CoreAnimation-based animations. So don't be too eager in replacing all your calls from:\n\n```objc\n    [CATransaction begin];\n    [CATransaction setDisableActions:YES];\n    view.frame = CGRectMake(...);\n    [CATransaction commit];\n```\n\nto:\n\n```objc\n    [UIView performWithoutAnimation:^{\n        view.frame = CGRectMake(...);\n    }];\n```\n\nHowever, most of the time this will do the job just fine, as long as you don't deal with CALayers directly.\n\nIn iOS 7, I had quite a few code paths (mostly `UITableViewCells`) that needed additional protection against accidental animations, for example, if a popover is resized and at the same time the displayed table view loads up new cells because of the height change. My usual workaround is wrapping the entire `layoutSubviews` into the animation-block-method:\n\n```objc\n- (void)layoutSubviews \n{\n    // Otherwise the popover animation could leak into our cells on iOS 7 legacy mode.\n    [UIView performWithoutAnimation:^{\n        [super layoutSubviews];\n        _renderView.frame = self.bounds;\n    }];\n}\n```\n\n## Dealing with Long Table Views\n\n`UITableView` is very efficient and fast, unless you begin using `tableView:heightForRowAtIndexPath:`, where it starts calling this for *every* element in your table, even the non-visible ones - just so that the underlying `UIScrollView` can get the correct `contentSize`. There were some workarounds before, but nothing great. In iOS 7, Apple finally acknowledged the problem and added `tableView:estimatedHeightForRowAtIndexPath:`, which defers most of the cost down to actual scrolling time. If you don't know the size of a cell at all, simply return `UITableViewAutomaticDimension`.\n\nThere's now a similar API for section headers/footers as well.\n\n## UISearchDisplayController\n\nApple's search controller learned a new trick to simplify moving the search bar into the navigation bar. Enable `displaysSearchBarInNavigationBar` for that (unless you also use a scope bar - then you're out of luck.) Now I would love to write that that's it, but sadly, `UISearchDisplayController` seems to be terribly broken on iOS 7, especially on iPad. Apple seems to have run out of time, so showing the search results will not hide the actual table view. Before 7, this wasn't an issue, but since the `searchResultsTableView` has a transparent background color, it looks pretty bad. As a workaround, you can either set an opaque background color, or move to some [more sophisticated ways of hacking](http://petersteinberger.com/blog/2013/fixing-uisearchdisplaycontroller-on-ios-7/) to get what you expect. I've had very mixed results with this control, including it not showing the search table view *at all* when using `displaysSearchBarInNavigationBar`.\n\nYour results may vary, but I've required some severe hacks to get `displaysSearchBarInNavigationBar` working:\n\n```objc\n- (void)restoreOriginalTableView \n{\n    if (PSPDFIsUIKitFlatMode() && self.originalTableView) {\n        self.view = self.originalTableView;\n    }\n}\n\n- (UITableView *)tableView \n{\n    return self.originalTableView ?: [super tableView];\n}\n\n- (void)searchDisplayController:(UISearchDisplayController *)controller \n  didShowSearchResultsTableView:(UITableView *)tableView \n{\n    // HACK: iOS 7 requires a cruel workaround to show the search table view.\n    if (PSPDFIsUIKitFlatMode()) {\n        if (!self.originalTableView) self.originalTableView = self.tableView;\n        self.view = controller.searchResultsTableView;\n        controller.searchResultsTableView.contentInset = UIEdgeInsetsZero; // Remove 64 pixel gap\n    }\n}\n\n- (void)searchDisplayController:(UISearchDisplayController *)controller \n  didHideSearchResultsTableView:(UITableView *)tableView \n{\n    [self restoreOriginalTableView];\n}\n```\n\nAlso, don't forget calling `restoreOriginalTableView` in `viewWillDisappear`, or things will crash badly. Remember that this is only one solution; there might be less radical ways that don't replace the view itself, but this really should be fixed by Apple. (TODO: RADAR!)\n\n## Pagination\n\n`UIWebView` learned a new trick to automatically paginate websites with `paginationMode`. There are a whole bunch of new properties related to this feature:\n\n```objc\n@property (nonatomic) UIWebPaginationMode paginationMode NS_AVAILABLE_IOS(7_0);\n@property (nonatomic) UIWebPaginationBreakingMode paginationBreakingMode NS_AVAILABLE_IOS(7_0);\n@property (nonatomic) CGFloat pageLength NS_AVAILABLE_IOS(7_0);\n@property (nonatomic) CGFloat gapBetweenPages NS_AVAILABLE_IOS(7_0);\n@property (nonatomic, readonly) NSUInteger pageCount NS_AVAILABLE_IOS(7_0);\n```\n\nNow while this might not be useful for most websites, it certainly is to build simple ebook readers or display text in a nicer way. For added fun, try setting it to `UIWebPaginationModeBottomToTop`.\n\n## Flying Popovers\n\nWonder why your popovers are flying around like crazy? There's a new delegate in the `UIPopoverControllerDelegate` protocol which allows you to control the madness:\n\n```objc\n-     (void)popoverController:(UIPopoverController *)popoverController\n  willRepositionPopoverToRect:(inout CGRect *)rect \n                       inView:(inout UIView **)view\n```\n\n`UIPopoverController` will behave if anchored to a `UIBarButtonItem`, but if you're showing it with a view and rect, you might have to implement this method and return something sane. This took me quite a long time to figure out - it's especially required if you dynamically resize your popovers via changing `preferredContentSize`. Apple now takes those sizing requests more serious and will move the popover around if there's not enough space left.\n\n## Keyboard Support\n\nApple didn't only give us [a whole new framework for game controllers](https://developer.apple.com/library/ios/documentation/ServicesDiscovery/Conceptual/GameControllerPG/Introduction/Introduction.html), it also gave us keyboard-lovers some attention! You'll find new defines for common keys like `UIKeyInputEscape` or `UIKeyInputUpArrow` that can be intercepted using the all-new [`UIKeyCommand`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKeyCommand_class/Reference/Reference.html#//apple_ref/occ/instp/UIKeyCommand/input) class. Before iOS 7, [unspeakable hacks were necessary to react to keyboard commands](http://petersteinberger.com/blog/2013/adding-keyboard-shortcuts-to-uialertview/). Now, let's grab a bluetooth keyboard and see what we can do with this new hotness!\n\nBefore starting, you need some basic understanding for the responder chain. Your `UIApplication` inherits from `UIResponder`, and so does `UIView` and `UIViewController`. If you've ever had to deal with `UIMenuItem` and weren't using [my block-based wrapper](https://github.com/steipete/PSMenuItem), you know this already. So events will be sent to the topmost responder and then trickle down level by level until they end at UIApplication. To capture key commands, you need to tell the system what key commands you're interested in (there's no catch-all). To do so, override the new `keyCommands` property:\n\n```objc\n- (NSArray *)keyCommands \n{\n    return @[[UIKeyCommand keyCommandWithInput:@\"f\"\n                                 modifierFlags:UIKeyModifierCommand  \n                                        action:@selector(searchKeyPressed:)]];\n}\n\n- (void)searchKeyPressed:(UIKeyCommand *)keyCommand \n{\n    // Respond to the event\n}\n```\n\n![The Responder Chain at work](/images/issue-5/responder-chain.png)\n\nNow don't get too excited; there are some caveats. This only works when the keyboard is visible (if there's some first responder like `UITextView`.) For truly global hotkeys, you still need to revert to the above-linked hackery. But apart from that, the routing is very elegant. Don't try overriding system shortcuts like cmd-V, as those will be mapped to `paste:` automatically.\n\nThere are also some new predefined responder actions like:\n\n```objc\n- (void)increaseSize:(id)sender NS_AVAILABLE_IOS(7_0);\n- (void)decreaseSize:(id)sender NS_AVAILABLE_IOS(7_0);\n```\n\nwhich are called for cmd+ and cmd- respectively, to increase/decrease content size.\n\n## Match the Keyboard Background\n\nApple finally made [`UIInputView`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIInputView_class/Reference/Reference.html) public, which provides a way to match the keyboard style with using `UIInputViewStyleKeyboard`. This allows you to write custom keyboards or (toolbar) extensions for the default keyboards that match the default style. This class has existed since the [dawn of time](https://github.com/nst/iOS-Runtime-Headers/commits/master/Frameworks/UIKit.framework/UIInputView.h), but now we can finally use it without resorting to hacks.\n\n`UIInputView` will only show a background if it's the the *root view* of your `inputView` or `inputAccessoryView` - otherwise it will be transparent.  Sadly, this doesn't enable you to implement a split keyboard without fill, but it's still better than using a simple UIToolbar. I haven't yet seen a spot where Apple uses this new API - it still seems to use a `UIToolbar` in Safari.\n\n## Know your Radio\n\nWhile most of the carrier information has been exposed in CTTelephony as early as iOS 4, it was usually special-case and not useful. With iOS 7, Apple added one method here, the most useful of them all: `currentRadioAccessTechnology`. This allows you to know if the phone is on dog-slow GPRS, on lighting-fast LTE, or on anything in between. Now there's no method that would give you the connection speed (since the phone can't really know that), but it's good enough to fine-tune a download manager to not try downloading six images *simultaneously* when the user's on EDGE. \n\nNow there's absolutely no documentation around `currentRadioAccessTechnology`, so it took some trial and error to make this work. Once you have the current value, you should register for the `CTRadioAccessTechnologyDidChangeNotification` instead of polling the property. To actually get iOS to emit those notifications, you need to carry an instance of `CTTelephonyNetworkInfo` around. Don't try to create a new instance of `CTTelephonyNetworkInfo` inside the notification, or it'll crash.\n\nIn this simple example, I am abusing the fact that capturing `telephonyInfo` in a block will retain it:\n\n```objc\nCTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new];\nNSLog(@\"Current Radio Access Technology: %@\", telephonyInfo.currentRadioAccessTechnology);\n[NSNotificationCenter.defaultCenter addObserverForName:CTRadioAccessTechnologyDidChangeNotification \n                                                object:nil \n                                                 queue:nil \n                                            usingBlock:^(NSNotification *note) \n{\n    NSLog(@\"New Radio Access Technology: %@\", telephonyInfo.currentRadioAccessTechnology);\n}];\n```\n\nThe log output can look something like this, when the phone's moving from Edge to 3G:\n\n```objc\niOS7Tests[612:60b] Current Radio Access Technology: CTRadioAccessTechnologyEdge\niOS7Tests[612:1803] New Radio Access Technology: (null)\niOS7Tests[612:1803] New Radio Access Technology: CTRadioAccessTechnologyHSDPA\n```\n\nApple exported all string symbols so it's easy to compare and detect the current technology.\n\n\n## Core Foundation, Autorelease and You.\n\nThere's a new helper in Core Foundation that people have been missing and hacking around for years:\n\n```objc\nCFTypeRef CFAutorelease(CFTypeRef CF_RELEASES_ARGUMENT arg)\n```\n\nIt does exactly what you expect it to do, and it's quite puzzling how long it took Apple to make it public. With ARC, most people solved returning Core Foundation objects via casting them to their NS-equivalent - like returning an `NSDictionary`, even though it's a `CFDictionaryRef` and simply using `CFBridgingRelease()`. This works well, until you need to return methods where no NS-equivalent is available, like `CFBagRef`. Then you either use id and lose any type safety, or you rename your method to `createMethod` and have to think about all the memory semantics and using CFRelease afterward. There were also hacks like [this one](http://favstar.fm/users/AndrePang/status/18099774996), using a non-ARC-file so you can compile it, but using CFAutorelease() is really the way to go. Also: Don't write code using Apple's namespace. All those custom CF-Macros are programmed to break sooner or later.\n\n## Image Decompression\n\nWhen showing an image via `UIImage`, it might need to be decompressed before it can be displayed (unless the source is a pixel buffer already). This can take significant time for JPG/PNG files and result in stuttering. Before iOS 6, this was usually solved by creating a new bitmap context and drawing the image into it. [(See how AFNetworking solves this)](https://github.com/AFNetworking/AFNetworking/blob/09658b352a496875c91cc33dd52c3f47b9369945/AFNetworking/AFURLResponseSerialization.m#L442-518).\n\nStarting with iOS 7, you can now force decompression directly at image creation time with the new `kCGImageSourceShouldCacheImmediately`:\n\n```objc\n+ (UIImage *)decompressedImageWithData:(NSData *)data \n{\n    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);\n    CGImageRef cgImage = CGImageSourceCreateImageAtIndex(source, 0, (__bridge CFDictionaryRef)@{(id)kCGImageSourceShouldCacheImmediately: @YES});\n    \n    UIImage *image = [UIImage imageWithCGImage:cgImage];\n    CGImageRelease(cgImage);\n    CFRelease(source);\n    return image;\n}\n```\n\nI was very excited when I first found out about this, but you really shouldn't be. In my tests, performance actually *decreased* when I enabled immediate caching. Either this method calls up to the main thread (unlikely) or perceived performance is simply worse because it locks in `copyImageBlockSetJPEG`, which is also used when showing a non-decrypted image on the main thread. In my app, I load small preview thumbnails from the main thread, and load the large page images from a background thread. Using `kCGImageSourceShouldCacheImmediately` now blocks the main thread where only a tiny decompression would take place, with a much more expensive operation on the background thread.\n\n![Image Decompression Stack Trace](/images/issue-5/image-decompression.png)\n\nThere's a lot more to image decompression which isn't new to iOS 7, like `kCGImageSourceShouldCache`, which controls the ability where the system can automatically unload decompressed image data. Make sure you're setting this to YES, otherwise all the extra work could be pointless. Interesting detail: Apple changed the *default* of `kCGImageSourceShouldCache` from NO to YES with the 64-bit runtime.\n    \n## Piracy Check\n\nApple added a way to evaluate the App Store receipt in Lion with the new [`appStoreReceiptURL`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/Reference/Reference.html#//apple_ref/occ/instm/NSBundle/appStoreReceiptURL) method on `NSBundle`, and finally also ported this to iOS. This allows you to check if your app was legitimately purchased or cracked. There's another important reason for checking the receipt. It contains the *initial purchase date*, which can be very useful when moving your app from a paid model to free and in-app purchases. You can use this initial purchase date to determine if your users get the extra content free (because they already paid for it), or if they have to purchase it.\n\nThe receipt also lets you check if the app was purchased via the volume purchase program and if that license wasn't revoked, there's a property named `SKReceiptPropertyIsVolumePurchase` indicating that.\n\nYou need to take special care when calling `appStoreReceiptURL`, since it exists as private API on iOS 6, but will call `doesNotRecognizeSelector:` when called from user code. Check the running (foundation) version before calling. During development, there won't be a file at the URL returned from this method. You will need to use StoreKit's [`SKReceiptRefreshRequest`](https://developer.apple.com/library/ios/documentation/StoreKit/Reference/SKReceiptRefreshRequest_ClassRef/SKReceiptRefreshRequest.html), also new in iOS 7, to download the certificate. Use a test user who made at least one purchase, or else it won't work:\n\n```objc\n// Refresh the Receipt\nSKReceiptRefreshRequest *request = [[SKReceiptRefreshRequest alloc] init];\n[request setDelegate:self];\n[request start];\n```\n\nVerifying the receipt requires a lot of code. You need to use OpenSSL and embed the [Apple Root Certificate](http://www.apple.com/certificateauthority/), and you should understand some basics about certificates, [PCKS containers](http://en.wikipedia.org/wiki/PKCS), and [ASN.1](http://de.wikipedia.org/wiki/Abstract_Syntax_Notation_One). There's some [sample code](https://github.com/rmaddy/VerifyStoreReceiptiOS) out there, but you shouldn't make it too easy for someone with less honorable intents - don't just copy the existing validation methods, at least modify them or write your own. You don't want a generic patcher app to undo your hard work in seconds.\n\nYou should definitely read Apple's guide on [Validating Mac App Store Receipts](https://developer.apple.com/library/mac/releasenotes/General/ValidateAppStoreReceipt/index.html#//apple_ref/doc/uid/TP40010573-CH1-SW6) - a lot of this applies to iOS as well. Apple also details the changes with its new \"Grand Unified Receipt\" in [Session 308 \"Using Receipts to Protect Your Digital Sales\" @ WWDC 2013](https://developer.apple.com/wwdc/videos/).\n\n## Comic Sans MS\n\nAdmit it. You've missed Comic Sans MS. With iOS 7, you can finally get it back. Now, downloadable fonts have been added to iOS 6, but back then the list of fonts was pretty small and not really interesting. Apple added some more fonts in iOS 7, including \"famous\" ones like [PT Sans](http://www.fontsquirrel.com/fonts/PT-Sans) or [Comic Sans MS](http://sixrevisions.com/graphics-design/comic-sans-the-font-everyone-loves-to-hate/). The `kCTFontDownloadableAttribute` was not declared in iOS 6, so this wasn't really usable until iOS 7, but Apple retroactively declared this property to be available on iOS 6 upward.\n\n![Who doesn't love Comic Sans MS](/images/issue-5/comic-sans-ms.png)\n\nThe list of fonts is [dynamic](http://mesu.apple.com/assets/com_apple_MobileAsset_Font/com_apple_MobileAsset_Font.xml) and might change in the future. Apple lists some of the available fonts in [Tech Note HT5484](http://support.apple.com/kb/HT5484), but the document is outdated and doesn't yet reflect the changes on iOS 7.\n\nHere's how you get an array of fonts that can be downloaded with `CTFontDescriptorRef`:\n\n```objc\nCFDictionary *descriptorOptions = @{(id)kCTFontDownloadableAttribute : @YES};\nCTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)descriptorOptions);\nCFArrayRef fontDescriptors = CTFontDescriptorCreateMatchingFontDescriptors(descriptor, NULL);\n```\n\nThe system won't check if the font is already on disk and will return the same list. Additionally, this method might do a network call and thus block. You don't want to call this from the main thread.\n\nTo download the font, use this block-based API:\n\n```objc\nbool CTFontDescriptorMatchFontDescriptorsWithProgressHandler(\n         CFArrayRef                          descriptors,\n         CFSetRef                            mandatoryAttributes,\n         CTFontDescriptorProgressHandler     progressBlock)\n```\n\nThis method handles the network call and calls your `progressBlock` with progress information until the download either succeeds or fails. Refer to Apple's [DownloadFont Example](https://developer.apple.com/library/ios/samplecode/DownloadFont/Listings/DownloadFont_ViewController_m.html) to see how this can be used.\n\nThere are a few gotchas here. This font will only be available during the current app run, and has to be loaded again into memory on the next run. Since fonts are saved in a shared place, you can't rely on them being available. They most likely will be, but it's not guaranteed and the system might clean this folder, or your app is being copied to a new device where the font doesn't yet exist, and you might run without a working network. On the Mac or in the Simulator you can obtain the `kCTFontURLAttribute` to get the absolute path of the font and speed up loading time, but this won't work on iOS, since the folder is outside of your app - you need to call `CTFontDescriptorMatchFontDescriptorsWithProgressHandler` again.\n\nYou can also subscribe to the new `kCTFontManagerRegisteredFontsChangedNotification` to be notified whenever new fonts are loaded into the font registry. You can find out more in [Session 223 \"Using Fonts with TextKit\" @ WWDC 2013](https://developer.apple.com/wwdc/videos/).\n\n## Can't Get Enough?\n\nDon't worry - there's a lot more new in iOS 7! [Over at NSHipster](http://nshipster.com/ios7/) you'll learn about speech synthesis, base64, the all-new `NSURLComponents`, `NSProgress`, bar codes, reading lists, and, of course, `CIDetectorEyeBlink`. And there's a lot more waiting we couldn't cover - study Apple's [iOS 7 API Diffs](https://developer.apple.com/library/ios/releasenotes/General/iOS70APIDiffs/index.html#//apple_ref/doc/uid/TP40013203), the [What's new in iOS](https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS7.html) guide, and the [Foundation Release Notes](https://developer.apple.com/library/mac/releasenotes/Foundation/RN-Foundation/index.html#//apple_ref/doc/uid/TP30000742) (those are for OS X - but since this is shared code, a lot applies to iOS as well).\nMany of the new methods don't even have documentation yet, so it's up to you to play around and blog about it!\n"
  },
  {
    "path": "2013-10-08-multitasking.md",
    "content": "---\ntitle: \"Multitasking in iOS 7\"\ncategory: \"5\"\ndate: \"2013-10-07 07:00:00\"\ntags: article\nauthor:\n  - name: David Caunt\n    url: https://twitter.com/dcaunt\n---\n\n\nPrior to iOS 7, developers were pretty limited in what they could do when their apps left the foreground. Aside from VOIP and location-based features, the only way to execute code in the background was to use background tasks, restricted to running for a few minutes. If you wanted to download a large video for offline viewing, or backup a user’s photos to your server, you could only complete part of the work.\n\niOS 7 adds two new APIs for updating your app’s UI and content in the background. The first, Background Fetch, allows you to fetch new content from the network at regular intervals. The second, Remote Notifications, is a new feature leveraging Push Notifications to notify an app when an event has occurred. Both of these new mechanisms help you to keep your app's interface up to date, and can schedule work on the new Background Transfer Service, which allows you to perform out-of-process network transfers (downloads and uploads).\n\nBackground Fetch and Remote Notifications are simple application delegate hooks with 30 seconds of wall-clock time to perform work before your app is suspended. They're not intended for CPU intensive work or long running tasks, rather, they are for queuing up long-running networking requests, like a large movie download, or performing quick content updates.\n\nFrom a user’s perspective, the only obvious change to multitasking is the new app switcher, which displays a snapshot of each app’s UI as it was when it left the foreground. But there’s a reason for displaying the snapshots – you can now update your app’s snapshot after you complete background work, showing a preview of new content. Social networking, news, or weather apps can now display the latest content without the user having to open the app. We'll see how to update the snapshot later.\n\n## Background Fetch\n \nBackground Fetch is a kind of smart polling mechanism which works best for apps that have frequent content updates, like social networking, news, or weather apps. The system wakes up the app based on a user’s behavior, and aims to trigger background fetches in advance of the user launching the app. For example, if the user always uses an app at 1 p.m., the system learns and adapts, performing fetches ahead of usage periods. Background fetches are coalesced across apps by the device’s radio in order to reduce battery usage, and if you report that new data was not available during a fetch, iOS can adapt, using this information to avoid fetches at quiet times.\n\nThe first step in enabling Background Fetch is to specify that you’ll use the feature in the [`UIBackgroundModes`](https://developer.apple.com/library/ios/documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW22) key in your info plist. The easiest way to do this is to use the new Capabilities tab in Xcode 5’s project editor, which includes a Background Modes section for easy configuration of multitasking options. \n\n![A screenshot showing Xcode 5’s new Capabilities tab](/images/issue-5/capabilities-on-bgfetch.jpg)\n\nAlternatively, you can edit the key manually:\n\n```xml\n<key>UIBackgroundModes</key>\n<array>\n    <string>fetch</string>\n</array>\n```\n\nNext, tell iOS how often you'd like to fetch:  \n\n```objc\n- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions\n{\n    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];\n\n    return YES;\n}\n```\n\nThe default fetch interval is never, so you'll need to set a time interval or the app won't ever be called in the background. The value of `UIApplicationBackgroundFetchIntervalMinimum` asks the system to manage when your app is woken, as often as possible, but you should specify your own time interval if this is unnecessary. For example, a weather app might only update conditions hourly. iOS will wait at least the specified time interval between background fetches.\n\nIf your application allows a user to logout, and you know that there won’t be any new data, you may want to set the `minimumBackgroundFetchInterval` back to `UIApplicationBackgroundFetchIntervalNever` to be a good citizen and to conserve resources.\n\nThe final step is to implement the following method in your application delegate:\n\n```objc\n- (void)                application:(UIApplication *)application \n  performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler\n{\n    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];\n    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];\n\n    NSURL *url = [[NSURL alloc] initWithString:@\"http://yourserver.com/data.json\"];\n    NSURLSessionDataTask *task = [session dataTaskWithURL:url \n                                        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {\n    \n        if (error) {\n            completionHandler(UIBackgroundFetchResultFailed);\n            return;\n        }\n    \n        // Parse response/data and determine whether new content was available\n        BOOL hasNewData = ...\n        if (hasNewData) {\n            completionHandler(UIBackgroundFetchResultNewData);\n        } else {\n            completionHandler(UIBackgroundFetchResultNoData);\n        }\n    }];\n\n    // Start the task\n    [task resume];\n}\n```\n\nThis is where you can perform work when you are woken by the system. Remember, you only have 30 seconds to determine whether new content is available, to process the new content, and to update your UI. This should be enough time to fetch data from the network and to fetch a few thumbnails for your UI, but not much more. When your network requests are complete and your UI has been updated, you should call the completion handler. \n\nThe completion handler serves two purposes. First, the system measures the power used by your process and records whether new data was available based on the `UIBackgroundFetchResult` argument you passed. Second, when you call the completion handler, a snapshot of your UI is taken and the app switcher is updated. The user will see the new content when he or she is switching apps. This completion handler snapshotting behavior is common to all of the completion handlers in the new multitasking APIs.\n\nIn a real-world application, you should pass the `completionHandler` to sub-components of your application and call it when you've processed data and updated your UI.\n\nAt this point, you might be wondering how iOS can snapshot your app's UI when it is running in the background, and how the application lifecycle works with Background Fetch. If your app is currently suspended, the system will wake it before calling `application: performFetchWithCompletionHandler:`. If your app is not running, the system will launch it, calling the usual delegate methods, including `application: didFinishLaunchingWithOptions:`. You can think of it as the app running exactly the same way as if the user had launched it from Springboard, except the UI is invisible, rendered offscreen.\n\nIn most cases, you'll perform the same work when the application launches in the background as you would in the foreground, but you can detect background launches by looking at the [`applicationState`](https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006728-CH3-SW77) property of UIApplication:\n\n```objc\n- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions\n{\n    NSLog(@\"Launched in background %d\", UIApplicationStateBackground == application.applicationState);\n\n    return YES;\n}\n```\n\n### Testing Background Fetch\n\nThere are two ways you can simulate a background fetch. The easiest method is to run your application from Xcode and click _Simulate Background Fetch_ under Xcode's Debug menu while your app is running.\n\nAlternatively, you can use a scheme to change how Xcode runs your app. Under the Xcode menu item Product, choose Scheme and then Manage Schemes. From here, edit or add a new scheme and check the _Launch due to a background fetch event_ checkbox as shown below.\n\n![A screenshot showing Xcode 5’s scheme editor](/images/issue-5/edit-scheme-simulate-background-fetch.png)\n\n## Remote Notifications\n\nRemote notifications allow you to notify your app when important events occur. You might have new instant messages to deliver, breaking news alerts to send, or the latest episode of your user's favorite TV show ready for him or her to download for offline viewing. Remote notifications are great for sporadic but immediately important content, where the delay between background fetches might not be acceptable. Remote Notifications can also be much more efficient than Background Fetch, as your application only launches when necessary.\n\nA Remote Notification is really just a normal Push Notification with the `content-available` flag set. You might send a push with an alert message informing the user that something has happened, while you update the UI in the background. But Remote Notifications can also be silent, containing no alert message or sound, used only to update your app’s interface or trigger background work. You might then post a local notification when you've finished downloading or processing the new content.\n\nSilent push notifications are rate-limited, so don't be afraid of sending as many as your application needs. iOS and the APNS servers will control how often they are delivered, and you won’t get into trouble for sending too many. If your push notifications are throttled, they might be delayed until the next time the device sends a keep-alive packet or receives another notification.\n\n### Sending Remote Notifications\n\nTo send a remote notification, set the content-available flag in a push notification payload. The content-available flag is the same key used to notify Newsstand apps, so most push scripts and libraries already support remote notifications. When you're sending a Remote Notification, you might also want to include some data in the notification payload, so your application can reference the event. This could save you a few networking requests and increase the responsiveness of your app.\n\nI recommend using [Nomad CLI’s Houston](http://nomad-cli.com/#houston) utility to send push messages while developing, but you can use your favorite library or script.\n\nYou can install Houston as part of the nomad-cli ruby gem:\n\n```\ngem install nomad-cli\n```\n\nAnd then send a notification with the apn utility included in Nomad\n\n```\n# Send a Push Notification to your Device\napn push <device token> -c /path/to/key-cert.pem -n -d content-id=42\n```\n\nHere the `-n` flag specifies that the content-available key should be included, and `-d` allows us to add our own data keys to the payload.\n\nThe resulting notification payload looks like this:\n\n```json\n{\n    \"aps\" : {\n        \"content-available\" : 1\n    },\n    \"content-id\" : 42\n}\n```\n\niOS 7 adds a new application delegate method, which is called when a push notification with the content-available key is received:\n\n```objc\n- (void)           application:(UIApplication *)application \n  didReceiveRemoteNotification:(NSDictionary *)userInfo \n        fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler\n{\n    NSLog(@\"Remote Notification userInfo is %@\", userInfo);\n\n    NSNumber *contentID = userInfo[@\"content-id\"];\n    // Do something with the content ID\n    completionHandler(UIBackgroundFetchResultNewData);\n}\n```\n\nAgain, the app is launched into the background and given 30 seconds to fetch new content and update its UI, before calling the completion handler. We could perform a quick network request as we did in the Background Fetch example, but let's use the powerful new Background Transfer Service to enqueue a large download task and see how we can update our UI when it completes.\n\n## NSURLSession and Background Transfer Service\n\nWhile `NSURLSession `is a new class in iOS 7, it also refers to the new technology in Foundation networking. Intended to replace `NSURLConnection`, familiar concepts and classes such as `NSURL`, `NSURLRequest`, and `NSURLResponse` are preserved. You’ll work with `NSURLConnection`’s replacement, `NSURLSessionTask`, to make network requests and handle their responses. There are three types of session tasks – data, download, and upload – each of which add syntactic sugar to `NSURLSessionTask`, so you should use the appropriate one for your use case.\n\nAn `NSURLSession` coordinates one or more of these `NSURLSessionTask`s and behaves according to the `NSURLSessionConfiguration` with which it was created. You may create multiple `NSURLSession`s to group related tasks with the same configuration. To interact with the Background Transfer Service, you'll create a session configuration using `[NSURLSessionConfiguration backgroundSessionConfiguration]`. Tasks added to a background session are run in an external process and continue even if your app is suspended, crashes, or is killed.\n\n[`NSURLSessionConfiguration`](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionConfiguration_class/Reference/Reference.html) allows you to set default HTTP headers, configure cache policies, restrict cellular network usage, and more. One option is the `discretionary` flag, which allows the system to schedule tasks for optimal performance. What this means is that your transfers will only go over Wifi when the device has sufficient power. If the battery is low, or only a cellular connection is available, your task won't run. The `discretionary` flag only has an effect if the session configuration object has been constructed by calling the [`backgroundSessionConfiguration:`](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionConfiguration_class/Reference/Reference.html#//apple_ref/occ/clm/NSURLSessionConfiguration/backgroundSessionConfiguration:) method and if the background transfer is initiated while your app is in the foreground. If the transfer is initiated from the background the transfer will _always_ run in discretionary mode.\n \nNow we know a little about `NSURLSession`, and how a background session functions, let's return to our Remote Notification example and add some code to enqueue a download on the background transfer service. When the download completes, we'll notify the user that the file is available for use.\n\n### NSURLSessionDownloadTask\n\nFirst of all, let's handle a Remote Notification and enqueue an `NSURLSessionDownloadTask` on the background transfer service. In `backgroundURLSession`, we create an `NURLSession` with a background session configuration and add our application delegate as the session delegate. The documentation advises against instantiating multiple sessions with the same identifier, so we use `dispatch_once` to avoid potential issues:\n\n```objc\n- (NSURLSession *)backgroundURLSession\n{\n    static NSURLSession *session = nil;\n    static dispatch_once_t onceToken;\n    dispatch_once(&onceToken, ^{\n        NSString *identifier = @\"io.objc.backgroundTransferExample\";\n        NSURLSessionConfiguration* sessionConfig = [NSURLSessionConfiguration backgroundSessionConfiguration:identifier];\n        session = [NSURLSession sessionWithConfiguration:sessionConfig \n                                                delegate:self \n                                           delegateQueue:[NSOperationQueue mainQueue]];\n    });\n\n    return session;\n}\n\n- (void)           application:(UIApplication *)application \n  didReceiveRemoteNotification:(NSDictionary *)userInfo \n        fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler\n{\n    NSLog(@\"Received remote notification with userInfo %@\", userInfo);\n\n    NSNumber *contentID = userInfo[@\"content-id\"];\n    NSString *downloadURLString = [NSString stringWithFormat:@\"http://yourserver.com/downloads/%d.mp3\", [contentID intValue]];\n    NSURL* downloadURL = [NSURL URLWithString:downloadURLString];\n\n    NSURLRequest *request = [NSURLRequest requestWithURL:downloadURL];\n    NSURLSessionDownloadTask *task = [[self backgroundURLSession] downloadTaskWithRequest:request];\n    task.taskDescription = [NSString stringWithFormat:@\"Podcast Episode %d\", [contentID intValue]];\n    [task resume];\n\n    completionHandler(UIBackgroundFetchResultNewData);\n}\n```\n\nWe create a download task using the `NSURLSession` class method and configure its request, and provide a description for use later. You must remember to call `[task resume]` to actually start the task, as all session tasks begin in the suspended state.\n\nNow we need to implement the `NSURLSessionDownloadDelegate` methods to receive callbacks when the download completes. You may also need to implement `NSURLSessionDelegate` or `NSURLSessionTaskDelegate` methods if you need to handle authentication or other events in the session lifecycle. You should consult Apple's document [Life Cycle of a URL Session with Custom Delegates](https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/URLLoadingSystem/NSURLSessionConcepts/NSURLSessionConcepts.html#//apple_ref/doc/uid/10000165i-CH2-SW42), which explains the full life cycle across all types of session tasks.\n\nNone of the `NSURLSessionDownloadDelegate` delegate methods are optional, though the only one where we need to take action in this example is `[NSURLSession downloadTask:didFinishDownloadingToURL:]`. When the task finishes downloading, you're provided with a temporary URL to the file on disk. You must move or copy the file to your app's storage, as it will be removed from temporary storage when you return from this delegate method.\n\n```objc\n#Pragma Mark - NSURLSessionDownloadDelegate\n\n- (void)         URLSession:(NSURLSession *)session \n               downloadTask:(NSURLSessionDownloadTask *)downloadTask\n  didFinishDownloadingToURL:(NSURL *)location\n{\n    NSLog(@\"downloadTask:%@ didFinishDownloadingToURL:%@\", downloadTask.taskDescription, location);\n\n    // Copy file to your app's storage with NSFileManager\n    // ...\n\n    // Notify your UI\n}\n\n- (void)  URLSession:(NSURLSession *)session \n        downloadTask:(NSURLSessionDownloadTask *)downloadTask \n   didResumeAtOffset:(int64_t)fileOffset \n  expectedTotalBytes:(int64_t)expectedTotalBytes\n{\n}\n\n- (void)         URLSession:(NSURLSession *)session \n               downloadTask:(NSURLSessionDownloadTask *)downloadTask \n               didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten \n  totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite\n{\n}\n```\n\nIf your app is still running in the foreground when the background session task completes, the above code will be sufficient. In most cases, however, your app won't be running, or it will be suspended in the background. In these cases, you must implement two application delegates methods so the system can wake your application. Unlike previous delegate callbacks, the application delegate is called twice, as your session and task delegates may receive several messages. The app delegate method `application: handleEventsForBackgroundURLSession:` is called before these `NSURLSession` delegate messages are sent, and `URLSessionDidFinishEventsForBackgroundURLSession` is called afterward. In the former method, you store a background `completionHandler`, and in the latter you call it to update your UI:\n\n```objc\n- (void)                  application:(UIApplication *)application \n  handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler\n{\n    // You must re-establish a reference to the background session, \n    // or NSURLSessionDownloadDelegate and NSURLSessionDelegate methods will not be called\n    // as no delegate is attached to the session. See backgroundURLSession above.\n    NSURLSession *backgroundSession = [self backgroundURLSession];\n\n    NSLog(@\"Rejoining session with identifier %@ %@\", identifier, backgroundSession);\n\n    // Store the completion handler to update your UI after processing session events\n    [self addCompletionHandler:completionHandler forSession:identifier];\n}\n\n- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session\n{\n    NSLog(@\"Background URL session %@ finished events.\\n\", session);\n\n    if (session.configuration.identifier) {\n        // Call the handler we stored in -application:handleEventsForBackgroundURLSession:\n        [self callCompletionHandlerForSession:session.configuration.identifier];\n    }\n}\n\n- (void)addCompletionHandler:(CompletionHandlerType)handler forSession:(NSString *)identifier\n{\n    if ([self.completionHandlerDictionary objectForKey:identifier]) {\n        NSLog(@\"Error: Got multiple handlers for a single session identifier.  This should not happen.\\n\");\n    }\n\n    [self.completionHandlerDictionary setObject:handler forKey:identifier];\n}\n\n- (void)callCompletionHandlerForSession: (NSString *)identifier\n{\n    CompletionHandlerType handler = [self.completionHandlerDictionary objectForKey: identifier];\n\n    if (handler) {\n        [self.completionHandlerDictionary removeObjectForKey: identifier];\n        NSLog(@\"Calling completion handler for session %@\", identifier);\n    \n        handler();\n    }\n}\n```\n\nThis two-stage process is necessary to update your app UI if you aren't already in the foreground when the background transfer completes. Additionally, if the app is not running at all when the background transfer finishes, iOS will launch it into the background, and the preceding application and session delegate methods are called after `application:didFinishLaunchingWithOptions:`.\n\n### Configuration and Limitation\n\nWe've briefly touched on the power of background transfers, but you should explore the documentation and look at the `NSURLSessionConfiguration` options that best support your use case. For example, `NSURLSessionTasks` support resource timeouts through the `NSURLSessionConfiguration`'s `timeoutIntervalForResource` property. You can use this to specify how long you want to allow for a transfer to complete before giving up entirely. You might use this if your content is only available for a limited time, or if failure to download or upload the resource within the given timeInterval indicates that the user doesn't have sufficient Wifi bandwidth. \n\nIn addition to download tasks, `NSURLSession` fully supports upload tasks, so you might upload a video to your server in the background and assure your user that he or she no longer needs to leave the app running, as might have been done in iOS 6. A nice touch would be to set the `sessionSendsLaunchEvents` property of your `NSURLSessionConfiguration` to `NO`, if your app doesn't need launching in the background when the transfer completes. Efficient use of system resources keeps both iOS and the user happy.\n\nFinally, there are a couple of limitations in using background sessions. As a delegate is required, you can't use the simple block-based callback methods on `NSURLSession`. Launching your app into the background is relatively expensive, so HTTP redirects are always taken. The background transfer service only supports HTTP and HTTPS and you cannot use custom protocols. The system optimizes transfers based on available resources and you cannot force your transfer to progress in the background at all times.\n\nAlso note that `NSURLSessionDataTasks` are not supported in background sessions at all, and you should only use these tasks for short-lived, small requests, not for downloads or uploads.\n\n## Summary\n\nThe powerful new multitasking and networking APIs in iOS 7 open up a whole range of possibilities for both new and existing apps. Consider the use cases in your app which can benefit from out-of-process network transfers and fresh data, and make the most of these fantastic new APIs. In general, implement background transfers as if your application is running in the foreground, making appropriate UI updates, and most of the work is already done for you.\n\n- Use the appropriate new API for your app’s content.\n- Be efficient, and call completion handlers as early as possible. \n- Completion handlers update your app’s UI snapshot.\n\n## Further Reading\n\n- [WWDC 2013 session “What’s New with Multitasking”](https://developer.apple.com/wwdc/videos/?id=204)\n- [WWDC 2013 session “What’s New in Foundation Networking”](https://developer.apple.com/wwdc/videos/?id=705)\n- [URL Loading System Programming Guide](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html#//apple_ref/doc/uid/10000165i)\n\n\n\n\n"
  },
  {
    "path": "2013-10-08-redesigning-for-ios-7.md",
    "content": "---\r\ntitle: \"Re-Designing an App for iOS 7\"\r\ncategory: \"5\"\r\ndate: \"2013-10-07 05:00:00\"\r\ntags: article\r\nauthor:\r\n  - name: Holger Riegel\r\n    url: http://pixelflut.net/\r\n  - name: Tobias Kreß\r\n    url: http://twitter.com/myrronth\r\n---\r\n\r\n\r\nWatching the WWDC presentation of the new version of iOS, we were looking at our app [Grocery List](http://appstore.com/grocerylistpx) and realized: This iOS version is a complete restart, like the first introduction of the iPhone seven years ago. It's not enough to simply change the design. We had to rethink and rebuild the application to fit within the new environment. So we did.\r\n\r\nWhile the basic behavior of an app should not be changed, we decided to\r\ndo so based on user feedback and our own usage that made us realize we\r\nhad to improve some workflows. As an example, in the old application,\r\nadding an amount and unit to a product is a multi-step process that\r\nrequires navigating through multiple controllers. In *Grocery List 2*,\r\nyou will be able to set the values in place without leaving the current\r\nscreen.\r\n\r\nOn our way to achieving this, we encountered some topics that we thought would be useful to share. We'll start with animations and gestures. After that, we'll take a look at the interface, the colors, and the fonts. To attract the user to open our app, we'll have a look on how to design an iOS 7 app icon. Finally, we will share with you our opinion on what the new update means.\r\n\r\n## Animations\r\n\r\nCurrent mobile devices are becoming more powerful with each iteration. Meanwhile, the animations of objects are increasingly more realistic due to the ability to calculate the physical constraints in real time. There is no longer a need to apply shadows and gradients to your interface. Instead, you could focus on the feel, the motion, and the impact of your interactions. You can create a new world with the same rules but without simulating the old one.\r\n\r\nThe new SDK allows you to easily create and use custom animations. Before iOS 7 changing the transition from one view controller to another required lots of extra work. The possibility to easily add your own animation helps the user keep his or her path through different screens without losing focus.\r\n\r\nIn *Grocery List*, we use a slightly modified transition to display a modal controller. But most of the animations and transitions are default. With *Grocery List 2* and the new APIs, we could have added more custom animations as before. But Apple's solution to handle transitions is a good fit to most of our problems. That's why navigating through controllers in our app behaves the same as default apps do. As mentioned before, some of our app's workflows have significantly changed. As a result, we will also use custom animations to support the user better keeping focus.\r\n\r\n![Comparison of the Grocery List and the default view controller push](/images/issue-5/redesign-animations.gif)\r\n\r\nThe default animations on iOS 7 will feel new and natural to most of the users, and you don't have to do much to use them and to make your users happy. But adding some custom animations in places where they fit in will improve the overall experience of your app. Just be careful not to overdo it.\r\n\r\n## Gestures\r\n\r\nAfter several years of experience with touch devices, Apple discovered that the wider use of gestures is becoming more natural to users. In iOS 7, there are now a lot more possibilities to do so than before. Newly integrated ones, like swiping on a table view cell to reveal a hidden menu or swiping from the left edge to go back to the previous controller, have become so familiar in no time at all that you would miss them if an app didn't support them. The benefit of the direct manipulation in place helps the user to finish his task more efficiently without losing focus.\r\n\r\nIn *Grocery List*, we didn't have any custom gestures. But for our goal to improve some workflows in the next version, we support swiping on a cell from both directions to present different options for a product. And instead of having to go back your navigation stack to get to your lists or templates, you can easily swipe from the right device edge to quickly access the menu.\r\n\r\n![Grocery List 2 gestures](/images/issue-5/redesign-gestures.png)\r\n\r\nButtons and links are visible and recognizable for the user but gestures are not. If you're planning to support your features with gestures, great! But if some features in your app rely on gestures and don't have an equivalent and visible control, always provide a good way to discover them. An interface always should be self-explanatory. If you need introduction screens or videos to describe the basic features of your app, you might be doing something wrong.\r\n\r\n## Interface\r\n\r\nProbably the most-discussed topic before the final iOS 7 presentation\r\nhas been the difference between flat and skeuomorphic design. iOS 7 has\r\ncompletely removed all real-world dependency from its design but has\r\nmostly maintained its well-known interaction model. The new, thin\r\ntoolbars icons help the content to stand out. But keep in mind, they also easily fail to be identified and to be self-explanatory - especially when there is no label to describe the action the icon triggers.\r\n\r\nWe discovered it isn't all about recreating or removing all appearances of real objects, but rather it's important to support the content in the best way. If adding a subtle shadow to your navigation bar helps your app's content to stand out, there is no need to avoid it. The most important thing is to increase contrast where it is necessary and to present your content in a usable way.\r\n\r\n*Grocery List* depends heavily on its closeness to the real world. Chalkboard as background, cells like paper, all framed by glossy wood. As nice as it looks, it is a challenging task to place controls you can interact with and to add new features which have to fit within its narrow context. With iOS 7 and its cleaner design, we don't have to be super realistic but can instead focus on improving the interaction to let the user achieve his or her goal. *Grocery List 2* will definitely be using the new design language but trying to keeping its own style.\r\n\r\n![Comparison of the Grocery List and Grocery List 2 interface](/images/issue-5/redesign-interface.png)\r\n\r\nAfter using iOS 7 on a device for a few weeks, interacting with it seems more convenient then it was with previous versions. The new animations and gestures, and the abstinence of skeuomorphic elements, allow the user to better focus on the content.\r\n\r\n## Colors\r\n\r\nThe main difference between iOS 6 and iOS 7 is the overall feeling of color. Apple has made the transition from a dark appearance to a bright one. The colors are getting more vibrant and saturated to support the more frequently used transparency and background blur. \r\n\r\nDue to the skeuomorphic approach of *Grocery List*, colors could not be adjusted heavily. They were primary defined by the materials that we wanted to imitate. While we like the friendlier look of iOS 7, most build-in apps are mainly white. In *Grocery List 2*, the app will primarily be defined by its color scheme that also fits in the grocery scope. We don't want our app to be seen as just another iOS 7 app; instead we are trying to create a unique appearance.\r\n\r\n![Comparison of colors in a build-in iOS 7 app and Grocery List 2](/images/issue-5/redesign-colors.png)\r\n\r\nColors affect the perception of your app. Don't let your app be all white like the build-in ones. Instead, create your own unique identity. With the clean and new design of iOS 7 and the lack of skeuomorphic elements, you can now use colors that pop to represent what you want to achieve with your app.\r\n\r\n## Fonts\r\n\r\nApple acknowledges the importance of type, as we see in the rewrite of the text system that has taken place in the new version. Labels and text fields now directly rely on the power of core text providing all typographic features a font has included. Ligatures, swooshes, and the like can be easily enabled by using the new Framework. And by getting your font object with text styles, your app will be able to display content at font sizes the user has chosen. For detailed information, have a look at [this great article](http://typographica.org/on-typography/beyond-helvetica-the-real-story-behind-fonts-in-ios-7/) about fonts in iOS 7.\r\n\r\nDue to the lack of \"real\" buttons and less chrome around text, text is getting more attention. Criticized from typographic experts for the overall use of the thin weight of *Helvetica Neue* in the early beta builds, Apple finally switched back to a more readable font weight.\r\n\r\nIn *Grocery List*, we use a slab-serif that matches the skeuomorphic style. Running the app on an iOS 7 device has shown that this font is not the best choice for the next version. We decided to use a custom, sans-serif font that could better support the overall appearance of our app than the default one could.\r\n\r\n![Comparison of the Grocery List and Grocery List 2 fonts](/images/issue-5/redesign-fonts.png)\r\n\r\nAs content is the fundament of apps, it is important to enhance its legibility and the key to good legibility is good typography. Although Apple's default font, *Helvetica Neue*, is a good choice for most use cases, it is worth considering using a custom font - especially when your app presents lots of text.\r\n\r\n## App Icon\r\n\r\nNot only has Apple changed the dimensions and outlines of the app icons in iOS 7, it has also changed the visual mood. App icons don't have an applied glow anymore, and most of the build-in app icons fit in the grid Apple has shown. Also, the icons are much simpler, removing the realistic effects and mostly only displaying simple symbolics on a colorful background. This is a transition away from little photorealistic illustrations toward the basic meaning of what an icon should be: Iconographic.\r\n\r\nFrom a skeuomorphic app icon with chalkboard and wood to a simple, colorful icon with a coherent symbol, the *Grocery List* app icon does not really fit into the new homescreen. For the next version, we simplified the icon to a shopping basket symbol and chose a background color that we can also use as a main color in the app. This combination helps make the icon pop.\r\n\r\n![Comparison of the Grocery List and Grocery List 2 app icons](/images/issue-5/redesign-app-icon.png)\r\n\r\nWith iOS 7, icons will automatically be scaled to fit the new dimensions and result in blurry images. Shadows and lighting might look weird due to the new super-ellipse outline that iOS 7 uses to mask your icon. If you don't plan to update your app to adopt the new iOS 7 style, you should at least update it to add the new icon sizes.\r\n\r\n## Conclusion\r\n\r\nWhile the whole look and feel of iOS 7 seems to be new and polished, the concept of navigating through an app is still the same as in the first iteration of iOS. Viewing data in lists and tables, pushing view controllers to navigate further, and receiving push notifications have become so familiar to the user that the change of colors and fonts and the removal of skeuomorphic elements does not interrupt the well-known flow.\r\n\r\nThough Apple doesn't force you to change your application in this sense, we recommend to always try to improve it and keep the user in mind.\r\n\r\n"
  },
  {
    "path": "2013-10-08-view-controller-transitions.md",
    "content": "---\ntitle:  \"View Controller Transitions\"\ncategory: \"5\"\ndate: \"2013-10-07 09:00:00\"\ntags: article\nauthor:\n  - name: Chris Eidhof\n    url: http://twitter.com/chriseidhof\n---\n\n## Custom Animations\n\nOne of the most exciting iOS 7 features for me is the new View\nController Transitioning API. Before iOS 7, I would also create custom\ntransitions between view controllers, but doing this was not really\nsupported, and a bit painful.  Making those transitions interactive,\nhowever, was harder.\n\nBefore we continue with the article, I'd like to issue a warning: this\nis a very new API, and while we normally try to write about best\npractices, there are no clear best practices yet. It'll probably take a\nfew months, at least, to figure them out. This article is not so much a\nrecommendation of best practices, but rather an exploration of a new\nfeature.  Please contact us if you find out better ways of using\nthis API, so we can update this article.\n\nBefore we look at the API, note how the default behavior of navigation\ncontrollers in iOS 7\nchanged: the animation between two view controllers in a navigation\ncontroller looks slightly different, and it is interactive. For example, to pop a\nview controller, you can now pan from the left edge of the screen and\ninteractively drag the current view controller to the right.\n\nThat said, let's have a look at the API. What I found interesting is the\nheavy use of protocols and not concrete objects. While it felt a bit\nweird at first, I prefer this kind of API. It gives us as programmers\nmuch more flexibility. First, let's try to do a very simple\nthing: having a custom animation when pushing a view controller in a\nnavigation controller (the [sample project](https://github.com/objcio/issue5-view-controller-transitions) for this article is on github). To do this, we have to implement one of the new\n`UINavigationControllerDelegate` methods:\n\n```objc\n- (id<UIViewControllerAnimatedTransitioning>)\n                   navigationController:(UINavigationController *)navigationController\n        animationControllerForOperation:(UINavigationControllerOperation)operation\n                     fromViewController:(UIViewController*)fromVC\n                       toViewController:(UIViewController*)toVC\n{\n    if (operation == UINavigationControllerOperationPush) {\n        return self.animator;\n    }\n    return nil;\n}\n```\n\nWe can look at the kind of operation (either push or pop) and return a\ndifferent animator based on that. Or, if we want to share code, it might\nbe the same object, and we might store the operation in a property. We\nmight also create a new object for each operation. There's a lot of flexibility\nhere.\n\nTo perform the animation, we create a custom object that implements the\n`UIViewControllerAnimatedTransitioning` protocol:\n\n```objc\n@interface Animator : NSObject <UIViewControllerAnimatedTransitioning>\n\n@end\n```\n\nThe protocol requires us to implement two methods, one for the animation\nduration:\n\n```objc\n- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext\n{\n    return 0.25;\n}\n```\n\nand one that performs the animation:\n\n```objc\n- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext\n{\n    UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];\n    UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];\n    [[transitionContext containerView] addSubview:toViewController.view];\n    toViewController.view.alpha = 0;\n    \n    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{\n        fromViewController.view.transform = CGAffineTransformMakeScale(0.1, 0.1);\n        toViewController.view.alpha = 1;\n    } completion:^(BOOL finished) {\n        fromViewController.view.transform = CGAffineTransformIdentity;\n        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];\n        \n    }];\n\n}\n```\n\nHere, you can see how the protocols are used: instead of giving a\nconcrete object with properties, this methods gets a transition context\nthat is of type `id<UIViewControllerContextTransitioning>`. The only\nthing that's extremely important, is that we call `completeTransition:` after we're done with the animation. This tells the\nanimation context that we're done and updates the view controller's\nstate accordingly. The other code is standard; we ask the transition\ncontext for the two view controllers, and just use plain `UIView`\nanimations. That's really all there is to it, and now we have a custom\nzooming animation.\n\nNote that we only have specified a custom transition for the push\nanimation. For the pop animation, iOS falls back to the default sliding\nanimation. Also, by implementing this method, the transition is not\ninteractive anymore. Let's fix that.\n\n## Interactive Animations\n\nMaking this animation interactive is really simple. We need to override another new\nnavigation controller delegate method:\n\n```objc\n- (id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController*)navigationController\n                          interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>)animationController\n{\n    return self.interactionController;\n}\n```\n\nNote that, in a non-interactive animation, this will return nil.\n\nThe interaction controller is an instance of\n`UIPercentDrivenInteractiveTransition`. No further configuration or setup is\nnecessary. We create a pan recognizer, and here's the code that handles\nthe panning:\n\n```objc\nif (panGestureRecognizer.state == UIGestureRecognizerStateBegan) {\n    if (location.x >  CGRectGetMidX(view.bounds)) {\n        navigationControllerDelegate.interactionController = [[UIPercentDrivenInteractiveTransition alloc] init];\n        [self performSegueWithIdentifier:PushSegueIdentifier sender:self];\n    }\n} \n```\n\nOnly when the user is on the right-hand side of the screen, do we set the\nnext animation to be interactive (by setting the `interactionController`\nproperty). Then we just perform the segue (or if\nyou're not using storyboards, push the view controller). To drive the\ntransition, we call a method on the interaction controller: \n\n```objc\nelse if (panGestureRecognizer.state == UIGestureRecognizerStateChanged) {\n    CGFloat d = (translation.x / CGRectGetWidth(view.bounds)) * -1;\n    [interactionController updateInteractiveTransition:d];\n} \n```\n\nThis will set the percentage based on how much we have panned. The\nreally cool thing here is that the interaction controller cooperates\nwith the animation controller, and because we used a normal `UIView`\nanimation, it controls the progression of the animation. We don't need to\nconnect the interaction controller to the animation controller, as all of\nthis happens automatically in a decoupled way.\n\nFinally, when the gesture recognizer ends or is canceled, we need to\ncall the appropriate methods on the interaction controller:\n\n```objc\nelse if (panGestureRecognizer.state == UIGestureRecognizerStateEnded) {\n    if ([panGestureRecognizer velocityInView:view].x < 0) {\n        [interactionController finishInteractiveTransition];\n    } else {\n        [interactionController cancelInteractiveTransition];\n    }\n    navigationControllerDelegate.interactionController = nil;\n}\n```\n\nIt's important that we set the interaction controller to nil after the\ntransition was completed or canceled. If the next transition is\nnon-interactive, we don't want to return our old interaction controller.\n\nNow we have a fully interactive custom transition. Using just plain\ngesture recognizers and a concrete object provided by UIKit, we achieve\nthis in only a few lines of code. For most custom transitions,\nyou can probably stop reading here and do everything with the methods described\nabove. However, if you want to have completely\ncustom animations or interactions, this is also possible. We'll look at\nthat in the next section.\n\n## Custom Animation Using GPUImage\n\nOne of the cool things we can do now is completely custom animations,\nbypassing UIView and even Core Animation. Just do all the animation\nyourself, [Letterpress-style](http://www.macstories.net/featured/a-conversation-with-loren-brichter/). In a first attempt, I did this with Core\nImage, however, on my old iPhone 4 I only managed to get around 9 FPS,\nwhich is definitely too far off the 60 FPS I wanted.\n\nHowever, after bringing in [GPUImage](https://github.com/BradLarson/GPUImage), it was simple to have a custom animation with really nice effects. The animation we want is pixelating and dissolving the two view controls into each other.\nThe approach is to take a snapshot of the two view\ncontrollers, and apply GPUIImage's image filters on the two snapshots. \n\nFirst, we create a custom class that implements both the animation and\ninteractive transition protocols:\n\n```objc\n@interface GPUImageAnimator : NSObject\n  <UIViewControllerAnimatedTransitioning,\n   UIViewControllerInteractiveTransitioning>\n\n@property (nonatomic) BOOL interactive;\n@property (nonatomic) CGFloat progress;\n\n- (void)finishInteractiveTransition;\n- (void)cancelInteractiveTransition;\n\n@end\n```\n\nTo make the animations perform really fast, we want to upload the images\nto the GPU once, and then do all the processing and drawing on the GPU,\nwithout going back to the CPU (the data transfer will be very slow). By\nusing a GPUImageView, we can do the drawing in OpenGL (without having to\ndo manual OpenGL code; we can keep writing high-level code).\n\nCreating the filter chain is very straightforward. Have a look at\n`setup` in the sample code to see how to do it. A bit more challenging\nis animating the filters. With GPUImage, we don't get automatic\nanimation, so we want to update our filters at each frame that's\nrendered. We can use the `CADisplayLink` class to do this:\n\n```objc\nself.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(frame:)];\n[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];\n```\n\nIn the `frame:` method, we can update our progress based on how much\ntime has elapsed, and update the filters accordingly:\n\n```objc\n- (void)frame:(CADisplayLink*)link\n{\n    self.progress = MAX(0, MIN((link.timestamp - self.startTime) / duration, 1));\n    self.blend.mix = self.progress;\n    self.sourcePixellateFilter.fractionalWidthOfAPixel = self.progress *0.1;\n    self.targetPixellateFilter.fractionalWidthOfAPixel = (1- self.progress)*0.1;\n    [self triggerRenderOfNextFrame];\n}\n```\n\nAnd that's pretty much all we need to do. In case of interactive\ntransitions, we need to make sure that we set the progress based on our\ngesture recognizer, not based on time, but the rest of the code is\npretty much the same. \n\nThis is really powerful, and you can use any of the existing filters in\nGPUImage, or write your own OpenGL shaders to achieve this.\n\n## Conclusion\n\nWe only looked at animating between two view controllers in a navigation\ncontroller, but you can do the same for tab bar controllers or your own\ncustom container view controllers. Also, the `UICollectionViewController`\nis now extended in such a way that you can automatically and\ninteractively animate between layouts, using the same mechanism. This is\nreally powerful.\n\nWhen talking to [Orta](https://twitter.com/orta) about this API, he\nmentioned that he already uses it a lot to create lighter view controllers.\nInstead of managing state within your view controller, you can just\ncreate a new view controller and have a custom animation between the\ntwo, moving views between the two view controllers during the\ntransition.\n\n## More Reading\n\n* [WWDC: Custom Transitions using View Controllers](http://asciiwwdc.com/2013/sessions/218)\n* [Custom UIViewController transitions](http://www.teehanlax.com/blog/custom-uiviewcontroller-transitions/)\n* [iOS 7: Custom Transitions](http://www.doubleencore.com/2013/09/ios-7-custom-transitions/)\n* [Custom View Controller Transitions with Orientation](http://whoisryannystrom.com/2013/10/01/View-Controller-Transition-Orientation/)\n"
  },
  {
    "path": "2013-11-08-build-process.md",
    "content": "---\ntitle:  \"The Build Process\"\ncategory: \"6\"\ndate: \"2013-11-08 11:00:00\"\ntags: article\nauthor:\n  - name: Florian Kugler\n    url: https://twitter.com/floriankugler\n---\n\nWe are kind of spoiled these days — we just hit a single button in Xcode which looks like it's supposed to play some music, and a few seconds later, our app is running. It's magical. Until something goes wrong.\n\nIn this article, we're going to take a high-level tour through the build process and discover how all this ties in with the project settings Xcode exposes in its interface. For a deeper look at how each step along the way actually works, I will refer you to the other articles in this issue.\n\n\n## Deciphering the Build Log\n\nOur first point of attack to learn about the inner workings of the Xcode build process is to have a look at the complete log file. Open the Log Navigator, select a build from the list, and Xcode will show you the log file in a prettified format.\n\n![Xcode build log navigator](/images/issue-6/build-log.png)\n\nBy default, this view hides a lot of information, but you can reveal the details of each task by selecting it and clicking on the expand button at the right side. Another option is to select one or more tasks from the list and hit Cmd-C. This will copy the full plain text to the clipboard. Last but not least, you also can dump the complete log into the clipboard by selecting \"Copy transcript for shown results\" from the Editor menu.\n\nIn our example, the log is just shy of 10,000 lines long (admittedly, the biggest chunk originates from compiling OpenSSL, not from our own code). So let's get started!\n\nThe first thing you'll notice is that the log output is split into several big chunks corresponding to the targets in your project:\n\n```\nBuild target Pods-SSZipArchive\n...\nBuild target Makefile-openssl\n...\nBuild target Pods-AFNetworking\n...\nBuild target crypto\n...\nBuild target Pods\n...\nBuild target ssl\n...\nBuild target objcio\n```\n\nOur project has several dependencies: AFNetworking and SSZipArchive, which are included as Pods, as well as OpenSSL, which is included as subproject.\n\nFor each of these targets, Xcode goes through a series of steps to actually translate the source code into machine-readable binaries for the selected platform(s). Let's take a closer look at the first target, SSZipArchive.\n\nWithin the log output for this target we see the details for each task performed along the way. For example, the first one is for processing a precompiled header file (in order to make it more readable, I have stripped out a lot of details):\n\n```\n(1) ProcessPCH /.../Pods-SSZipArchive-prefix.pch.pch Pods-SSZipArchive-prefix.pch normal armv7 objective-c com.apple.compilers.llvm.clang.1_0.compiler\n    (2) cd /.../Dev/objcio/Pods\n        setenv LANG en_US.US-ASCII\n        setenv PATH \"...\"\n    (3) /.../Xcode.app/.../clang \n            (4) -x objective-c-header \n            (5) -arch armv7 \n            ... configuration and warning flags ...\n            (6) -DDEBUG=1 -DCOCOAPODS=1 \n            ... include paths and more ...\n            (7) -c \n            (8) /.../Pods-SSZipArchive-prefix.pch \n            (9) -o /.../Pods-SSZipArchive-prefix.pch.pch\n```\n\nThese blocks appear for each task in the build process, so let's go through this one in some more detail.\n\n1. Each of these blocks starts with a line which describes the task.\n2. The following indented lines list the statements which are executed for this task. In this case, the working directory is changed, and the `LANG` and `PATH` environment variables are set.\n3. This is where all the fun happens. In order to process a `.pch` file, clang gets called with a ton of options. This line shows the complete call with all arguments. Let's look at a few of them...\n4. The `-x` flag specifies the language, which is, in this case, `objective-c-header`.\n5. The destination architecture is specified as `armv7`.\n6. Implicit `#define`s are added.\n7. The `-c` flag tells clang what it actually should do. `-c` means run the preprocessor, parser, type-checking, LLVM generation and optimization, and target specific assembly code generation stages. Finally, it means to run the assembler itself to produce a `.o` object file.\n8. The input file.\n9. The output file.\n\nThere is quite a lot going on, and we will not go through each of the possible tasks in great detail. The point is that you have complete insight into what tools get called and with which arguments behind the scenes during the build process.\n\nFor this target, there are actually two tasks to process `objective-c-header` files, although only one `.pch` file exists. A closer look at these tasks tells us what's going on:\n\n```\nProcessPCH /.../Pods-SSZipArchive-prefix.pch.pch Pods-SSZipArchive-prefix.pch normal armv7 objective-c ...\nProcessPCH /.../Pods-SSZipArchive-prefix.pch.pch Pods-SSZipArchive-prefix.pch normal armv7s objective-c ...\n```\n\nThe target builds for two architectures — armv7 and armv7s — and therefore clang has to process files twice, once for each architecture.\n\nFollowing the tasks of processing the precompiled header files, we find a couple of other task types for the SSZipArchive target:\n\n```\nCompileC ...\nLibtool ...\nCreateUniversalBinary ...\n```\n\nThese names are almost self-explanatory: `CompileC` compiles `.m` and `.c` files, `Libtool` creates a library from object files, and the `CreateUniversalBinary` task finally combines the two `.a` files from the previous stage (one for each architecture) into a universal binary file that runs on both armv7 and armv7s.\n\nSubsequently, similar steps happen for all the other dependencies in our project. AFNetworking gets compiled and linked together with SSZipArchive as pod library. OpenSSL gets built, processing the crypto and ssl targets. \n\nAfter all these dependencies have been prepared, we finally arrive at the target for our app. The log output for this target includes some other interesting tasks next to the ones we already saw during the compilation of libraries above:\n\n```\nPhaseScriptExecution ...\nDataModelVersionCompile ...\nLd ...\nGenerateDSYMFile ...\nCopyStringsFile ...\nCpResource ...\nCopyPNGFile ...\nCompileAssetCatalog ...\nProcessInfoPlistFile ...\nProcessProductPackaging /.../some-hash.mobileprovision ...\nProcessProductPackaging objcio/objcio.entitlements ...\nCodeSign ...\n```\n\nThe only task that doesn't have a self-explanatory name in this list is probably `Ld`, which is the name of the linker tool. It is very similar to `libtool`. In fact, `libtool` simply calls into `ld` and `lipo`. `ld` is used to create executables, `libtool` for libraries. Check out [Daniel's](/issues/6-build-tools/mach-o-executables/) and [Chris's](/issues/6-build-tools/compiler/) articles for more details about how compilation and linking works.\n\nEach of these steps will, in turn, call command line tools to do the actual work, just like we saw for the `ProcessPCH` step above. But instead of torturing you any longer with going through log files, we will explore these tasks from a different angle: How does Xcode know which tasks have to be performed?\n\n\n## Controlling the Build Process\n\nWhen you select a project in Xcode 5, the project editor is presented to you with six tabs at the top: General, Capabilities, Info, Build Settings, Build Phases, and Build Rules.\n\n![Xcode project editor tabs](/images/issue-6/project-editor-tabs.png)\n\nFor our purpose of understanding the build process, the last three are the most relevant.\n\n\n### Build Phases\n\nBuild phases represent the high-level plan of how to get from your code to an executable binary. They describe the different kind of tasks that have to be performed along the way. \n\n![Xcode build phases](/images/issue-6/build-phases.png)\n\nFirst, the target dependencies are established. These tell the build system which targets have to be built before the build of the current target can commence. This is not a \"real\" build phase. Xcode just presents the GUI together with the build phases.\n\nAfter a CocoaPods specific *script execution* build phase — see [Michele's article](/issues/6-build-tools/cocoapods-under-the-hood/) for more information about CocoaPods and the build process — the \"Compile Sources\" section specifies all the files that have to be compiled. Note that this doesn't say anything about *how* these files have to be compiled. We will learn more about this aspect when looking at build rules and build settings. Files that are in this section will be processed according to those rules and settings.\n\nWhen compilation is complete, the next step is to link everything together. And, lo and behold, that's what we find as the next build phase listed in Xcode: \"Link Binary with Libraries.\" This section lists all static and dynamic libraries that are to be linked with the object files generated by compilation in the previous step. There are important differences between how static and dynamic libraries get handled, but I'll refer you to Daniel's article about [Mach-O executables](/issues/6-build-tools/mach-o-executables/) for more details.\n\nWhen linking is done, the last build phase is copying static resources, like images and fonts, into the app bundle. PNG images are actually not only copied to their destination, but also optimized along the way (if you have PNG optimization turned on in build settings). \n\nAlthough copying static resources is the last build phase, the build process is not complete yet. For example, code signing still has to happen, but that's not considered to be a build phase; it belongs to the final build step, \"Packaging.\"\n\n\n#### Custom Build Phases\n\nYou have full control over these build phases if the default settings don't do what you need. For example, you can add build phases that run custom scripts, which [CocoaPods uses](/issues/6-build-tools/cocoapods-under-the-hood/) to do extra work. You can also add additional build phases to copy resources. This can be useful if you want to copy certain resources into specific target directories.\n\nAnother nice use of a custom build phase is to watermark your app icon with the version number and commit hash. To do this, you add a \"Run Script\" build phase where you retrieve the version number and commit hash with the following commands:\n\n```\nversion=`/usr/libexec/PlistBuddy -c \"Print CFBundleVersion\" \"${INFOPLIST_FILE}\"`\ncommit=`git rev-parse --short HEAD`\n```\n\nAfter that, you can modify the app icon using ImageMagick. For a complete example of how to do this, check out [this GitHub project](https://github.com/krzysztofzablocki/IconOverlaying).\n\nIf you'd like to encourage yourself or your coworkers to keep your source files concise, you can add a \"Run Script\" build phase that spits out a warning if a source file exceeds a certain size, in this example 200 lines.\n\n```\nfind \"${SRCROOT}\" \\( -name \"*.h\" -or -name \"*.m\" \\) -print0 | xargs -0 wc -l | awk '$1 > 200 && $2 != \"total\" { print $2 \":1: warning: file more than 200 lines\" }'\n```\n\n### Build Rules\n\nBuild rules specify how different file types should be compiled. Normally you don't have to change anything here, but if you want to add custom processing for a certain file type, you can simply add a new build rule.\n\nA build rule specifies which file type it applies to, how the file should be processed, and where the output should go. Let's say we have created a preprocessor that takes basic Objective-C implementation files as input, parses comments within this file for a language we've created to generate layout constraints, and outputs a `.m` file which includes the generated code. Since we cannot have a build rule which takes a `.m` file as input and output, we're going to use the extension `.mal` and add a custom build rule for that:\n\n![Custom build rule](/images/issue-6/custom-build-rule.png)\n\nThis rule specifies that it applies to all files matching `*.mal` and that those files should be processed using a custom script (which calls our preprocessor with the input and output paths as arguments). Finally, the rule tells the build system where it can find the output of this build rule.\n\nSince the output is just a plain `.m ` file in this case, it will get picked up by the build rule for compiling `.m` files and everything will proceed as if we had written the result of the preprocessing step manually as `.m` file to start with.\n\nIn the script we use a few variables to specify the correct paths and file names. You can find a list of all the variables available in Apple's [Build Setting Reference](https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html#//apple_ref/doc/uid/TP40003931-CH3-SW105). To see the values of all the existing environment variables during a build, you can add a \"Run Script\" build phase and check the \"Show environment variables in build log\" option.\n\n\n### Build Settings\n\nSo far, we have seen how build phases are used to define the steps in the build process and how build rules specify how each file type should be processed during compilation. In build settings, you can configure the details of how each of the tasks we saw before in the build log output are performed. \n\nYou will find a ton of options for each stage of the build process, from compilation over linking to code signing and packaging. Note how the settings are divided into sections, which roughly correlate to the build phases and, sometimes, specific file types for compilation.\n\nMany of these options have reasonably good documentation, which you can see in the quick help inspector on the right-hand side or in the [Build Setting Reference](https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html#//apple_ref/doc/uid/TP40003931-CH3-SW105). \n\n\n## The Project File\n\nAll the settings we have discussed above are saved to the project file (`.pbxproj`), in addition to other project-related information (e.g. file groups). You will rarely get in touch with the internals of this file until you have a merge conflict on it.\n\nI would encourage you to open a project file in your favorite text editor and to go through it from top to bottom. It is surprisingly readable, and you will recognize the meaning of most sections without many problems. Reading and understanding a complete project file like this will make merge conflicts on it much less scary.\n\nFirst, we look for an entry called `rootObject`. In our project file, this reveals the following line:\n\n```\nrootObject = 1793817C17A9421F0078255E /* Project object */;\n```\n\n  \nFrom there, we just follow the ID of this object (`1793817C17A9421F0078255E`) and find our main project definition:\n\n```\n/* Begin PBXProject section */\n    1793817C17A9421F0078255E /* Project object */ = {\n        isa = PBXProject;\n...\n```\n\nThis section contains several keys which we can follow further to understand how this file is constructed. For example, `mainGroup` points to the root file group. If you follow this reference you will quickly see how the project structure is represented in the `.pbxproj` file. But let's have a look at something which is related to the build process. The `target` key points to the build target definitions:\n\n```\ntargets = (\n    1793818317A9421F0078255E /* objcio */,\n    170E83CE17ABF256006E716E /* objcio Tests */,\n);\n```\n\nFollowing the first reference we find the target definition:\n\n```\n1793818317A9421F0078255E /* objcio */ = {\n    isa = PBXNativeTarget;\n    buildConfigurationList = 179381B617A9421F0078255E /* Build configuration list for PBXNativeTarget \"objcio\" */;\n    buildPhases = (\n        F3EB8576A1C24900A8F9CBB6 /* Check Pods Manifest.lock */,\n        1793818017A9421F0078255E /* Sources */,\n        1793818117A9421F0078255E /* Frameworks */,\n        1793818217A9421F0078255E /* Resources */,\n        FF25BB7F4B7D4F87AC7A4265 /* Copy Pods Resources */,\n    );\n    buildRules = (\n    );\n    dependencies = (\n        1769BED917CA8239008B6F5D /* PBXTargetDependency */,\n        1769BED717CA8236008B6F5D /* PBXTargetDependency */,\n    );\n    name = objcio;\n    productName = objcio;\n    productReference = 1793818417A9421F0078255E /* objcio.app */;\n    productType = \"com.apple.product-type.application\";\n};\n```\n\nThe `buildConfigurationList` points to the available configurations, usually \"Debug\" and \"Release.\" Following the debug reference, we finally end up where all the options from the build settings tab are stored:\n\n```\n179381B717A9421F0078255E /* Debug */ = {\n    isa = XCBuildConfiguration;\n    baseConfigurationReference = 05D234D6F5E146E9937E8997 /* Pods.xcconfig */;\n    buildSettings = {\n        ALWAYS_SEARCH_USER_PATHS = YES;\n        ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;\n        CODE_SIGN_ENTITLEMENTS = objcio/objcio.entitlements;\n...\n```\n\nThe `buildPhases` attribute simply lists all the build phases we have defined in Xcode. It's easy to identify them because, luckily, Xcode augments the IDs of the objects with their real names in a C-style comment. The `buildRules` attribute is empty because we have not defined any custom build rules in this project. `dependencies` lists the target dependencies defined in Xcode's build phase tab. \n\nNot so scary, is it? I'll leave it as an exercise for you to go through the remaining parts of the project file. Just follow the object IDs. Once you get the hang of it and understand how all the different sections relate to the project settings in Xcode, figuring out what went wrong in case of more complicated merge conflicts becomes pretty easy. You can even start to read project files on GitHub without cloning the project and opening it in Xcode.\n\n\n## Conclusion\n\nModern software is built on a complex stack of other software, like libraries and build tools. Those, in turn, are themselves built on top of a lower-level stack. It's like peeling an onion layer for layer. While the whole stack all the way down to the silicon is probably too complex for any one person to comprehend, the realization that you actually can peel off the next layer and understand what's going on there is very empowering. There is no magic; it's just large pile of layers stacked on top of each other, and each layer has fundamentally the same building blocks.\n\nLooking under the hood of the build system is peeling off one of these layers. We don't need to understand the whole stack below to gain some insight into what's going on when we hit the run button. We just take a look one level deeper and find a well-organized and controllable sequence of calls to other tools, which we can investigate further if we'd like to do so. I encourage you to read the other articles in this issue to take on the next layer of the onion!\n\n"
  },
  {
    "path": "2013-11-08-cocoapods-under-the-hood.md",
    "content": "---\ntitle:  \"CocoaPods Under The Hood\"\ncategory: \"6\"\ndate: \"2013-11-08 08:00:00\"\ntags: article\nauthor:\n  - name: Michele Titolo\n    url: https://twitter.com/micheletitolo\n---\n\n\nCocoaPods is a library dependency management tool for OS X and iOS applications. With CocoaPods, you can define your dependencies, called `pods`, and manage their versions easily over time and across development environments. \n\nThe philosophy behind CocoaPods is twofold. Firstly, including third-party code in your projects involves many hoops. For the beginning Objective-C developer, the project file is daunting. Going through the steps of configuring build phases and linker flags leaves a lot of room for human error. CocoaPods simplifies all of that, and automatically configures your compiler settings.\n\nSecondly, CocoaPods makes it easy to discover new third-party libraries. Now, this doesn't mean you should go and build a FrankenApp, where every part is written by somebody else and simply stitched together. It does mean that you can find really good libraries that shorten your development cycle and improve the quality of your software.\n\nIn this article, we will walk through the `pod install` process, and take a deeper look at what CocoaPods is doing behind the scenes.\n\n## Core Components\nCocoaPods is written in Ruby and actually is made of several Ruby Gems. The most important gems when explaining the integration process are [CocoaPods/CocoaPods](https://github.com/CocoaPods/CocoaPods/), [CocoaPods/Core](https://github.com/CocoaPods/Core), and [CocoaPods/Xcodeproj](https://github.com/CocoaPods/Xcodeproj) (yes, CocoaPods is a dependency manager that is built using a dependency manager!).\n\n### CocoaPods/CocoaPod\n\nThis is the user-facing component and is activated whenever you call a `pod` command. It includes all the functionality you need to actually use CocoaPods, and makes use of all of the other gems to perform tasks.\n\n### CocoaPods/Core\n\nThe Core gem provides support for working with the files that are involved with CocoaPods, mainly the Podfile and podspecs.\n\n#### Podfile\n\nThe Podfile is the file that defines the pods you want to use. It is highly customizable, and you can be as specific as you'd like. For more information, check out [the Podfile guide](http://guides.cocoapods.org/syntax/podfile.html).\n\n#### Podspec\n\nThe `.podspec` is a file that determines how a particular pod is added to a project. It supports features such as listing source files, frameworks, compiler flags, and any other dependencies that a library requires, to name a few.\n\n### CocoaPods/Xcodeproj\n\nThis gem handles all of the project file interactions. It has the ability to both create and modify `.xcodeproj` and `.xcworkspace` files. It is also useable as a standalone gem, so if you ever wanted to write scripts and easily modify the project file, this gem is for you.\n\n## Running `pod install`\n\nThere is a lot that happens when `pod install` runs. The easiest insight into this is running the command with `--verbose`. Run that command, `pod install --verbose` now, and then come back. It will look something like this:\n\n```\n$ pod install --verbose\n\nAnalyzing dependencies\n\nUpdating spec repositories\nUpdating spec repo `master`\n  $ /usr/bin/git pull\n  Already up-to-date.\n\n\nFinding Podfile changes\n  - AFNetworking\n  - HockeySDK\n\nResolving dependencies of `Podfile`\nResolving dependencies for target `Pods' (iOS 6.0)\n  - AFNetworking (= 1.2.1)\n  - SDWebImage (= 3.2)\n    - SDWebImage/Core\n\nComparing resolved specification to the sandbox manifest\n  - AFNetworking\n  - HockeySDK\n\nDownloading dependencies\n\n-> Using AFNetworking (1.2.1)\n\n-> Using HockeySDK (3.0.0)\n  - Running pre install hooks\n    - HockeySDK\n\nGenerating Pods project\n  - Creating Pods project\n  - Adding source files to Pods project\n  - Adding frameworks to Pods project\n  - Adding libraries to Pods project\n  - Adding resources to Pods project\n  - Linking headers\n  - Installing libraries\n    - Installing target `Pods-AFNetworking` iOS 6.0\n      - Adding Build files\n      - Adding resource bundles to Pods project\n      - Generating public xcconfig file at `Pods/Pods-AFNetworking.xcconfig`\n      - Generating private xcconfig file at `Pods/Pods-AFNetworking-Private.xcconfig`\n      - Generating prefix header at `Pods/Pods-AFNetworking-prefix.pch`\n      - Generating dummy source file at `Pods/Pods-AFNetworking-dummy.m`\n    - Installing target `Pods-HockeySDK` iOS 6.0\n      - Adding Build files\n      - Adding resource bundles to Pods project\n      - Generating public xcconfig file at `Pods/Pods-HockeySDK.xcconfig`\n      - Generating private xcconfig file at `Pods/Pods-HockeySDK-Private.xcconfig`\n      - Generating prefix header at `Pods/Pods-HockeySDK-prefix.pch`\n      - Generating dummy source file at `Pods/Pods-HockeySDK-dummy.m`\n    - Installing target `Pods` iOS 6.0\n      - Generating xcconfig file at `Pods/Pods.xcconfig`\n      - Generating target environment header at `Pods/Pods-environment.h`\n      - Generating copy resources script at `Pods/Pods-resources.sh`\n      - Generating acknowledgements at `Pods/Pods-acknowledgements.plist`\n      - Generating acknowledgements at `Pods/Pods-acknowledgements.markdown`\n      - Generating dummy source file at `Pods/Pods-dummy.m`\n  - Running post install hooks\n  - Writing Xcode project file to `Pods/Pods.xcodeproj`\n  - Writing Lockfile in `Podfile.lock`\n  - Writing Manifest in `Pods/Manifest.lock`\n\nIntegrating client project\n```\n\nThere's a lot going on here, but when broken down, it's all very simple. Let's walk through it.\n\n### Reading the Podfile\n\nIf you've ever wondered why the Podfile syntax looks kind of weird, that's because you are actually writing Ruby. It's just a simpler DSL to use than the other formats available right now.\n\nSo, the first step during installation is figuring out what pods are both explicitly or implicitly defined. CocoaPods goes through and makes a list of all of these, and their versions, by loading podspecs. Podspecs are stored locally in `~/.cocoapods`.\n\n#### Versioning and Conflicts\n\nCocoaPods uses the conventions established by [Semantic Versioning](http://semver.org/) to resolve dependency versions. This makes resolving dependencies much easier, since the conflict resolution system can rely on non-breaking changes between patch versions. Say, for example, that two different pods rely on two versions of CocoaLumberjack. If one relies on `2.3.1` and another `2.3.3`, the resolver can use the newer version, `2.3.3`, since it should be backward compatible with `2.3.1`.\n\nBut this doesn't always work. There are many libraries that don't use this convention, which makes resolution difficult.\n\nAnd of course, there will always be some manual resolution of conflicts. If one library depends on CocoaLumberjack `1.2.5` and another `2.3.1`, only the end user can resolve that by explicitly setting a version.\n\n### Loading Sources\n\nThe next step in the process is actually loading the sources. Each `.podspec` contains a reference to files, normally including a git remote and tag. These are resolved to commit SHAs, which are then stored in `~/Library/Caches/CocoaPods`. The files created in these directories are the responsibility of the Core gem. \n\nThe source files are then downloaded to the `Pods` directory using the information from the `Podfile`, `.podspec`, and caches.\n\n### Generating the Pods.xcodeproj\n\nEvery time `pod install` is run and changes are detected, the `Pods.xcodeproj` is updated using the Xcodeproj gem. If the file doesn't exist, it's created with some default settings. Otherwise, the existing settings are loaded into memory.\n\n### Installing Libraries\n\nWhen CocoaPods adds a library to the project, it adds a lot more than just the source. Since the change to each library getting its own target, for each library, several files are added. Each source needs:\n\n- An `.xcconfig` that contains the build settings\n- A private `.xcconfig` that merges these build settings with the default CocoaPods configuration\n- A `prefix.pch` which is required for building\n- A `dummy.m` which is also required for building\n\nOnce this is done for each pod target, the overall `Pods` target is created. This adds the same files, with the addition of a few more. If any source contains a resource bundle, instructions on adding that bundle to your app's target will be added to `Pods-Resources.sh`. There's also a `Pods-environment.h`, which has some macros for you to check whether or not a component comes from a pod. And lastly, two acknowledgement files are generated, one `plist`, one `markdown`, to help end users conform with licensing.\n\n### Writing to Disk\n\nUp until now, a lot of this work has been done using objects in memory. In order for this work to be reproducible, we need a file record of all of this. So the `Pods.xcodeproj` is written to disk, along with two other very important files, `Podfile.lock` and `Manifest.lock`.\n\n#### Podfile.lock\n\nThis is one of the most important files that CocoaPods creates. It keeps track of all of the resolved versions of pods that need to be installed. If you are ever curious as to what version of a pod was installed, check this file. This also helps with consistency across teams if this file is checked in to source control, which is recommended.\n\n#### Manifest.lock\n\nThis is a copy of the `Podfile.lock` that gets created every time you run `pod install`. If you've ever seen the error `The sandbox is not in sync with the Podfile.lock`, it's because this file is no longer the same as the `Podfile.lock`. Since the `Pods` directory is not always under version control, this is a way of making sure that developers update their pods before running, as otherwise the app would crash, or the build would fail in another, less visible, way.\n\n### xcproj\n\nIf you have [xcproj](https://github.com/0xced/xcproj) installed on your system, which we recommend, it will `touch` the `Pods.xcodeproj` to turn it into the old ASCII plist format. Why? The writing of those files is no longer supported, and hasn't been for a while, yet Xcode still relies on it. Without xcproj, your `Pods.xcodeproj` is written as an XML plist, and when you open it in Xcode, it will be rewritten, causing large file diffs.\n\n## The Result\n\nThe finished product of running `pod install` is that a lot of files have been added to your project and created on the system. This process usually only takes a few seconds. And, of course, everything that CocoaPods does can be done without it. But it'll take a lot longer than a few seconds.\n\n## Sidenote: Continuous Integration\n\nCocoaPods plays really well with Continuous Integration. And, depending on how you have your project set up, it's still fairly easy to get these projects building.\n\n### With a Version-Controlled Pods Folder\n\nIf you version control the `Pods` folder and everything inside it, you don't need to do anything special for continuous integration. Just make sure to build your `.xcworkspace` with the correct scheme selected, as you need to specify a scheme when building with a workspace.\n\n### Without a Pods Folder\n\nWhen you do not version control your `Pods` folder, you need to do a few more things to get continuous integration working properly. At the very least, the `Podfile` needs to be checked in. It's recommended that the generated `.xcworkspace` and `Podfile.lock` are also under version control for ease of use, as well as making sure that the correct pod versions are used.\n\nOnce you have that setup, the key with running CocoaPods on CI is making sure `pod install` is run before every build. On most systems, like Jenkins or Travis, you can just define this as a build step (in fact, Travis will do it automatically). With the release of [Xcode Bots, we haven't quite figured out a smooth process as of writing](https://groups.google.com/d/msg/cocoapods/eYL8QB3XjyQ/10nmCRN8YxoJ), but are working toward a solution, and we'll make sure to share it once we do.\n\n## Wrapping Up\n\nCocoaPods streamlines development with Objective-C, and our goal is to improve the discoverability of, and engagement in, third-party open-source libraries. Understanding what's happening behind the scenes can only help you make better apps. We've walked through the entire process, from loading specs and sources and creating the `.xcodeproj` and all its components, to writing everything to disk. So next time, run `pod install --verbose` and watch the magic happen.\n"
  },
  {
    "path": "2013-11-08-compiler.markdown",
    "content": "---\ntitle: \"The Compiler\"\ncategory: \"6\"\ndate: \"2013-11-08 10:00:00\"\ntags: article\nauthor:\n  - name: Chris Eidhof\n    url: http://twitter.com/chriseidhof\n---\n\n## What Does a Compiler Do?\n\nIn this article we'll have a look at what a compiler does, and how we can use\nthat to our advantage. \n\nRoughly speaking, the compiler has two tasks: converting our Objective-C code\ninto low-level code, and analyzing our code to make sure we didn't make any\nobvious mistakes. \n\nThese days, Xcode ships with clang as the compiler. Wherever we write compiler, you can read it as clang.\nclang is the tool that takes Objective-C code, analyzes it, and transforms it into a more low-level representation that resembles assembly code: LLVM Intermediate Representation. LLVM IR\nis low level, and operating system independent.  LLVM takes instructions and compiles them into native\nbytecode for the target platform. This can either be done just-in-time or at the same time as compilation. \n\nThe nice thing about having those LLVM instructions is that you can generate and run them on any platform that is supported by LLVM. For example, if you write your iOS app, it automatically runs on two very different architectures (Intel and ARM), and it's LLVM that takes care of translating the IR code into native bytecode for those platforms.\n\nLLVM benefits from having a three-tier architecture, which means it supports a lot of input languages (e.g. C, Objective-C, and C++, but also Haskell) in the first tier, then a shared optimizer in the second tier (which optimizes the LLVM IR), and different targets in the third tier (e.g. Intel, ARM, and PowerPC). If you want to add a language, you can focus on the first tier, and if you want to add another compilation target, you don't have to worry too much about the input languages. In the book *The Architecture of Open Source Applications* there's a great chapter by LLVM's creator, Chris Lattner, about the [LLVM architecture](http://www.aosabook.org/en/llvm.html).\n\nWhen compiling a source file, the compiler proceeds through several phases. To see the different phases, we can ask clang what it will do to compile the file *hello.m*:\n\n```\n% clang -ccc-print-phases hello.m\n\n0: input, \"hello.m\", objective-c\n1: preprocessor, {0}, objective-c-cpp-output\n2: compiler, {1}, assembler\n3: assembler, {2}, object\n4: linker, {3}, image\n5: bind-arch, \"x86_64\", {4}, image\n```\n\nIn this article, we'll focus on phases one and two. In [Mach-O Executables](/issue-6/mach-o-executables.html), Daniel explains phases three and four.\n\n### Preprocessing\n\nThe first thing that happens when you're compiling a source file is preprocessing. The preprocessor handles a macro processing language, which means it will replace macros in your text by their definitions. For example, if you write the following:\n\n```objc\n#import <Foundation/Foundation.h>\n```\n\nThe preprocessor will take that line, and substitute it with the contents of that file. If that header file contains any other macro definitions, they will get substituted too.\n\nThis is the reason why people tell you to keep your header files free of imports as much as you can, because anytime you import something, the compiler has to do more work. For example, in your header file, instead of writing:\n\n```objc\n#import \"MyClass.h\"\n```\n\nYou could write:\n\n```objc\n@class MyClass;\n```\n\nAnd by doing that you promise the compiler that there will be a class, `MyClass`. In the implementation file (the `.m` file), you can then import the `MyClass.h` and use it.\n\nNow suppose we have a very simple pure C program, named `hello.c`:\n\n```objc\n#include <stdio.h>\n\nint main() {\n  printf(\"hello world\\n\");\n  return 0;\n}\n```\n\nWe can run the preprocessor on this to see what the effect is:\n\n```objc\nclang -E hello.c | less\n```\n\nNow, have a look at that code. It's 401 lines. If we also add the following line to the top:\n\n```objc\n#import <Foundation/Foundation.h>\n```\n\nWe can run the command again, and see that our file has expanded to a whopping 89,839 lines. There are entire operating systems written in less lines of code.\n\nLuckily, the situation has recently improved a bit. There's now a feature called [modules](http://clang.llvm.org/docs/Modules.html) that makes this process a bit more high level.\n\n#### Custom Macros\n\nAnother example is when you define or use custom macros, like this:\n\n```objc\n#define MY_CONSTANT 4\n```\n\nNow, anytime you write `MY_CONSTANT` after this line, it'll get replaced by `4` before the rest of compilation starts. You can also define more interesting macros that take arguments:\n\n```objc\n#define MY_MACRO(x) x\n```\n\nThis article is too short to discuss the full scope of what is possible with the preprocessor, but it's a very powerful tool. Often, the preprocessor is used to inline code. We strongly discourage this. For example, suppose you have the following innocent-looking program:\n\n\n```objc\n#define MAX(a,b) a > b ? a : b\n\nint main() {\n  printf(\"largest: %d\\n\", MAX(10,100));\n  return 0;\n}\n```\n\nThis will work fine. However, what about the following program:\n\n\n```objc\n#define MAX(a,b) a > b ? a : b\n\nint main() {\n  int i = 200;\n  printf(\"largest: %d\\n\", MAX(i++,100));\n  printf(\"i: %d\\n\", i);\n  return 0;\n}\n```\n\nIf we compile this with `clang max.c`, we get the following result:\n\n```objc\nlargest: 201\ni: 202\n```\n\nThis is quite obvious when we run the preprocessor and expand all the macros by issuing `clang -E max.c`:\n\n```objc\nint main() {\n  int i = 200;\n  printf(\"largest: %d\\n\", i++ > 100 ? i++ : 100);\n  printf(\"i: %d\\n\", i);\n  return 0;\n}\n```\n\nIn this case, it's an obvious example of what could go wrong with macros, but things can go also wrong in more unexpected and hard-to-debug ways. Instead of using a macro, you should use `static inline` functions:\n\n```objc\n#include <stdio.h>\n\nstatic const int MyConstant = 200;\n\nstatic inline int max(int l, int r) {\n   return l > r ? l : r;\n}\n\nint main() {\n  int i = MyConstant;\n  printf(\"largest: %d\\n\", max(i++,100));\n  printf(\"i: %d\\n\", i);\n  return 0;\n}\n```\n\nThis will print the correct result (`i: 201`). Because the code is inlined, it will have the same performance as the macro variant, but it's a lot less error-prone. Also, you can set breakpoints, have type checking, and avoid unexpected behavior.\n\nThe only time when macros are a reasonable solution is for logging, since you can use `__FILE__` and `__LINE__` and assert macros.\n\n### Tokenization (Lexing)\n\nAfter preprocessing is done, every source `.m` file now has a bunch of definitions.\nThis text is converted from a string to a stream of tokens. For example, in the case of a simple Objective-C hello world program:\n\n```objc\nint main() {\n  NSLog(@\"hello, %@\", @\"world\");\n  return 0;\n}\n```\n\nWe can ask clang to dump the tokens for this program by issuing the following command: `clang -Xclang -dump-tokens hello.m`:\n\n```\nint 'int'        [StartOfLine]  Loc=<hello.m:4:1>\nidentifier 'main'        [LeadingSpace] Loc=<hello.m:4:5>\nl_paren '('             Loc=<hello.m:4:9>\nr_paren ')'             Loc=<hello.m:4:10>\nl_brace '{'      [LeadingSpace] Loc=<hello.m:4:12>\nidentifier 'NSLog'       [StartOfLine] [LeadingSpace]   Loc=<hello.m:5:3>\nl_paren '('             Loc=<hello.m:5:8>\nat '@'          Loc=<hello.m:5:9>\nstring_literal '\"hello, %@\"'            Loc=<hello.m:5:10>\ncomma ','               Loc=<hello.m:5:21>\nat '@'   [LeadingSpace] Loc=<hello.m:5:23>\nstring_literal '\"world\"'                Loc=<hello.m:5:24>\nr_paren ')'             Loc=<hello.m:5:31>\nsemi ';'                Loc=<hello.m:5:32>\nreturn 'return'  [StartOfLine] [LeadingSpace]   Loc=<hello.m:6:3>\nnumeric_constant '0'     [LeadingSpace] Loc=<hello.m:6:10>\nsemi ';'                Loc=<hello.m:6:11>\nr_brace '}'      [StartOfLine]  Loc=<hello.m:7:1>\neof ''          Loc=<hello.m:7:2>\n```\n\nWe can see that each token consists of a piece of text and a source location. The source location is from before macro expansion, so that if something goes wrong, clang can point you to the right spot.\n\n### Parsing\n\nNow the interesting part starts: our stream of tokens is parsed into an abstract syntax tree. Because Objective-C is a rather complicated language, parsing is not always easy. After parsing, a program is now available as an abstract syntax tree: a tree that represents the original program. Suppose we have a program `hello.m`:\n\n```objc\n#import <Foundation/Foundation.h>\n\n@interface World\n- (void)hello;\n@end\n\n@implementation World\n- (void)hello {\n  NSLog(@\"hello, world\");\n}\n@end\n\nint main() {\n   World* world = [World new];\n   [world hello];\n}\n```\n\nWhen we issue `clang -Xclang -ast-dump -fsyntax-only hello.m`, we get the following result printed to our command line:\n\n```\n@interface World- (void) hello;\n@end\n@implementation World\n- (void) hello (CompoundStmt 0x10372ded0 <hello.m:8:15, line:10:1>\n  (CallExpr 0x10372dea0 <line:9:3, col:24> 'void'\n    (ImplicitCastExpr 0x10372de88 <col:3> 'void (*)(NSString *, ...)' <FunctionToPointerDecay>\n      (DeclRefExpr 0x10372ddd8 <col:3> 'void (NSString *, ...)' Function 0x1023510d0 'NSLog' 'void (NSString *, ...)'))\n    (ObjCStringLiteral 0x10372de38 <col:9, col:10> 'NSString *'\n      (StringLiteral 0x10372de00 <col:10> 'char [13]' lvalue \"hello, world\"))))\n\n\n@end\nint main() (CompoundStmt 0x10372e118 <hello.m:13:12, line:16:1>\n  (DeclStmt 0x10372e090 <line:14:4, col:30>\n    0x10372dfe0 \"World *world =\n      (ImplicitCastExpr 0x10372e078 <col:19, col:29> 'World *' <BitCast>\n        (ObjCMessageExpr 0x10372e048 <col:19, col:29> 'id':'id' selector=new class='World'))\")\n  (ObjCMessageExpr 0x10372e0e8 <line:15:4, col:16> 'void' selector=hello\n    (ImplicitCastExpr 0x10372e0d0 <col:5> 'World *' <LValueToRValue>\n      (DeclRefExpr 0x10372e0a8 <col:5> 'World *' lvalue Var 0x10372dfe0 'world' 'World *'))))\n```\n\nEvery node in the abstract syntax tree is annotated with the original source position, so that if there's any problem later on, clang can warn about your program and give you the correct location.\n\n##### See Also\n\n* [Introduction to the clang AST](http://clang.llvm.org/docs/IntroductionToTheClangAST.html)\n\n### Static Analysis\n\nOnce the compiler has an abstract syntax tree, it can perform analyses on that tree to help catch you errors, such as in type checking, where it checks whether your program is type correct. For example, when you send a message to an object, it checks if the object actually implements that message.\nAlso, clang does more advanced analyses where it walks through your program to make sure you're not doing anything weird. \n\n#### Type Checking\n\nAny time you write code, clang is there to help you check that you didn't make any mistakes. One of the obvious things to check is if your program sends the correct messages to the correct objects and calls the correct functions on the correct values. If you have a plain `NSObject*`, you can't just send it the `hello` message, as clang will report an error.\nAlso, if you create a class `Test` that subclasses `NSObject`, like this:\n\n```objc\n@interface Test : NSObject\n@end\n```\n\nAnd you then try to assign an object with a different type to that object, the compiler will help you and warn you that what you're doing is probably not correct.\n\nThere are two types of typing: dynamic and static typing. Dynamic typing means that a type is checked at runtime, and static typing means that the types are checked at compile time. In the past, you could always send any message to any object, and at runtime, it would be determined if the object responds to that message. When this is checked only at runtime, it's called dynamic typing.\n\nWith static typing, this is checked at compile time. When you use ARC, the compiler checks a lot more types at compile time, because it needs to know which objects it works with. For example, you can not write the following code anymore:\n\n```objc\n[myObject hello]\n```\n\nIf there's no `hello` method defined anywhere in your program.\n\n#### Other Analyses\n\nThere are a lot of other analyses that clang does for you. If you clone the clang repository and go to `lib/StaticAnalyzer/Checkers`, you'll see all the static checkers. For example, there's `ObjCUnusedIVarsChecker.cpp`, which checks if ivars are unused. Or there is `ObjCSelfInitChecker.cpp`, which checks if you called `[self initWith...]` or `[super init]` before you start using `self` inside your initializer. Some of the other checks happen in other parts of the compiler. For example, in line 2,534 of `lib/Sema/SemaExprObjC.cpp`, you can see the line that does the following:\n\n```objc\n Diag(SelLoc, diag::warn_arc_perform_selector_leaks);\n```\n\nWhich produces the dreaded \"performSelector may cause a leak because its selector is unknown\" warning.\n\n## Code Generation\n\nNow, once your code is fully tokenized, parsed, and analyzed by clang, it can generate the LLVM code for you. To see what happens, we can have a look again at the program `hello.c`:\n\n```objc\n#include <stdio.h>\n\nint main() {\n  printf(\"hello world\\n\");\n  return 0;\n}\n```\n\nTo compile this into LLVM IR we can issue the following command:\n\n```\nclang -O3 -S -emit-llvm hello.c -o hello.ll\n```\n\nThis generates a `hello.ll` file which gives us the following output:    \n\n```\n; ModuleID = 'hello.c'\ntarget datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\ntarget triple = \"x86_64-apple-macosx10.9.0\"\n\n@str = private unnamed_addr constant [12 x i8] c\"hello world\\00\"\n\n; Function Attrs: nounwind ssp uwtable\ndefine i32 @main() #0 {\n  %puts = tail call i32 @puts(i8* getelementptr inbounds ([12 x i8]* @str, i64 0, i64 0))\n  ret i32 0\n}\n\n; Function Attrs: nounwind\ndeclare i32 @puts(i8* nocapture readonly) #1\n\nattributes #0 = { nounwind ssp uwtable \"less-precise-fpmad\"=\"false\" \"no-frame-pointer-elim\"=\"true\" \"no-frame-pointer-elim-non-leaf\" \"no-infs-fp-math\"=\"false\" \"no-nans-fp-math\"=\"false\" \"stack-protector-buffer-size\"=\"8\" \"unsafe-fp-math\"=\"false\" \"use-soft-float\"=\"false\" }\nattributes #1 = { nounwind }\n\n!llvm.ident = !{!0}\n\n!0 = metadata !{metadata !\"Apple LLVM version 6.0 (clang-600.0.41.2) (based on LLVM 3.5svn)\"}\n```\n\nYou can see that the `main` function is only two lines: one to print the string and one to return `0`.\n\nIt's also interesting to do the same thing for a very simple Objective-C program `five.m` that we compile and then view using `LLVM-dis < five.bc | less`:\n\n```objc\n#include <stdio.h>\n#import <Foundation/Foundation.h>\n\nint main() {\n  NSLog(@\"%@\", [@5 description]);\n  return 0;\n}\n```\n\nThere are a lot more things going on around, but here's the `main` function:\n\n```\ndefine i32 @main() #0 {\n  %1 = load %struct._class_t** @\"\\01L_OBJC_CLASSLIST_REFERENCES_$_\", align 8\n  %2 = load i8** @\"\\01L_OBJC_SELECTOR_REFERENCES_\", align 8, !invariant.load !5\n  %3 = bitcast %struct._class_t* %1 to i8*\n  %4 = tail call %0* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to %0* (i8*, i8*, i32)*)(i8* %3, i8* %2, i32 5)\n  %5 = load i8** @\"\\01L_OBJC_SELECTOR_REFERENCES_2\", align 8, !invariant.load !5\n  %6 = bitcast %0* %4 to i8*\n  %7 = tail call %1* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to %1* (i8*, i8*)*)(i8* %6, i8* %5)\n  tail call void (i8*, ...)* @NSLog(i8* bitcast (%struct.NSConstantString* @_unnamed_cfstring_ to i8*), %1* %7)\n  ret i32 0\n}\n```\n\nThe most important lines are line 4, which creates the `NSNumber` object, line\n`7`, which sends the `description` message to the number object, and line 8,\nwhich logs the string returned from the `description` message.\n\n### Optimizations\n\nTo see which kind of optimizations LLVM and clang can do, it's interesting to look at a slightly more complicated C example, the recursively defined `factorial` function:\n\n```objc\n#include <stdio.h>\n\nint factorial(int x) {\n   if (x > 1) return x * factorial(x-1);\n   else return 1;\n}\n\nint main() {\n  printf(\"factorial 10: %d\\n\", factorial(10));\n}\n```\n\nTo compile this without optimizations, run the following command:\n\n```\nclang -O0 -S -emit-llvm factorial.c -o factorial.ll\n```\n\nThe interesting part is to look at the code generated for the `factorial` function:\n\n```objc\ndefine i32 @factorial(i32 %x) #0 {\n  %1 = alloca i32, align 4\n  %2 = alloca i32, align 4\n  store i32 %x, i32* %2, align 4\n  %3 = load i32* %2, align 4\n  %4 = icmp sgt i32 %3, 1\n  br i1 %4, label %5, label %11\n\n; <label>:5                                       ; preds = %0\n  %6 = load i32* %2, align 4\n  %7 = load i32* %2, align 4\n  %8 = sub nsw i32 %7, 1\n  %9 = call i32 @factorial(i32 %8)\n  %10 = mul nsw i32 %6, %9\n  store i32 %10, i32* %1\n  br label %12\n\n; <label>:11                                      ; preds = %0\n  store i32 1, i32* %1\n  br label %12\n\n; <label>:12                                      ; preds = %11, %5\n  %13 = load i32* %1\n  ret i32 %13\n}\n```\n\nYou can see that at the line marked with `%9`, it calls itself recursively. This is quite inefficient, because the stack increases with each recursive call. To turn on optimizations, we can pass the flag `-O3` to clang:\n\n```\nclang -O3 -S -emit-llvm factorial.c -o factorial.ll\n```\n\nNow the code for the `factorial` function looks like this:\n\n```\ndefine i32 @factorial(i32 %x) #0 {\n  %1 = icmp sgt i32 %x, 1\n  br i1 %1, label %tailrecurse, label %tailrecurse._crit_edge\n\ntailrecurse:                                      ; preds = %tailrecurse, %0\n  %x.tr2 = phi i32 [ %2, %tailrecurse ], [ %x, %0 ]\n  %accumulator.tr1 = phi i32 [ %3, %tailrecurse ], [ 1, %0 ]\n  %2 = add nsw i32 %x.tr2, -1\n  %3 = mul nsw i32 %x.tr2, %accumulator.tr1\n  %4 = icmp sgt i32 %2, 1\n  br i1 %4, label %tailrecurse, label %tailrecurse._crit_edge\n\ntailrecurse._crit_edge:                           ; preds = %tailrecurse, %0\n  %accumulator.tr.lcssa = phi i32 [ 1, %0 ], [ %3, %tailrecurse ]\n  ret i32 %accumulator.tr.lcssa\n}\n```\n\nEven though our function was not written in a [tail-recursive](http://en.wikipedia.org/wiki/Tail_call) way, clang can still optimize it and now it's just one function with a loop. There are a lot more optimizations that clang will do on your code. A good example of what gcc can do is at [ridiculousfish.com](http://ridiculousfish.com/blog/posts/will-it-optimize.html).\n\n**More Reading**\n\n* [LLVM blog: posts tagged 'optimization'](http://blog.llvm.org/search/label/optimization)\n* [LLVM blog: vectorization improvements](http://blog.llvm.org/2013/05/llvm-33-vectorization-improvements.html)\n* [LLVM blog: greedy register allocation](http://blog.llvm.org/2011/09/greedy-register-allocation-in-llvm-30.html)\n* [The Polly project](http://polly.llvm.org/index.html)\n\n## How to Use this to Your Advantage\n\nNow that we've seen a full compile, from tokenization to parsing, from abstract syntax trees to analysis and compilation, we can wonder: why should we care?\n\n### Using libclang or clang Plugins\n\nThe cool thing about clang is that it's open-source and a really well-built project: almost everything is a library. That means that it's possible to create your own version of clang and only change those parts that you need. For example, you can change the way clang generates code, add better type checking, or perform your analyses. There are a lot of ways in which you can do this; the easiest way is by using a C library called [libclang](http://clang.llvm.org/doxygen/group__CINDEX.html). libclang provides you with a simple C API to clang, and you can use it to analyze all your sources. However, in my experience, as soon as you want to do something that's a bit more advanced, libclang is too limited. Also, there's [ClangKit](https://github.com/macmade/ClangKit), which is an Objective-C wrapper around some of the functionality provided by clang.\n\nAnother way is to use the C++ library provided by clang by directly using LibTooling. This is a lot more work, and involves C++, but gives you the full power of clang. You can do any kind of analyses, or even rewrite programs. If you want to add custom analyses to clang, want to write your own refactorer, need to rewrite a massive code base, or want to generate graphs and documentation from your project, LibTooling is your friend.\n\n### Writing an Analyzer\n\nFollow the instructions on [Tutorial for building tools using LibTooling](http://clang.LLVM.org/docs/LibASTMatchersTutorial.html) to build LLVM, clang, and clang-tools-extra. Be sure to leave some time aside for compilation; even though I have a very fast machine, I was still able to do the dishes during the time LLVM compiled.\n\nNext, go to your LLVM directory and do a `cd ~/llvm/tools/clang/tools/`. In this directory, you can create your own standalone clang tools. As an example, we created a small tool to help us detect correct usage of a library. Clone the [example repository](https://github.com/objcio/issue6-compiler-tool) into this directory, and type `make`. This will provide you with a binary called `example`.\n\nOur use case is as follows: suppose we have an `Observer` class, which\nlooks like this:\n\n```objc\n@interface Observer\n+ (instancetype)observerWithTarget:(id)target action:(SEL)selector;\n@end\n```\n\nNow, we want to check whenever this class is used that the `action` is\na method that exists on the `target` object. We can write a quick C++\nfunction that does this (be warned, this is the first C++ I ever wrote,\nso it's definitely not idiomatic):\n\n```\nvirtual bool VisitObjCMessageExpr(ObjCMessageExpr *E) {\n  if (E->getReceiverKind() == ObjCMessageExpr::Class) {\n    QualType ReceiverType = E->getClassReceiver();\n    Selector Sel = E->getSelector();\n    string TypeName = ReceiverType.getAsString();\n    string SelName = Sel.getAsString();\n    if (TypeName == \"Observer\" && SelName == \"observerWithTarget:action:\") {\n      Expr *Receiver = E->getArg(0)->IgnoreParenCasts();\n      ObjCSelectorExpr* SelExpr = cast<ObjCSelectorExpr>(E->getArg(1)->IgnoreParenCasts());\n      Selector Sel = SelExpr->getSelector();\n      if (const ObjCObjectPointerType *OT = Receiver->getType()->getAs<ObjCObjectPointerType>()) {\n        ObjCInterfaceDecl *decl = OT->getInterfaceDecl();\n        if (! decl->lookupInstanceMethod(Sel)) {\n          errs() << \"Warning: class \" << TypeName << \" does not implement selector \" << Sel.getAsString() << \"\\n\";\n          SourceLocation Loc = E->getExprLoc();\n          PresumedLoc PLoc = astContext->getSourceManager().getPresumedLoc(Loc);\n          errs() << \"in \" << PLoc.getFilename() << \" <\" << PLoc.getLine() << \":\" << PLoc.getColumn() << \">\\n\";\n        }\n      }\n    }\n  }\n  return true;\n}\n```\n\nThis method starts by looking for message expressions that have\n`Observer` as the receiver,  and `observerWithTarget:action:` as the\nselector, and then looks at the target and checks if the method exists.\nOf course, this is a bit of a contrived example, but if you want to\nmechanically verify things in your code base using the AST, this is the\nway to go.\n\n### More clang Possibilities\n\nThere are many more ways in which we can leverage clang. For example,\nit's possible to write compiler plugins (e.g. with the same checker as\nabove) and load them dynamically into your compiler. I haven't tested\nyet, but it should work with Xcode. For example, if you want to get a\nwarning about code style, you could write a clang plugin for that. (For\nsimple checks, see the [Build process](/issue-6/build-process.html)\narticle.)\n\nAlso, if you need to do a massive refactoring of your codebase where the\nnormal refactoring tools built into Xcode or AppCode are not sufficient,\nyou could write a simple refactoring tool using clang. It may sound\ndaunting, but in the tutorials linked below you'll see that it's not\nthat hard.\n\nFinally, if you really need to, you can compile your own clang and\ninstruct Xcode to use that. Again, this is not as hard as it sounds, and\ndefinitely fun to play around with.\n\n##### More Reading\n\n* [Clang Tutorial](https://github.com/loarabia/Clang-tutorial)\n* [X86_64 Assembly Language Tutorial](http://cocoafactory.com/blog/2012/11/23/x86-64-assembly-language-tutorial-part-1/)\n* [Custom clang Build with Xcode (I)](http://clang-analyzer.llvm.org/xcode.html) and [(II)](http://stackoverflow.com/questions/3297986/using-an-external-xcode-clang-static-analyzer-binary-with-additional-checks)\n* [Clang Tutorial (I)](http://kevinaboos.wordpress.com/2013/07/23/clang-tutorial-part-i-introduction/), [(II)](http://kevinaboos.wordpress.com/2013/07/23/clang-tutorial-part-ii-libtooling-example/) and [(III)](http://kevinaboos.wordpress.com/2013/07/29/clang-tutorial-part-iii-plugin-example/)\n* [Clang Plugin Tutorial](http://getoffmylawnentertainment.com/blog/2011/10/01/clang-plugin-development-tutorial/)\n* [LLVM blog: What every C programmer should know (I)](http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html) , [(II)](http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html) and [(III)](http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html)\n\n"
  },
  {
    "path": "2013-11-08-editorial.md",
    "content": "---\r\ntitle:  \"Editorial\"\r\ncategory: \"6\"\r\ndate: \"2013-11-08 12:00:00\"\r\ntags: editorial\r\n---\r\n\r\nWelcome to objc.io issue #6!\r\n\r\nThis month we're celebrating our first small milestone -- half a year of objc.io!\r\n\r\nOn this occasion, we've made several changes and additions to objc.io. You have probably already noticed that we enhanced our website. It's now showcasing beautiful covers for each issue, while increasing clarity and preparing it for more issues to come.\r\n\r\nFurthermore, objc.io is now also available as a [Newsstand app](https://itunes.apple.com/de/app/objc.io/id683718429) for iPhone and iPad with a monthly subscription. We will continue to publish all content on our website that is freely available to everyone. But if objc.io is valuable to you and you would like to support this project, the Newsstand app gives you a way to do so. We greatly appreciate your support!\r\n\r\nWe've added a [subscription](/subscribe.html) page to give you a better overview of all the ways to stay up-to-date. Next to the new objc.io app, we've also added support for Maverick's website push notifications.\r\n\r\nNow objc.io wouldn't be the same without all the fantastic contributions from the community. Thus far, fifteen developers have guest-authored sixteen articles in the first six issues. To make their contributions more visible, we've added a [contributors](/contributors.html) page. Thank you so much for helping out!\r\n\r\nLast but not least, objc.io also wouldn't be the same without you, the readers. We work really hard each month to write the best articles we can imagine. Your response has surpassed everything we imagined, and we have received so much great feedback, which easily makes all the effort we put into this project worthwhile. \r\n\r\nIn the first five months, we've served more than half a million page views. October has been the busiest month yet, with more than 50GB served across more than one million requests. Our mailing list is approaching 7,000 subscribers, and almost 4,500 people are following us on Twitter. Thank you so much!\r\n\r\nThat said, please go ahead and enjoy reading about the build process in this issue. This month, Michele Titolo and Mattes Groeger helped us out with articles about CocoaPods internals and continuous integration with Travis CI.\r\n\r\nAll the best from Berlin,\r\n\r\nChris, Daniel, and Florian.\r\n"
  },
  {
    "path": "2013-11-08-mach-o-executables.md",
    "content": "---\ntitle: \"Mach-O Executables\"\ncategory: \"6\"\ndate: \"2013-11-08 09:00:00\"\ntags: article\nauthor:\n  - name: Daniel Eggert\n    url: http://twitter.com/danielboedewadt\n---\n\nWhen we build an application in Xcode, part of what happens is that the sources files (`.m` and `.h`) get turned into an executable. This executable contains the byte code than will run on the CPU, the ARM processor on the iOS device, or the Intel processor on your Mac.\n\nWe'll walk through some of what the compiler does and what's inside such an executable. There's more to it than first meets the eye.\n\nLet's put Xcode aside for this and step into the land of command-line tools. When we build in Xcode, it simply calls a series of tools. Florian discusses how this works in more detail. We'll call these tools directly and take a look at what they do.\n\nHopefully this will give you a better understanding of how an executable on iOS or OS X -- a so-called *Mach-O executable* -- works and is put together.\n\n## xcrun\n\nSome infrastructure first: There's a command-line tool called `xcrun` which we'll use a lot. It may seem odd, but it's pretty awesome. This little tool is used to run other tools. Instead of running:\n\n```\n% clang -v\n```\n\nOn the Terminal, we'll use:\n\n```\n% xcrun clang -v\n```\n\nWhat `xcrun` does is to locate `clang` and run it with the arguments that follow `clang`.\n\nWhy would we do this? It may seem pointless. But `xcrun` allows us to (1) have multiple versions of Xcode and use the tools from a specific Xcode version, and (2) use the tools for a specific SDK (software development kit). If you happen to have both Xcode 4.5 and Xcode 5, with `xcode-select` and `xcrun` you can choose to use the tools (and header files, etc.) from the iOS SDK from Xcode 5, or the OS X tools from Xcode 4.5. On most other platforms, that'd be close to impossible. Check out the man pages for `xcrun` and `xcode-select` for more details. And you can use the developer tools from the command line without installing the *Command Line Tools*.\n\n\n## Hello World Without an IDE\n\nBack in Terminal, let's create a folder with a C file in it:\n\n```\n% mkdir ~/Desktop/objcio-command-line\n% cd !$\n% touch helloworld.c\n```\n\nNow edit this file in your favorite text editor -- even TextEdit.app will do:\n\n```\n% open -e helloworld.c\n```\n\nFill in this piece of code:\n\n```objc\n#include <stdio.h>\nint main(int argc, char *argv[])\n{\n    printf(\"Hello World!\\n\");\n    return 0;\n}\n```\n\nSave and return to Terminal to run this:\n\n```\n% xcrun clang helloworld.c\n% ./a.out\n```\n\nYou should now see a lovely `Hello World!` message on your terminal. You compiled a C program and ran it. All without an IDE. Take a deep breath. Rejoice.\n\nWhat did we just do here? We compiled `helloworld.c` into a Mach-O binary called `a.out`. That is the default name the compiler will use unless we specify something else.\n\nHow did this binary get generated? There are multiple pieces to look at and understand. We'll look at the compiler first.\n\n### Hello World and the Compiler\n\nThe compiler of choice nowadays is `clang` (pronounced /klæŋ/). Chris writes in more detail [about the compiler](/issues/6-build-tools/compiler/).\n\nBriefly put, the compiler will process the `helloworld.c` input file and produce the executable `a.out`. This processing consist of multiple steps/stages. What we just did is run all of them in succession:\n\n**Preprocessing**\n\n* Tokenization\n* Macro expansion\n* `#include` expansion\n\n**Parsing and Semantic Analysis**\n\n* Translates preprocessor tokens into a parse tree\n* Applies semantic analysis to the parse tree\n* Outputs an *Abstract Syntax Tree* (AST)\n\n**Code Generation and Optimization**\n\n* Translates an AST into low-level intermediate code (LLVM IR)\n* Responsible for optimizing the generated code\n* target-specific code generation\n* Outputs assembly\n\n**Assembler**\n\n* Translates assembly code into a target object file\n\n**Linker**\n\n* Merges multiple object files into an executable (or a dynamic library)\n\nLet's see how these steps look for our simple example.\n\n#### Preprocessing\n\nThe first thing the compiler will do is preprocess the file. We can tell clang to show us what it looks like if we stop after that step:\n\n```\n% xcrun clang -E helloworld.c\n```\n\nWow. That will output 413 lines. Let's open that in an editor to see what's going on:\n\n```\n% xcrun clang -E helloworld.c | open -f\n```\n\nAt the very top you'll see lots and lots of lines starting with a `#` (pronounced 'hash'). These are so-called *linemarker* statements that tell us which file the following lines are from. We need this. If you look at the `helloworld.c` file again, you'll see that the first line is:\n\n```\n#include <stdio.h>\n```\n\nWe have all used `#include` and `#import` before. What it does is to tell the preprocessor to insert the content of the file `stdio.h` where the `#include` statement was. This is a recursive process: The `stdio.h` header file in turn includes other files.\n\nSince there's a lot of recursive insertion going on, we need to be able to keep track of where the lines in the resulting source originate from. To do this, the preprocessor inserts a *linemarker* beginning with a `#` whenever the origin changes. The number following the `#` is the line number followed by the name of the file. The numbers at the very end of the line are flags indicating the start of a new file (1), returning to a file (2), that the following is from a system header (3), or that the file is to be treated as wrapped in an `extern\"C\"` block.\n\nIf you scroll to the very end of the output, you'll find our `helloworld.c` code:\n\n```\n# 2 \"helloworld.c\" 2\nint main(int argc, char *argv[])\n{\n printf(\"Hello World!\\n\");\n return 0;\n}\n```\n\nIn Xcode, you can look at the preprocessor output of any file by selecting **Product** -> **Perform Action** -> **Preprocess**. Note that it takes a few seconds for the Editor to load the preprocessed file -- it'll most likely be close to 100,000 lines long.\n\n#### Compilation\n\nNext up: parsing and code generation. We can tell `clang` to output the resulting assembly code like so:\n\n```\n% xcrun clang -S -o - helloworld.c | open -f\n```\n\nLet's take a look at the output. First we'll notice how some lines start with a dot `.`. These are assembler directives. The other ones are actual x86_64 assembly. Finally there are labels, which are similar to labels in C.\n\nLet's start with the first three lines:\n\n```\n    .section    __TEXT,__text,regular,pure_instructions\n    .globl  _main\n    .align  4, 0x90\n```\n\nThese three lines are assembler directives, not assembly code. The `.section` directive specifies into which section the following will go. More about sections in a bit.\n\nNext, the `.globl` directive specifies that `_main` is an external symbol. This is our `main()` function. It needs to be visible outside our binary because the system needs to call it to run the executable.\n\nThe `.align` directive specifies the alignment of what follows. In our case, the following code will be 16 (2^4) byte aligned and padded with `0x90` if needed.\n\nNext up is the preamble for the main function:\n\n```\n_main:                                  ## @main\n    .cfi_startproc\n## BB#0:\n    pushq   %rbp\nLtmp2:\n    .cfi_def_cfa_offset 16\nLtmp3:\n    .cfi_offset %rbp, -16\n    movq    %rsp, %rbp\nLtmp4:\n    .cfi_def_cfa_register %rbp\n    subq    $32, %rsp\n```\n\nThis part has a bunch of labels that work the same way as C labels do. They are symbolic references to certain parts of the assembly code. First is the actual start of our function `_main`. This is also the symbol that is exported. The binary will hence have a reference to this position.\n\nThe `.cfi_startproc` directive is used at the beginning of most functions. CFI is short for Call Frame Information. A *frame* corresponds loosely to a function. When you use the debugger and *step in* or *step out*, you're actually stepping in/out of call frames. In C code, functions have their own call frames, but other things can too. The `.cfi_startproc` directive gives the function an entry into `.eh_frame`, which contains unwind information -- this is how exception can unwind the call frame stack. The directive will also emit architecture-dependent instructions for CFI. It's matched by a corresponding `.cfi_endproc` further down in the output to mark the end of our `main()` function.\n\nNext, there's another label `## BB#0:` and then, finally, the first assembly code: `pushq %rbp`. This is where things get interesting. On OS X, we have x86_64 code, and for this architecture there's a so-called *application binary interface* (ABI) that specifies how function calls work at the assembly code level. Part of this ABI specifies that the `rbp` register (base pointer register) must be preserved across function calls. It's the main function's responsibility to make sure the `rbp` register has the same value once the function returns. `pushq %rbp` pushes its value onto the stack so that we can pop it later.\n\nNext, two more CFI directives: `.cfi_def_cfa_offset 16` and `.cfi_offset %rbp, -16`. Again, these will output information related to generating call frame unwinding information and debug information. We're changing the stack and the base pointer and these two basically tell the debugger where things are -- or rather, they'll cause information to be output which the debugger can later use to find its way.\n\nNow, `movq %rsp, %rbp` will allow us to put local variables onto the stack. `subq $32, %rsp` moves the stack pointer by 32 bytes, which the function can then use. We're first storing the old stack pointer in `rbp` and using that as a base for our local variables, then updating the stack pointer to past the part that we'll use.\n\nNext, we'll call `printf()`:\n\n```\nleaq    L_.str(%rip), %rax\nmovl    $0, -4(%rbp)\nmovl    %edi, -8(%rbp)\nmovq    %rsi, -16(%rbp)\nmovq    %rax, %rdi\nmovb    $0, %al\ncallq   _printf\n```\n\nFirst, `leaq` loads the pointer to `L_.str` into the `rax` register. Note how the `L_.str` label is defined further down in the assembly code. This is our C string `\"Hello World!\\n\"`. The `edi` and `rsi` registers hold the first and second function arguments. Since we'll call another function, we first need to store their current values. That's what we'll use the 32 bytes based off `rbp` we just reserved for. First a 32-bit 0, then the 32-bit value of the `edi` register (which holds `argc`), then the 64-bit value of the `rsi` register (which holds `argv`). We're not using those values later, but since the compiler is running without optimizations, it'll store them anyway.\n\nNow we'll put the first function argument for `printf()`, `rax`, into the first function argument register `edi`. The `printf()` function is a variadic function. The ABI-calling convention specifies that the number of vector registers used to hold arguments need to be stored in the `al` register. In our case it's 0. Finally, `callq` calls the `printf()` function:\n\n```\n    movl    $0, %ecx\n    movl    %eax, -20(%rbp)         ## 4-byte Spill\n    movl    %ecx, %eax\n```\n\nThis sets the `ecx` register to 0, saves (spills) the `eax` register onto the stack, then copies the 0 values in `ecx` into `eax`. The ABI specifies that `eax` will hold the return value of a function, and our `main()` function returns 0:\n\n```\n    addq    $32, %rsp\n    popq    %rbp\n    ret\n    .cfi_endproc\n```\n\nSince we're done, we'll restore the stack pointer by shifting the stack pointer `rsp` back 32 bytes to undo the effect of `subq $32, %rsp` from above. Finally, we'll pop the value of `rbp` we'd stored earlier and then return to the caller with `ret`, which will read the return address off the stack. The `.cfi_endproc` balances the `.cfi_startproc` directive.\n\nNext up is the output for our string literal `\"Hello World!\\n\"`:\n\n```\n    .section    __TEXT,__cstring,cstring_literals\nL_.str:                                 ## @.str\n    .asciz   \"Hello World!\\n\"\n```\n\nAgain, the `.section` directive specifies which section the following needs to go into. The `L_.str` label allows the actual code to get a pointer to the string literal. The `.asciz` directive tells the assembler to output a 0-terminated string literal. \n\nThis starts a new section `__TEXT __cstring`. This section contains C strings:\n\n```\nL_.str:                                 ## @.str\n    .asciz     \"Hello World!\\n\"\n```\n\nAnd these two lines create a null-terminated string. Note how `L_.str` is the name used further up to access the string.\n\nThe final `.subsections_via_symbols` directive is used by the static link editor.\n\nMore information about assembler directives can be found in Apple's [OS X Assembler Reference](https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/Assembler/). The AMD 64 website has documentation on the [application binary interface for x86_64](http://www.x86-64.org/documentation/abi.pdf). It also has a [Gentle Introduction to x86-64 Assembly](http://www.x86-64.org/documentation/assembly.html).\n\nAgain, Xcode lets you review the assembly output of any file by selecting **Product** -> **Perform Action** -> **Assemble**.\n\n#### Assembler\n\nThe assembler, simply put, converts the (human-readable) assembly code into machine code. It creates a target object file, often simply called *object file*. These files have a `.o` file ending. If you build your app with Xcode, you'll find these object files inside the `Objects-normal` folder inside the *derived data* directory of your project.\n\n#### Linker\n\nWe'll talk a bit more about the linker later. But simply put, the linker will resolve symbols between object files and libraries. What does that mean? Recall the\n\n```\ncallq   _printf\n```\n\nstatement. `printf()` is a function in the *libc* library. Somehow, the final executable needs to be able to know where in memory the `printf()` is, i.e. what the address of the `_printf` symbol is. The linker takes all object files (in our case, only one) and the libraries (in our case, implicitly *libc*) and resolves any unknown symbols (in our case, the `_printf`). It then encodes into the final executable that this symbol can be found in *libc*, and the linker then outputs the final executable that can be run: `a.out`.\n\n\n## Sections \n\nAs we mentioned above, there's something called sections. An executable will have multiple sections, i.e. parts. Different parts of the executable will each go into their own section, and each section will in turn go inside a segment. This is true for our trivial app, but also for the binary of a full-blown app.\n\nLet's take a look at the sections of our `a.out` binary. We can use the `size` tool to do that:\n\n```\n% xcrun size -x -l -m a.out \nSegment __PAGEZERO: 0x100000000 (vmaddr 0x0 fileoff 0)\nSegment __TEXT: 0x1000 (vmaddr 0x100000000 fileoff 0)\n    Section __text: 0x37 (addr 0x100000f30 offset 3888)\n    Section __stubs: 0x6 (addr 0x100000f68 offset 3944)\n    Section __stub_helper: 0x1a (addr 0x100000f70 offset 3952)\n    Section __cstring: 0xe (addr 0x100000f8a offset 3978)\n    Section __unwind_info: 0x48 (addr 0x100000f98 offset 3992)\n    Section __eh_frame: 0x18 (addr 0x100000fe0 offset 4064)\n    total 0xc5\nSegment __DATA: 0x1000 (vmaddr 0x100001000 fileoff 4096)\n    Section __nl_symbol_ptr: 0x10 (addr 0x100001000 offset 4096)\n    Section __la_symbol_ptr: 0x8 (addr 0x100001010 offset 4112)\n    total 0x18\nSegment __LINKEDIT: 0x1000 (vmaddr 0x100002000 fileoff 8192)\ntotal 0x100003000\n```\n\nOur `a.out` file has four segments. Some of these have sections.\n\nWhen we run an executable, the VM (virtual memory) system maps the segments into the address space (i.e. into memory) of the process. Mapping is very different in nature, but if you're unfamiliar with the VM system, simply assume that the VM loads the entire executable into memory -- even though that's not what's really happening. The VM pulls some tricks to avoid having to do so.\n\nWhen the VM system does this mapping, segments and sections are mapped with different properties, namely different permissions.\n\nThe `__TEXT` segment contains our code to be run. It's mapped as read-only and executable. The process is allowed to execute the code, but not to modify it. The code can not alter itself, and these mapped pages can therefore never become dirty.\n\nThe `__DATA` segment is mapped read/write but non-executable. It contains values that need to be updated.\n\nThe first segment is `__PAGEZERO`. It's 4GB large. Those 4GB are not actually in the file, but the file specifies that the first 4GB of the process' address space will be mapped as non-executable, non-writable, non-readable. This is why you'll get an `EXC_BAD_ACCESS` when reading from or writing to a `NULL` pointer, or some other value that's (relatively) small. It's the operating system trying to prevent you from [causing havoc](http://www.xkcd.com/371/).\n\nWithin segments, there are sections. These contain distinct parts of the executable. In the `__TEXT` segment, the `__text` section contains the compiled machine code. `__stubs` and `__stub_helper` are used for the dynamic linker (`dyld`). This allows for lazily linking in dynamically linked code. `__const` (which we don't have in our example) are constants, and similarly `__cstring` contains the literal string constants of the executable (quoted strings in source code).\n\nThe `__DATA` segment contains read/write data. In our case we only have `__nl_symbol_ptr` and `__la_symbol_ptr`, which are *non-lazy* and *lazy* symbol pointers, respectively. The lazy symbol pointers are used for so-called undefined functions called by the executable, i.e. functions that are not within the executable itself. They're lazily resolved. The non-lazy symbol pointers are resolved when the executable is loaded.\n\nOther common sections in the `__DATA` segment are `__const`, which will contain constant data which needs relocation. An example is `char * const p = \"foo\";` -- the data pointed to by `p` is not constant. The `__bss` section contains uninitialized static variables such as `static int a;` -- the ANSI C standard specifies that static variables must be set to zero. But they can be changed at run time. The `__common` section contains uninitialized external globals, similar to `static` variables. An example would be `int a;` outside a function block. Finally, `__dyld` is a placeholder section, used by the dynamic linker.\n\nApple's [OS X Assembler Reference](https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/Assembler/) has more information about some of the section types.\n\n\n### Section Content\n\nWe can inspect the content of a section with `otool(1)` like so:\n\n```\n% xcrun otool -s __TEXT __text a.out \na.out:\n(__TEXT,__text) section\n0000000100000f30 55 48 89 e5 48 83 ec 20 48 8d 05 4b 00 00 00 c7 \n0000000100000f40 45 fc 00 00 00 00 89 7d f8 48 89 75 f0 48 89 c7 \n0000000100000f50 b0 00 e8 11 00 00 00 b9 00 00 00 00 89 45 ec 89 \n0000000100000f60 c8 48 83 c4 20 5d c3 \n```\n\nThis is the code of our app. Since `-s __TEXT __text` is very common, `otool` has a shortcut to it with the `-t` argument. We can even look at the disassembled code by adding `-v`:\n\n```\n% xcrun otool -v -t a.out\na.out:\n(__TEXT,__text) section\n_main:\n0000000100000f30    pushq   %rbp\n0000000100000f31    movq    %rsp, %rbp\n0000000100000f34    subq    $0x20, %rsp\n0000000100000f38    leaq    0x4b(%rip), %rax\n0000000100000f3f    movl    $0x0, 0xfffffffffffffffc(%rbp)\n0000000100000f46    movl    %edi, 0xfffffffffffffff8(%rbp)\n0000000100000f49    movq    %rsi, 0xfffffffffffffff0(%rbp)\n0000000100000f4d    movq    %rax, %rdi\n0000000100000f50    movb    $0x0, %al\n0000000100000f52    callq   0x100000f68\n0000000100000f57    movl    $0x0, %ecx\n0000000100000f5c    movl    %eax, 0xffffffffffffffec(%rbp)\n0000000100000f5f    movl    %ecx, %eax\n0000000100000f61    addq    $0x20, %rsp\n0000000100000f65    popq    %rbp\n0000000100000f66    ret\n```\n\nThis is the same stuff, this time disassembled. It should look familiar -- it's what we looked at a bit further back when compiling the code. The only difference is that we don't have any of the assembler directives in the code anymore; this is the bare binary executable.\n\nIn a similar fashion, we can look at other sections:\n\n```\n% xcrun otool -v -s __TEXT __cstring a.out\na.out:\nContents of (__TEXT,__cstring) section\n0x0000000100000f8a  Hello World!\\n\n```\n\nOr:\n\n```\n% xcrun otool -v -s __TEXT __eh_frame a.out \na.out:\nContents of (__TEXT,__eh_frame) section\n0000000100000fe0    14 00 00 00 00 00 00 00 01 7a 52 00 01 78 10 01 \n0000000100000ff0    10 0c 07 08 90 01 00 00 \n```\n\n#### Side Note on Performance\n\nOn a side note: The `__DATA` and `__TEXT` segments have performance implications. If you have a very large binary, you might want to check out Apple's documentation on [Code Size Performance Guidelines](https://developer.apple.com/legacy/library/documentation/Performance/Conceptual/CodeFootprint/CodeFootprint.pdf). Moving data into the `__TEXT` segment is beneficial, because those pages are never dirty. \n\n#### Arbitrary Sections\n\nYou can add arbitrary data as a section to your executable with the `-sectcreate` linker flag. This is how you'd add a Info.plist to a single file executable. The Info.plist data needs to go into a `__info_plist` section of the `__TEXT` segment. You'd pass `-sectcreate segname sectname file` to the linker by passing\n\n```\n-Wl,-sectcreate,__TEXT,__info_plist,path/to/Info.plist\n```\n\nto clang. Similarly, `-sectalign` specifies the alignment. If you're adding an entirely new segment, check out `-segprot` to specify the protection (read/write/executable) of the segment. These are all documented in the main page for the linker, i.e. `ld(1)`.\n\nYou can get to sections using the functions defined in `/usr/include/mach-o/getsect.h`, namely `getsectdata()`, which will give you a pointer to the sections data and return its length by reference.\n\n### Mach-O\n\nExecutables on OS X and iOS are [Mach-O](https://en.wikipedia.org/wiki/Mach-o) executables:\n\n```\n% file a.out \na.out: Mach-O 64-bit executable x86_64\n```\n\nThis is true for GUI applications too:\n\n```\n% file /Applications/Preview.app/Contents/MacOS/Preview \n/Applications/Preview.app/Contents/MacOS/Preview: Mach-O 64-bit executable x86_64\n```\n\nApple has detailed information about the [Mach-O file format](https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/MachORuntime/index.html).\n\nWe can use `otool(1)` to peek into the executable's Mach header. It specifies what this file is and how it's to be loaded. We'll use the `-h` flag to print the header information:\n\n```\n% otool -v -h a.out           a.out:\nMach header\n      magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags\nMH_MAGIC_64  X86_64        ALL LIB64     EXECUTE    16       1296   NOUNDEFS DYLDLINK TWOLEVEL PIE\n```\n\nThe `cputype` and `cpusubtype` specify the target architecture this executable can run on. The `ncmds` and `sizeofcmds` are the load commands which we can look at with the `-l` argument:\n\n```\n% otool -v -l a.out | open -f\na.out:\nLoad command 0\n      cmd LC_SEGMENT_64\n  cmdsize 72\n  segname __PAGEZERO\n   vmaddr 0x0000000000000000\n   vmsize 0x0000000100000000\n...\n```\n\nThe load commands specify the logical structure of the file and its layout in virtual memory. Most of the information `otool` prints out is derived from these load commands. Looking at the `Load command 1` part, we find `initprot r-x`, which specifies the protection mentioned above: read-only (no-write) and executable.\n\nFor each segment and each section within a segment, the load command specifies where in memory it should end up, and with what protection, etc. Here's the output for the `__TEXT __text` section:\n\n```\nSection\n  sectname __text\n   segname __TEXT\n      addr 0x0000000100000f30\n      size 0x0000000000000037\n    offset 3888\n     align 2^4 (16)\n    reloff 0\n    nreloc 0\n      type S_REGULAR\nattributes PURE_INSTRUCTIONS SOME_INSTRUCTIONS\n reserved1 0\n reserved2 0\n```\n\nOur code will end up in memory at 0x100000f30. Its offset in the file is 3888. If you look at the disassembly output from before of `xcrun otool -v -t a.out`, you'll see that the code is, in fact, at 0x100000f30.\n\nWe can also take a look at which dynamic libraries the executable is using:\n\n```\n% otool -v -L a.out\na.out:\n    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)\n    time stamp 2 Thu Jan  1 01:00:02 1970\n```\n\nThis is where our executable will find the `_printf` symbol it's using.\n\n\n\n## A More Complex Sample\n\nLet's look at a slightly more complex sample with three files:\n\n`Foo.h`:\n\n```objc\n#import <Foundation/Foundation.h>\n\n@interface Foo : NSObject\n\n- (void)run;\n\n@end\n```\n\n`Foo.m`:\n\n```objc\n#import \"Foo.h\"\n\n@implementation Foo\n\n- (void)run\n{\n    NSLog(@\"%@\", NSFullUserName());\n}\n\n@end\n```\n\n`helloworld.m`:\n\n```objc\n#import \"Foo.h\"\n\nint main(int argc, char *argv[])\n{\n    @autoreleasepool {\n        Foo *foo = [[Foo alloc] init];\n        [foo run];\n        return 0;\n    }\n}\n```\n\n### Compiling Multiple Files\n\nIn this sample, we have more than one file. We therefore need to tell clang to first generate object files for each input file:\n\n```\n% xcrun clang -c Foo.m\n% xcrun clang -c helloworld.m\n```\n\nWe're never compiling the header file. Its purpose is simply to share code between the implementation files which do get compiled. Both `Foo.m` and `helloworld.m` pull in the content of the `Foo.h` through the `#import` statement.\n\nWe end up with two object files:\n\n```\n% file helloworld.o Foo.o\nhelloworld.o: Mach-O 64-bit object x86_64\nFoo.o:        Mach-O 64-bit object x86_64\n```\n\nIn order to generate an executable, we need to link these two object files and the Foundation framework with each other:\n\n```\nxcrun clang helloworld.o Foo.o -Wl,`xcrun --show-sdk-path`/System/Library/Frameworks/Foundation.framework/Foundation\n```\n\nWe can now run our code:\n\n```\n% ./a.out \n2013-11-03 18:03:03.386 a.out[8302:303] Daniel Eggert\n```\n\n### Symbols and Linking\n\nOur small app was put together from two object files. The `Foo.o` object file contains the implementation of the `Foo` class, and the `helloworld.o` object file contains the `main()` function and calls/uses the `Foo` class.\n\nFurthermore, both of these use the Foundation framework. The `helloworld.o` object file uses it for the autorelease pool, and it indirectly uses the Objective-C runtime in form of the `libobjc.dylib`. It needs the runtime functions to make message calls. This is similar to the `Foo.o` object file.\n\nAll of these are represented as so-called *symbols*. We can think of a symbol as something that'll be a pointer once the app is running, although its nature is slightly different.\n\nEach function, global variable, class, etc. that is defined or used results in a symbol. When we link object files into an executable, the linker (`ld(1)`) resolves symbols as needed between object files and dynamic libraries.\n\nExecutables and object files have a symbol table that specify their symbols. If we take a look at the `helloworld.o` object file with the `nm(1)` tool, we get this:\n\n```\n% xcrun nm -nm helloworld.o\n                 (undefined) external _OBJC_CLASS_$_Foo\n0000000000000000 (__TEXT,__text) external _main\n                 (undefined) external _objc_autoreleasePoolPop\n                 (undefined) external _objc_autoreleasePoolPush\n                 (undefined) external _objc_msgSend\n                 (undefined) external _objc_msgSend_fixup\n0000000000000088 (__TEXT,__objc_methname) non-external L_OBJC_METH_VAR_NAME_\n000000000000008e (__TEXT,__objc_methname) non-external L_OBJC_METH_VAR_NAME_1\n0000000000000093 (__TEXT,__objc_methname) non-external L_OBJC_METH_VAR_NAME_2\n00000000000000a0 (__DATA,__objc_msgrefs) weak private external l_objc_msgSend_fixup_alloc\n00000000000000e8 (__TEXT,__eh_frame) non-external EH_frame0\n0000000000000100 (__TEXT,__eh_frame) external _main.eh\n```\n\nThese are all symbols of that file. `_OBJC_CLASS_$_Foo` is the symbol as the `Foo` Objective-C class. It's an *undefined, external* symbol of the `Foo` class. *External* means it's not private to this object file, as opposed to `non-external` symbols which are private to the particular object file. Our `helloworld.o` object file references the class `Foo`, but it doesn't implement it. Hence, its symbol table ends up having an entry marked as undefined.\n\nNext, the `_main` symbol for the `main()` function is also *external* because it needs to be visible in order to get called. It, however, is implemented in `helloworld.o` as well, and resides at address 0 and needs to go into the `__TEXT,__text` section. Then there are four Objective-C runtime functions. These are also undefined and need to be resolved by the linker.\n\nIf we turn toward the `Foo.o` object file, we get this output:\n\n```\n% xcrun nm -nm Foo.o\n0000000000000000 (__TEXT,__text) non-external -[Foo run]\n                 (undefined) external _NSFullUserName\n                 (undefined) external _NSLog\n                 (undefined) external _OBJC_CLASS_$_NSObject\n                 (undefined) external _OBJC_METACLASS_$_NSObject\n                 (undefined) external ___CFConstantStringClassReference\n                 (undefined) external __objc_empty_cache\n                 (undefined) external __objc_empty_vtable\n000000000000002f (__TEXT,__cstring) non-external l_.str\n0000000000000060 (__TEXT,__objc_classname) non-external L_OBJC_CLASS_NAME_\n0000000000000068 (__DATA,__objc_const) non-external l_OBJC_METACLASS_RO_$_Foo\n00000000000000b0 (__DATA,__objc_const) non-external l_OBJC_$_INSTANCE_METHODS_Foo\n00000000000000d0 (__DATA,__objc_const) non-external l_OBJC_CLASS_RO_$_Foo\n0000000000000118 (__DATA,__objc_data) external _OBJC_METACLASS_$_Foo\n0000000000000140 (__DATA,__objc_data) external _OBJC_CLASS_$_Foo\n0000000000000168 (__TEXT,__objc_methname) non-external L_OBJC_METH_VAR_NAME_\n000000000000016c (__TEXT,__objc_methtype) non-external L_OBJC_METH_VAR_TYPE_\n00000000000001a8 (__TEXT,__eh_frame) non-external EH_frame0\n00000000000001c0 (__TEXT,__eh_frame) non-external -[Foo run].eh\n```\n\nThe fifth-to-last line shows that `_OBJC_CLASS_$_Foo` is defined and external to `Foo.o` -- it has this class's implementation.\n\n`Foo.o` also has undefined symbols. First and foremost are the symbols for `NSFullUserName()`, `NSLog()`, and `NSObject` that we're using.\n\nWhen we link these two object files and the Foundation framework (which is a dynamic library), the linker tries to resolve all undefined symbols. It can resolve `_OBJC_CLASS_$_Foo` that way. For the others, it will need to use the Foundation framework.\n\nWhen the linker resolves a symbol through a dynamic library (in our case, the Foundation framework), it will record inside the final linked image that the symbol will be resolved with that dynamic library. The linker records that the output file depends on that particular dynamic library, and what the path of it is. That's what happens with the `_NSFullUserName`, `_NSLog`, `_OBJC_CLASS_$_NSObject`, `_objc_autoreleasePoolPop`, etc. symbols in our case.\n\nWe can look at the symbol table of the final executable `a.out` and see how the linker resolved all the symbols:\n\n```\n% xcrun nm -nm a.out \n                 (undefined) external _NSFullUserName (from Foundation)\n                 (undefined) external _NSLog (from Foundation)\n                 (undefined) external _OBJC_CLASS_$_NSObject (from CoreFoundation)\n                 (undefined) external _OBJC_METACLASS_$_NSObject (from CoreFoundation)\n                 (undefined) external ___CFConstantStringClassReference (from CoreFoundation)\n                 (undefined) external __objc_empty_cache (from libobjc)\n                 (undefined) external __objc_empty_vtable (from libobjc)\n                 (undefined) external _objc_autoreleasePoolPop (from libobjc)\n                 (undefined) external _objc_autoreleasePoolPush (from libobjc)\n                 (undefined) external _objc_msgSend (from libobjc)\n                 (undefined) external _objc_msgSend_fixup (from libobjc)\n                 (undefined) external dyld_stub_binder (from libSystem)\n0000000100000000 (__TEXT,__text) [referenced dynamically] external __mh_execute_header\n0000000100000e50 (__TEXT,__text) external _main\n0000000100000ed0 (__TEXT,__text) non-external -[Foo run]\n0000000100001128 (__DATA,__objc_data) external _OBJC_METACLASS_$_Foo\n0000000100001150 (__DATA,__objc_data) external _OBJC_CLASS_$_Foo\n```\n\nWe see that all the Foundation and Objective-C runtime symbols are still undefined, but the symbol table now has information about how to resolve them, i.e. in which dynamic library they're to be found.\n\nThe executable also knows where to find these libraries:\n\n```\n% xcrun otool -L a.out\na.out:\n    /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1056.0.0)\n    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)\n    /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 855.11.0)\n    /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)\n```\n\nThese undefined symbols are resolved by the dynamic linker `dyld(1)` at runtime. When we run the executable, `dyld` will make sure that `_NSFullUserName`, etc. point to their implementation inside Foundation, etc.\n\nWe can run `nm(1)` against Foundation and check that these symbols are, in fact, defined there:\n\n```\n% xcrun nm -nm `xcrun --show-sdk-path`/System/Library/Frameworks/Foundation.framework/Foundation | grep NSFullUserName\n0000000000007f3e (__TEXT,__text) external _NSFullUserName \n```\n\n### The Dynamic Link Editor\n\nThere are a few environment variables that can be useful to see what `dyld` is up to. First and foremost, `DYLD_PRINT_LIBRARIES`. If set, `dyld` will print out what libraries are loaded:\n\n```\n% (export DYLD_PRINT_LIBRARIES=; ./a.out )\ndyld: loaded: /Users/deggert/Desktop/command_line/./a.out\ndyld: loaded: /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation\ndyld: loaded: /usr/lib/libSystem.B.dylib\ndyld: loaded: /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation\ndyld: loaded: /usr/lib/libobjc.A.dylib\ndyld: loaded: /usr/lib/libauto.dylib\n[...]\n```\n\nThis will show you all seventy dynamic libraries that get loaded as part of loading Foundation. That's because Foundation depends on other dynamic libraries, which, in turn, depend on others, and so forth. You can run\n\n```\n% xcrun otool -L `xcrun --show-sdk-path`/System/Library/Frameworks/Foundation.framework/Foundation\n```\n\nto see a list of the fifteen dynamic libraries that Foundation uses.\n\n\n### The dyld's Shared Cache\n\nWhen you're building a real-world application, you'll be linking against various frameworks. And these in turn will use countless other frameworks and dynamic libraries. The list of all dynamic libraries that need to get loaded gets large quickly. And the list of interdependent symbols even more so. There will be thousands of symbols to resolve. This works takes a long time: several seconds.\n\nIn order to shortcut this process, the dynamic linker on OS X and iOS uses a shared cache that lives inside `/var/db/dyld/`. For each architecture, the OS has a single file that contains almost all dynamic libraries already linked together into a single file with their interdependent symbols resolved. When a Mach-O file (an executable or a library) is loaded, the dynamic linker will first check if it's inside this *shared cache* image, and if so, use it from the shared cache. Each process has this dyld shared cache mapped into its address space already. This method dramatically improves launch time on OS X and iOS.\n"
  },
  {
    "path": "2013-11-08-travis-ci.markdown",
    "content": "---\ntitle: \"Travis CI for iOS\"\ncategory: \"6\"\ndate: \"2013-11-08 07:00:00\"\ntags: article\nauthor:\n  - name: Mattes Groeger\n    url: https://twitter.com/MattesGroeger\n---\n\nHave you ever tried to set up a [continuous integration](http://en.wikipedia.org/wiki/Continuous_integration) server for iOS? From my personal experience, it is not easy. You have to organize a Mac and install software and plugins. You have to manage user accounts and provide security. You have to grant access to the repositories and configure all build steps and certificates. During the project lifetime, you have to keep the server healthy and up to date.\n\nIn the end, you will spend a lot of time maintaining the server -- time you wanted to save in the first place. But if your project is hosted on [GitHub](https://github.com/), there is hope: [Travis CI](https://travis-ci.org/). This service offers continuous integration, which means it takes care of all the hosting aspects. In the [Ruby](https://www.ruby-lang.org/) world, Travis CI is well-known already. Since April 2013, the software supports iOS and Mac as well.\n\nIn this article, I want to show you step by step how to set up your project for Travis. This does not only include building and running unit tests, but also shipping the app to all your test devices. For demonstration purposes, I put an [example project](https://github.com/objcio/issue-6-travis-ci) on GitHub. At the end of this article, I will give some tips on how to tackle problems with Travis.\n\n<!-- More -->\n\n### GitHub Integration\n\nWhat I really like about Travis is how well it is embedded in the GitHub web UI. One example is pull requests. Travis will run the build for each request. The pull request on GitHub will look like this if everything is ok:\n\n![](/images/issue-6/github_ready_to_merge.jpg)\n\nIn case the build doesn't succeed, the page will be colored accordingly on GitHub:\n\n![](/images/issue-6/github_merge_with_caution.jpg)\n\n## Link Travis and GitHub\n\nLet's have a look on how to link your GitHub project with Travis. Sign in on [the Travis website](https://travis-ci.org/) using your GitHub account. For private repositories, you need to sign up for a [Travis Pro account](https://magnum.travis-ci.com).\n\nOnce signed in, you have to enable your project for Travis. Navigate to the [profile page](https://travis-ci.org/profile), which lists all your GitHub projects. Please note, when you create a new repository later, use the `Sync now` button. Travis only updates the list occasionally.\n\n![](/images/issue-6/objc_travis_flick.jpg)\n\nEnable your project now by using the switch. Afterward, you should see the Travis hook in your GitHub project settings. The next step is telling Travis what to do once it gets notified of a change.\n\n## Minimal Project Configuration\n\nTravis CI needs some basic information about your project. Create a file called `.travis.yml` in the project root with the following content:\n\n```\nlanguage: objective-c\n```\n\nTravis builds run in a virtual machine environment. They are [pre-configured](http://about.travis-ci.org/docs/user/osx-ci-environment/) with [Ruby](https://www.ruby-lang.org/en/), [Homebrew](http://brew.sh/), [CocoaPods](http://cocoapods.org/), and some [default build scripts](https://github.com/jspahrsummers/objc-build-scripts). The above configuration should be enough to build your project.\n\nThe pre-installed build script analyzes your Xcode project and builds each target. The build succeeds if everything compiled without error and the tests didn't break. Push your changes to GitHub now and see if the build succeeds.\n\nWhile this is really easy to set up, it might not work for your project. There is not much documentation on how to configure the default build behavior. I had, for example, [code signing issues](https://github.com/travis-ci/travis-ci/issues/1322) because it didn't use the `iphonesimulator` SDK. If that minimal solution doesn't suit you, let's instead have a look at how to use Travis with a custom build command.\n\n## Custom Build Commands\n\nTravis builds your project from the command line. Therefore, the first step is to make your project compile locally. As part of the Xcode Command Line Tools, Apple ships [`xcodebuild`](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/xcodebuild.1.html).\n\nOpen your terminal and type:\n\n```\nxcodebuild --help\n```\n\nThis should list all possible arguments for `xcodebuild`. If it fails, make sure [the Command Line Tools](http://stackoverflow.com/a/9329325) are properly installed. This is how a typical build command would look:\n\n```\nxcodebuild -project {project}.xcodeproj -target {target} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO\n```\n\nThe `iphonesimulator` SDK is set in order to avoid signing issues. This is necessary until we include the certificates later. By setting `ONLY_ACTIVE_ARCH=NO` we make sure that we can build for the simulator architecture. You can also set additional attributes (e.g. `configuration`). Type `man xcodebuild` to read the documentation.\n\nFor `CocoaPods` projects, you have to specify the `workspace` and `scheme` instead:\n\n```\nxcodebuild -workspace {workspace}.xcworkspace -scheme {scheme} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO\n```\n\nSchemes get created automatically with XCode, but this won't happen on the server. Make sure to declare the schemes as `shared` and add them to the repository. Otherwise it will work locally but not on Travis CI.\n\n![](/images/issue-6/objc_shared_schemes.jpg)\n\nThe `.travis.yml` for our example project would look like this now:\n\n```\nlanguage: objective-c\nscript: xcodebuild -workspace TravisExample.xcworkspace -scheme TravisExample -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO\n```\n\n## Running Tests\n\nUsually for testing, you would use a command like this (note the `test` attribute):\n\n```\nxcodebuild test -workspace {workspace}.xcworkspace -scheme {test_scheme} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO\n```\n\nUnfortunately, `xcodebuild` doesn't properly support test targets and application tests for iOS. There are [attempts to fix this problem](http://www.raingrove.com/2012/03/28/running-ocunit-and-specta-tests-from-command-line.html) but I suggest using Xctool instead.\n\n### Xctool\n\n[Xctool](https://github.com/facebook/xctool) is a command-line tool from Facebook to make building and testing easier. The output is less verbose than with `xcodebuild`. It can create a nicely structured and colored output instead. It also fixes the problems with logic and application tests.\n\nTravis comes with xctool pre-installed. To test it locally, install it with [Homebrew](http://brew.sh/):\n\n```\nbrew update\nbrew install xctool\n```\n\nThe usage is really simple, as it takes exactly the same arguments as `xcodebuild`:\n\n```\nxctool test -workspace TravisExample.xcworkspace -scheme TravisExampleTests -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO\n```\n\nOnce we get these commands working locally, it's time to put them in our `.travis.yml`:\n\n```\nlanguage: objective-c\nscript:\n  - xctool -workspace TravisExample.xcworkspace -scheme TravisExample -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO\n  - xctool test -workspace TravisExample.xcworkspace -scheme TravisExampleTests -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO\n```\n\nWhat we looked at so far is enough to use Travis for library projects. We can ensure that it compiles properly and tests pass. But for iOS apps, we want to test on a physical device. That means we have to distribute it to all our test devices. Of course, we want to do this automatically using Travis. As a first step, we have to sign our app.\n\n## App Signing\n\nIn order to sign our app on Travis, we have to create all necessary certificates and profiles. As every iOS developer knows, this might be the most difficult step. Afterward, we will write some scripts that do the signing on the server.\n\n### Certificates and Profiles\n\n**1. Apple Worldwide Developer Relations Certification Authority**\n\nDownload it [from the Apple](http://developer.apple.com/certificationauthority/AppleWWDRCA.cer) page or export it from your Keychain. Save it in your project under `scripts/certs/apple.cer`.\n\n**2. iPhone Distribution Certificate + Private Key**\n\nCreate a new distribution certificate if you don't have one already. To do this, go to your [Apple Developer Account](https://developer.apple.com/account/overview.action) and follow the steps to create a new certificate for Production (`Certificates` > `Production` > `Add` > `App Store and Ad Hoc`). Make sure to download and install the certificate. Afterward, you should find it in your Keychain with a private key attached to it. Open the application `Keychain Access` on your Mac:\n\n![](/images/issue-6/dist_cert_keychain.jpg)\n\nExport the certificate into `scripts/certs/dist.cer` by right-clicking the item and choosing `Export...`. Then export the private key and save it into `scripts/certs/dist.p12`. Enter a password of your choice.\n\nAs Travis needs to know this password, we have to store it somewhere. Of course, we don't want to save it as plain text. But we can make use of the [Travis secure environment variables](http://about.travis-ci.org/docs/user/build-configuration/#Secure-environment-variables). Open terminal and navigate to your project folder that contains the `.travis.yml`. First, let's install the Travis gem by running `gem install travis`. Afterward, you can add the password by executing:\n\n```\ntravis encrypt \"KEY_PASSWORD={password}\" --add\n```\n\nThis will add an encrypted environment variable called `KEY_PASSWORD` to your `.travis.yml`. It can then be used in any script executed by Travis CI.\n\n**3. iOS Provisioning Profile (Distribution)**\n\nIf you haven't already, create a new distribution profile. Based on your account, you can either create an [Ad Hoc](https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/AppDistributionGuide/TestingYouriOSApp/TestingYouriOSApp.html) or [In House](https://developer.apple.com/programs/ios/enterprise/gettingstarted/) profile (`Provisioning Profiles` > `Distribution` > `Add` > `Ad Hoc` or `In House`). Download and save it under `scripts/profile/`.\n\nAs we need to access this profile from Travis, we have to store the name as a global environment variable. Add it to the `.travis.yml` global environment variables section. For example, if the file name were to be `TravisExample_Ad_Hoc.mobileprovision`, add this:\n\n```\nenv:\n  global:\n  - APP_NAME=\"TravisExample\"\n  - 'DEVELOPER_NAME=\"iPhone Distribution: {your_name} ({code})\"'\n  - PROFILE_NAME=\"TravisExample_Ad_Hoc\"\n```\n\nThere are two more environment variables declared. The `APP_NAME` (line 3) is usually the same name as your main target. The `DEVELOPER_NAME` (line 4) is what you see when checking the XCode `Build Settings` of your main target under `Code Signing Identity` > `Release`. Search for the `Ad Hoc` or `In House` profile of your app. Take the text part that is in black letters. Depending on your setup, it may or may not include a code in brackets.\n\n## Encrypt Certificates and Profiles\n\nIf your GitHub project is public, you might want to encrypt your certificates and profiles, as they contain sensitive data. If you have a private repository, you can move on to the next section.\n\nFirst, we have to come up with a password that encrypts all our files (the secret). In our example, let's choose \"foo,\" but you should come up with a more secure password for your project. On the command line, encrypt all three sensitive files using `openssl`:\n\n```\nopenssl aes-256-cbc -k \"foo\" -in scripts/profile/TravisExample_Ad_Hoc.mobileprovision -out scripts/profile/TravisExample_Ad_Hoc.mobileprovision.enc -a\nopenssl aes-256-cbc -k \"foo\" -in scripts/certs/dist.cer -out scripts/certs/dist.cer.enc -a\nopenssl aes-256-cbc -k \"foo\" -in scripts/certs/dist.p12 -out scripts/certs/dist.p12.enc -a\n```\n\nThis will create encrypted versions of our files with the ending `.enc`. You can now remove or ignore the original files. At the very least, make sure not to commit them, otherwise they will show up on GitHub. If you accidentally committed or pushed them already, [get some help](https://help.github.com/articles/remove-sensitive-data).\n\nNow that our files are encrypted, we need to tell Travis to decrypt them again. For that, Travis needs the secret. We use the same approach that we used already for the `KEY_PASSWORD`:\n\n```\ntravis encrypt \"ENCRYPTION_SECRET=foo\" --add\n```\n\nLastly, we have to tell Travis which files to decrypt. Add the following commands to the `before-script` phase in the `.travis.yml`:\n\n```\nbefore_script:\n- openssl aes-256-cbc -k \"$ENCRYPTION_SECRET\" -in scripts/profile/TravisExample_Ad_Hoc.mobileprovision.enc -d -a -out scripts/profile/TravisExample_Ad_Hoc.mobileprovision\n- openssl aes-256-cbc -k \"$ENCRYPTION_SECRET\" -in scripts/certs/dist.cer.enc -d -a -out scripts/certs/dist.cer\n- openssl aes-256-cbc -k \"$ENCRYPTION_SECRET\" -in scripts/certs/dist.p12.enc -d -a -out scripts/certs/dist.p12\n```\n\nWith that, your files on GitHub will be secured, while Travis can still read and use them. There is only one security issue that you have to be aware of: You could (accidentally) expose a decrypted environment variable in the Travis build log. Note, however, that decryption is disabled for pull requests.\n\n### Add Scripts\n\nNow we have to make sure that the certificates get imported in the Travis CI keychain. To do this, we should add a new file, `add-key.sh`, in the `scripts` folder:\n\n```\n#!/bin/sh\n\n# Create a custom keychain\nsecurity create-keychain -p travis ios-build.keychain\n\n# Make the custom keychain default, so xcodebuild will use it for signing\nsecurity default-keychain -s ios-build.keychain\n\n# Unlock the keychain\nsecurity unlock-keychain -p travis ios-build.keychain\n\n# Set keychain timeout to 1 hour for long builds\n# see http://www.egeek.me/2013/02/23/jenkins-and-xcode-user-interaction-is-not-allowed/\nsecurity set-keychain-settings -t 3600 -l ~/Library/Keychains/ios-build.keychain\n\n# Add certificates to keychain and allow codesign to access them\nsecurity import ./scripts/certs/apple.cer -k ~/Library/Keychains/ios-build.keychain -T /usr/bin/codesign\nsecurity import ./scripts/certs/dist.cer -k ~/Library/Keychains/ios-build.keychain -T /usr/bin/codesign\nsecurity import ./scripts/certs/dist.p12 -k ~/Library/Keychains/ios-build.keychain -P $KEY_PASSWORD -T /usr/bin/codesign\n\n\n# Put the provisioning profile in place\nmkdir -p ~/Library/MobileDevice/Provisioning\\ Profiles\ncp \"./scripts/profile/$PROFILE_NAME.mobileprovision\" ~/Library/MobileDevice/Provisioning\\ Profiles/\n```\n\nHere we create a new temporary keychain called `ios-build` that will contain all the certificates, and make sure it's ready to be used for codesigning. Note that we use the `$KEY_PASSWORD` here to import the private key. As a final step, the mobile provisioning profile is copied into the `Library` folder.\n\nAfter creating this file, make sure to give it executable rights. On the command line, type `chmod a+x scripts/add-key.sh`. You have to do this for the following scripts as well.\n\nNow that all certificates and profiles are imported we can sign our application. Please note that we have to build the app before we can sign it. As we need to know where the build is stored on disk, I recommend specifying the output folder by declaring `OBJROOT` and `SYMROOT` in the build command. Also, we should create a release version by setting the SDK to `iphoneos` and the configuration to `Release`:\n\n```\nxctool -workspace TravisExample.xcworkspace -scheme TravisExample -sdk iphoneos -configuration Release OBJROOT=$PWD/build SYMROOT=$PWD/build ONLY_ACTIVE_ARCH=NO 'CODE_SIGN_RESOURCE_RULES_PATH=$(SDKROOT)/ResourceRules.plist'\n```\n\nIf you run this command, you should find the app binary in the `build/Release-iphoneos` folder afterward. Now we can sign it and create the `IPA` file. Do this by creating a new script:\n\n```\n#!/bin/sh\nif [[ \"$TRAVIS_PULL_REQUEST\" != \"false\" ]]; then\n  echo \"This is a pull request. No deployment will be done.\"\n  exit 0\nfi\nif [[ \"$TRAVIS_BRANCH\" != \"master\" ]]; then\n  echo \"Testing on a branch other than master. No deployment will be done.\"\n  exit 0\nfi\n\nPROVISIONING_PROFILE=\"$HOME/Library/MobileDevice/Provisioning Profiles/$PROFILE_NAME.mobileprovision\"\nOUTPUTDIR=\"$PWD/build/Release-iphoneos\"\n\nxcrun -log -sdk iphoneos PackageApplication \"$OUTPUTDIR/$APP_NAME.app\" -o \"$OUTPUTDIR/$APP_NAME.ipa\" -sign \"$DEVELOPER_NAME\" -embed \"$PROVISIONING_PROFILE\"\n```\n\nLines two through nine are quite important. You don't want to create a new release while working on a feature branch. The same is true for pull requests. Builds for pull requests wouldn't work anyway, as secured environment variables [are disabled](http://about.travis-ci.org/docs/user/build-configuration/#Secure-environment-variables).\n\nIn line fourteen, we do the actual signing. This results in two new files in the `build/Release-iphoneos` folder: `TravisExample.ipa` and `TravisExample.app.dsym`. The first one contains the app which is ready to be delivered to your phone. The `dsym` file contains debug information of your binary. This is important for logging crashes on the devices. We will use both files later when we distribute the app.\n\nThe last script will remove the temporary keychain again and delete the mobile provisioning profile. It is not really necessary but helps when testing locally:\n\n```\n#!/bin/sh\nsecurity delete-keychain ios-build.keychain\nrm -f \"~/Library/MobileDevice/Provisioning Profiles/$PROFILE_NAME.mobileprovision\"\n```\n\nAs a last step, we have to tell Travis when to execute these three scripts. The keys should be added before the app is built and the signing and cleanup should happen afterwards. Add/replace the following steps in your `.travis.yml`:\n\n```\nbefore_script:\n- ./scripts/add-key.sh\n- ./scripts/update-bundle.sh\nscript:\n- xctool -workspace TravisExample.xcworkspace -scheme TravisExample -sdk iphoneos -configuration Release OBJROOT=$PWD/build SYMROOT=$PWD/build ONLY_ACTIVE_ARCH=NO\nafter_success:\n- ./scripts/sign-and-upload.sh\nafter_script:\n- ./scripts/remove-key.sh\n```\n\nWith that done, we can push everything to GitHub and wait for Travis to sign our app. We can verify if it worked by investigating the Travis console on the project page. If everything works fine, we can have a look on how to distribute the signed app to our testers.\n\n## Distributing the App\n\nThere are two well-known services that help you with distributing your app: [TestFlight](http://testflightapp.com) and [HockeyApp](http://hockeyapp.net). Choose whatever is more sufficient for your needs. Personally, I prefer HockeyApp, but I'll show how to integrate both services.\n\nWe will extend our existing shell script `sign-and-build.sh` for that. Let's create some release notes first:\n\n```\nRELEASE_DATE=`date '+%Y-%m-%d %H:%M:%S'`\nRELEASE_NOTES=\"Build: $TRAVIS_BUILD_NUMBER\\nUploaded: $RELEASE_DATE\"\n```\n\nNote that we use a global environment variable set by Travis here (`TRAVIS_BUILD_NUMBER`).\n\n### TestFlight\n\nCreate a [TestFlight account](https://testflightapp.com/register/) and set up your app. In order to use the TestFlight API, you need to get the [api_token](https://testflightapp.com/account/#api) and [team_token](https://testflightapp.com/dashboard/team/edit/?next=/api/doc/) first. Again, we have to make sure to encrypt them. On the command line execute:\n\n```\ntravis encrypt \"TESTFLIGHT_API_TOKEN={api_token}\" --add\ntravis encrypt \"TESTFLIGHT_TEAM_TOKEN={team_token}\" --add\n```\n\nNow we can call the API accordingly. Add this to the `sign-and-build.sh`:\n\n```\ncurl http://testflightapp.com/api/builds.json \\\n  -F file=\"@$OUTPUTDIR/$APP_NAME.ipa\" \\\n  -F dsym=\"@$OUTPUTDIR/$APP_NAME.app.dSYM.zip\" \\\n  -F api_token=\"$TESTFLIGHT_API_TOKEN\" \\\n  -F team_token=\"$TESTFLIGHT_TEAM_TOKEN\" \\\n  -F distribution_lists='Internal' \\\n  -F notes=\"$RELEASE_NOTES\"\n```\n\nMake sure NOT to use the verbose flag (`-v`), as this would expose your decrypted tokens!\n\n### HockeyApp\n\nSign up for a [HockeyApp account](http://hockeyapp.net/plans) and create a new app. Then grab the `App ID` from the overview page. Next, we have to generate an API token. Go to [this page](https://rink.hockeyapp.net/manage/auth_tokens) and create one. If you want to automatically distribute new versions to all testers, choose the 'Full Access' version.\n\nEncrypt both tokens:\n\n```\ntravis encrypt \"HOCKEY_APP_ID={app_id}\" --add\ntravis encrypt \"HOCKEY_APP_TOKEN={api_token}\" --add\n```\n\nThen call their API from the `sign-and-build.sh` script:\n\n```\ncurl https://rink.hockeyapp.net/api/2/apps/$HOCKEY_APP_ID/app_versions \\\n  -F status=\"2\" \\\n  -F notify=\"0\" \\\n  -F notes=\"$RELEASE_NOTES\" \\\n  -F notes_type=\"0\" \\\n  -F ipa=\"@$OUTPUTDIR/$APP_NAME.ipa\" \\\n  -F dsym=\"@$OUTPUTDIR/$APP_NAME.app.dSYM.zip\" \\\n  -H \"X-HockeyAppToken: $HOCKEY_APP_TOKEN\"\n```\n\nNote that we also upload the `dsym` file. If you integrate the [TestFlight](https://testflightapp.com/sdk/ios/doc/) or [HockeyApp SDK](http://hockeyapp.net/releases/), you can get human-readable crash reports without further ado.\n\n## Troubleshooting Travis\n\nUsing Travis over the last month wasn't always flawless. It's important to know how to approach issues with your build without having direct access to the build environment.\n\nAs of writing this article, there are no VM images available for download. If your build doesn't work anymore, first try to reproduce the problem locally. Run the exact same build commands that Travis executes locally:\n\n```\nxctool ...\n```\n\nFor debugging the shell scripts, you have to define the environment variables first. What I did for this is create a new shell script that sets all the environment variables. This script is added to the `.gitignore` file because you don't want it exposed to the public. For the example project, my `config.sh` looks like this:\n\n```\n#!/bin/bash\n\n# Standard app config\nexport APP_NAME=TravisExample\nexport DEVELOPER_NAME=iPhone Distribution: Mattes Groeger\nexport PROFILE_NAME=TravisExample_Ad_Hoc\nexport INFO_PLIST=TravisExample/TravisExample-Info.plist\nexport BUNDLE_DISPLAY_NAME=Travis Example CI\n\n# Edit this for local testing only, DON'T COMMIT it:\nexport ENCRYPTION_SECRET=...\nexport KEY_PASSWORD=...\nexport TESTFLIGHT_API_TOKEN=...\nexport TESTFLIGHT_TEAM_TOKEN=...\nexport HOCKEY_APP_ID=...\nexport HOCKEY_APP_TOKEN=...\n\n# This just emulates Travis vars locally\nexport TRAVIS_PULL_REQUEST=false\nexport TRAVIS_BRANCH=master\nexport TRAVIS_BUILD_NUMBER=0\n```\n\nIn order to expose these environment variables, execute this (be sure `config.sh` has executable rights):\n\n```\n. ./config.sh\n```\n\nThen try `echo $APP_NAME` to check if it worked. If it did, you can run any of your shell scripts locally without modifications.\n\nIf you get different build results locally, you might have different versions of your libraries and gems installed. Try to imitate the exact same setup as on the Travis VM. They have a list of their installed software versions [online](http://about.travis-ci.org/docs/user/osx-ci-environment/). You can also figure out the exact versions of all gems and libraries by putting debug information into your Travis config:\n\n```\ngem cocoapod --version\nbrew --version\nxctool -version\nxcodebuild -version -sdk\n```\n\nAfter you install the exact same versions locally, re-run the build.\n\nIf you still don't get the same results, try to do a clean checkout into a new directory. Also, make sure all caches are cleared. As Travis sets up a new virtual machine for each build, it doesn't have to deal with cache problems, but your local test environment might have to.\n\nOnce you can reproduce the exact same behavior as on the server, you can start to investigate what the problem is. It really depends, then, on your concrete scenario of how to approach it. Usually Google is a great help in figuring out what could be the cause of your problem.\n\nIf, after all, the problem seems to affect other projects on Travis as well, it might be an issue with the Travis environment itself. I saw this happening several times (especially in the beginning). In this case, try to contact their support. My experience is that they react super fast.\n\n## Criticism\n\nThere are some limitations when using Travis CI compared to other solutions on the market. As Travis runs from a pre-configured VM, you have to install custom dependencies for every build. That costs additional time. They put [effort in providing caching mechanisms](http://about.travis-ci.org/docs/user/caching/) lately, though.\n\nTo some extent, you rely on the setup that Travis provides. For example, you have to deal with the currently installed version of Xcode. If you use a version that is newer than Travis CI, you probably won't be able to run your build on the server. It would be helpful if there were different VMs set up for each major Xcode version.\n\nFor complex projects, you might want to split up your build jobs into compiling the app, running integration tests, and so forth. This way, you get the build artifacts faster, without having to wait for all tests to be processed. There is [no direct support](https://github.com/travis-ci/travis-ci/issues/249) for dependent builds so far.\n\nWhen pushing your project to GitHub, Travis gets triggered instantly. But builds usually won’t start right away. They will be put in a [global language-specific build queue](http://about.travis-ci.org/blog/2012-07-27-improving-the-quality-of-service-on-travis-ci/). However, the pro version allows more builds to be executed concurrently.\n\n## Conclusion\n\nTravis CI provides you with a fully functional continuous integration environment that builds, tests, and distributes your iOS apps. For open source projects, this service is even free. Community projects benefit from the great GitHub integration. You might have seen [buttons like this](http://about.travis-ci.org/docs/user/status-images/) already:\n\n![](/images/issue-6/TravisExample-iOS.png)\n\nEven for commercial projects, their support for private GitHub repositories with Travis Pro opens up an easy and fast way to use continuous integration.\n\nIf you haven't tried Travis yet, go and do it now. It's awesome!\n\n## Further Links\n\n* [Example Project](https://github.com/objcio/issue-6-travis-ci)\n* [Travis CI](http://www.travis-ci.com/)\n* [Travis CI Pro](https://magnum.travis-ci.com/)\n* [Xctool](https://github.com/facebook/xctool)\n* [HockeyApp](http://hockeyapp.net/)\n* [TestFlight](https://testflightapp.com/)\n"
  },
  {
    "path": "2013-12-07-collections.md",
    "content": "---\ntitle: \"The Foundation Collection Classes\"\ncategory: \"7\"\ndate: \"2013-12-09 11:00:00\"\ntags: article\nauthor:\n  - name: Peter Steinberger\n    url: https://twitter.com/steipete\n---\n\n\n## NSArray, NSSet, NSOrderedSet, and NSDictionary\n\nFoundation's collection classes are the basic building blocks of every Mac/iOS application. In this article, we're going to have an in-depth look at both the \"old\" (`NSArray`, `NSSet`) and the \"new\" (`NSMapTable`, `NSHashTable`, `NSPointerArray`) classes, explore detailed performance of each of them, and discuss when to use what.\n\nAuthor Note: This article contains several benchmark results, however they are by no means meant to be exact and there's no variation/multiple runs applied. Their goal is to give you a direction of what's faster and general runtime statistics. All tests have been made on an iPhone 5s with Xcode 5.1b1 and iOS 7.1b1 and a 64-bit binary. Compiler settings were release built with -Ofast. Vectorize loops and unroll loops (default settings) have both been disabled .\n\n### Big O Notation\n\nFirst, we need some theoretical background. Performance is usually described with the [Big O Notation](https://en.wikipedia.org/wiki/Big_O_notation). It defines the *limiting behavior* of a function and is often used to characterize algorithms on their performance. O defines the upper bound of the growth rate of the function. To see just how big the difference is, see commonly used O notations and the number of operations needed.\n\n![](/images/issue-7/big-o-notation.png)\n\nFor example, if you sort an array with 50 elements, and your sorting algorithm has a complexity of O(n^2), there will be 2,500 operations necessary to complete the task. Furthermore, there's also overhead in internal management and calling that method - so it's 2,500 operations times constant. O(1) is the ideal complexity, meaning constant time. [Good sorting algorithms usually need O(n\\*log n) time.](http://en.wikipedia.org/wiki/Sorting_algorithm#Comparison_of_algorithms)\n\n### Mutability\n\nMost collection classes exist in two versions: mutable and immutable (default). This is quite different than most other frameworks and feels a bit weird at first. However, others are now adopting this as well: [.NET introduced immutable collections as an official extension](http://blogs.msdn.com/b/dotnet/archive/2013/09/25/immutable-collections-ready-for-prime-time.aspx) only a few months ago.\n\nWhat's the big advantage? **Thread safety**. Immutable collections are fully thread safe and can be iterated from multiple threads at the same time, without any risk of mutation exceptions. Your API should *never* expose mutable collections.\n\nOf course there's a cost when going from immutable and mutable and back - the object has to be copied twice, and all objects within will be retained/released. Sometimes it's more efficient to hold an internal mutable collection and return a copied, immutable object on access.\n\nUnlike other frameworks, Apple does not provide thread-safe mutable variants of its collection classes, with the exception of `NSCache` - which really doesn't count since it's not meant to be a generic container. Most of the time, you really don't want synchronization at the collection level, but rather higher up in the hierarchy. Imagine some code that checks for the existence of a key in a dictionary, and depending on the result, sets a new key or returns something else - you usually want to group multiple operations together, and a thread-safe mutable variant would not help you here.\n\nThere are *some* valid use cases for a synchronized, thread-safe mutable collection, and it takes only a few lines to build something like that via subclassing and composition, e.g. for [`NSDictionary`](https://gist.github.com/steipete/7746843) or [NSArray](https://github.com/Cue/TheKitchenSync/blob/master/Classes/Collections/CueSyncArray.mm).\n\nNotably, some of the more modern collection classes like `NSHashTable`, `NSMapTable`, and `NSPointerArray` are mutable by default and don't have immutable counterparts. They are meant for internal class use, and a use case where you would want those immutable would be quite unusual.\n\n## NSArray\n\n`NSArray` stores objects as ordered collections and is probably the most-used collection class. That's why it even got its own syntactic sugar syntax with the shorthand-literal `@[...]`, which is much shorter than the old `[NSArray arrayWithObjects:..., nil]`.\n\n`NSArray` implements `objectAtIndexedSubscript:` and thus we can use a C-like syntax like `array[0]` instead of the older `[array objectAtIndex:0]`.\n\n### Performance Characteristics\n\nThere's a lot more to `NSArray` than you might think, and it uses a variety of internal variants depending on how many objects are being stored. The most interesting part is that Apple doesn't guarantee O(1) access time on individual object access - as you can read in the note about Computational Complexity in the [CFArray.h CoreFoundation header](http://www.opensource.apple.com/source/CF/CF-855.11/CFArray.h):\n\n>The access time for a value in the array is guaranteed to be at worst O(lg N) for any implementation, current and future, but will often be O(1) (constant time). Linear search operations similarly have a worst case complexity of O(N\\*lg N), though typically the bounds will be tighter, and so on. Insertion or deletion operations will typically be linear in the number of values in the array, but may be O(N\\*lg N) clearly in the worst case in some implementations. There are no favored positions within the array for performance; that is, it is not necessarily faster to access values with low indices, or to insert or delete values with high indices, or whatever.\n\nWhen measuring, it turns out that `NSArray` has some [additional interesting performance characteristics](http://ridiculousfish.com/blog/posts/array.html). Inserting/deleting elements at the beginning/end is usually an O(1) operation, where random insertion/deletion usually will be O(N).\n\n### Useful Methods\n\nMost methods of `NSArray` use `isEqual:` to check against other objects (like `containsObject:`). There's a special method named `indexOfObjectIdenticalTo:` that goes down to pointer equality, and thus can speed up searching for objects a lot - if you can ensure that you're searching within the same set. \n\nWith iOS 7, we finally got a public `firstObject` method, which joins `lastObject`, and both simply return `nil` for an empty array - regular access would throw an `NSRangeException`.\n\nThere's a nice detail about the construction of (mutable) arrays that can be used to save code. If you are creating a mutable array from a source that might be nil, you usually have some code like this:\n\n```objc\nNSMutableArray *mutableObjects = [array mutableCopy];\nif (!mutableObjects) {\n    mutableObjects = [NSMutableArray array];\n}\n```\n\nor via the more concise [ternary operator](http://en.wikipedia.org/wiki/%3F:):\n    \n```objc\nNSMutableArray *mutableObjects = [array mutableCopy] ?: [NSMutableArray array];\n```\n\nThe better solution is to use the fact that `arrayWithArray:` will return an object in either way - even if the source array is nil:\n    \n```objc\nNSMutableArray *mutableObjects = [NSMutableArray arrayWithArray:array];\n```\n\nThe two operations are almost equal in performance. Using `copy` is a bit faster, but then again, it's highly unlikely that this will be your app bottleneck. **Side Note:** Please don't use `[@[] mutableCopy]`. The classic `[NSMutableArray array]` is a lot better to read.\n    \nReversing an array is really easy: `array.reverseObjectEnumerator.allObjects`. We'll use the fact that `reverseObjectEnumerator` is pre-supplied and every `NSEnumerator` implements `allObjects`, which returns a new array. And while there's no native `randomObjectEnumerator`, you can write a custom enumerator that shuffles the array or use [some great open source options](https://github.com/mattt/TTTRandomizedEnumerator/blob/master/TTTRandomizedEnumerator/TTTRandomizedEnumerator.m).\n\n### Sorting Arrays\n\nThere are various ways to sort an array. If it's string based, `sortedArrayUsingSelector:` is your first choice:\n\n```objc\nNSArray *array = @[@\"John Appleseed\", @\"Tim Cook\", @\"Hair Force One\", @\"Michael Jurewitz\"];\nNSArray *sortedArray = [array sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];\n```\n\n   \nThis works equally well for number-based content, since `NSNumber` implements `compare:` as well:\n    \n```objc\nNSArray *numbers = @[@9, @5, @11, @3, @1];\nNSArray *sortedNumbers = [numbers sortedArrayUsingSelector:@selector(compare:)];\n```\n\nFor more control, you can use the function-pointer-based sorting methods:\n\n```objc\n- (NSData *)sortedArrayHint;\n- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator \n                              context:(void *)context;\n- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator \n                              context:(void *)context hint:(NSData *)hint;\n```\n\nApple added an (opaque) way to speed up sorting using `sortedArrayHint`. \n\n>The hinted sort is most efficient when you have a large array (N entries) that you sort once and then change only slightly (P additions and deletions, where P is much smaller than N). You can reuse the work you did in the original sort by conceptually doing a merge sort between the N “old” items and the P “new” items. To obtain an appropriate hint, you use `sortedArrayHint` when the original array has been sorted, and keep hold of it until you need it (when you want to re-sort the array after it has been modified).\n\nSince blocks are around, there are also the newer block-based sorting methods:\n\n```objc\n- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr;\n- (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts \n                    usingComparator:(NSComparator)cmptr;\n```\n\nPerformance-wise, there's not much difference between the different methods. Interestingly, the selector-based approach is actually the fastest. [You'll find the source code the benchmarks used here on GitHub.](https://github.com/steipete/PSTFoundationBenchmark):\n\n`Sorting 1000000 elements. selector: 4947.90[ms] function: 5618.93[ms] block: 5082.98[ms].`\n    \n### Binary Search\n\n`NSArray` has come with built-in [binary search](http://en.wikipedia.org/wiki/Binary_search_algorithm) since iOS 4 / Snow Leopard:\n\n```objc\ntypedef NS_OPTIONS(NSUInteger, NSBinarySearchingOptions) {\n        NSBinarySearchingFirstEqual     = (1UL << 8),\n        NSBinarySearchingLastEqual      = (1UL << 9),\n        NSBinarySearchingInsertionIndex = (1UL << 10),\n};\n\n- (NSUInteger)indexOfObject:(id)obj \n              inSortedRange:(NSRange)r \n                    options:(NSBinarySearchingOptions)opts \n            usingComparator:(NSComparator)cmp;\n```\n\nWhy would you want to use this? Methods like `containsObject:` and `indexOfObject:` start at index 0 and search every object until the match is found - they don't require the array to be sorted but have a performance characteristic of O(n). Binary search, on the other hand, requires the array to be sorted, but only needs O(log n) time. Thus, for one million entries, binary search requires, at most, 21 comparisons, while the naive linear search would require an average of 500,000 comparisons.\n\nHere's a simple benchmark of just how much faster binary search is:\n\n`Time to search for 1000 entries within 1000000 objects. Linear: 54130.38[ms]. Binary: 7.62[ms]`\n\nFor comparison, the search for a specific index with `NSOrderedSet` took 0.23 ms - that's more than 30 times faster, even compared to binary search.\n\nKeep in mind that sorting is expensive as well. Apple uses merge sort, which takes O(n\\*log n), so if you just have to call `indexOfObject:` once, there's no need for binary search.\n\nWith specifying `NSBinarySearchingInsertionIndex`, you can find the correct insertion index to keep an already sorted array sorted after inserting new elements.\n\n### Enumeration and Higher-Order Messaging\n\nFor a benchmark, we look at a common use case. Filter elements from an array into another array. This tests both the various enumeration ways, as well as the APIs specific to filtering:\n\n```objc\n// First variant, using `indexesOfObjectsWithOptions:passingTest:`.\nNSIndexSet *indexes = [randomArray indexesOfObjectsWithOptions:NSEnumerationConcurrent \n                                                   passingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {\n    return testObj(obj);\n}];\nNSArray *filteredArray = [randomArray objectsAtIndexes:indexes];\n\n// Filtering using predicates (block-based or text)    \nNSArray *filteredArray2 = [randomArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id obj, NSDictionary *bindings) {\n    return testObj(obj);\n}]];\n\n// Block-based enumeration \nNSMutableArray *mutableArray = [NSMutableArray array];\n[randomArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {\n    if (testObj(obj)) {\n        [mutableArray addObject:obj];\n    }\n}];\n\n// Classic enumeration\nNSMutableArray *mutableArray = [NSMutableArray array];\nfor (id obj in randomArray) {\n    if (testObj(obj)) {\n        [mutableArray addObject:obj];\n    }\n}\n\n// Using NSEnumerator, old school.\nNSMutableArray *mutableArray = [NSMutableArray array];\nNSEnumerator *enumerator = [randomArray objectEnumerator];\nid obj = nil;\nwhile ((obj = [enumerator nextObject]) != nil) {\n    if (testObj(obj)) {\n        [mutableArray addObject:obj];\n    }\n}\n\n// Using objectAtIndex: (via subscripting)\nNSMutableArray *mutableArray = [NSMutableArray array];\nfor (NSUInteger idx = 0; idx < randomArray.count; idx++) {\n    id obj = randomArray[idx];\n    if (testObj(obj)) {\n        [mutableArray addObject:obj];\n    }\n}\n```\n\n<table><thead><tr><th style=\"text-align: left;padding-right:1em;\">Enumeration Method / Time [ms]</th><th style=\"text-align:right;padding-right:1em;\">10.000.000 elements</th><th style=\"text-align:right;padding-right:1em;\">10.000 elements</th></tr></thead><tbody><tr><td style=\"text-align: left;\"><code>indexesOfObjects:</code>, concurrent</td><td style=\"text-align: right;padding-right:1em;\">1844.73</td><td style=\"text-align: right;padding-right:1em;\">2.25</td>\n</tr><tr><td style=\"text-align: left;padding-right:1em;\"><code>NSFastEnumeration</code> (<code>for in</code>)</td><td style=\"text-align: right;padding-right:1em;\">3223.45</td><td style=\"text-align: right;padding-right:1em;\">3.21</td>\n</tr><tr><td style=\"text-align: left;padding-right:1em;\"><code>indexesOfObjects:</code></td><td style=\"text-align: right;padding-right:1em;\">4221.23</td><td style=\"text-align: right;padding-right:1em;\">3.36</td>\n</tr><tr><td style=\"text-align: left;padding-right:1em;\"><code>enumerateObjectsUsingBlock:</code></td><td style=\"text-align: right;padding-right:1em;\">5459.43</td><td style=\"text-align: right;padding-right:1em;\">5.43</td>\n</tr><tr><td style=\"text-align: left;padding-right:1em;\"><code>objectAtIndex:</code></td><td style=\"text-align: right;padding-right:1em;\">5282.67</td><td style=\"text-align: right;padding-right:1em;\">5.53</td>\n</tr><tr><td style=\"text-align: left;padding-right:1em;\"><code>NSEnumerator</code></td><td style=\"text-align: right;padding-right:1em;\">5566.92</td><td style=\"text-align: right;padding-right:1em;\">5.75</td>\n</tr><tr><td style=\"text-align: left;padding-right:1em;\"><code>filteredArrayUsingPredicate:</code></td><td style=\"text-align: right;padding-right:1em;\">6466.95</td><td style=\"text-align: right;padding-right:1em;\">6.31</td>\n</tr></tbody></table>\n    \n    \nTo better understand the performance measured here, we first have to look at how the array is enumerated. \n\n`indexesOfObjectsWithOptions:passingTest:` has to call a block each time and thus is slightly less efficient than the classical for-based enumeration that uses the `NSFastEnumeration` technique. However, if we enable concurrent enumeration on the former, then it wins by a wide margin, and is almost twice as fast. Which makes sense, considering that the iPhone 5s has two cores. What's not visible here is that `NSEnumerationConcurrent` only makes sense for a large number of objects - if your data set is small, it really doesn't matter much what method you are going to use. Even worse, the additional thread management overhead for `NSEnumerationConcurrent` will actually make results slower than without.\n\nThe real \"loser\" here is `filteredArrayUsingPredicate:`. `NSPredicate` still has a reason to be mentioned here, since one can write [quite sophisticated expressions](http://nshipster.com/nspredicate/), especially with the non-block-based variant. People who use Core Data should be familiar with that.\n\nFor completeness, we also added a benchmark using `NSEnumerator` - however there really is no reason to use this anymore. While it is surprisingly fast (still faster than using the `NSPredicate`-based filtering), it certainly has more runtime overhead than fast enumeration - nowadays it only exists for backward compatibility. Even a non-optimized access for via `objectAtIndex:` is faster here.\n\n### NSFastEnumeration\n\nApple added [`NSFastEnumeration`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSFastEnumeration_protocol/Reference/NSFastEnumeration.html) in OS X 10.5 and it has been in iOS ever since the first release. Before that, there was `NSEnumeration`, which returned one element at a time, and thus as quite a runtime overhead with each iteration. With fast enumeration, Apple returns a chunk of data with `countByEnumeratingWithState:objects:count:`. The chunk is parsed as a C array of `id`s. This is where the additional speed comes from; iterating a C array is much faster, and can potentially be even further optimized by the compiler. Manually implementing fast enumeration is quite tricky, so Apple's [FastEnumerationSample](https://developer.apple.com/library/ios/samplecode/FastEnumerationSample/Introduction/Intro.html) is a good starting point, and there's also [an excellent article by Mike Ash on this topic](http://www.mikeash.com/pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html).\n\n\n### Should I Use arrayWithCapacity:?\n\nWhen initializing `NSArray`, you can optionally specify the expected count. When benchmarking this, it turns out that there is no difference in performance - the measured timings are almost equal and within the statistical uncertainty. A little birdie at Apple told me that this hint is indeed not used. However, using `arrayWithCapacity:` can still be useful, as it can help understanding the code as part of an implicit documentation:\n\n`Adding 10.000.000 elements to NSArray. no count 1067.35[ms] with count: 1083.13[ms].`\n\n\n### Subclassing Notes\nThere is rarely a reason why you would want to subclass the basic collection classes. Most of the time, the better solution is going down to CoreFoundation level and using custom callbacks to customize the behavior.\n\nTo create a case-insensitive dictionary, one could subclass `NSDictionary` and write custom accessors that always lowercase (or uppercase) the string, and similar changes for storing. The better and faster solution is to instead provide a different set of `CFDictionaryKeyCallBacks` where you can provide custom `hash` and `isEqual:` callbacks. [You'll find an example in this gist](https://gist.github.com/steipete/7739473). The beauty is that - thanks to [toll-free bridging](https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/Toll-FreeBridgin/Toll-FreeBridgin.html) - it's still a simple dictionary and can be consumed by any API that takes an `NSDictionary`.\n\nOne example where a subclass is useful is the use case for an ordered dictionary. .NET has a `SortedDictionary`, Java has `TreeMap`, C++ has `std::map`. While you *could* use C++'s STL container, you won't get any automated `retain/release`, which would make using those much more cumbersome. Because `NSDictionary` is a [class cluster](https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/ClassClusters/ClassClusters.html), subclassing is quite different than one would expect. It's outside of the boundaries of this article, but one [real-world example of an ordered dictionary is here](https://github.com/nicklockwood/OrderedDictionary/blob/master/OrderedDictionary/OrderedDictionary.m).\n\n## NSDictionary\n\nA dictionary stores arbitrary key/value pairs of objects. For historical reasons, the initializer uses the reversed object to key notation, `[NSDictionary dictionaryWithObjectsAndKeys:object, key, nil],` while the newer literal shorthand starts with key `@{key : value, ...}`.\n\nKeys in `NSDictionary` are copied and they need to be constant. If the key changes after being used to put a value in the dictionary, the value may not be retrievable.\nAs an interesting detail, keys are copied when using an `NSDictionary`, but are only retained when using a toll-free bridged `CFDictionary`. There's no notion of a generic object copy for CoreFoundation-classes, thus copy wasn't possible at that time (\\*). This only applies if you use `CFDictionarySetValue()`. If you use a toll-free bridged `CFDictionary` via `setObject:forKey`, Apple added additional logic that will still copy your key. This does not work the other way around - using an `NSDictionary` object casted to `CFDictionary` and used via `CFDictionarySetValue()` will call back to `setObject:forKey` and copy the key.\n\n(\\*) There is one prepared key callback `kCFCopyStringDictionaryKeyCallBacks` that will copy strings, and because `CFStringCreateCopy()` calls back to `[NSObject copy]` for an ObjC class we could abuse this callback to create a key-copying `CFDictionary`.\n\n### Performance Characteristics\n\nApple is rather quiet when it comes to defining computational complexity. The only note around this can be found in the [CoreFoundation headers of `CFDictionary`](http://www.opensource.apple.com/source/CF/CF-855.11/CFDictionary.h):\n\n>The access time for a value in the dictionary is guaranteed to be at worst O(N) for any implementation, current and future, but will often be O(1) (constant time). Insertion or deletion operations will typically be constant time as well, but are O(N\\*N) in the worst case in some implementations. Access of values through a key is faster than accessing values directly (if there are any such operations). Dictionaries will tend to use significantly more memory than a array with the same number of values.\n\nThe dictionary - much like array - uses different implementations depending on the size and switches between them transparently.\n\n### Enumeration and Higher-Order Messaging\n\nAgain, there are several ways how to best filter a dictionary:\n\n```objc\n// Using keysOfEntriesWithOptions:passingTest:,optionally concurrent\nNSSet *matchingKeys = [randomDict keysOfEntriesWithOptions:NSEnumerationConcurrent \n                                               passingTest:^BOOL(id key, id obj, BOOL *stop) \n{\n    return testObj(obj);\n}];\nNSArray *keys = matchingKeys.allObjects;\nNSArray *values = [randomDict objectsForKeys:keys notFoundMarker:NSNull.null];\n__unused NSDictionary *filteredDictionary = [NSDictionary dictionaryWithObjects:values \n                                                                        forKeys:keys];    \n    \n// Block-based enumeration.\nNSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];\n[randomDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {\n    if (testObj(obj)) {\n        mutableDictionary[key] = obj;\n    }\n}];\n\n// NSFastEnumeration\nNSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];\nfor (id key in randomDict) {\n    id obj = randomDict[key];\n    if (testObj(obj)) {\n        mutableDictionary[key] = obj;\n    }\n}\n\n // NSEnumeration\n NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];\n NSEnumerator *enumerator = [randomDict keyEnumerator];\n id key = nil;\n while ((key = [enumerator nextObject]) != nil) {\n       id obj = randomDict[key];\n       if (testObj(obj)) {\n           mutableDictionary[key] = obj;\n       }\n }\n\n// C-based array enumeration via getObjects:andKeys:\nNSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];\nid __unsafe_unretained objects[numberOfEntries];\nid __unsafe_unretained keys[numberOfEntries];\n[randomDict getObjects:objects andKeys:keys];\nfor (int i = 0; i < numberOfEntries; i++) {\n    id obj = objects[i];\n    id key = keys[i];\n    if (testObj(obj)) {\n       mutableDictionary[key] = obj;\n    }\n }\n```\n\n<table><thead><tr><th style=\"text-align: left;min-width:22em;\">Filtering/Enumeration Method</th><th style=\"text-align: right;\">Time [ms], 50.000 elements</th><th style=\"text-align: right;\">1.000.000 elements</th></tr></thead><tbody><tr><td style=\"text-align: left;\"><code>keysOfEntriesWithOptions:</code>, concurrent</td><td style=\"text-align: right;\">16.65</td><td style=\"text-align: right;\">425.24</td>\n</tr><tr><td style=\"text-align: left;\"><code>getObjects:andKeys:</code></td><td style=\"text-align: right;\">30.33</td><td style=\"text-align: right;\">798.49*</td>\n</tr><tr><td style=\"text-align: left;\"><code>keysOfEntriesWithOptions:</code></td><td style=\"text-align: right;\">30.59</td><td style=\"text-align: right;\">856.93</td>\n</tr><tr><td style=\"text-align: left;\"><code>enumerateKeysAndObjectsUsingBlock:</code></td><td style=\"text-align: right;\">36.33</td><td style=\"text-align: right;\">882.93</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSFastEnumeration</code></td><td style=\"text-align: right;\">41.20</td><td style=\"text-align: right;\">1043.42</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSEnumeration</code></td><td style=\"text-align: right;\">42.21</td><td style=\"text-align: right;\">1113.08</td>\n</tr></tbody></table>\n\n\n(\\*) There's a caveat when using `getObjects:andKeys:`. In the above code example, we're using a C99 feature called [variable-length arrays](http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html) (as normally, the array count needs to be a fixed variable). This will allocate memory on the stack, which is a bit more convenient, but also limited. The above code example will crash for a large number of elements, so use `malloc`/`calloc`-based allocation (and `free`) to be on the safe side.\n\nWhy is `NSFastEnumeration` so slow here? Iterating the dictionary usually requires both key and object; fast enumeration can only help for the key, and we have to fetch the object every time ourselves. Using the block-based `enumerateKeysAndObjectsUsingBlock:` is more efficient since both objects can be more efficiently prefetched. \n\nThe winner - again - is concurrent iteration via `keysOfEntriesWithOptions:passingTest:` and `objectsForKeys:notFoundMarker:`. This is a bit more code, but this can be nicely encapsulated in a category. \n\n### Should I Use dictionaryWithCapacity:?\n\nBy now you already now how this test works, and the short answer is NO, the `count` parameter doesn't change anything:\n\n`Adding 10000000 elements to `NSDictionary`. no count 10786.60[ms] with count: 10798.40[ms].`\n\n### Sorting\n\nThere's not much to say about dictionary sorting. You can only sort the key array as a new object, thus you can use any of the regular `NSArray` sorting methods as well:\n\n```objc\n- (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator;\n- (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptr;\n- (NSArray *)keysSortedByValueWithOptions:(NSSortOptions)opts \n                          usingComparator:(NSComparator)cmptr;\n```\n\n### Shared Keys\n\nStarting with iOS 6 and 10.8, it's possible to have a pre-generated key set for a new dictionary, using `sharedKeySetForKeys:` to create the key set from an array, and `dictionaryWithSharedKeySet:` to create the dictionary. Usually `NSDictionary` copies its keys. When using a shared key set it will instead reuse those objects, which saves memory. According to the [Foundation Release Notes](https://developer.apple.com/library/mac/releasenotes/Foundation/RN-FoundationOlderNotes/), `sharedKeySetForKeys:` will calculate a minimal perfect hash that eliminates any need for probe looping during a dictionary lookup, thus making keyed access even faster.\n\nThis makes it perfect for use cases like a JSON parser, although in our limited testing we couldn't see Apple using it in `NSJSONSerialization`. (Dictionaries created with shared key sets are of subclass `NSSharedKeyDictionary`; regular dictionaries are `__NSDictionaryI`/`__NSDictionaryM`, with I/M indicating mutability; and toll-free bridged dictionaries are of class `_NSCFDictionary`, both mutable and immutable variants.)\n\n**Interesting detail**: Shared-key dictionaries are **always mutable**, even when calling 'copy' on them. This behavior is not documented but can be easily tested:\n\n```objc\nid sharedKeySet = [NSDictionary sharedKeySetForKeys:@[@1, @2, @3]]; // returns NSSharedKeySet\nNSMutableDictionary *test = [NSMutableDictionary dictionaryWithSharedKeySet:sharedKeySet];\ntest[@4] = @\"First element (not in the shared key set, but will work as well)\";\nNSDictionary *immutable = [test copy];\nNSParameterAssert(immutable == 1);\n((NSMutableDictionary *)immutable)[@5] = @\"Adding objects to an immutable collection should throw an exception.\";\nNSParameterAssert(immutable == 2);\n```\n\n \n\n## NSSet\n\n`NSSet` and its mutable variant `NSMutableSet` are an unordered collection of objects. Checking for existence is usually an O(1) operation, making this much faster for this use case than `NSArray`. `NSSet` can only work efficiently if the hashing method used is balanced; if all objects are in the same hash bucket, then `NSSet` is not much faster in object-existence checking than `NSArray`.\n\nVariants of `NSSet` are also `NSCountedSet`, and the non-toll-free counter-variant `CFBag`/`CFMutableBag`.\n\n`NSSet` retains its object, but per the set contract, that object needs to be immutable. Adding objects to a set and then later changing that object will result in weird bugs and will corrupt the state of the set.\n\n`NSSet` has far less methods than `NSArray`. There is no sorting method but there are a few convenience enumeration methods. Some important methods are `allObjects` to convert the objects into an `NSArray` and `anyObject`, which returns either any object or nil, if the set is empty. \n\n### Set Manipulation\n\n`NSMutableSet` has several powerful set methods like `intersectSet:`, `minusSet:`, and `unionSet:`.\n\n![Set-Union](/images/issue-7/set.png)\n\n### Should I Use setWithCapacity:?\n\nAgain, we test if there is any noticeable speed difference when we initialize a set with a given capacity:\n\n`Adding 1.000.000 elements to `NSSet`. no count 2928.49[ms] with count: 2947.52[ms].`\n\nThis falls under measurement uncertainty - there's no noticeable time difference. There is [evidence that at least in the previous version of the runtime, this had much more of a performance impact](http://www.cocoawithlove.com/2008/08/nsarray-or-nsset-nsdictionary-or.html).\n\n### Performance Characteristics of NSSet\n\nApple doesn't provide any notes about the computational complexity in the [CFSet headers](http://www.opensource.apple.com/source/CF/CF-855.11/CFSet.h): \n\n<table><thead><tr><th style=\"text-align: left;\">Class / Time [ms]</th><th style=\"text-align: right;\">1.000.000 elements</th></tr></thead><tbody><tr><td style=\"text-align: left;\"><code>NSMutableSet</code>, adding</td><td style=\"text-align: right;\">2504.38</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMutableArray</code>, adding</td><td style=\"text-align: right;\">1413.38</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMutableSet</code>, random access</td><td style=\"text-align: right;\">4.40</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMutableArray</code>, random access</td><td style=\"text-align: right;\">7.95</td>\n</tr></tbody></table>\n\nThis benchmark is pretty much what we expected: `NSSet` calls `hash` and `isEqual:` on each added object and manages a buckets of hashes, so it takes more time on adding elements. Random access is hard to test with a set, since all there is is `anyObject`.\n\nThere was no need for including `containsObject:` in the benchmark. It is magnitudes faster on a set - that's their speciality, after all.\n\n## NSOrderedSet\n`NSOrderedSet` was first introduced in iOS 5 and Mac OS X 10.7, and there's almost no API directly using it, except for CoreData. It seems like a great class with the best of both `NSArray` and `NSSet`: having the benefits of instant object-existence checking, uniqueness, and fast random access.\n\n`NSOrderedSet` has great API methods, which makes it convenient to work with other set or ordered set objects. Union, intersection, and minus are supported just like in `NSSet`. It has most of the sort methods that are in `NSArray`, with the exception of the old function-based sort methods and binary search - after all, `containsObject:` is super fast, so there's no need for that.\n\nThe `array` and `set` accessors will respectively return an `NSArray` or `NSSet`, but with a twist! Those objects are facade objects that act like immutable objects and will update themselves as the ordered set is updated. This is good to know when you're planning to iterate those objects on different threads and get mutation exceptions. Internally, the classes used are are `__NSOrderedSetSetProxy` and `__NSOrderedSetArrayProxy`.\n\nSide Note: If you're wondering why `NSOrderedSet` isn't a subclass of `NSSet`, there's [a great article on NSHipster explaining the downsides of mutable/immutable class clusters](http://nshipster.com/nsorderedset/).\n\n### Performance Characteristics of NSOrderedSet\n\nIf you look at this benchmark, you see where `NSOrderedSet` starts getting expensive. All those benefits can't come for free:\n\n<table><thead><tr><th style=\"text-align: left;\">Class / Time [ms]</th><th style=\"text-align: right;\">1.000.000 elements</th></tr></thead><tbody><tr><td style=\"text-align: left;\"><code>NSMutableOrderedSet</code>, adding</td><td style=\"text-align: right;\"><strong>3190.52</strong></td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMutableSet</code>, adding</td><td style=\"text-align: right;\">2511.96</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMutableArray</code>, adding</td><td style=\"text-align: right;\">1423.26</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMutableOrderedSet</code>, random access</td><td style=\"text-align: right;\"><strong>10.74</strong></td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMutableSet</code>, random access</td><td style=\"text-align: right;\">4.47</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMutableArray</code>, random access</td><td style=\"text-align: right;\">8.08</td>\n</tr></tbody></table>\n\n\nThis benchmark adds custom strings to each of these collection classes, and later randomly accesses those.\n\n`NSOrderedSet` will also take up more memory than either `NSSet` or `NSArray`, since it needs to maintain both hashed values and indexes.\n\n## NSHashTable\n\n`NSHashTable` is modeled after `NSSet`, but is much more flexible when it comes to object/memory handling. While some of the features of `NSHashTable` can be achieved via custom callbacks on `CFSet`, hash table can hold objects weakly and will properly nil out itself when the object is deallocated - something that would be quite ugly when manually added to an `NSSet`. It's also mutable by default - there is no immutable counterpart.\n\n`NSHashTable` has both an ObjC and a raw C API, where the C API can be used to store arbitrary objects. Apple introduced this class in 10.5 Leopard, but only added it quite recently in iOS 6. Interestingly enough, they only ported the ObjC API; the more powerful C API is excluded on iOS.\n\n`NSHashTable` is wildly configurable via the `initWithPointerFunctions:capacity:` - we're only picking the most common use cases, which are also predefined using `hashTableWithOptions:`. The most useful option has its own convenience constructor via `weakObjectsHashTable`.\n\n### NSPointerFunctions\n\nThese pointer functions are valid for `NSHashTable`, `NSMapTable`, and `NSPointerArray`, and define the acquisition and retention behavior for the objects saved in these collections. Here are the most useful options. For the full list, see `NSPointerFunctions.h`.\n\nThere are two groups of options. Memory options determine memory management, and personalities define hashing and equality.\n\n`NSPointerFunctionsStrongMemory` creates a collection that retains/releases objects, much like a regular `NSSet` or `NSArray`.\n     \n`NSPointerFunctionsWeakMemory` uses an equivalent of `__weak` to store objects and will automatically evict deallocated objects.\n\n`NSPointerFunctionsCopyIn` copies the objects before they are added to the collection. \n\n\n`NSPointerFunctionsObjectPersonality` uses `hash` and `isEqual:` from the object (default). \n\n`NSPointerFunctionsObjectPointerPersonality` uses direct-pointer comparison for `isEqual:` and `hash`.\n\n### Performance Characteristics of NSHashTable\n\n<table><thead><tr><th style=\"text-align: left;\">Class / Time [ms]</th><th style=\"text-align: right;\">1.000.000 elements</th></tr></thead><tbody><tr><td style=\"text-align: left;\"><code>NSHashTable</code>, adding</td><td style=\"text-align: right;\">2511.96</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMutableSet</code>, adding</td><td style=\"text-align: right;\">1423.26</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSHashTable</code>, random access</td><td style=\"text-align: right;\">3.13</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMutableSet</code>, random access</td><td style=\"text-align: right;\">4.39</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSHashTable</code>, containsObject</td><td style=\"text-align: right;\">6.56</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMutableSet</code>, containsObject</td><td style=\"text-align: right;\">6.77</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSHashTable</code>, NSFastEnumeration</td><td style=\"text-align: right;\">39.03</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMutableSet</code>, NSFastEnumeration</td><td style=\"text-align: right;\">30.43</td>\n</tr></tbody></table>\n\n\nIf you just need the features of an `NSSet`, then stick at `NSSet`. `NSHashTable` takes almost twice as long to add objects, but has quite similar performance characteristics.\n\n## NSMapTable\n\n`NSMapTable` is similar to `NSHashTable`, but modeled after `NSDictionary`. Thus, we can control object acquisition/retention for both the keys and objects separately, via `mapTableWithKeyOptions:valueOptions:`. Since storing one part weak is again the most useful feature of `NSMapTable`, there are now four convenience constructors for this use case:\n\n* `strongToStrongObjectsMapTable`\n* `weakToStrongObjectsMapTable`\n* `strongToWeakObjectsMapTable`\n* `weakToWeakObjectsMapTable`\n\nNote that - unless created with `NSPointerFunctionsCopyIn` - any of the defaults will retain (or weakly reference) the key object, and not copy it, thus matching the behavior of `CFDictionary` and not `NSDictionary`. This can be quite useful if you need a dictionary whose key does not implement `NSCopying`, like `UIView`.\n\nIf you're wondering why Apple \"forgot\" adding subscripting to `NSMapTable`, you now know why. Subscripting requires an `id<NSCopying>` as key, which is not necessary for `NSMapTable`. There's no way to add subscripting to it without having an invalid API contract or weakening subscripting globally with removing the `NSCopying` protocol.\n\nYou can convert the contents to an ordinary `NSDictionary` using `dictionaryRepresentation`. This returns a regular dictionary and not a proxy - unlike `NSOrderedSet`:\n\n### Performance of NSMapTable\n\n<table><thead><tr><th style=\"text-align: left;\">Class / Time [ms]</th><th style=\"text-align: right;\">1.000.000 elements</th></tr></thead><tbody><tr><td style=\"text-align: left;\"><code>NSMapTable</code>, adding</td><td style=\"text-align: right;\">2958.48</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMutableDictionary</code>, adding</td><td style=\"text-align: right;\">2522.47</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMapTable</code>, random access</td><td style=\"text-align: right;\">13.25</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMutableDictionary</code>, random access</td><td style=\"text-align: right;\">9.18</td>\n</tr></tbody></table>\n\n`NSMapTable` is only marginally slower than `NSDictionary`. If you need a dictionary that doesn't retain its keys, go for it, and leave `CFDictionary` behind.\n\n## NSPointerArray\n\nThe `NSPointerArray` class is a sparse array that works similar to an `NSMutableArray`, but can also hold `NULL` values, and the `count` method will reflect those empty spots. It can be configured with various options from `NSPointerFunctions`, and has convenience constructors for the common use cases, `strongObjectsPointerArray`, and `weakObjectsPointerArray`.\n\nBefore you can use `insertPointer:atIndex:`, we need to make space by directly setting the `count` property, or you will get an exception. Alternatively, using `addPointer:` will automatically increase array size if needed.\n\nYou can convert an `NSPointerArray` into a regular `NSArray` via `allObjects`. In that case, all `NULL` values are compacted, and only existing objects are added - thus the object indexes of this array will most likely be different than in the pointer array. Careful: if you are storing anything other than objects into the pointer array, attempting to call `allObjects` will crash with `EXC_BAD_ACCESS`, as it tries to retain the \"objects\" one by one.\n\nFrom a debugging point of view, `NSPointerArray` didn't get much love. The `description` method simply returns `<NSConcretePointerArray: 0x17015ac50>`. To get to the objects you need to, call `[pointerArray allObjects]`, which, of course, will change the indexes if there are any `NULLs` in between.\n\n### Performance of NSPointerArray\n\nWhen it comes to performance, `NSPointerArray` is really, really slow, so think twice if you plan to use it on a large data set. In our benchmark we're comparing `NSMutableArray` with `NSNull` as an empty marker and `NSPointerArray` with a `NSPointerFunctionsStrongMemory` configuration (so that objects are properly retained). Then in an array of 10,000 elements, we fill every tenth entry with a string \"Entry %d\". The benchmark includes the total time it takes for `NSMutableArray` to be filled with `NSNull.null`. For `NSPointerArray`, we use `setCount:` instead:\n\n\n<table><thead><tr><th style=\"text-align: left;\">Class / Time [ms]</th><th style=\"text-align: right;\">10.000 elements</th></tr></thead><tbody><tr><td style=\"text-align: left;\"><code>NSMutableArray</code>, adding</td><td style=\"text-align: right;\">15.28</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSPointerArray</code>, adding</td><td style=\"text-align: right;\"><strong>3851.51</strong></td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMutableArray</code>, random access</td><td style=\"text-align: right;\">0.23</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSPointerArray</code>, random access</td><td style=\"text-align: right;\">0.34</td>\n</tr></tbody></table>\n\nNotice that `NSPointerArray` requires more than **250x (!)** more time than `NSMutableArray`. This is really surprising and unexpected. Tracking memory is harder and it's likely that `NSPointerArray` is more efficient here, but since we use one shared instance for `NSNull` to mark empty objects, there shouldn't be much overhead except pointers.\n\n## NSCache\n`NSCache` is quite an odd collection. Added in iOS 4 / Snow Leopard, it's mutable by default, and also **thread safe**. This makes it perfect to cache objects that are expensive to create. It automatically reacts to memory warnings and will clean itself up based on a configurable \"cost.” In contrast to `NSDictionary`, keys are retained and not copied.\n\nThe eviction method of `NSCache` is non-deterministic and not documented. It's not a good idea to put in super-large objects like images that might fill up your cache faster than it can evict itself. (This was the case of many memory-related crashes in [PSPDFKit](http://PSPDFKit.com), where we initially used `NSCache` for storing pre-rendered images of pages, before switching to custom caching code based on a LRU linked list.)\n\n`NSCache` can also be configured to automatically evict objects that implement the `NSDiscardableContent` protocol. A popular class implementing this property is `NSPurgeableData`, which as been added at the same time, but was [\"not fully thread safe\" until OS X 10.9 (there's no information if this has affected iOS as well, or if this fix landed in iOS 7)](https://developer.apple.com/library/mac/releasenotes/Foundation/RN-Foundation/index.html#//apple_ref/doc/uid/TP30000742).\n\n### Performance of NSCache\n\nSo how does `NSCache` hold up compared to an `NSMutableDictionary`? The added thread safety surely takes some overhead. Out of curiosity, I've also added a custom, thread-safe dictionary subclass ([`PSPDFThreadSafeMutableDictionary`](https://gist.github.com/steipete/5928916)) that synchronizes access via an `OSSpinLock`:\n\n<table><thead><tr><th style=\"text-align: left;min-width:28em;\">Class / Time [ms]</th><th style=\"text-align: right;\">1.000.000 elements</th><th style=\"text-align: right;\">iOS 7x64 Simulator</th><th style=\"text-align: right;\">iPad Mini iOS 6</th></tr></thead><tbody><tr><td style=\"text-align: left;\"><code>NSMutableDictionary</code>, adding</td><td style=\"text-align: right;\">195.35</td><td style=\"text-align: right;\">51.90</td><td style=\"text-align: right;\">921.02</td>\n</tr><tr><td style=\"text-align: left;\"><code>PSPDFThreadSafeMutableDictionary</code>, adding</td><td style=\"text-align: right;\">248.95</td><td style=\"text-align: right;\">57.03</td><td style=\"text-align: right;\">1043.79</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSCache</code>, adding</td><td style=\"text-align: right;\">557.68</td><td style=\"text-align: right;\">395.92</td><td style=\"text-align: right;\">1754.59</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSMutableDictionary</code>, random access</td><td style=\"text-align: right;\">6.82</td><td style=\"text-align: right;\">2.31</td><td style=\"text-align: right;\">23.70</td>\n</tr><tr><td style=\"text-align: left;\"><code>PSPDFThreadSafeMutableDictionary</code>, random access</td><td style=\"text-align: right;\">9.09</td><td style=\"text-align: right;\">2.80</td><td style=\"text-align: right;\">32.33</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSCache</code>, random access</td><td style=\"text-align: right;\">9.01</td><td style=\"text-align: right;\"><strong>29.06</strong></td><td style=\"text-align: right;\">53.25</td>\n</tr></tbody></table>\n\n\n`NSCache` holds up pretty well, and random access is equally fast as our custom thread-safe dictionary. Adding is slower, as expected, because `NSCache` also keeps an optional cost factor around, has to determine when to evict objects, and so on - it's not a very fair comparison in that regard. Interestingly, it performs almost ten times worse when run in the Simulator. This is true for all variants, 32 or 64 bit. It also looks like it has been optimized in iOS 7 or simply benefits from the 64-bit runtime. When testing with an older device, the performance overhead of using `NSCache` is far more noticeable.\n\nThe difference between iOS 6 (32 bit) and iOS 7 (64 bit) is also far more noticeable since the 64-bit runtime uses [tagged pointers](http://www.mikeash.com/pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html), and thus our `@(idx)` boxing is much more efficient there.\n\n## NSIndexSet\n\nThere are a few use cases where `NSIndexSet` (and its mutable variant, `NSMutableIndexSet`) really shines, and you will find various usages throughout Foundation. It can save a collection of unsigned integers in a very efficient way, especially if it's only one or a few ranges. As the name \"set\" already implies, each `NSUInteger` is either in the index set or isn't. If you need to store an arbitrary number of integers that are not unique, better use an `NSArray`. \n\nThis is how you would convert an array of integers to an `NSIndexSet`:\n\n```objc\nNSIndexSet *PSPDFIndexSetFromArray(NSArray *array) {\n    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];\n    for (NSNumber *number in array) {\n        [indexSet addIndex:[number unsignedIntegerValue]];\n    }\n    return [indexSet copy];\n}\n```\n\nGetting all indexes out of the index set was a bit fiddly before we had blocks, with `getIndexes:maxCount:inIndexRange:` being the fastest way, next to using `firstIndex` and iterating until `indexGreaterThanIndex:` returned `NSNotFound`. With the arrival of blocks, working with `NSIndexSet` has become a lot more convenient:\n\n```objc\nNSArray *PSPDFArrayFromIndexSet(NSIndexSet *indexSet) {\n    NSMutableArray *indexesArray = [NSMutableArray arrayWithCapacity:indexSet.count];\n    [indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {\n       [indexesArray addObject:@(idx)];\n    }];\n    return [indexesArray copy];\n}\n```\n\n### Performance of NSIndexSet\n\nThere's no equivalent to `NSIndexSet` in Core Foundation, and Apple doesn't make any promises to performance. A comparison between `NSIndexSet` and `NSSet` is also relatively unfair to begin with, since the regular set requires boxing for the numbers. To mitigate this, the benchmark will prepare pre-boxed `NSUInteger`s, and will call `unsignedIntegerValue` on both loops:\n\n<table><thead><tr><th style=\"text-align: left;min-width:20em;\">Class / Time per Entries [ms]</th><th style=\"text-align: right;\">#1.000</th><th style=\"text-align: right;\">#10.000</th><th style=\"text-align: right;\">#1.000.000</th><th style=\"text-align: right;\">#10.000.000</th><th style=\"text-align: right;\">#1.000.000, iPad Mini iOS 6</th></tr></thead><tbody><tr><td style=\"text-align: left;\"><code>NSIndexSet</code>, adding</td><td style=\"text-align: right;\">0.28</td><td style=\"text-align: right;\">4.58</td><td style=\"text-align: right;\">98.60</td><td style=\"text-align: right;\">9396.72</td><td style=\"text-align: right;\">179.27</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSSet</code>, adding</td><td style=\"text-align: right;\">0.30</td><td style=\"text-align: right;\">2.60</td><td style=\"text-align: right;\">8.03</td><td style=\"text-align: right;\">91.93</td><td style=\"text-align: right;\">37.43</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSIndexSet</code>, random access</td><td style=\"text-align: right;\">0.10</td><td style=\"text-align: right;\">1.00</td><td style=\"text-align: right;\">3.51</td><td style=\"text-align: right;\">58.67</td><td style=\"text-align: right;\">13.44</td>\n</tr><tr><td style=\"text-align: left;\"><code>NSSet</code>, random access</td><td style=\"text-align: right;\">0.17</td><td style=\"text-align: right;\">1.32</td><td style=\"text-align: right;\">3.56</td><td style=\"text-align: right;\">34.42</td><td style=\"text-align: right;\">18.60</td>\n</tr></tbody></table>\n\n\nWe'll see that at around 1 million entries, `NSIndexSet` starts becoming slower than `NSSet`, but only because of the new runtime and tagged pointers. Running the same test on iOS 6 shows that `NSIndexSet` is faster, even with this high number of entries. Realistically, in most apps, you won't add that many integers into the index set. What's not measured here is that `NSIndexSet` certainly has a greatly optimized memory layout compared to `NSSet`\n\n## Conclusion\n\nThis article provides you with some real-world benchmarks to make informed choices when using Foundation's collection classes. Next to the discussed classes, there are some less common but still useful ones, especially `NSCountedSet`, [`CFBag`](http://nshipster.com/cfbag/), [`CFTree`](https://developer.apple.com/library/mac/documentation/corefoundation/Reference/CFTreeRef/Reference/reference.html), [`CFBitVector`](https://developer.apple.com/library/mac/documentation/corefoundation/Reference/CFBitVectorRef/Reference/reference.html), and [`CFBinaryHeap`](https://developer.apple.com/library/mac/documentation/corefoundation/Reference/CFBinaryHeapRef/Reference/reference.html).    \n"
  },
  {
    "path": "2013-12-09-communication-patterns.md",
    "content": "---\ntitle:  \"Communication Patterns\"\ncategory: \"7\"\ndate: \"2013-12-09 08:00:00\"\ntags: article\nauthor:\n  - name: Florian Kugler\n    url: https://twitter.com/floriankugler\n---\n\n\nEvery application consists of multiple more or less loosely coupled objects that need to communicate with each other to get the job done. In this article we will go through all the available options, look at examples how they are used within Apple’s frameworks, and extract some best-practice recommendations regarding when you should use which mechanism. \n\nAlthough this issue is about the Foundation framework, we will look beyond the communication mechanisms that are part of Foundation -- KVO and Notifications -- and also talk about delegation, blocks, and target-action. \n\nOf course, there are cases where there is no definitive answer as to what pattern should be used, and the choice comes down to a matter of taste. But there are also many cases that are pretty clear cut.\n\nIn this article, we will often use the terms \"recipient\" and \"sender.” What we mean with those in the context of communication patterns is best explained by a few examples: a table view is the sender, while its delegate is the recipient. A Core Data managed object context is the sender of the notifications it posts, and whatever picks them up is the recipient. A slider is the sender of a action message, and the responder that implements this action is the recipient. An object with a KVO-compliant property that changes is the sender, while the observer is the recipient. Getting the hang of it?\n\n\n\n## Patterns\n\nFirst we will have a look at the specific characteristics of each available communication pattern. Based on this, we will construct a flow chart in the next section that helps to choose the right tool for the job. Finally, we will go over some examples from Apple's frameworks and reason why they decided to use specific patterns for certain use cases.\n\n\n### KVO\n\nKVO is a mechanism to notify objects about property changes. It is implemented in Foundation and many frameworks built on top of Foundation rely on it. To read more about best practices and examples of how to use KVO, please read Daniel's [KVO and KVC article](/issues/7-foundation/key-value-coding-and-observing/) in this issue.\n\nKVO is a viable communication pattern if you're only interested in changed values of another object. There are a few more requirements though. First, the recipient -- the object that will receive the messages about changes -- has to know about the sender -- the object with values that are changed. Furthermore, the recipient also needs to know about the lifespan of the sender, because it has to unregister the observer before the sender object gets deallocated. If all these requirements are met, the communication can even be one-to-many, since multiple observers can register for updates from the object in question. \n\nIf you plan to use KVO on Core Data objects, you have to know that things work a bit differently here. This has to do with Core Data's faulting mechanism. Once a managed object turns into a fault, it will fire the observers on its properties although their values haven't changed.\n\n\n### Notifications\n\nNotifications are a very good tool to broadcast messages between relatively unrelated parts of your code, especially if the messages are more informative in kind and you don't necessarily expect anyone to do something with them. \n\nNotifications can be used to send arbitrary messages and they can even contain a payload in form of their `userInfo` dictionary or by subclassing `NSNotification`. What makes notifications unique is that the sender and the recipient don't have to know each other. They can be used to send information between very loosely coupled modules. Therefore, the communication is one-way -- you cannot reply to a notification.\n\n\n### Delegation\n\nDelegation is a widespread pattern throughout Apple's frameworks. It allows us to customize an object's behavior and to be notified about certain events. For the delegation pattern to work, the message sender needs to know about the recipient (the delegate), but not the other way around. The coupling is further loosened, because the sender only knows that its delegate conforms to a certain protocol.\n\nSince a delegate protocol can define arbitrary methods, you can model the communication exactly to your needs. You can hand over payloads in the form of method arguments, and the delegate can even respond in terms of the delegate method's return value. Delegation is a very flexible and straightforward communication pattern if you only need to communicate between two specific objects that are in relative proximity to each other in terms of their place in your app architecture.\n\nBut there's also the danger of overusing the delegation pattern. If two objects are that tightly coupled to each other that one cannot function without the other, there's no need to define a delegate protocol. In these cases, the objects can know of the other's type and talk to each other directly. Two modern examples of this are `UICollectionViewLayout` and `NSURLSessionConfiguration`. \n\n<a name=\"blocks\"> </a>\n\n### Blocks\n\nBlocks are a relatively recent addition to Objective-C, first available in OS X 10.6 and iOS 4. Blocks can often fulfill the role of what previously would have been implemented using the delegation pattern. However, both patterns have unique sets of requirements and advantages.\n\nOne pretty clear criterium of when not to use blocks has to do with the danger of creating [retain cycles](https://developer.apple.com/library/mac/documentation/cocoa/conceptual/memorymgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-1000810). If the sender needs to retain the block and cannot guarantee that this reference will be a nilled out, then every reference to `self` from within the block becomes a potential retain cycle.\n\nLet's assume we wanted to implement a table view, but we want to use block callbacks instead of a delegate pattern for the selection methods, like this:\n\n```objc\nself.myTableView.selectionHandler = ^void(NSIndexPath *selectedIndexPath) {\n    // handle selection ...\n};\n```\n\nThe issue here is that `self` retains the table view, and the table view has to retain the block in order to be able to use it later. The table view cannot nil out this reference, because it cannot tell when it will not need it anymore. If we cannot guarantee that the retain cycle will be broken and we will retain the sender, then blocks are not a good choice.\n\n`NSOperation` is a good example of where this does not become a problem, because it breaks the retain cycle at some point:\n\n```objc\nself.queue = [[NSOperationQueue alloc] init];\nMyOperation *operation = [[MyOperation alloc] init];\noperation.completionBlock = ^{\n    [self finishedOperation];\n};\n[self.queue addOperation:operation];\n```\n\nAt first glance this seems like a retain cycle: `self` retains the queue, the queue retains the operation, the operation retains the completion block, and the completion block retains `self`. However, adding the operation to the queue will result in the operation being executed at some point and then being removed from the queue afterward. (If it doesn't get executed, we have a bigger problem anyway.) Once the queue removes the operation, the retain cycle is broken.\n\nAnother example: let's say we're implementing a video encoder class, on which we call an `encodeWithCompletionHandler:` method. To make this non-problematic, we have to guarantee that the encoder object nils out its reference to the block at some point. Internally, this would have to look something like this:\n\n```objc\n@interface Encoder ()\n@property (nonatomic, copy) void (^completionHandler)();\n@end\n\n@implementation Encoder\n\n- (void)encodeWithCompletionHandler:(void (^)())handler\n{\n    self.completionHandler = handler;\n    // do the asynchronous processing...\n}\n\n// This one will be called once the job is done\n- (void)finishedEncoding\n{\n    self.completionHandler();\n    self.completionHandler = nil; // <- Don't forget this!\n}\n\n@end\n```\n\nOnce our job is done and we've called the completion block, we nil it out. \n\nBlocks are a very good fit if a message we call has to send back a one-off response that is specific to this method call, because then we can break potential retain cycles. Additionally, if it helps readability to have the code processing the message together with the message call, it's hard to argue against the use of blocks. Along these lines, a very common use case of blocks are completion handlers, error handlers, and the like.\n\n\n### Target-Action\n\nTarget-Action is the typical pattern used to send messages in response to user-interface events. Both `UIControl` on iOS and `NSControl`/`NSCell` on the Mac have support for this pattern. Target-Action establishes a very loose coupling between the sender and the recipient of the message. The recipient of the message doesn't know about the sender, and even the sender doesn't have to know up front what the recipient will be. In case the target is `nil`, the action will travel up the [responder chain](https://developer.apple.com/library/ios/documentation/general/conceptual/Devpedia-CocoaApp/Responder.html) until it finds an object that responds to it. On iOS, each control can even be associated with multiple target-action pairs.\n\nA limitation of target-action-based communication is that the messages sent cannot carry any custom payloads. On the Mac action methods always receive the sender as first argument. On iOS they optionally receive the sender and the event that triggered the action as arguments. But beyond that, there is no way to have a control send other objects with the action message.\n\n\n## Making the Right Choice\n\nBased on the characteristics of the different patterns outlined above, we have constructed a flowchart that helps to make good decisions of which pattern to use in what situation. As a word of warning: the recommendation of this chart doesn't have to be the final answer; there might be other alternatives that work equally well. But in most cases it should guide you to the right pattern for the job.\n\n![Decision flow chart for communication patterns in Cocoa](/images/issue-7/communication-patterns-flow-chart.png)\n\nThere are a few other details in this chart which deserve further explanation:\n\nOne of the boxes says, *sender is KVO compliant*. This doesn't mean only that the sender sends KVO notifications when the value in question changes, but also that the observer knows about the lifespan of the sender. If the sender is stored in a weak property, it can get nilled out at any time and the observer will leak.\n\nAnother box in the bottom row says, *message is direct response to method call*. This means that the receiver of the method call needs to talk back to the caller of the method as a direct response of the method call. It mostly also means that it makes sense to have the code processing this message in the same place as the method call.\n\nLastly, in the lower right, a decision question states, *sender can guarantee to nil out reference to block?*. This refers to the discussion [above](#blocks) about block-based APIs and potential retain cycles. If the sender cannot guarantee that the reference to the block it’s holding will be nilled out at some point, you're asking for trouble with retain cycles.\n\n\n## Framework Examples\n\nIn this section, we will go through some examples from Apple's frameworks to see if the decision flow outlined before actually makes sense, and why Apple chose the patterns as they are.\n\n\n### KVO\n\n`NSOperationQueue` uses KVO to observe changes to the state properties of its operations (`isFinished`, `isExecuting`, `isCancelled`). When the state changes, the queue gets a KVO notification. Why do operation queues use KVO for this? \n\nThe recipient of the messages (the operation queue) clearly knows the sender (the operation) and controls its lifespan by retaining it. Furthermore, this use case only requires a one-way communication mechanism. When it comes to the question of if the operation queue is only interested in value changes of the operation, the answer is less clear. But we can at least say that what has to be communicated (the change of state) can be modeled as value changes. Since the state properties are useful to have beyond the operation queue's need to be up to date about the operation's status, using KVO is a logical choice in this scenario.\n\n![Decision flow chart for communication patterns in Cocoa](/images/issue-7/kvo-flow-chart.png)\n\nKVO is not the only choice that would work though. We could also imagine that the operation queue becomes the operation's delegate, and the operation would call methods like `operationDidFinish:` or `operationDidBeginExecuting:` to signal changes in its state to the queue. This would be less convenient though, because the operation would have to keep its state properties up to date in addition to calling these methods. Furthermore, the queue would have to keep track of the state of all its operations, because it cannot ask for them anymore.\n\n\n### Notifications\n\nCore Data uses notifications to communicate events like changes within a managed object context (`NSManagedObjectContextObjectsDidChangeNotification`). \n\nThe change notification is sent by managed object contexts, so that we cannot assume that the recipient of this message necessarily knows about the sender. Since the origin of the message is clearly not a UI event, multiple recipients might be interested in it, and all it needs is a one-way communication channel, notifications are the only feasible choice in this scenario.\n\n![Decision flow chart for communication patterns in Cocoa](/images/issue-7/notification-flow-chart.png)\n\n\n### Delegation\n\nTable view delegates fulfill a variety of functions, from managing accessory views over editing to tracking the cells that are on screen. For the sake of this example, we'll look at the `tableView:didSelectRowAtIndexPath:` method. Why is this implemented as a delegate call? Why not as a target-action pattern?\n\nAs we've outlined in the flowchart above, target-action only works if you don't have to transport any custom payloads. In the selection case, the collection view tells us not only that a cell got selected, but also which cell got selected by handing over its index path. If we maintain this requirement to send the index path, our flowchart guides us straight to the delegation pattern.\n\n![Decision flow chart for communication patterns in Cocoa](/images/issue-7/delegation-flow-chart.png)\n\nWhat about the option to not send the index path with the selection message, but rather retrieve it by asking the table view about the selected cells once we've received the message? This would be pretty inconvenient, because then we would have to do our own bookkeeping of which cells are currently selected in order to tell which cell was newly selected in the case of multiple selection.\n\nSimilarly, we could envision being notified about a changed selection by simply observing a property with selected index paths on the table view. However, we would run into the same problem as outlined above, where we couldn’t distinguish which cell was recently selected/deselected without doing our own bookkeeping of it.\n\n\n### Blocks\n\nFor a block-based API we're going to look at `-[NSURLSession dataTaskWithURL:completionHandler:]` as an example. What is the communication back from the URL loading system to the caller of it like? First, as caller of this API, we know the sender of the message, but we don't retain it. Furthermore, it's a one way-communication that is a directly coupled to the `dataTaskWithURL:` method call. If we apply all these factors into the flowchart, we directly end up at the block-based communication pattern.\n\n![Decision flow chart for communication patterns in Cocoa](/images/issue-7/block-flow-chart.png)\n\nAre there other options? For sure, Apple's own `NSURLConnection` is the best example. `NSURLConnection` was crafted before Objective-C had blocks, so they needed to take a different route and implemented this communication using the delegation pattern. Once blocks were available, Apple added the method `sendAsynchronousRequest:queue:completionHandler:` to `NSURLConnection` in OS X 10.7 and iOS 5, so that you didn't need the delegate any longer for simple tasks. \n\nSince `NSURLSession` is a very modern API that was just added in OS X 10.9 and iOS 7, blocks are now the pattern of choice to do this kind of communication (`NSURLSession` still has a delegate, but for other purposes). \n\n\n### Target-Action\n\nAn obvious use case for the target-action pattern are buttons. Buttons don't have to send any information except that they have been clicked (or tapped). For this purpose, target-action is a very flexible pattern to inform the application of this user interface event. \n\n![Decision flow chart for communication patterns in Cocoa](/images/issue-7/target-action-flow-chart.png)\n\nIf the target is specified, the action message gets sent straight to this object. However, if the target is `nil`, the action message bubbles up the responder chain to look for an object that can process it. In this case, we have a completely decoupled communication mechanism where the sender doesn't have to know the recipient, and the other way around.\n\nThe target-action pattern is perfect for user interface events. No other communication pattern can provide the same functionality. Notifications come the closest in terms of total decoupling of sender and recipient, but what makes target-action special is the use of the responder chain. Only one object gets to react to the action, and the action travels a well-defined path through the responder hierarchy until it gets picked up by something.\n\n\n## Conclusion\n\nThe number of patterns available to communicate information between objects can be overwhelming at first. The choice of which pattern to use often feels ambiguous. But once we investigate each pattern more closely, they all have very unique requirements and capabilities.\n\nThe decision flowchart is a good start to create clarity in the choice of a particular pattern, but of course it's not the end to all questions. We're happy to hear from you if it matches up with the way you're using these patterns, or if you think there's something missing or misleading. \n\n\n"
  },
  {
    "path": "2013-12-09-editorial.md",
    "content": "---\r\ntitle:  \"Editorial\"\r\ncategory: \"7\"\r\ndate: \"2013-12-09 12:00:00\"\r\ntags: editorial\r\n---\r\n\r\nWelcome to objc.io issue #7!\r\n\r\nThis month we picked the Foundation framework as our topic.\n\nThe roots of the Foundation framework go all the way back to the era of [NeXTSTEP](https://en.wikipedia.org/wiki/Nextstep), which has been around for 25 years and covers a lot of ground.\n\nMost of Foundation will not be covered here, but we're taking a stab at some of it in the issue. We picked a few topics that are relevant to all Objective-C developers: collection classes, key-value coding and key-value observing, value objects, and value formatters. We have also included a more general article about communication patterns; it goes beyond the patterns found in Foundation to cover a somewhat more exotic topic: `NSLinguisticTagger`.\n\nThis month we have to thank [Peter Steinberger](http://petersteinberger.com), [Klaas Pieter Annema](https://twitter.com/klaaspieter), and [Oliver Mason](https://twitter.com/ojmason) for their awesome contributions. objc.io wouldn't be possible without all the support from the community. Be sure to check out our [contributors' page](/contributors.html) to see what we mean.\n\nSpeaking of support, last month we launched the [objc.io Newsstand app](https://itunes.apple.com/de/app/objc.io/id683718429), which, we said, you should consider to be our donate button. The response has been fantastic. That said, we are not going to go out and buy any fast cars any time soon. The money will instead go toward costs of running the website, and we will donate any surplus to a project / projects in line with the spirit of objc.io as a community collaboration.\n\nIn the interest of transparency, it’s important to note that our costs are almost entirely made up of hosting, design work, and copy editing. Additionally, we will put a little bit of money aside every month so that we can improve objc.io in the future. But after that, there's still a fair amount left. \n\nAs mentioned above, we decided to put the money where it makes a difference. Starting this month, we will donate extra money to a charitable cause. We'll have more news for you on this topic next month.\n\n\r\nAll the best from Berlin,\r\n\r\nChris, Daniel, and Florian.\r\n"
  },
  {
    "path": "2013-12-09-key-value-coding-and-observing.md",
    "content": "---\ntitle: \"Key-Value Coding and Observing\"\ncategory: \"7\"\ndate: \"2013-12-09 09:00:00\"\ntags: article\nauthor:\n  - name: Daniel Eggert\n    url: http://twitter.com/danielboedewadt\n---\n\n\nKey-value coding and key-value observing are two formalized mechanisms that allow us to simplify our code by harnessing the dynamic and introspective properties of the Objective-C language. In this article, we’ll take a look at some examples on how to put this to use.\n\n## Observing Changes to Model Objects\n\nIn Cocoa, the Model-View-Controller pattern, a controller's responsibility is to keep the view and the model synchronized. There are two parts to this: when the model object changes, the views have to be updated to reflect this change, and when the user interacts with controls, the model has to be updated accordingly.\n\n*Key-Value Observing* helps us update the views to reflect changes to model objects. The controller can observe changes to those property values that the views depend on.\n\nLet's look at a sample: Our model class `LabColor` is a color in the [Lab color space](https://en.wikipedia.org/wiki/Lab_color_space) where the components are *L*, *a*, and *b* (instead of red, green, and blue). We want sliders to change the values and a big rectangle that shows the color.\n\nOur model class will have three properties for the components:\n\n```objc\n@property (nonatomic) double lComponent;\n@property (nonatomic) double aComponent;\n@property (nonatomic) double bComponent;\n```\n\n### Dependent Properties\n\nWe need to create a `UIColor` from this that we can use to display the color. We'll add three additional properties for the red, green, and blue components and another property for the `UIColor`:\n\n```objc\n@property (nonatomic, readonly) double redComponent;\n@property (nonatomic, readonly) double greenComponent;\n@property (nonatomic, readonly) double blueComponent;\n\n@property (nonatomic, strong, readonly) UIColor *color;\n```\n\nWith this, we have all we need for our class interface:\n\n```objc\n@interface LabColor : NSObject\n\n@property (nonatomic) double lComponent;\n@property (nonatomic) double aComponent;\n@property (nonatomic) double bComponent;\n\n@property (nonatomic, readonly) double redComponent;\n@property (nonatomic, readonly) double greenComponent;\n@property (nonatomic, readonly) double blueComponent;\n\n@property (nonatomic, strong, readonly) UIColor *color;\n\n@end\n```\n\nThe math for calculating the red, green, and blue components is outlined [on Wikipedia](https://en.wikipedia.org/wiki/Lab_color_space#CIELAB-CIEXYZ_conversions). It looks something like this:\n\n```objc\n- (double)greenComponent;\n{\n    return D65TristimulusValues[1] * inverseF(1./116. * (self.lComponent + 16) + 1./500. * self.aComponent);\n}\n\n[...]\n\n- (UIColor *)color\n{\n    return [UIColor colorWithRed:self.redComponent * 0.01 green:self.greenComponent * 0.01 blue:self.blueComponent * 0.01 alpha:1.];\n}\n```\n\nNothing too exciting here. What's interesting to us is that this `greenComponent` property depends on the `lComponent` and `aComponent` properties. This is important to key-value observing; whenever we set the `lComponent` property we want anyone interested in either of the red-green-blue components or the `color` property to be notified.\n\nThe mechanism that the Foundation framework provides for expressing dependencies is:\n\n```objc\n+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key\n```\n\nand more specifically:\n\n```objc\n+ (NSSet *)keyPathsForValuesAffecting<Key>\n```\n\nIn our concrete case, that'll look like so:\n\n```objc\n+ (NSSet *)keyPathsForValuesAffectingRedComponent\n{\n    return [NSSet setWithObject:@\"lComponent\"];\n}\n\n+ (NSSet *)keyPathsForValuesAffectingGreenComponent\n{\n    return [NSSet setWithObjects:@\"lComponent\", @\"aComponent\", nil];\n}\n\n+ (NSSet *)keyPathsForValuesAffectingBlueComponent\n{\n    return [NSSet setWithObjects:@\"lComponent\", @\"bComponent\", nil];\n}\n\n+ (NSSet *)keyPathsForValuesAffectingColor\n{\n    return [NSSet setWithObjects:@\"redComponent\", @\"greenComponent\", @\"blueComponent\", nil];\n}\n```\n\nWe have now fully expressed the dependencies. Note that we're able to do chaining of these dependencies. For example, this would allow us to safely subclass and override the `redComponent` method and the dependency would continue to work.\n\n### Observing Changes\n\nLet's turn toward the view controller. The `NSViewController` subclass owns the model object `LabColor` as a property:\n\n```objc\n@interface ViewController ()\n\n@property (nonatomic, strong) LabColor *labColor;\n\n@end\n```\n\nWe want to register the view controller to receive key-value observation notifications. The method on `NSObject` to do this is:\n\n```objc\n- (void)addObserver:(NSObject *)anObserver\n         forKeyPath:(NSString *)keyPath\n            options:(NSKeyValueObservingOptions)options\n            context:(void *)context\n```\n\nThis will cause:\n\n```objc\n- (void)observeValueForKeyPath:(NSString *)keyPath\n                      ofObject:(id)object\n                        change:(NSDictionary *)change\n                       context:(void *)context\n```\n\nto get called on `anObserver` whenever the value of `keyPath` changes. This API can seem a bit daunting. To make things worse, we have to remember to call:\n\n```objc\n- (void)removeObserver:(NSObject *)anObserver\n            forKeyPath:(NSString *)keyPath\n```\n\nto remove the observer, otherwise our app will crash in strange ways.\n\nFor most intents and purposes, *key-value observing* can be done in a much simpler and more elegant way by using a helper class. We'll add a so-called *observation token* property to our view controller:\n\n```objc\n@property (nonatomic, strong) id colorObserveToken;\n```\n\nand when the `labColor` gets set on the view controller, we'll simply observe changes to its `color` by overriding the setter for `labColor`, like so:\n\n```objc\n- (void)setLabColor:(LabColor *)labColor\n{\n    _labColor = labColor;\n    self.colorObserveToken = [KeyValueObserver observeObject:labColor\n                                                     keyPath:@\"color\"\n                                                      target:self\n                                                    selector:@selector(colorDidChange:)\n                                                     options:NSKeyValueObservingOptionInitial];\n}\n\n- (void)colorDidChange:(NSDictionary *)change;\n{\n    self.colorView.backgroundColor = self.labColor.color;\n}\n```\n\nThe [`KeyValueObserver` helper class](https://github.com/objcio/issue-7-lab-color-space-explorer/blob/master/Lab%20Color%20Space%20Explorer/KeyValueObserver.m) simply wraps the calls to `-addObserver:forKeyPath:options:context:`, `-observeValueForKeyPath:ofObject:change:context:` and `-removeObserverForKeyPath:` and keeps the view controller code free of clutter.\n\n\n### Tying it Together\n\nThe view controller finally needs to react to changes of the *L*, *a*, and *b* sliders:\n\n```objc\n- (IBAction)updateLComponent:(UISlider *)sender;\n{\n    self.labColor.lComponent = sender.value;\n}\n\n- (IBAction)updateAComponent:(UISlider *)sender;\n{\n    self.labColor.aComponent = sender.value;\n}\n\n- (IBAction)updateBComponent:(UISlider *)sender;\n{\n    self.labColor.bComponent = sender.value;\n}\n```\n\nThe entire code is available as a [sample project](https://github.com/objcio/issue-7-lab-color-space-explorer) on our GitHub repository.\n\n\n## Manual vs. Automatic Notification\n\nWhat we did above may seem a bit like magic, but what happens is that calling `-setLComponent:` etc. on a `LabColor` instance will automatically cause:\n\n```objc\n- (void)willChangeValueForKey:(NSString *)key\n```\n\nand:\n\n```objc\n- (void)didChangeValueForKey:(NSString *)key\n```\n\nto get called prior to or after running the code inside the `-setLComponent:` method. This happens both if we implement `-setLComponent:` and if we (as in our case) choose to auto-synthesize the accessors for `lComponent`.\n\nThere are cases when we want or need to override `-setLComponent:` and control whether change notifications are sent out, like so:\n\n```objc\n+ (BOOL)automaticallyNotifiesObserversForLComponent;\n{\n    return NO;\n}\n\n- (void)setLComponent:(double)lComponent;\n{\n    if (_lComponent == lComponent) {\n        return;\n    }\n    [self willChangeValueForKey:@\"lComponent\"];\n    _lComponent = lComponent;\n    [self didChangeValueForKey:@\"lComponent\"];\n}\n```\n\nWe disable automatic invocation of `-willChangeValueForKey:` and `-didChangeValueForKey:`, and then call it ourselves. We should only call `-willChangeValueForKey:` and `-didChangeValueForKey:` inside the setter if we've disabled automatic invocation. And in most cases, this optimization doesn't buy us much.\n\nIf we modify the instance variables (e.g. `_lComponent`) outside the accessor, we need to be careful to similarly wrap those changes in `-willChangeValueForKey:` and `-didChangeValueForKey:`. But in most cases, the code stays simpler if we make sure to always use the accessors.\n\n## Key-Value Observing and the Context\n\nThere may be reasons why we don't want to use the `KeyValueObserver` helper class. There's a slight overhead of creating another object. If we're observing a lot of keys, that might be noticeable, however unlikely that is.\n\nIf we're implementing a class that registers itself as an observer with:\n\n```objc\n- (void)addObserver:(NSObject *)anObserver\n         forKeyPath:(NSString *)keyPath\n            options:(NSKeyValueObservingOptions)options\n            context:(void *)context\n```\n\nit is *very* important that we pass a `context` that's unique to this class. We recommend putting:\n\n```objc\nstatic int const PrivateKVOContext;\n```\n\nat the top of the class' `.m` file and then calling the API with a pointer to this `PrivateKVOContext` as the context, like so:\n\n```objc\n[otherObject addObserver:self forKeyPath:@\"someKey\" options:someOptions context:&PrivateKVOContext];\n```\n\nand then implement the `-observeValueForKeyPath:...` method like so\n\n```objc\n- (void)observeValueForKeyPath:(NSString *)keyPath\n                      ofObject:(id)object\n                        change:(NSDictionary *)change\n                       context:(void *)context\n{\n    if (context == &PrivateKVOContext) {\n        // Observe values here\n    } else {\n        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];\n    }\n}\n```\n\nThis ensures that subclassing works. With this pattern, both superclasses and subclasses can safely observe the same keys on the same objects without clashing. Otherwise, we'll end up running into odd behavior that's very difficult to debug.\n\n## Advanced Key-Value Observing\n\nWe often want to update some UI when a value changes, but we also need to initially run the code to update the UI once. We can use KVO to do both by specifying the `NSKeyValueObservingOptionInitial`. That will cause the KVO notification to trigger during the call to `-addObserver:forKeyPath:...`.\n\n### Before and After the Fact\n\nWhen we register for KVO, we can also specify `NSKeyValueObservingOptionPrior`. This allows us to be notified before the value is changed. This directly corresponds to the point in time when `-willChangeValueForKey:` gets called.\n\nIf we register with `NSKeyValueObservingOptionPrior` we will receive two notifications: one before the change and one after the change. The first one will have another key in the `change` dictionary, and we can test if it's the notification prior to the change or the one after, like so:\n\n```objc\nif ([change[NSKeyValueChangeNotificationIsPriorKey] boolValue]) {\n    // Before the change\n} else {\n    // After the change\n}\n```\n\n### Values\n\nIf we need either the old or the new value (or both) of the key, we can ask KVO to pass those as part of the notification by specifying `NSKeyValueObservingOptionNew` and/or `NSKeyValueObservingOptionOld`.\n\nThis is often easier and better that using `NSKeyValueObservingOptionPrior`. We would extract the old and new values with:\n\n```objc\nid oldValue = change[NSKeyValueChangeOldKey];\nid newValue = change[NSKeyValueChangeNewKey];\n```\n\nKVO basically stores the values for the corresponding key at the point in time where `-willChangeValueForKey:` and `-didChangeValueForKey:` were called, respectively.\n\n### Indexes\n\nKVO also has very powerful support for notifying about changes to collections. These are returned for collection proxy objects returned by:\n\n```objc\n-mutableArrayValueForKey:\n-mutableSetValueForKey:\n-mutableOrderedSetValueForKey:\n```\n\nWe'll describe how these work further below. If you use these, the change dictionary will contain information about the change kind (insertion, removal, or replacement) and for ordered relations, the change dictionary also contains information about the affected indexes.\n\nThe combination of collection proxy objects and these detailed change notifications can be used to efficiently update the UI when presenting large collections, but they require quite a bit of work.\n\n## Key-Value Observing and Threading\n\nIt is important to note that KVO happens synchronously and on the same thread as the actual change. There's no queuing or run-loop magic going on. The manual or automatic call to `-didChange...` will trigger the KVO notification to be sent out.\n\nHence, we must be very careful about not making changes to properties from another thread unless we can be sure that everybody observing that key can handle the change notification in a thread-safe manner. Generally speaking, we cannot recommend mixing KVO and multithreading. If we are using multiple queues or threads, we should not be using KVO between queues or threads.\n\nThe fact that KVO happens synchronously is very powerful. As long as we're running on a single thread (e.g. the main queue) KVO ensures two important things.\n\nFirst, if we call a KVO compliant setter, like:\n\n```objc\nself.exchangeRate = 2.345;\n```\n\nwe are guaranteed that all observers of `exchangeRate` have been notified by the time the setter returns.\n\nSecond, if the key path is observed with `NSKeyValueObservingOptionPrior`, someone accessing the `exchangeRate` property will stay the same until the `-observe...` method is called.\n\n## Key-Value Coding\n\nKey-value coding in its simplest form allows us to access a property like:\n\n```objc\n@property (nonatomic, copy) NSString *name;\n```\n\nthrough:\n\n```objc\nNSString *n = [object valueForKey:@\"name\"]\n```\n\nand:\n\n```objc\n[object setValue:@\"Daniel\" forKey:@\"name\"]\n```\n\nNote that this works for properties with object values, as well as scalar types (e.g. `int` and `CGFloat`) and structs (e.g. `CGRect`). Foundation will automatically do the wrapping and unwrapping for us. For example, if the property is:\n\n```objc\n@property (nonatomic) CGFloat height;\n```\n\nwe can set it with:\n\n```objc\n[object setValue:@(20) forKey:@\"height\"]\n```\n\nKey-value coding allows us to access properties using strings to identify properties. These strings are called *keys*. In certain situations, this will give us a lot of flexibility which we can use to simplify our code. We'll look at an example in the next section, *Simplifying Form-Like User Interfaces*.\n\nBut there's more to key-value coding. Collections (`NSArray`, `NSSet`, etc.) have powerful collection operators which can be used with key-value coding. And finally, an object can support key-value coding for keys that are not normal properties e.g. through proxy objects.\n\n\n### Simplifying Form-Like User Interfaces\n\nLet's say we have an object:\n\n```objc\n@interface Contact : NSObject\n\n@property (nonatomic, copy) NSString *name;\n@property (nonatomic, copy) NSString *nickname;\n@property (nonatomic, copy) NSString *email;\n@property (nonatomic, copy) NSString *city;\n\n@end\n```\n\nand a detail view controller that has four corresponding `UITextField` properties:\n\n```objc\n@interface DetailViewController ()\n\n@property (weak, nonatomic) IBOutlet UITextField *nameField;\n@property (weak, nonatomic) IBOutlet UITextField *nicknameField;\n@property (weak, nonatomic) IBOutlet UITextField *emailField;\n@property (weak, nonatomic) IBOutlet UITextField *cityField;\n\n@end\n```\n\nWe can now simplify the update logic. First, we need two methods that return us all model keys of interest to use and that map those keys to the keys of their corresponding text field respectively:\n\n```objc\n- (NSArray *)contactStringKeys;\n{\n    return @[@\"name\", @\"nickname\", @\"email\", @\"city\"];\n}\n\n- (UITextField *)textFieldForModelKey:(NSString *)key;\n{\n    return [self valueForKey:[key stringByAppendingString:@\"Field\"]];\n}\n```\n\nWith this, we can update the text field from the model, like so:\n\n```objc\n- (void)updateTextFields;\n{\n    for (NSString *key in self.contactStringKeys) {\n        [self textFieldForModelKey:key].text = [self.contact valueForKey:key];\n    }\n}\n```\n\nWe can also use a single-action method for all four text fields to update the model:\n\n```objc\n- (IBAction)fieldEditingDidEnd:(UITextField *)sender\n{\n    for (NSString *key in self.contactStringKeys) {\n        UITextField *field = [self textFieldForModelKey:key];\n        if (field == sender) {\n            [self.contact setValue:sender.text forKey:key];\n            break;\n        }\n    }\n}\n```\n\nNote: We will add validation to this later, as pointed out at [*Key-Value Validation*](#key-value-validation).\n\nFinally, we need to make sure the text fields get updated when needed:\n\n```objc\n- (void)viewWillAppear:(BOOL)animated;\n{\n    [super viewWillAppear:animated];\n    [self updateTextFields];\n}\n\n- (void)setContact:(Contact *)contact\n{\n    _contact = contact;\n    [self updateTextFields];\n}\n```\n\nAnd with this, our [detail view controller](https://github.com/objcio/issue-7-contact-editor/blob/master/Contact%20Editor/DetailViewController.m) is working.\n\nCheck out the entire project on our [GitHub repository](https://github.com/objcio/issue-7-contact-editor). It also uses [*Key-Value Validation*](#key-value-validation) as discussed further below.\n\n\n### Key Paths\n\nKey-value coding also allows you to go through relations, e.g. if `person` is an object that has a property called `address`, and `address` in turn has a property called `city`, we can retrieve that through:\n\n```objc\n[person valueForKeyPath:@\"address.city\"]\n```\n\nNote that we’re calling `-valueForKeyPath:` instead of `-valueForKey:` in this case.\n\n### Key-Value Coding Without `@property`\n\nWe can implement a key-value coding-compliant attribute without `@property` and `@synthesize` / auto-synthesize. The most straightforward example would be to simply implement the `-<key>` and `-set<Key>:` methods. For example, if we want to support setting `name`, we would implement:\n\n```objc\n- (NSString *)name;\n- (void)setName:(NSString *)name;\n```\n\nThis is straightforward, and identical to how `@property` works.\n\nOne thing to be aware of, though, is how `nil` is handled for scalar and struct values. Let's say we want to support key-value coding for `height` by implementing:\n\n```objc\n- (CGFloat)height;\n- (void)setHeight:(CGFloat)height;\n```\n\nWhen we call:\n\n```objc\n[object setValue:nil forKey:@\"height\"]\n```\n\nthis would throw an exception. In order to be able to handle `nil` values, we need to make sure to override `-setNilValueForKey:`, like so:\n\n```objc\n- (void)setNilValueForKey:(NSString *)key\n{\n    if ([key isEqualToString:@\"height\"]) {\n        [self setValue:@0 forKey:key];\n    } else\n        [super setNilValueForKey:key];\n}\n```\n\nWe can make a class support key-value coding by overriding:\n\n```objc\n- (id)valueForUndefinedKey:(NSString *)key;\n- (void)setValue:(id)value forUndefinedKey:(NSString *)key;\n```\n\nThis may seem odd, but it allows a class to dynamically support certain keys. Using these two methods comes with a performance hit, though.\n\nAs a side note, it is worth mentioning that Foundation supports accessing instance variables directly. Use that feature sparingly. Check the documentation for `+accessInstanceVariablesDirectly`. It defaults to `YES`, which causes Foundation to look for an instance variable called `_<key>`, `_is<Key>`, `<key>`, or `is<Key>`, in that order.\n\n\n\n### Collection Operators\n\nAn oft-overlooked feature of key-value coding is its support for collection operators. For example, we can get the maximum value from an array with:\n\n```objc\nNSArray *a = @[@4, @84, @2];\nNSLog(@\"max = %@\", [a valueForKeyPath:@\"@max.self\"]);\n```\n\nor, if we have an array of `Transaction` objects that have an `amount` property, we can get the maximum `amount` with:\n\n```objc\nNSArray *a = @[transaction1, transaction2, transaction3];\nNSLog(@\"max = %@\", [a valueForKeyPath:@\"@max.amount\"]);\n```\n\nWhen we call `[a valueForKeyPath:@\"@max.amount\"]`, this will call `-valueForKey:@\"amount\"` on each element in the array `a` and then return the maximum of those.\n\nApple's documentation for key-value coding has a section called [Collection Operators](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/KeyValueCoding/Articles/CollectionOperators.html) that describes this in detail.\n\n\n\n### Key-Value Coding Through Collection Proxy Objects\n\nWe can expose collections (`NSArray`, `NSSet`, etc.) in the same way as normal objects. But key-value coding also allows us to implement a key-value coding-compliant collection though proxy objects. This is an advanced technique. We'll rarely find use for it, but it's a powerful trick to have in the tool chest.\n\nWhen we call `-valueForKey:` on an object, that object can return collection proxy objects for an `NSArray`, an `NSSet`, or an `NSOrderedSet`. The class doesn't implement the normal `-<Key>` method but instead implements a number of methods that the proxy uses.\n\nIf we want the class to be able to support returning an `NSArray` through a proxy object for the key `contacts`, we could implement:\n\n```objc\n- (NSUInteger)countOfContacts;\n- (id)objectInContactsAtIndex:(NSUInteger)idx;\n```\n\nDoing so, when we call `[object valueForKey:@\"contacts”]`, this will return an `NSArray` that proxies all calls to those two methods. But the array will support *all* methods on `NSArray`. The proxying is transparent to the caller. In other words, the caller doesn't know if we return a normal `NSArray` or a proxy.\n\nWe can do the same for an `NSSet` and `NSOrderedSet`. The methods that we have to implement are:\n\n<table><thead><tr><th style=\"text-align:left;padding-right:1em;\">NSArray</th><th style=\"text-align:left;padding-right:1em;\">NSSet&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th><th style=\"text-align:left;padding-right:1em;\">NSOrderedSet&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th></tr></thead><tbody><tr><td style=\"text-align:left;padding-right:1em;\"><code>-countOf&lt;Key&gt;</code></td><td style=\"text-align:left;padding-right:1em;\"><code>-countOf&lt;Key&gt;</code></td><td style=\"text-align:left;padding-right:1em;\"><code>-countOf&lt;Key&gt;</code></td>\n</tr><tr><td style=\"text-align:left;padding-right:1em;\"></td><td style=\"text-align:left;padding-right:1em;\"><code>-enumeratorOf&lt;Key&gt;</code></td><td style=\"text-align:left;padding-right:1em;\"><code>-indexIn&lt;Key&gt;OfObject:</code></td>\n</tr><tr><td style=\"text-align:left;padding-right:1em;\">One of</td><td style=\"text-align:left;padding-right:1em;\"><code>-memberOf&lt;Key&gt;:</code></td><td style=\"text-align:left;padding-right:1em;\">\n</td></tr><tr><td style=\"text-align:left;padding-right:1em;\"><code>-objectIn&lt;Key&gt;AtIndex:</code></td><td style=\"text-align:left;padding-right:1em;\"></td><td style=\"text-align:left;padding-right:1em;\">One of</td>\n</tr><tr><td style=\"text-align:left;padding-right:1em;\"><code>-&lt;key&gt;AtIndexes:</code></td><td style=\"text-align:left;padding-right:1em;\"></td><td style=\"text-align:left;padding-right:1em;\"><code>-objectIn&lt;Key&gt;AtIndex:</code></td>\n</tr><tr><td style=\"text-align:left;padding-right:1em;\"></td><td style=\"text-align:left;padding-right:1em;\"></td><td style=\"text-align:left;padding-right:1em;\"><code>-&lt;key&gt;AtIndexes:</code></td>\n</tr><tr><td style=\"text-align:left;padding-right:1em;\">Optional (performance)</td><td style=\"text-align:left;padding-right:1em;\"></td><td style=\"text-align:left;padding-right:1em;\">\n</td></tr><tr><td style=\"text-align:left;padding-right:1em;\"><code>-get&lt;Key&gt;:range:</code></td><td style=\"text-align:left;padding-right:1em;\"></td><td style=\"text-align:left;padding-right:1em;\">Optional (performance)</td>\n</tr><tr><td style=\"text-align:left;padding-right:1em;\"></td><td style=\"text-align:left;padding-right:1em;\"></td><td style=\"text-align: left;\"><code>-get&lt;Key&gt;:range:</code></td>\n</tr></tbody></table>\n\n\nThe *optional* methods can improve performance of the proxy object.\n\nUsing these proxy objects only makes sense in special situations, but in those cases it can be very helpful. Imagine that we have a very large existing data structure and the caller doesn't need to access all elements (at once).\n\nAs a (perhaps contrived) example, we could write a class that contains a huge list of primes, like so:\n\n```objc\n@interface Primes : NSObject\n\n@property (readonly, nonatomic, strong) NSArray *primes;\n\n@end\n\n\n\n@implementation Primes\n\nstatic int32_t const primes[] = {\n    2, 101, 233, 383, 3, 103, 239, 389, 5, 107, 241, 397, 7, 109,\n    251, 401, 11, 113, 257, 409, 13, 127, 263, 419, 17, 131, 269,\n    421, 19, 137, 271, 431, 23, 139, 277, 433, 29, 149, 281, 439,\n    31, 151, 283, 443, 37, 157, 293, 449, 41, 163, 307, 457, 43,\n    167, 311, 461, 47, 173, 313, 463, 53, 179, 317, 467, 59, 181,\n    331, 479, 61, 191, 337, 487, 67, 193, 347, 491, 71, 197, 349,\n    499, 73, 199, 353, 503, 79, 211, 359, 509, 83, 223, 367, 521,\n    89, 227, 373, 523, 97, 229, 379, 541, 547, 701, 877, 1049,\n    557, 709, 881, 1051, 563, 719, 883, 1061, 569, 727, 887,\n    1063, 571, 733, 907, 1069, 577, 739, 911, 1087, 587, 743,\n    919, 1091, 593, 751, 929, 1093, 599, 757, 937, 1097, 601,\n    761, 941, 1103, 607, 769, 947, 1109, 613, 773, 953, 1117,\n    617, 787, 967, 1123, 619, 797, 971, 1129, 631, 809, 977,\n    1151, 641, 811, 983, 1153, 643, 821, 991, 1163, 647, 823,\n    997, 1171, 653, 827, 1009, 1181, 659, 829, 1013, 1187, 661,\n    839, 1019, 1193, 673, 853, 1021, 1201, 677, 857, 1031,\n    1213, 683, 859, 1033, 1217, 691, 863, 1039, 1223, 1229,\n};\n\n- (NSArray *)primes;\n{\n    return [self valueForKey:@\"backingPrimes\"];\n}\n\n- (NSUInteger)countOfBackingPrimes;\n{\n    return (sizeof(primes) / sizeof(*primes));\n}\n\n- (id)objectInBackingPrimesAtIndex:(NSUInteger)idx;\n{\n    NSParameterAssert(idx < sizeof(primes) / sizeof(*primes));\n    return @(primes[idx]);\n}\n\n@end\n```\n\nWe would be able to run:\n\n```objc\nPrimes *primes = [[Primes alloc] init];\nNSLog(@\"The last prime is %@\", [primes.primes lastObject]);\n```\n\nThis would call `-countOfPrimes` once and then `-objectInPrimesAtIndex:` once with `idx` set to the last index. It would *not* have to wrap all the integers into an `NSNumber` first and then wrap all those into an `NSArray`, only to then extract the last object.\n\nThe [*Contacts Editor* sample app](https://github.com/objcio/issue-7-contact-editor) uses the same method to wrap a C++ `std::vector` -- in a contrived example. But it illustrates how this method can be used.\n\n\n#### Mutable Collections\n\nWe can even use collection proxies for mutable collections, i.e. `NSMutableArray`, `NSMutableSet`, and `NSMutableOrderedSet`.\n\nAccessing such a mutable collection works slightly differently. The caller now has to call one of these methods: \n\n```objc\n- (NSMutableArray *)mutableArrayValueForKey:(NSString *)key;\n- (NSMutableSet *)mutableSetValueForKey:(NSString *)key;\n- (NSMutableOrderedSet *)mutableOrderedSetValueForKey:(NSString *)key;\n```\n\nAs a trick, we can have the class return a mutable collection proxy through:\n\n```objc\n- (NSMutableArray *)mutableContacts;\n{\n    return [self mutableArrayValueForKey:@\"wrappedContacts\"];\n}\n```\n\nand then implement the correct methods for the key `wrappedContacts`.\n\n\nWe would have to implement both the method listed above for the immutable collection, as well as these:\n\n<table><thead><tr><th style=\"text-align:left;padding-right:1em;\">NSMutableArray&nbsp;/&nbsp;NSMutableOrderedSet&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th><th style=\"text-align:left;padding-right:1em;\">NSMutableSet&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th></tr></thead><tbody><tr><td style=\"text-align: left;\">At least 1 insertion and 1 removal method</td><td style=\"text-align: left;padding-right:1em;\">At least 1 addition and 1 removal method</td>\n</tr><tr><td style=\"text-align: left;padding-right:1em;\"><code>-insertObject:in&lt;Key&gt;AtIndex:</code></td><td style=\"text-align: left;padding-right:1em;\"><code>-add&lt;Key&gt;Object:</code></td>\n</tr><tr><td style=\"text-align: left;padding-right:1em;\"><code>-removeObjectFrom&lt;Key&gt;AtIndex:</code></td><td style=\"text-align: left;padding-right:1em;\"><code>-remove&lt;Key&gt;Object:</code></td>\n</tr><tr><td style=\"text-align: left;padding-right:1em;\"><code>-insert&lt;Key&gt;:atIndexes:</code></td><td style=\"text-align: left;padding-right:1em;\"><code>-add&lt;Key&gt;:</code></td>\n</tr><tr><td style=\"text-align: left;padding-right:1em;\"><code>-remove&lt;Key&gt;AtIndexes:</code></td><td style=\"text-align: left;padding-right:1em;\"><code>-remove&lt;Key&gt;:</code></td>\n</tr><tr><td style=\"text-align: left;padding-right:1em;\"></td><td style=\"text-align: left;padding-right:1em;\">\n</td></tr><tr><td style=\"text-align: left;padding-right:1em;\">Optional (performance) one of</td><td style=\"text-align: left;padding-right:1em;\">Optional (performance)</td>\n</tr><tr><td style=\"text-align: left;padding-right:1em;\"><code>-replaceObjectIn&lt;Key&gt;AtIndex:withObject:</code></td><td style=\"text-align: left;padding-right:1em;\"><code>-intersect&lt;Key&gt;:</code></td>\n</tr><tr><td style=\"text-align: left;padding-right:1em;\"><code>-replace&lt;Key&gt;AtIndexes:with&lt;Key&gt;:</code></td><td style=\"text-align: left;padding-right:1em;\"><code>-set&lt;Key&gt;:</code></td>\n</tr></tbody></table>\n\n\nAs noted above, these mutable collection proxy objects are also very powerful in combination with key-value observing. The KVO mechanism will put detailed change information into the change dictionary when these collections are mutated.\n\nThere are batch-change methods (taking multiple objects) and ones that only take a single object. We recommend picking the one that's the easiest to implement for the given task -- with a slight favor for the batch update ones.\n\nIf we implement these methods, we need to be careful about automatic versus manual KVO compliance. By default, Foundation assumes automatic notifications and will send out fine-grained change notifications. If we choose to implement the fine-grained notifications ourselves through:\n\n```objc\n-willChange:valuesAtIndexes:forKey:\n-didChange:valuesAtIndexes:forKey:\n```\n\nor:\n\n```objc\n-willChangeValueForKey:withSetMutation:usingObjects:\n-didChangeValueForKey:withSetMutation:usingObjects:\n```\n\nwe need to make sure to turn automatic notifications off, otherwise KVO will send out two notifications for every change.\n\n### Common Key-Value Observing Mistakes\n\nFirst and foremost, KVO compliance is part of an API. If the owner of a class doesn't promise that the property is KVO compliant, we cannot make any assumption about KVO to work. Apple does document which properties are KVO compliant. For example, the `NSProgress` class lists most of its properties to be KVO compliant.\n\nSometimes people try to trigger KVO by putting `-willChange` and `-didChange` pairs with nothing in between *after* a change was made. This will cause a KVO notification to be posted, but it breaks observers relying on the `NSKeyValueObservingOld` option. Namely this affects KVO's own support for observing key paths. KVO relies on the `NSKeyValueObservingOld` property to support observing key paths.\n\nWe would also like to point out that collections as such are not observable. KVO is about observing *relationships* rather than collections. We cannot observe an `NSArray`; we can only observe a property on an object -- and that property may be an `NSArray`. As an example, if we have a `ContactList` object, we can observe its `contacts` property, but we cannot pass an `NSArray` to `-addObserver:forKeyPath:...` as the object to be observed.\n\nLikewise, observing `self` doesn't always work. It's probably not a good design pattern, either.\n\n\n### Debugging Key-Value Observing\n\nInside `lldb` you can dump the observation info of an observed object, like so:\n\n```objc\n(lldb) po [observedObject observationInfo]\n```\n\nThis prints lots of information about who's observing what.\n\nThe format is private and we mustn't rely on anything about it -- Apple is free to change it at any point in time. But it's a very powerful debugging tool.\n\n\n<a name=\"key-value-validation\"> </a>\n\n## Key-Value Validation\n\nOn a final note, key-value validation is also part of the key-value coding API. It's a consistent API for validation property values, but it hardly provides any logic or functionality on its own.\n\nBut if we're writing model classes that can validate values, we should implement the API the way set forward by key-value validation to make sure it's consistent. Key-value validation is the Cocoa convention for validating values in model classes.\n\nLet us stress this again: key-value coding will not do any validation, and it will not call the key-value validation methods. Your controller will have to do that. Implementing your validation methods according to key-value validation will make sure they're consistent, though.\n\nA simple example would be:\n\n```objc\n- (IBAction)nameFieldEditingDidEnd:(UITextField *)sender;\n{\n    NSString *name = [sender text];\n    NSError *error = nil;\n    if ([self.contact validateName:&name error:&error]) {\n        self.contact.name = name;\n    } else {\n        // Present the error to the user\n    }\n    sender.text = self.contact.name;\n}\n```\n\nThe powerful thing is that we're asking the model class (`Contact` in this case) to validate the `name`, and at the same time we're giving the model class an opportunity to sanitize the name.\n\nIf we want to make sure the name doesn't have any leading white space, that's logic which should live inside the model object. The `Contact` class would implement the key-value validation method for the `name` property like this:\n\n```objc\n- (BOOL)validateName:(NSString **)nameP error:(NSError * __autoreleasing *)error\n{\n    if (*nameP == nil) {\n        *nameP = @\"\";\n        return YES;\n    } else {\n        *nameP = [*nameP stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];\n        return YES;\n    }\n}\n```\n\nThe [*Contact Editor* sample](https://github.com/objcio/issue-7-contact-editor) illustrates this in the `DetailViewController` and `Contact` class.\n\n"
  },
  {
    "path": "2013-12-09-linguistic-tagging.md",
    "content": "---\ntitle:  \"Linguistic Tagging\"\ncategory: \"7\"\ndate: \"2013-12-09 06:00:00\"\ntags: article\nauthor:\n  - name: Oliver Mason\n    url: https://twitter.com/ojmason\n---\n\nOne of the main challenges when handling natural language (as opposed to programming languages) is ambiguity. While programming languages are designed to have one and only one possible interpretation, human languages derive a lot of their power from being vague and unspecific, because sometimes you don't want to tell someone exactly how you feel about something. This is perfectly fine for social interaction, but it's a real pain when trying to process human language with a computer.\n\nA straightforward example of this is the use of tokens. A tokeniser for a programming language has unambiguous rules about what constitutes a token, and what type it is (statement separator, identifier, reserved keyword, etc.). In languages, this is far from obvious. Is _can't_ one token or two? And, depending on your answer, what about _cannot_ or _can not_, which presumably should be the same? Various compound words can be written as one word (_bookshelf_), two words (_lawn mower_), or separated by a hyphen (_life-cycle_). Certain characters (such as the hyphen or the apostrophe) can have various interpretations, and choosing the correct one often depends on context (‘is this apostrophe at the end of the word a possessive marker or a closing single quote from the beginning of the sentence?’).\n\nSentences are equally bad: Simply assuming a full stop terminates a sentence fails miserably when we use abbreviations or ordinal numbers. Often it is possible to find a solution, but some sentences are genuinely impossible to identify unless we do a full-scale analysis of a paragraph. This is the stuff that we humans don't even consciously think about.\n\nBut we want to be able to handle human language, as it is a more user-friendly way to interact with our software.Instead of tapping or clicking on small buttons (or typing on a tiny virtual keyboard) we would want to just tell the computer what to do, or get the computer to analyze newspaper articles for us, and give us a brief summary of\nthe issues we are interested in. Some of this is still out of our reach (at least until Apple provides us with an API to interact with Siri), but some things are already possible now. Enter `NSLinguisticTagger`.\n\n`NSLinguisticTagger` is one of the worst-named classes in Foundation, as it is far more than a mere part-of-speech tagger. It is a tokeniser, a sentence splitter, a named-entity recognizer, a lemmatizer, and a part-of-speech tagger. In other words, it is almost all you need to do some serious computational linguistics processing.\n\nTo illustrate the use of the `NSLinguisticTagger` class, we'll develop a quick tool for finding stuff: we have a directory full of texts (such as news articles, or emails, or whatever else), and we'll be able to type in a word which will return to us all the sentences that contain the word. We will ignore function words (such as _the_, _of_, or _and_), as they are too common to be useful in this context. What we will actually implement for now is the first step only: extracting the relevant words from a single file. But this can easily be extended to provide the full functionality.\n\nThe source code is on [GitHub](https://github.com/objcio/issue-7-linguistic-tagging), and a sample text is also included. This is a Guardian article on a trade deal between the UK and China. When running the text through the software, you will notice that it does not always work perfectly, but that is to be expected: human language, unlike any formal language, is messy and does not easily fit into clean systems of rules. A lot of theoretical issues (even as basic as parts of speech) are somewhat unsolved, as we still know very little about how languages can best be described. The word classes, for example, are based on Latin, but that does not mean they are necessarily appropriate for English. At best, they are a rough approximation. But for many practical purposes, it's kind\nof good enough to not have to worry about it too much.\n\n## Tag Schemes\n\nThe central approach to annotating or labeling text is that of the tag scheme. There are a number of available tag schemes:\n\n* `NSLinguisticTagSchemeTokenType`\n* `NSLinguisticTagSchemeLexicalClass`\n* `NSLinguisticTagSchemeNameType`\n* `NSLinguisticTagSchemeNameTypeOrLexicalClass`\n* `NSLinguisticTagSchemeLemma`\n* `NSLinguisticTagSchemeLanguage`\n* `NSLinguisticTagSchemeScript`\n\n\n\nAn `NSLinguisticTagger` instance iterates over all items in a text and calls a block with the requested tag-scheme values. The most basic one is the token type: word, punctuation, white space, or 'other'. We can use this to identify which items are actual words, and for our application, we simply discard anything that is not a word. Lexical class refers to part of speech. This is a fairly basic set of labels (which would not be fine-grained enough for a proper linguistic analysis) which we can use to distinguish between the content words we want (nouns, verbs, adjectives, and adverbs) and the function words we want to ignore (conjunctions, prepositions, determiners, etc.). A full set of possible\nvalues is available from the [`NSLinguisticTagger` class documentation](https://developer.apple.com/library/mac/documentation/cocoa/reference/NSLinguisticTagger_Class/Reference/Reference.html).\n\nName type refers to named entity recognition; we can see whether something refers to a person, a place, or an organization. Again, this is quite basic compared to what is used in natural language processing, but it can be very useful, if, for example, you want to search for references to particular people or locations. A potential use case\nfor this could be \"give me a list of all the politicians which are mentioned in this text,” where you scan the text for the names of persons, which you then look up in a database (such as Wikipedia) to check whether they are in fact politicians or not. This can also be combined with lexical class, as it often implies a class of 'name'.\n\nA lemma is the canonical form of a word, or its base form. This is not that much of an issue for English, but much more important for other languages. It is basically the form you would look up in a dictionary. For example, the word _tables_ is a plural noun, and its lemma is _table_, the singular. Similarly, the verb _running_ is transformed into the infinitive, _run_. This can be very useful if you want to treat variant forms in the same way, and that is actually what we will be doing for our sample application (as it helps keep the index size down).\n\nLanguage concerns the language we are dealing with. If you're on iOS, then you are currently (as of iOS 7) limited to English only. On OS X (as of 10.9/Mavericks) you have a slightly larger list available; the method `+[NSLinguisticTagger availableTagSchemesForLanguage:]` lists all schemes available for a given language. The likely reason for limiting the number on iOS is that the resource files take up a lot of space, which is fine on a laptop or desktop machine, but not so good on a phone or tablet.\n\nScript is the writing system, such as Latin, Cyrillic, etc. For English, we'll use 'Latin.’ If you know what language you will be dealing with, setting it using the `setOrthography` method will improve tagging results, especially for relatively short segments.\n\n## Tag Options\n\nNow that we have identified what the `NSLinguisticTagger` can recognize for us, we have to tell it what we want and how we want it. There are a number of options which define the tagger's behavior. These are all of type `NSUInteger` and can be combined with a bitwise OR.\n\nThe first one is 'omit words,’ which seems somewhat pointless, unless you want to only look at punctuation or other non-words. More useful are the next three, 'omit punctuation,’ 'omit whitespace,’ and 'omit other.’ Unless you want\nto do a full-scale linguistic analysis of the text, you will mainly be interested in the words, and not so much in all the commas and full-stops in between them. With these options, you can simply tell the tagger to suppress them and you will not have to worry about them any more. The final option, 'join names,’ reflects that names can sometimes\nbe more than one token. With this option chosen, they will be combined so you can treat them as a single unit. You might not always want to do this, but it can be useful. In the sample text, for example, the string \"Owen Patterson\" is recognized as a name, and is returned as a single unit.\n\n## Processing Architecture\n\nOur program will index a number of texts held in separate files (which we assume are encoded in UTF-8). We will have a `FileProcessor` class which handles a single file, chopping it up into words and passing those on to another class that does something with them. That latter class will implement the `WordReceiver` protocol, which contains a single method: \n\n```objc\n-(void)receiveWord:(NSDictionary*)word\n```\n\nWe represent the word not as an `NSString`, but as a dictionary, as it will have several attributes attached to it: the actual token, its part of speech or name type, its lemma, the number of the sentence it is in, and the position within that sentence. We also want to store the filename itself for indexing purposes. The `FileProcessor` is called\nwith:\n\n```objc\n- (BOOL)processFile:(NSString*)filename\n```\n\nwhich triggers the analysis and returns `YES` if all went well, and `NO` in case of an error. It first creates an `NSString` from the file, and then passes it to an instance of `NSLinguisticTagger` for processing.\n\nThe main `NSLinguisticTagger` method iterates over a range within an `NSString` and calls a block for every element that has been found. In order to simplify this a little, we will first split the text into sentences, and then iterate over each sentence separately. This makes it easier to keep track of sentence IDs. For the tagging, we will work a lot with\n`NSRange` items, which are used to demarcate a region in the source text that an annotation applies to. We start off by creating a range that has to be within the first sentence and use it to get the full extent of the initial sentence for tagging:\n\n```objc\nNSRange currentSentence = [tagger sentenceRangeForRange:NSMakeRange(0, 1)];\n```\n\nOnce we have finished dealing with this sentence, we check whether we have successfully completed our text, or whether there are more sentences available:\n\n```objc\nif (currentSentence.location + currentSentence.length == [fileContent length]) {\n    currentSentence.location = NSNotFound;\n} else {\n    NSRange nextSentence = NSMakeRange(currentSentence.location + currentSentence.length + 1, 1);\n    currentSentence = [tagger sentenceRangeForRange:nextSentence];\n}\n```\n\nIf we have reached the end of the text, `NSNotFound` is used to signal to the `while` loop that it should terminate.\nIf we use a range that is outside of the text, `NSLinguisticTagger` simply crashes ungraciously with an exception.\n\nThe main work then happens within a single method call in our sentence-processing loop:\n\n```objc\nwhile (currentSentence.location != NSNotFound) {\n    __block NSUInteger tokenPosition = 0;\n    [tagger enumerateTagsInRange:currentSentence\n                          scheme:NSLinguisticTagSchemeNameTypeOrLexicalClass\n                         options:options\n                      usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) \n    {\n        NSString *token = [fileContent substringWithRange:tokenRange];\n        NSString *lemma = [tagger tagAtIndex:tokenRange.location \n                                      scheme:NSLinguisticTagSchemeLemma \n                                  tokenRange: NULL \n                               sentenceRange:NULL];\n        if (lemma == nil) {\n            lemma = token;\n        }\n        [self.delegate receiveWord:@{\n            @\"token\": token, \n            @\"postag\": tag, \n            @\"lemma\": lemma, \n            @\"position\": @(tokenPosition), \n            @\"sentence\": @(sentenceCounter), \n            @\"filename\": filename\n        }];\n        tokenPosition++;\n    }];\n}\n```\n\nWe ask the tagger for name types or lexical classes, given a set of options (joining names, omitting punctuation, and white space).  We then get the tag and extent of each item found, and retrieve further information about it. The token is the actual part of the string, simply described by the character range. Lemma is the base form, which will be `nil` if unavailable, so we need to check for that and use the token string as a fallback. Once we have collected that information, we package it up in a dictionary and send it to our delegate for processing.\n\nIn our sample app, we simply log all the words we are receiving, but we can basically do whatever we want here. To allow searching, we could filter out all words that are not nouns, verbs, adjectives, adverbs, or names, and store their location in an index database. Using the lemma instead of the token value, we can conflate inflected variants\n(_pig_ and _pigs_), which will keep the index size smaller and also retrieve more relevant words than if we were only looking up the actual token. Bear in mind that you then probably also want to lemmatize any queries, otherwise a search of _pigs_ would turn up nothing.\n\nTo make it more realistic, I have added some basic HTML tags around the header information of the sample text, identifying the title, byline, and date, for example. Running this through the tagger comes up with the problem that `NSLinguisticTagger` is not aware of HTML, and tries to process the mark-up as text. Here are the first three received words:\n\n\n```\n{\n    filename = \"/Users/oliver/tmp/guardian-article.txt\";\n    lemma = \"<\";\n    position = 0;\n    postag = Particle;\n    sentence = 0;\n    token = \"<\";\n}\n{\n    filename = \"/Users/oliver/tmp/guardian-article.txt\";\n    lemma = h1;\n    position = 1;\n    postag = Verb;\n    sentence = 0;\n    token = h1;\n}\n{\n    filename = \"/Users/oliver/tmp/guardian-article.txt\";\n    lemma = \">\";\n    position = 2;\n    postag = Adjective;\n    sentence = 0;\n    token = \">\";\n}\n```\n\nNot only are tags split into parts and treated as words, but they also get weird and completely wrong tags. So, if you are processing files that contain mark-up, it is best to filter that out first. Maybe, instead of splitting the whole text into sentences as we have done in the sample app, you would want to identify tags and return an `NSRange` that covers the area between tags. Or, strip them out completely, which is a better option if\nthere are in-line tags (such as bold/italics or hyperlinks).\n\n## Results\n\nThe performance of the tagger is surprisingly good, given that it has to work with general language. If you are only dealing with a restricted domain (such as texts about technology), you can make assumptions which are not possible when handling unrestricted texts. But Apple's tagger has to work without any knowledge of what gets thrown\nat it, so given that, it makes comparatively few errors. Obviously, some names will not be recognized, such as the place name _Chengdu_. But on the other hand, the tagger copes fine with most of the names of people in the text. For some reason, the date (_Wednesday 4 December 2013 10.35 GMT_) is taken as a personal name, presumably based on\nRobinson Crusoe's naming conventions. And while the Environment Secretary, _Owen Patterson_, is recognized, the\narguably more important Prime Minister, _David Cameron_, is not, despite _David_ being a more common name.\n\nThat is one problem with a probabilistic tagger: it sometimes is hard to understand why words are tagged in a particular way. And there are no hooks into the tagger that would allow you, for example, to provide a list of known names of places, people, or organizations. You just have to make do with the default settings. For that reason,\nit is always best to test any application using the tagger with plenty of data, as by inspecting the results you can then get a feel for what works and what does not.\n\n\n## Probabilities\n\nThere are several ways to implement a part-of-speech tagger: the two main approaches are rule-based and stochastic. Both have a (reasonably large) set of rules that tell you that after an adjective you can have a noun, but not a determiner, or you have a matrix of probabilities that gives you the likelihood for a particular tag occurring in a\nspecific context. You can also have both a probabilistic base model which uses a few rules to correct recurring typical errors and a so-called hybrid tagger. As developing rule sets for different languages is much more effort than automatically training a stochastic language model, my guess is that `NSLinguisticTagger` is purely probabilistic. This\nimplementation detail is also exposed by the:\n\n```objc\n- (NSArray *)possibleTagsAtIndex:(NSUInteger)charIndex \n                          scheme:(NSString *)tagScheme \n                      tokenRange:(NSRangePointer)tokenRange \n                   sentenceRange:(NSRangePointer)sentenceRange \n                          scores:(NSArray **)scores\n```\n\nmethod. This accounts for the fact that sometimes (or most times, actually) there is more than one possible tag value, and the tagger has to make a choice which could potentially be wrong. With this method, you can get a list of the possible options, together with their probability scores. The highest-scoring word will have been chosen by the tagger,\nbut here you can also have access to the second and subsequent alternatives, in case you do want to perhaps build a rule-based post-processor to improve the tagger's performance.\n\nOne caveat with this method is that it did have a bug in it, in that it did not actually return any scores. This bug was fixed in OS X 10.9/Mavericks, so if you need to support, earlier versions be aware that you cannot use this method.\nIt also works fine on iOS7.\n\nHere is some example output for _When is the next train..._:\n\n<table><thead><tr><th style=\"text-align: left;padding-right:1em;\">When</th><th style=\"text-align: left;padding-right:1em;\">is</th><th style=\"text-align: left;padding-right:1em;\">the</th><th style=\"text-align: left;padding-right:1em;\">next</th><th style=\"text-align: left;padding-right:1em;\">train</th></tr></thead><tbody><tr><td style=\"text-align: left;padding-right:1em;\">Pronoun, 0.9995162</td><td style=\"text-align: left;padding-right:1em;\">Verb, 1</td><td style=\"text-align: left;padding-right:1em;\">Determiner, 0.9999986</td><td style=\"text-align: left;padding-right:1em;\">Adjective, 0.9292629</td><td style=\"text-align: left;padding-right:1em;\">Noun, 0.8741992</td>\n</tr><tr><td style=\"text-align: left;padding-right:1em;\">Conjunction, 0.0004337671</td><td style=\"text-align: left;padding-right:1em;\"></td><td style=\"text-align: left;padding-right:1em;\">Adverb, 1.344403e-06</td><td style=\"text-align: left;padding-right:1em;\">Adverb, 0.0636334</td><td style=\"text-align: left;padding-right:1em;\">Verb, 0.1258008</td>\n</tr><tr><td style=\"text-align: left;padding-right:1em;\">Adverb, 4.170838e-05</td><td style=\"text-align: left;padding-right:1em;\"></td><td style=\"text-align: left;padding-right:1em;\"></td><td style=\"text-align: left;padding-right:1em;\">Preposition, 0.007003677</td><td style=\"text-align: left;padding-right:1em;\">\n</td></tr><tr><td style=\"text-align: left;padding-right:1em;\">Noun, 8.341675e-06</td><td style=\"text-align: left;padding-right:1em;\"></td><td style=\"text-align: left;padding-right:1em;\"></td><td style=\"text-align: left;padding-right:1em;\">Noun, 0.0001000525</td><td style=\"text-align: left;padding-right:1em;\">\n</td></tr></tbody></table>\n\nAs you can see, the correct tag has, by far, the highest probability in this case. For most applications, you can thus keep it simple and just accept the tag provided by the tagger, without having to drill down into the probabilities. But it is good to know that you have access to them in case you want to accept that the tagger gets it wrong occasionally and have a backup. Of course, you will not know when the tagger does get it wrong without checking it yourself! One clue, however, could be the probability differential: if the probabilities are fairly close (unlike\nin the example above), then that might indicate a likely error.\n\n## Conclusion\n\nProcessing natural language is hard, and Apple provides us with a decent tool that will easily support most use cases without being too complex to use. Of course, it is not perfect, but then so are none of the state-of-the-art language processing tools. While on iOS, currently only English is supported, that might change as specs improve\nand more memory becomes available for storing the (undoubtedly rather large) language models. Until then, we are a bit limited, but there are still loads of possibilities of adding language support to your applications, be it simply highlighting verbs in a text editor, making sense of typed user input, or processing external data files.\n`NSLinguisticTagger` will help you with that.\n\n"
  },
  {
    "path": "2013-12-09-nsformatter.md",
    "content": "---\ntitle:  \"Custom Formatters\"\ncategory: \"7\"\ndate: \"2013-12-09 07:00:00\"\ntags: article\nauthor:\n  - name: Klaas Pieter Annema\n    url: https://twitter.com/klaaspieter\n---\n\nWhen formatting data into a user-readable format we tend to use quick one-off solutions. This is a shame because Foundation comes with `NSFormatter`, which is perfectly suited for this task and can be easily reused throughout your code base. Heck, if you're on a Mac, AppKit classes have built-in support for `NSFormatter`, making your life a lot easier.\n\n## Built-in Formatters\n\nFoundation comes with the abstract `NSFormatter` class and two concrete subclasses: `NSNumberFormatter` and `NSDateFormatter`. We're going to skip these and jump right into the deep end, implementing our own subclass.\n\nIf you need a more subtle introduction, I recommend reading this [NSHipster post](http://nshipster.com/nsformatter/).\n\n## Introduction\n\n`NSFormatter` by itself doesn't do anything, except throw errors. I have yet to find a programmer who wants this, but if such a thing tickles your fancy, go for it!\n\nBecause we don't like errors, we'll implement an `NSFormatter` subclass that can transform instances of `UIColor` to a human-readable name. For example, the following code will return the string \"Blue\":\n\n```objc\nKPAColorFormatter *colorFormatter = [[KPAColorFormatter alloc] init];\n[colorFormatter stringForObjectValue:[UIColor blueColor]] // Blue\n```\n\nTwo methods are required when implementing a `NSFormatter` subclass: `stringForObjectValue:` and `getObjectValue:forString:errorDescription:`. We'll start of with the first because that's the one you'll use most often. The second is, as far as I know, most often used in OS X and actually not very useful. More on that later.\n\n## The Initializer\n\nHold your horses, as first we need to do some setup. There is no pre-defined mapping from colors to their names, so we need to define this. For the sake of simplicity, this will be our initializer:\n\n```objc\n- (id)init;\n{\n    return [self initWithColors:@{\n        [UIColor redColor]: @\"Red\",\n        [UIColor blueColor]: @\"Blue\",\n        [UIColor greenColor]: @\"Green\"\n    }];\n}\n```\n\nOur 'known' colors are a dictionary keyed by a `UIColor` with the English name as values. I'll leave the implementation of the `initWithColors:` method to your imagination. Or, if you're that person who looks at the answers on the last page of the puzzle book, go ahead and take a look at the [Github repo][Github repo]. \n\n## Formatting Object Values\n\nThe first thing we need to do in `stringForObjectValue:` is verify that the value is of the expected class. We can only format `UIColor`s so this is the start of our method:\n\n```objc\n- (NSString *)stringForObjectValue:(id)value;\n{\n    if (![value isKindOfClass:[UIColor class]]) {\n        return nil;\n    }\n    \n    // To be continued...\n}\n```\n\nAfter we've verified that the value is what we expect it to be, we can do the real magic. Recall that our formatter has a dictionary of color names keyed by their color. To make it work, all we need to do is look up the name using the color value as key:\n\n```objc\n- (NSString *)stringForObjectValue:(id)value;\n{\n    // Previously on KPAColorFormatter\n    \n    return [self.colors objectForKey:value];\n}\n```\n\nThis is the simplest implementation possible. A more advanced (and useful) formatter would also be able to look up color names that don't exist in our dictionary by finding the closest known color. I'll leave that as an exercise to the reader. Or, if you don't work out much, take a look at the [Github repo][Github repo].\n\n## Reverse Formatting\n\nAny formatter should also support reverse formatting from a string back to an instance of the class. This is done using `getObjectValue:forString:errorDescription:`. The reason for this is that on OS X, formatters are often used in combination with `NSCell`s.\n\n`NSCell`'s have a `objectValue` property. By default, `NSCell` will use the `objectValue`'s description, but it can optionally use a formatter. In the case of `NSTextFieldCell`, a user can also enter a string value and you, as the programmer, now can expect `objectValue` to be an instance of `UIColor` that represents that string value. In our case, the user could enter “Blue” and we should return, by reference, a `[UIColor blueColor]` instance.\n\nThere are two parts to implementing reverse formatting: the part where the formatter can successfully transform the string value into an instance of `UIColor`, and one where it cannot. Let's start with the happy path:\n\n```objc\n- (BOOL)getObjectValue:(out __autoreleasing id *)obj \n             forString:(NSString *)string \n      errorDescription:(out NSString *__autoreleasing *)error;\n{\n    __block UIColor *matchingColor = nil;\n    [self.colors enumerateKeysAndObjectsUsingBlock:^(UIColor *color, NSString *name, BOOL *stop) {\n        if([name isEqualToString:string]) {\n            matchingColor = color;\n            *stop = YES;\n        }\n    }];\n\n    if (matchingColor) {\n        *obj = matchingColor;\n        return YES;\n    } // Snip\n```\n\nThere is some optimization that can be done here, but let's not do that prematurely. This enumerates through every object in our colors dictionary and when a name is found it will return the color instance associated with it by reference. We also return `YES` to notify the caller that we were able to turn the string back into an object.\n\nNow the error path:\n\n```objc\nif (matchingColor) {\n    // snap\n} else if (error) {\n    *error = [NSString stringWithFormat:@\"No known color for name: %@\", string];\n}\n\nreturn NO;\n```\n\nIf we can't find a matching color we check if the caller is interested in errors, and if so, return it by reference. The check for `error` here is important. If you don't do this you _will_ crash. We also return `NO` to notify the caller that conversion was not successful.\n\n## Localization\n\nThat's it! We have a fully functional `NSFormatter` subclass, for English speakers, living in the United States.\n\nThat's about [319 million people](http://www.wolframalpha.com/input/?i=population+of+the+united+states) compared to [7.13 billion](http://www.wolframalpha.com/input/?i=population+of+the+world) in the entire world. In other words [96 percent](http://www.wolframalpha.com/input/?i=ratio+of+population+of+the+United+States+vs+population+of+the+world) of your potential users aren't impressed, yet. Of course I hear you say: most of those don't own iPhones or Macs and it's really a much smaller number. Where's the fun in that? Party pooper!\n\nIf you take a look at `NSNumberFormatter` and `NSDateFormatter` you'll see that both have a locale property taking an instance of `NSLocale`. Let's extend our formatter to support this and return a translated name based on the locale.\n\n### Translating\n\nThe first thing we need to do is translate the color strings. Messing with genstring, and `*.lprojs` is outside of the scope of this article. I mean, you have to get back to work at some point right? If not, [there][Translating article 1] [are][Translating article 2] [a][Translating article 3] [bunch][Translating article 4] [of][Translating article 5] [articles][Translating article 6] [about][Translating article 7] [it][Translating article 8]. Read them all? Awesome! No more work, time to go home.\n\n[Translating article 1]: https://developer.apple.com/internationalization/\n[Translating article 2]: http://nshipster.com/nslocalizedstring/\n[Translating article 3]: https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPInternational/Introduction/Introduction.html\n[Translating article 4]: http://www.tethras.com/apple\n[Translating article 5]: http://www.ibabbleon.com/iphone_app_localization.html\n[Translating article 6]: http://www.getlocalization.com/library/get-localization-mac/\n[Translating article 7]: http://www.daveoncode.com/2010/05/15/iphone-applications-localization/\n[Translating article 8]: http://useyourloaf.com/blog/2010/12/15/localize-iphone-application-name.html\n\n### Localized Formatting\n\nBack to implementing localization. After we have access to the translated strings, we need to update `stringForObjectValue:` so it knows where to get the translation. Those who have worked with `NSLocalizedString` before probably already went ahead and replaced every string with `NSLocalizedString`. Wrong! \n\nWe are dealing with a dynamic locale here and `NSLocalizedString` will only find the translation for the current default language. Now in 99 percent of the cases, this is probably what you want, hence the default behavior, but we need to look up the language dynamically using the locale set on our formatter.\n\nThe new implementation of `stringForObjectValue:`\n\n```objc\n- (NSString *)stringForObjectValue:(id)value;\n{\n    // Previously on... don't you hate these? I just watched that 20 seconds ago!\n\n    NSString *languageCode = [self.locale objectForKey:NSLocaleLanguageCode];\n    NSURL *bundleURL = [[NSBundle bundleForClass:self.class] URLForResource:languageCode \n                                                              withExtension:@\"lproj\"];\n    NSBundle *languageBundle = [NSBundle bundleWithURL:bundleURL];\n    return [languageBundle localizedStringForKey:name value:name table:nil];\n}\n```\n\nThis leaves room for some refactoring, but bear with me. It's easier to read if all the code is in the same place.\n\nWe first find the language code for the locale, after which we look up the `NSBundle` for that language. We then ask the bundle to give us the translation of the English name. If it can't be found, the `name:` argument (our English name) will be returned as a fallback value. In case you're interested, this is exactly what `NSLocalizedString` does except that we're dynamically looking up the bundle.\n\n### Localized Reverse Formatting\n\nThat leaves us with reverse formatting from a translated color name back to color instances. Honestly, I don't think it's worth it. Our current implementation works perfect for probably 99 percent of the cases. In the other one percent where you are using this formatter on a Mac, on an `NSCell`, and you're allowing your users to enter color names which you're going to try and parse, you'll need something a lot more complicated than a simple `NSFormatter` subclass. Also, you probably shouldn't be letting your users enter colors using text. [`NSColorPanel`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSColorPanel_Class/) is a much better solution.\n\n## Attributed Strings\n\nNow that our formatter does what we want it to do, let's implement something completely useless. You know, because we can.\n\nFormatters also have support for attributed strings. I find that whether or not you want to use that support really depends on your specific application and its user interface. Therefore, you will probably want to make that part of your formatter configurable.\n\nFor ours, I want to set the text color to the color that we're formatting. This is what that looks like:\n\n```objc\n- (NSAttributedString *)attributedStringForObjectValue:(id)value \n                                 withDefaultAttributes:(NSDictionary *)defaultAttributes;\n{\n    NSString *string = [self stringForObjectValue:value];\n\n    if  (!string) {\n        return nil;\n    }\n\n    NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:defaultAttributes];\n    attributes[NSForegroundColorAttributeName] = value;\n    return [[NSAttributedString alloc] initWithString:string attributes:attributes];\n}\n```\n\nFirst we format the string like normal, after which we check that formatting was successful. Then we merge the default attributes with our foreground color attribute. Finally, we return the attributed string. Simple, right?\n\n## Convenience\n\nBecause initializing built-in formatters [is slow](https://twitter.com/ID_AA_Carmack/status/28939697453), it has become common practice to also expose a convenience class method on your formatter. The formatter should use same defaults and the current locale. This is the implementation for our formatter:\n\n```objc\n+ (NSString *)localizedStringFromColor:(UIColor *)color;\n{\n    static dispatch_once_t onceToken;\n    dispatch_once(&onceToken, ^{\n        KPAColorFormatterReusableInstance = [[KPAColorFormatter alloc] init];\n    });\n\n    return [KPAColorFormatterReusableInstance stringForObjectValue:color];\n}\n```\n\nUnless your formatter is doing really crazy formatting, like `NSNumberFormatter` and `NSDateFormatter`, you probably don't need this for performance reasons. But it's still good to do because it makes using your formatter easier.\n\n## Wrapping Up\n\nOur color formatter can now format a `UIColor` instance into a human-readable name and the other way around. There is a lot more that `NSFormatter` can do that we haven't covered yet. Especially on the Mac where, because of its integration with `NSCell`, you can use more advanced stuff, like specifying and validating a string representation while the user is editing.\n\nThere is also more we can do to make our formatter more customizable. For example, my implementation will attempt to look up the closest color name if there is no direct match. Sometimes you may not want this so our formatter should have a Boolean property to control this behavior. Similarly, our attributed string formatting might not be what you want either and should also be more customizable.\n\nThat said, we ended up with a pretty solid formatter. All the code (with an OS X example) is available on [Github][Github repo]. My implementation is also available on [CocoaPods][CocoaPods]. If you desperately need this functionality in your app, add `pod \"KPAColorFormatter\"` to your Podfile and have fun experimenting!\n\n[Github repo]: https://github.com/klaaspieter/KPAColorFormatter\n[Cocoapods]: http://cocoapods.org\n"
  },
  {
    "path": "2013-12-09-value-objects.md",
    "content": "---\ntitle: \"Value Objects\"\ncategory: \"7\"\ndate: \"2013-12-09 10:00:00\"\ntags: article\nauthor:\n  - name: Chris Eidhof\n    url: http://twitter.com/chriseidhof\n---\n\n\nIn this article, we'll look at how to write [value objects](http://en.wikipedia.org/wiki/Value_object) in Objective-C. In doing that, we'll touch upon important protocols and methods in Objective-C. A value object is an object that holds some values, and can be compared for equality. Often, value objects can be used as model objects. For example, consider a simple `Person` object:\n\n```objc\n@interface Person : NSObject\n\n@property (nonatomic,copy) NSString* name;\n@property (nonatomic) NSDate* birthDate;\n@property (nonatomic) NSUInteger numberOfKids;\n\n@end\n```\n\nCreating these kinds of objects is the bread and butter of our work, and while these objects look deceivingly simple, there are a lot of subtleties involved. \n\nOne thing that a lot of us learned the hard way is that these objects should be immutable. Once you create a `Person` object, it's impossible to change it anymore. We'll touch upon mutability later in this issue.\n\n## Properties\n\nThe first thing to notice is that we use properties to define the attributes that a `Person` has.\nCreating the properties is quite mechanical: for normal properties, you make them `nonatomic`. By default, object properties are `strong`, and scalar properties are `assign`. There's one exception: for properties that have a mutable counterpart, you want to define them as `copy`. For example, the `name` property is of type `NSString`. What could happen is that somebody creates a `Person` object and assigns a value of type `NSMutableString`. Then, sometime later, he or she might change the mutable string. If our property would have been `strong` instead of `copy`, our `Person` object would have changed too, which is not what we want. It's the same for containers, such as arrays or dictionaries. \n\nBe aware that the copy is shallow; the containers might still contain mutable objects. For example, if you have an `NSMutableArray* a` containing `NSMutableDictionary` elements, then `[a copy]` will give you an immutable array, but the elements will be the same `NSMutableDictionary` objects.\nAs we'll see later, copy for immutable objects is free, but it increases the retain count.\n\nIn older code, you might not see properties, as they are a relatively recent addition to Objective-C. Instead of properties, you might see custom getters and setters, or plain instance variables. For modern code, it seems most people agree on using properties, which is also what we recommend.\n\n**More Reading:** [`NSString`: `copy` or `retain`](http://stackoverflow.com/questions/387959/nsstring-property-copy-or-retain)\n\n##  Initializers\n\nIf we want immutable objects, we should make sure that they can't be modified after being created. We can do this by adding an initializer and making our properties readonly in the interface. Our interface then looks like this:\n\n```objc\n@interface Person : NSObject\n\n@property (nonatomic,readonly) NSString* name;\n@property (nonatomic,readonly) NSDate* birthDate;\n@property (nonatomic,readonly) NSUInteger numberOfKids;\n\n- (instancetype)initWithName:(NSString*)name\n                   birthDate:(NSDate*)birthDate\n                numberOfKids:(NSUInteger)numberOfKids;\n\n@end\n```\n\nThen, in our implementation, we have to use instance variables instead of properties:\n\n```objc\n@implementation Person\n\n- (instancetype)initWithName:(NSString*)name\n                   birthDate:(NSDate*)birthDate\n                numberOfKids:(NSUInteger)numberOfKids\n{\n    self = [super init];\n    if (self) {\n        _name = [name copy];\n        _birthDate = birthDate;\n        _numberOfKids = numberOfKids;\n    }\n    return self;\n}\n\n@end\n```\n\nNow, we can construct new `Person` objects, but not modify them anymore. This is very helpful; when writing other classes that work with `Person` objects, we know that these values won't change as we are working with them. Also note that `copy` is not part of the interface anymore: it is now an implementation detail.\n\n## Comparing for Equality\n\nTo compare for equality, we have to implement the `isEqual:` method. We want the `isEqual:` method to be true if and only if all properties are equal. There are two good articles by Mike Ash ([Implement Equality and Hashing](http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html)) and NSHipster ([Equality](http://nshipster.com/equality/)) that explain how to do this. First, let's write `isEqual:`:\n\n```objc\n- (BOOL)isEqual:(id)obj\n{\n    if(![obj isKindOfClass:[Person class]]) return NO;\n    \n    Person* other = (Person*)obj;\n\n    BOOL nameIsEqual = self.name == other.name || [self.name isEqual:other.name];\n    BOOL birthDateIsEqual = self.birthDate == other.birthDate || [self.birthDate isEqual:other.birthDate];\n    BOOL numberOfKidsIsEqual = self.numberOfKids == other.numberOfKids;\n    return nameIsEqual && birthDateIsEqual && numberOfKidsIsEqual;\n}\n```\n\nNow, we check if we're the same kind of class. If not, we're definitely not equal. Then, for each object property, we check if the pointer is equal. The left operand of the `||` might seem superfluous, but it's there to return `YES` if both properties are `nil`. To compare scalar values like `NSUInteger` for equality, we can just use `==`.\n\nOne thing that's good to note: here, we split up the different properties into their own `BOOL`s. In practice, it might make more sense to combine them into one big clause, because then you get the lazy evaluation for free. In the example above, if the `name` is not equal, we don't need to check any of the other properties. By combining everything into one if statement we get that optimization for free.\n\nNext, as [per the documentation](https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/isEqual:), we need to implement a hash function as well. Apple says:\n\n> If two objects are equal, they must have the same hash value. This last point is particularly important if you define isEqual: in a subclass and intend to put instances of that subclass into a collection. Make sure you also define hash in your subclass.\n\nFirst, we can try to run the following code, without having a `hash` function implemented:\n\n```objc\nPerson* p1 = [[Person alloc] initWithName:name birthDate:start numberOfKids:0];\nPerson* p2 = [[Person alloc] initWithName:name birthDate:start numberOfKids:0];\nNSDictionary* dict = @{p1: @\"one\", p2: @\"two\"};\nNSLog(@\"%@\", dict);\n```\n\nThe first time I ran the above code, everything was sort of okay, and there were two items in the dictionary. The second time, there was only one. Things just get very unpredictable, so let's just do as the documentation says.\n\nAs you might remember from your computer science classes, writing a good hash function is not easy. A good hash function needs to be *deterministic* and *uniform*. Deterministic means that the same input needs to generate the same hash value. Uniform means that the result of the hash function should map the inputs uniformly over the output range. The more uniform your output, the better the performance when you use these objects in a collection.\n\nFirst, just for kicks, let's have a look at what happens when we don't have a hash function, and we try to use `Person` objects as keys in a dictionary:\n\n```objc\nNSMutableDictionary* dictionary = [NSMutableDictionary dictionary];\n\nNSDate* start = [NSDate date];\nfor (int i = 0; i < 50000; i++) {\n    NSString* name = randomString();\n    Person* p = [[Person alloc] initWithName:name birthDate:[NSDate date] numberOfKids:i++];\n    [dictionary setObject:@\"value\" forKey:p];\n}\nNSLog(@\"%f\", [[NSDate date] timeIntervalSinceDate:start]);\n```\n\nThis takes 29 seconds on my machine. In comparison, when we implement a basic `hash` function, the same code runs in 0.4 seconds. These are not proper benchmarks, but do give a very good indication of why it's important to implement a proper `hash` function. For the `Person` class, we can start with a hash function like this:\n\n```objc\n- (NSUInteger)hash\n{\n    return self.name.hash ^ self.birthDate.hash ^ self.numberOfKids;\n}\n```\n\nThis will take the three hashes from our properties and `XOR` them. In this case, it's good enough for our purposes, because the `NSString` hashing function is good for short strings (it used to only perform well for strings [up to 96 characters](http://www.abakia.de/blog/2012/12/05/nsstring-hash-is-bad/), but now that has changed. See [CFString.c](http://www.opensource.apple.com/source/CF/CF-855.11/CFString.c), look for `hash`). For serious hashing, your hashing function depends on the data you have. This is covered in [Mike Ash's article](http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html) and [elsewhere](http://www.burtleburtle.net/bob/hash/spooky.html).\n\nIn [the documentation](https://developer.apple.com/library/mac/documentation/cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/hash) for `hash`, there's the following paragraph:\n\n> If a mutable object is added to a collection that uses hash values to\n> determine the object’s position in the collection, the value returned by\n> the hash method of the object must not change while the object is in the\n> collection. Therefore, either the hash method must not rely on any of the\n> object’s internal state information or you must make sure the object’s\n> internal state information does not change while the object is in the\n> collection. Thus, for example, a mutable dictionary can be put in a hash\n> table but you must not change it while it is in there. (Note that it can\n> be difficult to know whether or not a given object is in a collection.)\n\nThis is another very important reason to make sure your objects are immutable. Then you don't even have to worry about this problem.\n\n**More Reading:**\n\n* [A hash function for CGRect](https://gist.github.com/steipete/6133152)\n* [A Hash Function for Hash Table Lookup](http://www.burtleburtle.net/bob/hash/doobs.html)\n* [SpookyHash: a 128-bit noncryptographic hash](http://www.burtleburtle.net/bob/hash/spooky.html)\n* [Why do hash functions use prime numbers?](http://computinglife.wordpress.com/2008/11/20/why-do-hash-functions-use-prime-numbers/)\n\n\n## NSCopying\n\nTo make sure our objects are useful, it's convenient to have implemented the `NSCopying` protocol. This allows us, for example, to use them in container classes. For a mutable variant of our class, `NSCopying` can be implemented like this:\n\n```objc\n- (id)copyWithZone:(NSZone *)zone\n{\n    Person* p = [[Person allocWithZone:zone] initWithName:self.name\n                                                birthDate:self.birthDate\n                                             numberOfKids:self.numberOfKids];\n    return p;\n}\n```\n\nHowever, in the protocol documentation, they mention another way to implement `NSCopying`:\n\n> Implement NSCopying by retaining the original instead of creating a new copy when the class and its contents are immutable.\n\nSo, for our immutable version, we can just do this:\n\n```objc\n- (id)copyWithZone:(NSZone *)zone\n{\n    return self;\n}\n```\n\n## NSCoding\n\nIf we want to serialize our objects, we can do that by implementing `NSCoding`. This protocol exists of two required methods:\n\n```objc\n- (id)initWithCoder:(NSCoder *)decoder\n- (void)encodeWithCoder:(NSCoder *)encoder\n```\n\nImplementing this is equally straightforward as implementing the equals methods, and also quite mechanical:\n\n```objc\n- (id)initWithCoder:(NSCoder *)aDecoder\n{\n    self = [super init];\n    if (self) {\n        _name = [aDecoder decodeObjectForKey:@\"name\"];\n        _birthDate = [aDecoder decodeObjectForKey:@\"birthDate\"];\n        _numberOfKids = [aDecoder decodeIntegerForKey:@\"numberOfKids\"];\n    }\n    return self;\n}\n\n- (void)encodeWithCoder:(NSCoder *)aCoder\n{\n    [aCoder encodeObject:self.name forKey:@\"name\"];\n    [aCoder encodeObject:self.birthDate forKey:@\"birthDate\"];\n    [aCoder encodeInteger:self.numberOfKids forKey:@\"numberOfKids\"];\n}\n```\n\nRead more about it on [NSHipster](http://nshipster.com/nscoding/) and [Mike Ash's blog](http://www.mikeash.com/pyblog/friday-qa-2013-08-30-model-serialization-with-property-lists.html). By the way, don't use `NSCoding` when dealing with untrusted sources, such as data coming from the network, because the data may be tampered with. By [modifying the archived data](https://developer.apple.com/library/mac/documentation/security/conceptual/securecodingguide/Articles/ValidatingInput.html#//apple_ref/doc/uid/TP40007246-SW9), it's very possible to perform a remote code execution attack. Instead, use [`NSSecureCoding`](http://nshipster.com/nssecurecoding/) or a custom format like JSON.\n\n## Mantle\n\nNow, we're left with the question: can we automate this? It turns out we can.  One way would be code generation, but luckily there's a better alternative: [Mantle](https://github.com/github/Mantle). Mantle uses introspection to generate `isEqual:` and `hash`. In addition, it provides methods that help you create dictionaries, which can then be used to write and read JSON. Of course, doing this generically and at runtime will not be as efficient as writing your own, but on the other hand, doing it automatically is a process that is much less prone to errors.\n\n## Mutability\n\n\nIn C, and also in Objective-C, mutable values are the default. In a way, they are very convenient, in that you can change anything at anytime. When building smaller systems, this is mostly not a problem. However, as many of us learned the hard way, when building larger systems, it is much easier when things are immutable. In Objective-C, we've had immutable objects for a long time, and now other languages are also starting to add it.\n\nWe'll look at two problems with mutable objects. One is that they might change when you don't expect it, and the other one is when using mutable objects in a multithreading context.\n\n### Unexpected Changes\n\nSuppose we have a table view controller, which has a `people` property:\n\n```objc\n@interface ViewController : UITableViewController\n\n@property (nonatomic) NSArray* people;\n\n@end\n```\n\nAnd in our implementation, we just map each array element to a cell:\n\n```objc\n - (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView\n {\n     return 1;\n }\n \n - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section\n {\n     return self.people.count;\n }\n```\n\nNow, in the code that sets up the above view controller, we might have code like this:\n\n```objc\nself.items = [NSMutableArray array];\n[self loadItems]; // Add 100 items to the array\ntableVC.people = self.items;\n[self.navigationController pushViewController:tableVC animated:YES];\n```\n\nThe table view will start firing methods like `tableView:numberOfRowsInSection:`, and at first, everything will be fine. But suppose that, at some point, we do the following:\n\n```objc\n[self.items removeObjectAtIndex:1];\n```\n\nThis changes our `items` array, but it *also* changes the `people` array in our table view controller. If we do this without any further communication to the table view controller, the table view will still think there are 100 items, whereas our array will only contain 99. Bad things will happen. Instead, what we should have done is declare our property as `copy`:\n\n```objc\n @interface ViewController : UITableViewController\n \n @property (nonatomic, copy) NSArray* items;\n \n @end\n```\n\nNow, whenever we assign a mutable array to items, an immutable copy will be made. If we assign a regular (immutable) array value, the copy operation is free, and it only increases the retain count.\n\n### Multithreading\n\nSuppose we have a mutable object, `Account`, representing a bank account, that has a method `transfer:to:`:\n\n```objc\n- (void)transfer:(double)amount to:(Account*)otherAccount\n{\n    self.balance = self.balance - amount;\n    otherAccount.balance = otherAccount.balance + amount;\n}\n```\n\nMultithreaded code can go wrong in many ways. For example, if thread A reads `self.balance`, thread B might be modifying it before thread A continues. For a good explanation of all the dangers involved, see our [second issue](/issues/2-concurrency/).\n\nIf we have immutable objects instead, things are much easier. We cannot modify them, and this forces us to provide mutability at a completely different level, yielding much simpler code.\n\n### Caching\n\nAnother thing where immutability can help is when caching values. For example, suppose you've parsed a markdown document into a tree structure with nodes for all the different elements. If you want to generate HTML out of that, you can cache the value, because you know no children will ever change. If you have mutable objects, you would need to either generate the HTML from scratch every time, or build optimizations and observe every single object. With immutability, you don't have to worry about invalidating caches. Of course, this might come with a performance penalty. In almost all cases, however, the simplicity will outweigh the slight decrease in performance.\n\n### Immutability in Other Languages\n\nImmutable objects are one of the concepts inspired by functional programming languages like [Haskell](http://www.haskell.org/). In Haskell, values are immutable by default. Haskell programs typically have a [purely functional](http://en.wikipedia.org/wiki/Purely_functional) core, where there are no mutable objects, there is no state, and there are no side-effects like I/O. \n\nWe can learn from this in Objective-C programming. By having immutable objects where possible, our programs become much easier to test. There's a great [talk by Gary Bernhardt](https://www.destroyallsoftware.com/talks/boundaries) that shows how having immutable objects helps us write better software. In the talk he uses Ruby, but the concepts apply equally well to Objective-C.\n\n**Further Reading:**\n\n* [Cocoa Encyclopedia: Object Mutability](https://developer.apple.com/library/mac/documentation/General/Conceptual/CocoaEncyclopedia/ObjectMutability/ObjectMutability.html#//apple_ref/doc/uid/TP40010810-CH5-SW1)\n* [Mutability and Caching](http://garbagecollective.quora.com/Mutability-aliasing-and-the-caches-you-didnt-know-you-had)"
  },
  {
    "path": "2014-01-08-communicating-with-the-quadcopter.md",
    "content": "---\ntitle: \"Communicating with the Quadcopter\"\ncategory: \"8\"\ndate: \"2014-01-08 10:00:00\"\ntags: article\nauthor:\n  - name: Daniel Eggert\n    url: http://twitter.com/danielboedewadt\n---\n\n\nThe [AR Drone](http://ardrone2.parrot.com) [quadcopter](https://en.wikipedia.org/wiki/Quadcopter) is a small, Linux-based computer. Its WiFi interface acts as a WiFi hotspot. Once we've joined that, we can read the drone over WiFi at the [IP address](https://en.wikipedia.org/wiki/Ip_address) `192.168.1.1`.\n\n## UDP -- User Datagram Protocol\n\nThe communication with the drone happens over UDP, which is short for [User Datagram Protocol](https://en.wikipedia.org/wiki/User_Datagram_Protocol). UDP is one of the dominant [transport-layer](https://en.wikipedia.org/wiki/Transport_layer) protocols in use today. The other is TCP.\n\nLet us sidestep for a moment and look at TCP, or [Transmission Control Protocol](https://en.wikipedia.org/wiki/Transmission_Control_Protocol). Just about anything communicating through the internet uses TCP at the transport layer, and for a good reason, as using TCP is extremely convenient. The API for using TCP is rather straightforward, and TCP is well supported by all the hardware that the internet traffic has to travel through to get from one device on the internet to another. Using TCP is simple. Once you've opened a connection, you can write data into a so-called socket, and the other end can read that data from its socket. TCP makes sure that the exact data that is written in one end arrives at the other end. It hides a lot of complexity. TCP is based on top of [IP](https://en.wikipedia.org/wiki/Internet_Protocol), and lower-level IP data may not arrive in the order it is sent. It may, in fact, never arrive. But TCP hides this complexity. It is modeled after normal [Unix pipes](https://en.wikipedia.org/wiki/Pipeline_%28Unix%29). TCP also manages the throughput; it constantly adapts the rate at which data is transmitted to best utilize the available bandwidth. TCP does so much magic to pull off this trick that the de-facto standard books on TCP are three volumes with a total of more than 2,556 pages of detailed explanations:\n*TCP/IP Illustrated:*\n[The Protocols](http://www.amazon.com/dp/0321336313),\n[The Implementation](http://www.amazon.com/dp/020163354X),\n[TCP for Transactions](http://www.amazon.com/dp/0201634953).\n\nUDP, on the other hand, is a relatively simple protocol. But using it involves a lot of pain for the developer. When you send data over UDP, there's no way to know if that data reaches the other end. There's no way to know in which order data arrives. And there's no way to know how rapidly you can send data without data starting to drop because the available bandwidth changes.\n\nThat said, UDP has a very simple model: UDP allows you to send so-called datagrams (packets) from one machine to another. These datagrams or packets are received at the other end as the same packets (unless they've been lost on the way).\n\nIn order to use UDP, an application uses a [datagram socket](https://en.wikipedia.org/wiki/Datagram_socket), which binds a combination of an IP address and a [service port](https://en.wikipedia.org/wiki/Port_number) on both ends, and, as such, establishes host-to-host communication. Data sent on a given socket can be read on a matching socket on the receiving side.\n\nNote, that UDP is a *connectionless* protocol. There's no connection setup on the network. The socket simply keeps track of where to send packets and when packets arrive, if they should be captured by that socket.\n\n## UDP and AR Drone\n\nThe AR Drone interface is built on top of three UDP ports. As discussed above, using UDP is an arguable design choice, but [Parrot](http://www.parrot.com/) chose to do so.\n\nThe IP address of the drone is `192.168.1.1` and there are three ports we can use to connect over UDP:\n\n* Navigation Data Port = 5554\n* On-Board Video Port = 5555\n* AT Command Port = 5556\n\nWe need to use the *AT Command Port* to send commands to the drone. We can use the *Navigation Data Port* to retrieve data back from the drone. We'll talk about these two separately since they work quite differently. That said, they both rely on UDP sockets. Let's first see how that is done.\n\n## The UDP API\n\nApple doesn't provide an Objective-C wrapper or helper to work with UDP. This may be surprising at first. After all, the protocol dates back to 1980. The main reason, though, is very likely that hardly anything is using UDP, and if we use UDP, accessing the Unix C API for UDP is going to be least part of our worries. TCP is what we'll use in most cases, and for that, there are plenty of API options.\n\nThe C API we'll use is defined in `sys/socket.h`, `netinet/in.h`, `arpa/inet.h`. And `ARPA` refers to *Advanced Research Projects Agency*, the guys who invented the internet.\n\n### Creating a UDP Socket\n\nFirst off, we'll create a *socket* with:\n\n```objc\nint nativeSocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);\n```\n\n`PF_INET` is the domain of the socket. In this case, *internet*. `SOCK_DGRAM` specified that type to be a *datagram* socket (as opposed to a stream socket). Finally, `IPPROTO_UDP` specifies that the protocol is *UDP*. This socket now works similarly to a file descriptor that we would have obtained by calling the `open(2)` function.\n\nNext, we'll create a `struct` with our own address and the address of the drone. The type is `struct sockaddr_in` -- a socket address. We'll use `sin_me` for *our* address, and `sin_other` for the *other* end's address:\n\n```objc\nstruct sockaddr_in sin_me = {};\nsin_me.sin_len = (__uint8_t) sizeof(sin);\nsin_me.sin_family = AF_INET;\nsin_me.sin_port = htons(0);\nsin_me.sin_addr.s_addr = htonl(INADDR_ANY);\n\nstruct sockaddr_in sin_other = {};\nsin_other.sin_len = (__uint8_t) sizeof(sin_other);\nsin_other.sin_family = AF_INET;\nsin_other.sin_port = htons(self.port);\nint r = inet_aton([self.address UTF8String], &sin_other.sin_addr)\n```\n\nInitializing the `struct` with ` = {}` is a good practice in general, regardless of what struct you use, because it ensures that everything starts out being zero -- otherwise the values would be undefined, based on whatever happens to be on the stack. We'd easily run into odd bugs that only happen sometimes.\n\nNext, we're setting the fields of the `struct sockaddr_in` to specify the *socket address* to be used, with `sin_len` as the length of the structure. This allows support for multiple types of addresses. `sin_family` is the type of address. There's a long list of address families, but when connecting over the internet, it'll always be either `AF_INET` for [IPv4](https://en.wikipedia.org/wiki/Ipv4) or `AF_INET6` for [IPv6](https://en.wikipedia.org/wiki/Ipv6). Then we're setting the port and the IP address.\n\nOn *our* side, we set the port to `0` and the address to `INADDR_ANY`. A port number of 0 means that a random port number will be assigned to *our* side. `INADDR_ANY` results in the address that can route packets to the address of the other end (the drone).\n\nThe drone's address is set with `inet_aton(3)`, which converts the C string `192.168.1.1` into the corresponding four bytes `0xc0`, `0xa2`, `0x1`, `0x1` -- the IP address of the drone. Note that we're calling `htons(3)` and `htonl(3)` on the address and the port number. `htons` is short for *host-to-network-short* and `htonl` is short for *host-to-network-long*. The [endianness](https://en.wikipedia.org/wiki/Endianness) of most data networking (including IP) is big-endian. To ensure that our data is of the right endianness, we need to call these two functions.\n\nWe now bind the socket to our socket address with:\n\n```objc\nint r2 = bind(nativeSocket, (struct sockaddr *) &sin_me, sizeof(sin_me));\n```\n\nFinally, we connect the other end's socket address with the socket:\n\n```objc\nint r3 = connect(nativeSocket, (struct sockaddr *) &sin_other, sizeof(sin_other));\n```\n\nThis last step is optional. We could also specify the destination address every time we send a packet.\n\nIn our sample code, this is implemented inside `-[DatagramSocket configureIPv4WithError:]`, which also has some error handling.\n\n### Sending Data\n\nOnce we have a socket, sending data is a trivial matter. If we have an `NSData` object called `data`, we can call:\n\n```objc\nssize_t const result = sendto(nativeSocket, [data bytes], data.length, 0, NULL, 0);\nif (result < 0) {\n    NSLog(@\"sendto() failed: %s (%d)\", strerror(errno), errno);\n} else if (result != data.length) {\n    NSLog(@\"sendto() failed to send all bytes. Sent %ld of %lu bytes.\", result, (unsigned long) data.length);\n}\n```\n\nNote that [UDP](https://en.wikipedia.org/wiki/User_Datagram_Protocol) is unreliable by design. Once we've called `sendto(2)`, there's nothing more we can do to know what's happening to the data being transmitted over the internet.\n\n### Receiving Data\n\nReceiving data is, at its core, quite simple too. The function `recvfrom(2)` expects two arguments: The first argument is the socket address `sin_other`, which is the socket we want to receive data from. The second argument is a pointer to a buffer, into which the data will be written. Upon success, it returns the number of bytes read:\n\n```objc\nNSMutableData *data  = [NSMutableData dataWithLength:65535];\nssize_t count = recvfrom(nativeSocket, [data mutableBytes], [data length], 0, (struct sockaddr *) &sin_other, &length);\nif (count < 0) {\n    NSLog(@\"recvfrom() failed: %s (%d)\", strerror(errno), errno);\n    data = nil;\n} else {\n    data.length = count;\n}\n```\n\nOne thing to note, though, is that the `recvfrom(2)` call is blocking. The thread that calls it will wait until it can read data. Usually that's not what we want. With [GCD](https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref), we can set up an event source that will fire whenever the socket has data available to be read. This is the recommended way to read data from a socket.\n\nIn our case, the `DatagramSocket` class implements this method to set up the event source:\n\n```objc\n- (void)createReadSource\n{\n    self.readEventSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, self.nativeSocket, 0, self.readEventQueue);\n    __weak DatagramSocket *weakSelf = self;\n    dispatch_source_set_event_handler(self.readEventSource, ^{\n        [weakSelf socketHasBytesAvailable];\n    });\n    dispatch_resume(self.readEventSource);\n}\n```\n\nEvent sources start out in a suspended state. That's why we must call `dispatch_resume(3)`. Otherwise, no event would ever get delivered to the source. The `-socketHasBytesAvailable` then calls `recvfrom(2)` on the socket.\n\n### Default Values\n\nAs a small sidestep, we'll point out how the `nativeSocket` property:\n\n```objc\n@property (nonatomic) int nativeSocket;\n```\n\nis implemented:\n\n```objc\n@synthesize nativeSocket = _nativeSocket;\n- (void)setNativeSocket:(int)nativeSocket;\n{\n    _nativeSocket = nativeSocket + 1;\n}\n\n- (int)nativeSocket\n{\n    return _nativeSocket - 1;\n}\n```\n\nWe're subtracting one from the underlying instance variable. The reason for this is that, firstly, the Objective-C runtime guarantees all instance variables to be initialized to zero after `-alloc` has been called. And secondly, sockets are considered valid as long as they're non-negative, i.e. zero and up are valid socket numbers.\n\nBy offsetting the value, we can safely check if the socket value has been set even before `-init` has been called.\n\n\n### Putting It All Together\n\nOur [`DatagramSocket` class](https://github.com/objcio/issue-8-quadcopter-navigator/blob/master/DatagramSocket.m) wraps all the low-level UDP socket workings. The `DroneCommunicator` class uses it to communicate with the drone on both the *Navigation Data Port* 5554 and the *AT Command Port* 5556, like this:\n\n```objc\nNSError *error = nil;\nself.commandSocket = [DatagramSocket ipv4socketWithAddress:DroneAddress\n                                                      port:ATCommandPort\n                                           receiveDelegate:self\n                                              receiveQueue:[NSOperationQueue mainQueue]\n                                                     error:&error];\n\nself.navigationDataSocket = [DatagramSocket ipv4socketWithAddress:DroneAddress\n                                                             port:NavigationDataPort\n                                                  receiveDelegate:self\n                                                     receiveQueue:[NSOperationQueue mainQueue]\n                                                            error:&error];\n```\n\nThe delegate method then branches based on the socket:\n\n```objc\n- (void)datagramSocket:(DatagramSocket *)datagramSocket didReceiveData:(NSData *)data;\n{\n    if (datagramSocket == self.navigationDataSocket) {\n        [self didReceiveNavigationData:data];\n    } else if (datagramSocket == self.commandSocket) {\n        [self didReceiveCommandResponseData:data];\n    }\n}\n```\n\nThe only data that our sample app processes is the navigation data. This is done by the `DroneNavigationState` class, like this:\n\n```objc\n- (void)didReceiveNavigationData:(NSData *)data;\n{\n    DroneNavigationState *state = [DroneNavigationState stateFromNavigationData:data];\n    if (state != nil) {\n        self.navigationState = state;\n    }\n}\n```\n\n## Sending Commands\n\nWith the UDP socket up and running, sending commands is relatively straightforward. The so-called *AT Command Port* accepts plain ASCII commands, which look something like this:\n\n```objc\nAT*CONFIG=1,\"general:navdata_demo\",\"FALSE\"\nAT*CONFIG=2,\"control:altitude_max\",\"1600\"\nAT*CONFIG=3,\"control:flying_mode\",\"1000\"\nAT*COMWDG=4\nAT*FTRIM=5\n```\n\nThe [AR Drone SDK](https://projects.ardrone.org/projects/show/ardrone-api) contains a PDF document called *ARDrone Developer Guide*, which describes all AT commands in more detail.\n\nWe created a series of convenience and helper methods inside the `DroneCommunicator` class, so that the above can be sent with:\n\n\n```objc\n[self setConfigurationKey:@\"general:navdata_demo\" toString:@\"FALSE\"];\n[self setConfigurationKey:@\"control:altitude_max\" toString:@\"1600\"];\n[self setConfigurationKey:@\"control:flying_mode\" toString:@\"1000\"];\n[self sendCommand:@\"COMWDG\" arguments:nil];\n[self sendCommand:@\"FTRIM\" arguments:nil];\n```\n\nAll drone commands start with `AT*`, followed by the command name, and `=`, followed by the arguments separated by commas. The first argument is the sequence number of the command.\n\nFor this, we created `-sendCommand:arguments:`, which inserts the command sequence number at index 0:\n\n```objc\n- (int)sendCommand:(NSString *)command arguments:(NSArray *)arguments;\n{\n    NSMutableArray *args2 = [NSMutableArray arrayWithArray:arguments];\n    self.commandSequence++;\n    NSString *seq = [NSString stringWithFormat:@\"%d\", self.commandSequence];\n    [args2 insertObject:seq atIndex:0];\n    [self sendCommandWithoutSequenceNumber:command arguments:args2];\n    return self.commandSequence;\n}\n```\n\nand in turn calls `-sendCommandWithoutSequenceNumber:arguments:`, which prefixes the `AT*` and concatenates the command and arguments:\n\n```objc\n- (void)sendCommandWithoutSequenceNumber:(NSString *)command arguments:(NSArray *)arguments;\n{\n    NSMutableString *atString = [NSMutableString stringWithString:@\"AT*\"];\n    [atString appendString:command];\n    NSArray* processedArgs = [arguments valueForKey:@\"description\"];\n    if (0 < arguments.count) {\n        [atString appendString:@\"=\"];\n        [atString appendString:[processedArgs componentsJoinedByString:@\",\"]];\n    }\n    [atString appendString:@\"\\r\"];\n    [self sendString:atString];\n}\n```\n\nFinally, the completed string is converted to data and passed to the socket by:\n\n```objc\n- (void)sendString:(NSString*)string\n{\n    NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding];\n    if (data != nil) {\n        [self.commandSocket asynchronouslySendData:data];\n    } else {\n        NSLog(@\"Unable to convert string to ASCII: %@\", string);\n    }\n}\n```\n\n### Encoding Floating Point Values\n\nFor some strange reason, the people that engineered the drone protocol decided that floating point values should be sent as the integers that have the same bit pattern. This is truly odd, but we have to play along.\n\nLet's say we'd want to tell the drone to move forward at the relative speed 0.5. The `float` value 0.5 looks like this in binary:\n\n```objc\n0011 1111 0000 0000 0000 0000 0000 0000\n```\n\nIf we reinterpret this value as a 32-bit integer, it's 1056964608. Hence, we'd send:\n\n```objc\nAT*PCMD=6,1,0,1056964608,0,0\n```\n\nto the drone.\n\nIn our case, we're using numbers wrapped in `NSNumber` and the resulting code looks like this:\n\n```objc\nNSNumber *number = (id) self.flightState[i];\nunion {\n    float f;\n    int i;\n} u;\nu.f = number.floatValue;\n[result addObject:@(u.i)];\n```\n\nThe trick here is to use a `union` -- a lesser-known part of the C language. Unions allow multiple different types (in this case, `int` and `float`) to reside at the same memory location. We then store the floating point value into `u.f` and read the integer value from `u.i`.\n\n*Note:* It is illegal to use code like `int i = *((int *) &f)` -- this is not correct C code and results in undefined behavior. The resulting code will sometimes work, but it sometimes won't. Do not do this. You can read more about this on the [llvm blog](http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html) under *Violating Type Rules*. Sadly the *AR Drone Developer Guide* gets this wrong.\n"
  },
  {
    "path": "2014-01-08-editorial.md",
    "content": "---\r\ntitle:  \"Editorial\"\r\ncategory: \"8\"\r\ndate: \"2014-01-08 12:00:00\"\r\ntags: editorial\r\n---\r\n\r\nWelcome to objc.io issue #8!\r\n\r\nHappy 2014!\r\n\r\nThis issue of objc.io is a bit different than what we have done so far. This is our holiday issue, where we decided to spend a few days during the holiday break working on a small and fun project that we would then write about.\r\n\r\n<video poster=\"/images/issue-8/quadcopter-video-poster.jpg\" controls=\"1\">\r\n  <source src=\"/images/quadcopter.m4v\"></source>\r\n</video>\r\n\r\nWe wanted to do something with a [quadcopter](https://en.wikipedia.org/wiki/Quadrocopter), but we didn't yet have a detailed plan of what we wanted to build. Once we started experimenting with this, it quickly became apparent to us how complicated it is to work with real hardware. In the end, we had to cut quite a few corners to create something that would still end up as a self-contained project within a few days. \r\n\r\nWe want to thank [Felix Geisendörfer](http://felixge.de) -- the initiator of the [nodecopter](http://nodecopter.com) events -- for his advice and support. Luckily, Felix lives just around the corner from Chris, and he was kind enough to lend one of his drones and a few battery packs to us.\r\n\r\nWe briefly outline the overall goals of the project in the [overview article](/issues/8-quadcopter/the-quadcopter-project/). There, we also talk about how the project changed while we were working on it. The [other articles in this issue](/issues/8-quadcopter) will go into more technical detail regarding how we implemented this project.\n\r\nWe hope you have as much fun reading as we had hacking the quadcopter project.\r\n\r\n\r\n\r\nAll the best from Berlin,\r\n\r\nChris, Daniel, and Florian.\r\n"
  },
  {
    "path": "2014-01-08-the-quadcopter-client-app.md",
    "content": "---\ntitle:  \"The Client App\"\ncategory: \"8\"\ndate: \"2014-01-08 08:00:00\"\ntags: article\nauthor:\n  - name: Florian Kugler\n    url: https://twitter.com/floriankugler\n---\n\n\nThe client app is the component in [this project](/issues/8-quadcopter/the-quadcopter-project/) that sends the target location coordinates to the phone strapped to the drone. It's a pretty simple task, but there are a few interesting bits to it, like the use of the new (as of iOS 7) Multipeer Connectivity APIs and [NSSecureCoding](https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSSecureCoding_Protocol_Ref/content/NSSecureCoding.html).\n\nThe app exposes a very simple -- and not very pretty -- interface:\n\n![Screenshot of the client app](/images/issue-8/client-app.jpg)\n\n\n## Multipeer Connectivity\n\nIn order to establish a connection between the client and the navigation app on the drone, we're going to use the new Multipeer Connectivity APIs. For our purposes, we only need to connect two devices to each other, so the multipeer APIs are not used to their full potential here. But the code is actually the same if more clients were to join.\n\n\n### Advertising\n\nWe decided to make the client app the advertiser, and the navigation app on the drone is the browser. The client starts advertising using the following simple statements:\n\n```objc\nNSString *displayName = [UIDevice currentDevice].name;\nself.peer = [[MCPeerID alloc] initWithDisplayName:displayName];\nself.advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:self.peer discoveryInfo:nil serviceType:ServiceTypeIdentifier];\nself.advertiser.delegate = self;\n[self.advertiser startAdvertisingPeer];\n```\n\nOnce another device that is browsing for clients with the same service type discovers the advertiser, we'll receive a delegate callback in order to establish the connection:\n\n```objc\n- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void (^)(BOOL accept, MCSession *session))invitationHandler\n{\n    self.session = [[MCSession alloc] initWithPeer:self.peer];\n    self.session.delegate = self;\n    invitationHandler(YES, self.session);\n}\n```\n\nOnce we receive the invitation, we create a new session object, set ourselves as delegate of the session, and accept the invitation by calling the `invitationHandler` with `YES` and the session as arguments.\n\nIn order to be able to show the status of the connection on screen, we're going to implement another session delegate method. Since we're only connecting to one other device, we simply using the number of currently connected peers being greater than zero as indicator for being connected or not:\n\n```objc\n- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state\n{\n    [[NSOperationQueue mainQueue] addOperationWithBlock:^{\n        NSString *notificationName = session.connectedPeers.count > 0 ? MultiPeerConnectionDidConnectNotification : MultiPeerConnectionDidDisconnectNotification;\n        [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:self];\n    }];\n}\n```\n    \nSince five of the six methods in the `MCSessionDelegate` protocol are required, we have to add all those too, although we don't need them for our specific purposes.\n\nAt this point, the connection is established and we can use the session's `sendData:toPeers:withMode:error:` method to send data. We'll look more into this later on.\n\n\n### Browsing\n\nThe navigation app running on the phone flying on the drone has to initiate the connection by sending the client an invitation. This is equally straightforward to do. The first step is to start browsing for peers:\n\n```objc\nMCPeerID* peerId = [[MCPeerID alloc] initWithDisplayName:@\"Drone\"];\nself.browser = [[MCNearbyServiceBrowser alloc] initWithPeer:peerId serviceType:ServiceTypeIdentifier];\nself.browser.delegate = self;\n[self.browser startBrowsingForPeers];\n```\n\nOnce a peer is found, we get a delegate callback and can invite the peer into our session:\n\n```objc\n- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info\n{\n    self.session = [[MCSession alloc] initWithPeer:self.peerId];\n    self.session.delegate = self;\n    [browser invitePeer:peerID toSession:self.session withContext:nil timeout:0];\n}\n```\n\nOnce the client sends some data, we'll receive it via the session's delegate method, `session:didReceiveData:fromPeer:`.\n\n\n## Transmitting Data\n\nEvery peer in a multipeer session can very easily send data using the `sendData:toPeers:withMode:error:` method. We only have to figure out how to package the data in order to send it over the air.\n\nOne of the most common options would be to simply encode it as JSON. Although this would easily work for our purposes, we will do something a little bit more interesting by using [NSSecureCoding](https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSSecureCoding_Protocol_Ref/content/NSSecureCoding.html). It doesn't really make a difference for our example, but if you need to transmit more data, this is more efficient than encoding and decoding JSON. \n\nFirst, we create a class to package the data we need to send in:\n\n```objc\n@interface RemoteControlCommand : NSObject <NSSecureCoding>\n\n+ (instancetype)commandFromNetworkData:(NSData *)data;\n- (NSData *)encodeAsNetworkData;\n\n@property (nonatomic) CLLocationCoordinate2D coordinate;\n@property (nonatomic) BOOL stop;\n@property (nonatomic) BOOL takeoff;\n@property (nonatomic) BOOL reset;\n\n@end\n```\n    \nIn order to enable secure coding (ensuring that the received data is actually of the type we expect) we have to add the `supportsSecureCoding` class method to our implementation:\n\n```objc\n+ (BOOL)supportsSecureCoding;\n{\n    return YES;\n}\n```\n\nNext up, we'll add methods to encode an instance of this object and package it into a `NSData` object to be able to send it over the multipeer connection:\n\n```objc\n- (NSData *)encodeAsNetworkData;\n{\n    NSMutableData *data = [NSMutableData data];\n    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];\n    archiver.requiresSecureCoding = YES;\n    [archiver encodeObject:self forKey:@\"command\"];\n    [archiver finishEncoding];\n    return data;\n}\n\n- (void)encodeWithCoder:(NSCoder *)coder;\n{\n    [coder encodeDouble:self.coordinate.latitude forKey:@\"coordinate.latitude\"];\n    [coder encodeDouble:self.coordinate.longitude forKey:@\"coordinate.longitude\"];\n    [coder encodeBool:self.stop forKey:@\"stop\"];\n    [coder encodeBool:self.takeoff forKey:@\"takeoff\"];\n    [coder encodeBool:self.reset forKey:@\"reset\"];\n}\n```\n\nNow we can easily send a control command with a few lines of code:\n\n```objc\nRemoteControlCommand *command = [RemoteControlCommand alloc] init];\ncommand.coordinate = self.location.coordinate;\nNSData *data = [command encodeAsNetworkData];\nNSError *error;\n[self.session sendData:data toPeers:self.session.connectedPeers withMode:MCSessionSendDataReliable error:&error];\n```\n    \nIn order for the receiving end to be able to decode the data, we're adding another class method to our `RemoteControlCommand` class:\n\n```objc\n+ (instancetype)commandFromNetworkData:(NSData *)data;\n{\n    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];\n    unarchiver.requiresSecureCoding = YES;\n    RemoteControlCommand *result = [unarchiver decodeObjectOfClass:self forKey:@\"command\"];\n    return result;\n}\n```\n\nLastly, we need to implement `initWithCoder:` so that the encoded object can get decoded from the data:\n\n```objc\n- (id)initWithCoder:(NSCoder *)coder;\n{\n    self = [super init];\n    if (self != nil) {\n        CLLocationCoordinate2D coordinate = {};\n        coordinate.latitude = [coder decodeDoubleForKey:@\"coordinate.latitude\"];\n        coordinate.longitude = [coder decodeDoubleForKey:@\"coordinate.longitude\"];\n        self.coordinate = coordinate;\n        self.stop = [coder decodeBoolForKey:@\"stop\"];\n        self.takeoff = [coder decodeBoolForKey:@\"takeoff\"];\n        self.reset = [coder decodeBoolForKey:@\"reset\"];\n    }\n    return self;\n}\n```\n\n## Tying It All Together\n\nNow that we have the multipeer connection in place and we can encode and decode the remote control commands, we're ready to actually send location coordinates or control commands over the air. For the sake of example, we will only look at transmitting coordinates, since it's exactly the same for the other commands.\n\nAs discussed in the [project overview](/issues/8-quadcopter/the-quadcopter-project/), this client app can either send its current geolocation, or alternatively, a position picked on a map, in order to make testing the drone navigation easier. For the first case, we just need to implement `CLLocationManager`'s delegate method `locationManager:didUpdateLocations:` and store the current location in a property:\n\n```objc\n- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations\n{\n    self.location = locations.lastObject;\n}\n```\n\nTo send the current location on a regular basis, we set up a timer:\n\n```objc\n- (void)startBroadcastingLocation\n{\n    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(broadcastLocation) userInfo:nil repeats:YES];\n}\n```\n\nAnd last but not least, the `broadcastLocation` method that's now getting called once per second will create a `RemoteControlCommand` object and send it off to connected peers:\n\n```objc\n- (void)broadcastLocation\n{\n    RemoteControlCommand *command = [RemoteControlCommand alloc] init];\n    command.coordinate = self.location.coordinate;\n    NSData *data = [command encodeAsNetworkData];\n    NSError *error;\n    [self.session sendData:data toPeers:self.session.connectedPeers withMode:MCSessionSendDataReliable error:&error];\n    if (error) {\n        NSLog(@\"Error transmitting location: %@\", error);\n    }\n}\n```\n    \nAnd that's pretty much it. Follow along the other articles about the [navigation app on the drone](/issues/8-quadcopter/the-quadcopter-navigator-app/) and the [Core Foundation networking APIs](/issues/8-quadcopter/communicating-with-the-quadcopter) used to communicate with the drone to see how the receiving end of these commands interacts with the drone and actually makes it fly!\n\n\n\n\n\n\n\n\n\n\n\n"
  },
  {
    "path": "2014-01-08-the-quadcopter-navigator-app.md",
    "content": "---\ntitle:  \"The Navigator App\"\ncategory: \"8\"\ndate: \"2014-01-08 09:00:00\"\ntags: article\nauthor:\n  - name: Chris Eidhof\n    url: https://twitter.com/chriseidhof\n---\n\nIn this article, we'll tie together all the different parts of our system and build the navigator app. This is the app that will run on the iPhone that's attached to our drone; you can check out the app [on GitHub](https://github.com/objcio/issue-8-quadcopter-navigator). Even though the app is meant to be used without direct interaction, during testing we made a small UI that showed us the drone's state and allowed us to perform commands manually.\n\n## High-Level Overview\n\nIn our app, we have a couple of classes:\n\n* The `DroneCommunicator` takes care of all the communication with the drone over UDP. This is all explained in [Daniel's article](/issues/8-quadcopter/communicating-with-the-quadcopter/). \n* The `RemoteClient` is the class that takes care of communicating with our remote client over Multipeer Connectivity. What happens on the client's side is explained in [Florian's](/issues/8-quadcopter/the-quadcopter-client-app/) article.\n* The `Navigator` takes a target location and calculates the direction we need to fly in, as well as the distance to the target.\n* The `DroneController` talks with the navigator and sends commands to the drone communicator based on the navigator's direction and distance.\n* The `ViewController` has a small UI, and takes care of setting up the other classes and connecting them. This last part could be done in a different class, but for our purposes, everything is simple enough to keep it in the view controller.\n\n## View Controller\n\nThe most important part of our view controller is the setup method. Here, we create a communicator, a navigator, a drone controller, and a remote client. In other words: we set up the whole stack needed for communicating with the drone and the client app and start the navigator:\n\n```objc\n- (void)setup\n{\n    self.communicator = [[DroneCommunicator alloc] init];\n    [self.communicator setupDefaults];\n\n    self.navigator = [[Navigator alloc] init];\n    self.droneController = [[DroneController alloc] initWithCommunicator:self.communicator navigator:self.navigator];\n    self.droneController.delegate = self;\n    self.remoteClient = [[RemoteClient alloc] init];\n    [self.remoteClient startBrowsing];\n    self.remoteClient.delegate = self;\n}\n```\n\nThe view controller also is the `RemoteClient`'s delegate. This means that whenever our client app sends a new location or land/reset/takeoff commands, we need to handle that. For example, when we receive a new location, we do the following:\n\n```objc\n- (void)remoteClient:(RemoteClient *)client didReceiveTargetLocation:(CLLocation *)location\n{\n    self.droneController.droneActivity = DroneActivityFlyToTarget;\n    self.navigator.targetLocation = location;\n}\n```\n\nThis makes sure the drone starts flying (as opposed to hovering) and updates the navigator's target location.\n\n## Navigator\n\nThe navigator is the class that, given a target location, calculates distance from the current location and the distance in which the drone should fly. To do this, we first need to start listening to core location events:\n\n```objc\n- (void)startCoreLocation\n{\n    self.locationManager = [[CLLocationManager alloc] init];\n    self.locationManager.delegate = self;\n    \n    self.locationManager.distanceFilter = kCLDistanceFilterNone;\n    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;\n    [self.locationManager startUpdatingLocation];\n    [self.locationManager startUpdatingHeading];\n}\n```\n\nIn our navigator, we will have two different directions: an absolute direction and a relative direction. The absolute direction is between two locations. For example, the absolute direction from Amsterdam to Berlin is almost straight east. The relative direction also takes our compass into account; given that we want to move from Amsterdam to Berlin, and we're looking to the east, our relative direction is zero. For rotating the drone, we will use the relative direction. If it's zero, we can fly straight ahead. If it's less than zero, we rotate to the right, and if it's larger than zero, we rotate to the left.\n\nTo calculate the absolute direction to our target, we created a helper method on `CLLocation` that calculates the direction between two locations:\n\n```objc\n- (OBJDirection *)directionToLocation:(CLLocation *)otherLocation;\n{\n    return [[OBJDirection alloc] initWithFromLocation:self toLocation:otherLocation];\n}\n```\n\nAs our drone can only fly very small distances (the battery is drained within 10 minutes), we can take a geometrical shortcut and pretend we are on a flat plane, instead of on the earth's surface:\n\n```objc\n- (double)heading;\n{\n    double y = self.toLocation.coordinate.longitude - self.fromLocation.coordinate.longitude;\n    double x = self.toLocation.coordinate.latitude - self.fromLocation.coordinate.latitude;\n    \n    double degree = radiansToDegrees(atan2(y, x));\n    return fmod(degree + 360., 360.);\n}\n```\n\nIn the navigator, we will get callbacks with the location and the heading, and we just store those two values in a property. For example, to calculate the distance in which we should fly, we take the absolute heading, subtract our current heading (this is the same thing as you see in the compass value), and clamp the result between -180 and 180. In case you're wondering why we're subtracting 90 as well, this is because we taped the iPhone to our drone at an angle of 90 degrees:\n\n```objc\n- (CLLocationDirection)directionDifferenceToTarget;\n{\n    CLLocationDirection result = (self.direction.heading - self.lastKnownSelfHeading.trueHeading - 90);\n    // Make sure the result is in the range -180 -> 180\n    result = fmod(result + 180. + 360., 360.) - 180.;\n    return result;\n}\n```\n\nThat's pretty much all our navigator does. Given the current location and heading, it calculates the distance to the target and the direction in which the drone should fly. We made both these properties observable.\n\n## Drone Controller\n\nThe drone controller is initialized with the navigator and the communicator, and based on the distance and direction, it sends commands to the drone. Because these commands need to be sent almost continuously, we create a timer:\n\n```objc\nself.updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.25\n                                                    target:self\n                                                  selector:@selector(updateTimerFired:)\n                                                  userInfo:nil\n                                                   repeats:YES];\n```\n\nWhen the timer fires, and when we're flying toward a target, we have to send the drone the appropriate commands. If we're close enough, we just hover. Otherwise, we rotate toward the target, and if we're headed roughly in the right direction, we fly forward as well:\n\n```objc\n- (void)updateDroneCommands;\n{\n    if (self.navigator.distanceToTarget < 1) {\n        self.droneActivity = DroneActivityHover;\n    } else {\n        static double const rotationSpeedScale = 0.01;\n        self.communicator.rotationSpeed = self.navigator.directionDifferenceToTarget * rotationSpeedScale;\n        BOOL roughlyInRightDirection = fabs(self.navigator.directionDifferenceToTarget) < 45.;\n        self.communicator.forwardSpeed = roughlyInRightDirection ? 0.2 : 0;\n    }\n}\n```\n\n## Remote Client\n\nThis is the class that takes care of the communication with our [client](/issue-8/the-quadcopter-client-app.html). We use the Multipeer Connectivity framework, which turned out to be very convenient. First, we need to create a session and a nearby service browser:\n\n```objc\n- (void)startBrowsing\n{\n    MCPeerID* peerId = [[MCPeerID alloc] initWithDisplayName:@\"Drone\"];\n\n    self.browser = [[MCNearbyServiceBrowser alloc] initWithPeer:peerId serviceType:@\"loc-broadcaster\"];\n    self.browser.delegate = self;\n    [self.browser startBrowsingForPeers];\n\n    self.session = [[MCSession alloc] initWithPeer:peerId];\n    self.session.delegate = self;\n}\n```\n\nIn our case, we don't care a single bit about security, and we always invite all the peers:\n    \n```objc\n- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info\n{\n    [browser invitePeer:peerID toSession:self.session withContext:nil timeout:0];\n}\n```\n\nWe need to implement all the methods of both `MCNearbyServiceBrowserDelegate` and `MCSessionDelegate`, otherwise the app crashes. The only method where we do something is `session:didReceiveData:fromPeer:`. We parse the commands that our peer sends us and call the appropriate delegate methods. In our simple app, the view controller is the delegate, and when we receive a new location, we update the navigator. This will make the drone fly toward that new location.\n\n## Conclusion\n\nThis article describes the simple app. Originally, we put most of the code in the app delegate and in our view controller. This proved to be easiest for quick hacking and testing. However, as always, writing the code is the simple part, and reading the code is the hard part. Therefore, we refactored everything neatly into separate logical classes. \n\nWhen working with hardware, it can be quite time-consuming to test everything. For example, in the case of our quadcopter, it takes a while to start the device, send the commands, and run after the device when it's flying. Therefore, we tested as many things offline as we could. We also added a plethora of log statements, so that we could always debug things.\n"
  },
  {
    "path": "2014-01-08-the-quadcopter-project.md",
    "content": "---\ntitle:  \"The Project\"\ncategory: \"8\"\ndate: \"2014-01-08 11:00:00\"\ntags: article\nauthor:\n  - name: Florian Kugler\n    url: https://twitter.com/floriankugler\n---\n\n## The Initial Plan\n\nOur first idea was to do some sort of indoor navigation of the drone using Bluetooth beacons. With an iPhone attached to the drone, it should be possible to derive its current position using triangulation from a bunch of iBeacons positioned in the room. At least, so we thought...\n\nHowever, our first experiments of measuring the distance between the beacon and the iPhone by evaluating the signal strength were very disappointing. The signal seemed too noisy to determine the distance more than two or three meters (approximately six to 10 feet) away from any beacon with reasonable accuracy.\n\nWe ditched this plan and started to look for alternatives.\n\n\n## The Revised Plan\n\nSince we didn't want to let go of the core idea of attaching an iPhone to the drone and letting it autonomously do the navigation, we decided to give it a shot with plain old GPS. Of course, this meant that we needed to move outdoors for enough space and a good GPS signal. As it turned out, outdoor testing in Berlin during the winter is quite cold, and even light wind caused the drone to drift a lot...\n\nThe overall plan was to have one iPhone attached to the drone, connecting to it over WiFi. It measures its current location and orientation via Core Location and controls the drone toward some target coordinates.\n\nTo make it a bit more interesting, we added a second iPhone into the mix. This one connected to the iPhone on the drone via the new multipeer APIs, and sent its own location as the target location for the drone. The iPhone on the drone would then try to move the drone (and itself) toward the second iPhone. Additionally, take-off and landing commands could also be sent via this connection to the drone.\n\nThere is a running track close to Chris's house, and the idea of running on the track and having the drone follow us was quite tempting. Unfortunately, we didn't get quite so far; the cold temperatures and the wind outside equally affected the drone, the drone's battery lifetime, and our ability to hit the right keys on the keyboard after a short while. (It wasn't as bad for Chris -- we strapped his iPhone to the drone, so he was constantly chasing after the thing to make sure it didn't fly away with his phone!)\n\n\n### The Drone\n\nWe used a standard AR Drone 2.0 for this project. To mount the iPhone to the drone, we simply wrapped the phone in some bubble wrap and duct-taped it to the drone's body. Initially, we tried to attach it to the top of the drone, but this turned out to be too unstable. These drones have basically no payload capacity whatsoever, so even the light weight of the iPhone affected the flight stability significantly.\n\n![The iPhone mounted above the quadcopter](/images/issue-8/iphone-above.jpg)\n\nThe drone drifted off after takeoff a couple of times, so we decided to strap the phone to the bottom of the drone, in order to lower the center of mass. This turned out to work really well. Since the lowest point of the whole drone now was the phone beneath it, we used the widespread [zip tie mod](http://www.youtube.com/watch?v=wit3EmCo3Fs) to protect the phone in case the drone came crashing down somewhat harder (which probably was also a relief for the people living in the apartment below...).\n\n![The iPhone mounted beneath the quadcopter](/images/issue-8/iphone-below.jpg)\n\n&nbsp; \n\n\n### The Navigator App\n\nAs mentioned above, the iPhone attached to the drone is connected via WiFi to the drone itself. Over this connection we can send navigation commands via a UDP API. This all feels a bit obscure, but once we figured out the basics, it worked pretty well. Daniel goes more into detail in [his article](/issues/8-quadcopter/communicating-with-the-quadcopter/) of how we used the Core Foundation networking classes to get this to work.\n\nAlong with the actual communication between the phone and the drone, the navigator app also has to deal with the navigation part. It uses Core Location to measure its current position and orientation and then calculates the distance to the target. More importantly, it also determines the angular deviation of its current orientation to the target. You can read more about how this was done in [Chris's article](/issues/8-quadcopter/the-quadcopter-navigator-app/).\n\nLastly, the navigator app has to connect to the client app via multipeer and receive some basic control commands and the target location for the navigation of the drone.\n\n\n### The Client App\n\nThe client app's only job is to transmit the target location coordinates to the phone attached to the drone, and to send basic commands like takeoff and land. It advertises itself for the multipeer connection and simply broadcasts its location to all connected peers.\n\n![Screenshot of the client app](/images/issue-8/client-app.jpg)\n\nSince we wanted to have a way to test the whole setup without running around too much, and since we also wanted to stay indoors, we added two different modes to this app. The first mode simply transmits the center location of a map view as its current location. This way, we could pan around the map and simulate changing target locations. The other mode transmits the phone's real location as reported by Core Location.\n\nWe ended up using only the first mode in our short test flights due to time constraints and the very uncomfortable weather conditions outside. Therefore, our idea of the drone chasing somebody around the running track unfortunately didn't work out. \n\nStill, it was a fun project and we got to experiment with some interesting APIs. Check out the subsequent articles about [Core Foundation networking](/issues/8-quadcopter/communicating-with-the-quadcopter/), the [navigator app](/issues/8-quadcopter/the-quadcopter-navigator-app/), and the [client app](/issues/8-quadcopter/the-quadcopter-client-app) for more details.\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
  },
  {
    "path": "2014-02-10-editorial.md",
    "content": "---\r\ntitle:  \"Editorial\"\r\ncategory: \"9\"\r\ndate: \"2014-02-10 12:00:00\"\r\ntags: editorial\r\n---\r\n\r\nWelcome to objc.io issue #9!\r\n\r\nThis month's issue is all about strings. \r\n\r\nStrings are everywhere in all apps. We use them all the time. Localization of strings and the complexity of Unicode are all but trivial, and we hope to shed some light on these topics. We give you an overview of Unicode itself, cover best practices, discuss string localization, explain string parsing, and touch upon string drawing. These topics are relevant to every iOS and OS X developer, and hopefully we can provide you with a few small gems that'll ease your day-to-day work.\r\n\r\nA small update on the Newsstand front: the subscription revenue from our [objc.io Newsstand app](https://itunes.apple.com/de/app/objc.io/id683718429) has now covered the costs we have incurred since we launched [the first issue](/issues/1-view-controllers) last June. We have some exciting plans for the not-too-distant future, so keep reading the editorial to hear the latest.\n\r\nAll the best from Berlin,\r\n\r\nChris, Daniel, and Florian.\r\n"
  },
  {
    "path": "2014-02-10-string-localization.md",
    "content": "---\ntitle:  \"String Localization\"\ncategory: \"9\"\ndate: \"2014-02-10 09:00:00\"\ntags: article\nauthor:\n  - name: Florian Kugler\n    url: https://twitter.com/floriankugler\n---\n\nLocalizing apps into multiple languages comes with a variety of different tasks. Since this issue is all about strings, we're going to take a look at the topic of string localization. String localization comes in two different flavors: strings in code and strings in nib files and storyboards. We're going to focus on strings in code in this article.\n\n\n## NSLocalizedString\n\nAt the heart of string localization is the macro `NSLocalizedString`. There are three more lesser-known variants of it: `NSLocalizedStringFromTable`, `NSLocalizedStringFromTableInBundle`, and `NSLocalizedStringWithDefaultValue`. All of them use `NSBundle`'s `localizedStringForKey:value:table:` method to do the heavy lifting.\n\nThe point of using those macros is twofold. First off, it makes the code easier to read. It's a lot shorter than `NSBundle`'s `localizedStringForKey:value:table:`. Second, these macros get picked up by the [`genstrings`](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/genstrings.1.html) tool that creates strings files you can then translate. It parses `.c` and `.m` source files and generates `.strings` files with one entry per unique localizable string key.\n\nTo run `genstrings` over all `.m` files in your project, you can invoke it, for example, like this:\n\n```\nfind . -name *.m | xargs genstrings -o en.lproj\n```\n\nThe `-o` argument specifies the output directory where the `.strings` file will be written. By default, the strings file will be called `Localizable.strings`. Beware that `genstrings` is going to overwrite existing strings files. Specifying the `-a` option tells it to append to existing files rather than to overwrite them.\n\nIn general though, you probably want to generate the strings files in another directory and then merge them with existing files using your favorite merge tool, in order to preserve existing entries that are already translated.\n\nStrings files are very simple text files mapping keys to values:\n\n```\n/* Insert new contact button */\n\"contact-editor.insert-new-contact-button\" = \"Insert contact\";\n/* Delete contact button */\n\"contact-editor.delete-contact-button\" = \"Delete contact\";\n```\n\nYou can do more complicated things like including format placeholders in localizable strings, but we'll talk about this later. \n\nBy the way: strings files can now be [saved as UTF-8 files](http://gigliwood.com/blog/to-hell-with-utf-16-strings.html), because Xcode will convert them to UTF-16 during the build process.\n\n\n### What's a Localizable String?\n\nGenerally, all strings you want to show to the user in one form or another have to be localized. These can be simple labels or button titles, or more complex strings that are constructed at runtime from format strings and data. \n\nWhen localizing strings, it is important to define one localizable string per instance, due to grammatical rules. For example, if you have an app where you need to show strings like \"Paul invited you\" and \"You invited Paul,\" it might be tempting to just localize the string \"%@ invited %@\" and then insert the localized string for \"you\" in the appropriate place.\n\nThis seems to work just fine in English, but it's important to remember that these kinds of tricks that work in your language will almost always fail in other languages. In German, for example, \"Paul invited you\" would be translated as \"Paul hat dich eingeladen,\" whereas \"You invited Paul\" would be \"Du hast Paul eingeladen\".\n\nThe correct approach would be to define two localizable strings: \"%@ invited you\" and \"You invited %@.\" Only then can translators account for the specific grammar rules of each language.\n\nNever dissect sentences into parts, but always keep them together as one localizable string. And even if a second sentence appears to be constructed the same way in your language, create a second string if the grammatical structure is not exactly the same.\n\n\n### String Key Best Practices\n\nWhen using the `NSLocalizedString` macro, the first argument you have to specify is a key for this particular string. You'll often see that developers simply use the term in their base language as key. While this might be convenient in the beginning, it is actually a really bad idea and can lead to very bad localizations.\n\nEach key is unique within a localizable strings file. Hence, whatever term is unique in your language can only have one translation in every other language as well. This quickly fails because what's expressed using one term in one language often requires different terms in another language.\n\nFor example consider the term \"run.\" In English, this term can refer to the noun \"run\" as well as to the verb \"to run.\" Both of these will probably translate differently. And even both of these (noun and verb) might translate differently based on context.\n\nYou can imagine a fitness app that uses this term in different places with different meanings. However, if you localize it as:\n\n```objc\nNSLocalizedString(@\"Run\", nil)\n```\n\nyou will end up with only one entry in the strings file, regardless of whether you specify different comments or no comment at all as the second argument. Let's consider the German translation again: \"run\" in the sense of \"the run\" or \"a run,\" would be translated as \"Lauf,\" whereas \"run\" in the sense of \"to run,\" would be translated as \"laufen,\" or maybe something entirely different like \"loslaufen,\" \"Los geht's,\" or whatever sounds good in the specific context.\n\nGood localizable string keys have to fulfill two requirements: first, they must be unique for each context they're used in, and second, they must stick out if we forgot to translate something.\n\nWe recommend using a name-spaced approach like this:\n\n```objc\nNSLocalizedString(@\"activity-profile.title.the-run\", nil)\nNSLocalizedString(@\"home.button.start-run\", nil)\n```\n\nBy defining the keys like that, you can create a nice separation between different parts of the app and immediately provide some context within the key, like specifying that a certain string is used as a title or as a button. We're omitting the comments in this example for the sake of brevity, but you should use them if the key does not provide enough context. Be sure to only use [ASCII](http://en.wikipedia.org/wiki/ASCII) characters in string keys.\n\n\n### Splitting Up the String File\n\nAs we mentioned in the beginning, `NSLocalizedString` has a few siblings that allow for more control of string localization. `NSLocalizedStringFromTable` takes the key, the table, and the comment as arguments. The table argument refers to the string table that should be used for this localized string. `genstrings` will create one strings file per table identifier with the file name `<table>.strings`.\n\nThis way, you can split up your strings files into several smaller files. This can be very helpful if you're working on a large project or in a bigger team, and it can also make merging regenerated strings files with their existing counterparts easier.\n\nInstead of calling:\n\n```objc\nNSLocalizedStringFromTable(@\"home.button.start-run\", @\"ActivityTracker\", @\"some comment..\")\n```\n\neverywhere, you can make your life a bit easier by defining your own custom string localization functions:\n\n```objc\nstatic NSString * LocalizedActivityTrackerString(NSString *key, NSString *comment) {\n    return [[NSBundle mainBundle] localizedStringForKey:key value:key table:@\"ActivityTracker\"];\n}\n```\n\nIn order to generate the strings file for all usages of this function, you have to call `genstrings` with the `-s` option:\n\n```\nfind . -name *.m | xargs genstrings -o en.lproj -s LocalizedActivityTrackerString\n```\n\nThe `-s` argument specifies the base name of the localization functions. The previous call will also pick up the functions called `<base name>FromTable`, `<base name>FromTableInBundle`, and `<base name>WithDefaultValue`, if you choose to define and use those.\n\n<a name=\"localized-format-strings\"> </a>\n### Localized Format Strings\n\nOften we have to localize strings that contain some data that can only be inserted at runtime. To achieve this, we can use format strings, and Foundation comes with some gems to make this feature really powerful. (See [Daniel's article](/issues/9-strings/working-with-strings/) for more details on format strings.)\n\nA simple example would be to display a string like \"Run 1 out of 3 completed.\" We would build the string like this:\n\n```objc\nNSString *localizedString = NSLocalizedString(@\"activity-profile.label.run %lu out of %lu completed\", nil);\nself.label.text = [NSString localizedStringWithFormat:localizedString, completedRuns, totalRuns];\n```\n\nOften translations will need to reorder those format specifiers in order to construct a grammatically correct sentence. Luckily, this can be done easily in the strings file:\n\n```\n\"activity-profile.label.run %lu out of %lu completed\" = \"Von %2$lu Läufen hast du %1$lu absolviert\";\n```\n\nAdmittedly, this German translation is actually not very good -- I just made it up to demonstrate the reordering of format specifiers...\n\nIf you need simple localization of integers or floats, you can use the `+localizedStringWithFormat:` variant. For more advanced cases of number formatting, you should use `NSNumberFormatter` though; more on that later.\n\n\n\n#### Plural and Gender Support\n\nAs of OS X 10.9 and iOS 7, localized format strings can do much cooler stuff than just simple replacement of format specifiers with numbers or strings. The problem Apple tries to address is that different languages handle plural and gender forms very differently.\n\nLet's have a look at the previous example again: @\"%lu out of %lu runs completed.\" This works as long as there is always more than one run to be completed. So we would have to define two different strings for the case of n = 1 and n > 1:\n\n```objc\n@\"%lu out of one run completed\"\n@\"%lu out of %lu runs completed\"\n```\n\nAgain, that works fine in English, but unfortunately it will fail for many other languages. For example, Hebrew has three different forms: one for when n = 1 or n is a multiple of ten, another one for when n = 2, and a third for all other cases. In Croatian, there's one form for numbers that end with 1 (such as 1, 11, 21, ...): \"31 od 32 staze završene\" as opposed to \"5 od 8 staza završene\" (\"staze\" vs. \"staza\"). Many languages also have special rules for non-integral numbers.\n\nTo see the whole scope of this problem, you really should take a look at this [Unicode Language Plural Rules](http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html) overview. It's just stunning.\n\nIn order to do this the correct way (from 10.9 and iOS 7 onward), we're going to refer to this localized string like this:\n\n```objc\n[NSString localizedStringWithFormat:NSLocalizedString(@\"activity-profile.label.%lu out of %lu runs completed\"), completedRuns, totalRuns];\n```\n\nThen we create a `.stringsdict` file next to the normal `.strings` file. So if the strings file is called `Localizable.strings`, we need to create a `Localizable.stringsdict` file. It's important to still have a `.strings` file around, even if it is just an empty file. The `.stringsdict` file is a property list (`plist`) file that is more complex than a normal string file, but in return it allows correct plural handling in all languages without any plural logic in our code.\n\nAn example looks like this:\n\n```xml\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n    <key>activity-profile.label.%lu out of %lu runs completed</key>\n    <dict>\n        <key>NSStringLocalizedFormatKey</key>\n        <string>%lu out of %#@lu_total_runs@ completed</string>\n        <key>lu_total_runs</key>\n        <dict>\n            <key>NSStringFormatSpecTypeKey</key>\n            <string>NSStringPluralRuleType</string>\n            <key>NSStringFormatValueTypeKey</key>\n            <string>lu</string>\n            <key>one</key>\n            <string>%lu run</string>\n            <key>other</key>\n            <string>%lu runs</string>\n        </dict>\n    </dict>\n</dict>\n</plist>\n```\n\nThe keys of the top-level dictionary are simply the keys of the localized strings. Each dictionary then contains the `NSStringLocalizedFormatKey` entry that specifies the format string to be used for this localization. In order to substitute different values for different numbers, the format string syntax has been extended. So we can write something like `%#@lu_total_runs@` and then define a dictionary for the `lu_total_runs` key. Within this dictionary, we specify that this is a plural rule (setting `NSStringFormatSpecTypeKey` to `NSStringPluralRuleType`), designate the format specifier to use (in this case `lu`), and define the different values for the different plural variants. We can choose from \"zero,\" \"one,\" \"two,\" \"few,\" \"many,\" and \"others.\"\n\nThis is an extremely powerful feature. It can not only be used to account for the crazy amount of differences in plural forms for different languages, but also to use different wording for different numbers. \n\nBut we can go even further than that and define recursive rules. To make the output in our example nicer, there are a few cases we could provide customized strings for:\n\n```\nCompleted runs    Total Runs    Output\n------------------------------------------------------------------\n0                 0+            No runs completed yet\n1                 1             One run completed\n1                 2+            One of x runs completed\n2+                2+            x of y runs completed\n```\n\nWe can achieve this using the string dictionary file without writing a single line of code. The strings dictionary for this example looks like this:\n\n```xml\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n    <key>scope.%lu out of %lu runs</key>\n    <dict>\n        <key>NSStringLocalizedFormatKey</key>\n        <string>%1$#@lu_completed_runs@</string>\n        <key>lu_completed_runs</key>\n        <dict>\n            <key>NSStringFormatSpecTypeKey</key>\n            <string>NSStringPluralRuleType</string>\n            <key>NSStringFormatValueTypeKey</key>\n            <string>lu</string>\n            <key>zero</key>\n            <string>No runs completed yet</string>\n            <key>one</key>\n            <string>One %2$#@lu_total_runs@</string>\n            <key>other</key>\n            <string>%lu %2$#@lu_total_runs@</string>\n        </dict>\n        <key>lu_total_runs</key>\n        <dict>\n            <key>NSStringFormatSpecTypeKey</key>\n            <string>NSStringPluralRuleType</string>\n            <key>NSStringFormatValueTypeKey</key>\n            <string>lu</string>\n            <key>one</key>\n            <string>run completed</string>\n            <key>other</key>\n            <string>of %lu runs completed</string>\n        </dict>\n    </dict>\n</dict>\n</plist>\n```\n\nStrings returned from `localizedStringForKey:value:table:` that were instantiated from a `.stringsdict` entry are proxy objects carrying the additional information contained in the strings dictionary file. This information is preserved through `copy` and `mutableCopy` calls. However, once you mutate such a string object, the additional localization information is lost. Please see the [Foundation release notes of OS X 10.9](https://developer.apple.com/library/Mac/releasenotes/Foundation/RN-Foundation/index.html) for further details.\n\n\n## Upper and Lower Casing\n\nWhenever you need to uppercase or lowercase a string that should be presented to the user, you should use the localized versions of those `NSString` methods: `lowercaseStringWithLocale:` and `uppercaseStringWithLocale:`.\n\nWhen using these methods you have to pass the [locale](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSLocale_Class/Reference/Reference.html) that should be used to perform the uppercasing or lowercasing in a localized fashion. When using one of the `NSLocalizedString` macros, we don't have to worry about this, since it will automatically do the right thing, also in terms of falling back to the default language if the user's preferred language is not available.\n\nTo ensure a consistent appearance of the user interface, it can be a good idea to use the locale that is used to localize the rest of the interface. Please see the section below about [choosing the right locale](#choosing-the-right-locale).\n\n\n## Localized File Path Names\n\nIn general, you should always use `NSURL` to represent file paths. Once you do this, it's very easy to retrieve the localized file name:\n\n```objc\nNSURL *url = [NSURL fileURLWithPath:@\"/Applications/System Preferences.app\"];\nNSString *name;\n[url getResourceValue:&name forKey:NSURLLocalizedTypeDescriptionKey error:NULL];\nNSLog(@\"localized name: %@\", name);\n\n// output: System Preferences.app\n```\n\nThat's exactly what we'd expect on an English system, but once we switch, for example, to Arabic, System Preferences now is called تفضيلات النظام.app.\n\nRetrieving a file's localized name like this also respects the user's Finder setting of whether the file extension should be hidden or not. If you need to show which type the file is, you can use ask for the `NSURLLocalizedTypeDescriptionKey` in the same way.\n\nLocalized file names are only for user interface purposes, and cannot be used to actually access the file resource. Please see [Daniel's article about common string patterns](/issues/9-strings/working-with-strings/) to read more about working with file paths.\n\n\n## Formatters\n\nThere is a huge variety of how numbers and dates are formatted in different languages. Luckily Apple has already done the heavy lifting for us, so that we only have to remember to always use the [`NSNumberFormatter`](https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/Classes/NSNumberFormatter_Class/Reference/Reference.html) or [`NSDateFormatter`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html) classes whenever we want to display a number or date in the user interface.\n\nKeep in mind that number and date formatters are mutable objects and not thread-safe. \n\n### Formatting Numbers\n\nNumber formatter objects are highly configurable, but mostly you will want to use one of the predefined number styles. After all, the main advantage of using a number formatter is not having to worry about the specifics of a certain language anymore.\n\nUsing the different number styles on my machine, which has its number format set to German, produces the following result for the number `2.5`:\n\n```\nNumber Style                        Result for German     Result for Arabic\n---------------------------------------------------------------------------\nNSNumberFormatterNoStyle            2                          ٢\nNSNumberFormatterDecimalStyle       2,5                        ٢٫٥\nNSNumberFormatterCurrencyStyle      2,50 €                     ٢٫٥٠٠ د.أ.‏\nNSNumberFormatterScientificStyle    2,5E0                      ٢٫٥اس٠\nNSNumberFormatterPercentStyle       250 %                      ٢٥٠٪\nNSNumberFormatterSpellOutStyle      zwei Komma fünf            إثنان فاصل خمسة\n```\n\nThere is a nice thing in the output of the number formatter you cannot see in this table: in the currency and percent number styles, it inserts not a normal space before the currency or percent symbol, but a non-breaking one. This way the number will never be separated from its symbol by a line break. (And isn't the spell-out style cool?)\n\nBy default, a number formatter will use the system setting to determine the locale. As we already mentioned with uppercasing and lowercasing above, it is important to configure the number formatter with the right locale to match the rest of the user interface. Read more on this [below](#choosing-the-right-locale).\n\n\n### Formatting Dates\n\nAs with numbers, date formatting is very complex and we really should hand this job over to `NSDateFormatter`. With date formatters, you can choose from a variety of different [date and time styles](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSDateFormatterStyle) that will come out correctly for all locales. Again, make sure to [choose the right locale](#choosing-the-right-locale) matching the rest of the interface.\n\nSometimes you might want to show a date in a way that's not possible with the default date and time styles of `NSDateFormatter`. Instead of using a simple format string (which will most likely yield wrong results in many other languages), `NSDateFormatter` provides the `+dateFormatFromTemplate:options:locale:` method for this purpose.\n\nFor example, if you want to show only the day of the month and the short form of the month, there is no default style that provides this. So let's build our own formatter:\n\n```objc\nNSString *format = [NSDateFormatter dateFormatFromTemplate:@\"dMMM\" \n                                                   options:0\n                                                    locale:locale];\nNSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];\n[dateFormatter setDateFormat:format];\n\nNSString *output = [dateFormatter stringFromDate:[NSDate date]];\nNSLog(@\"Today's day and month: %@\", output);\n```\n\nThe big advantage of this method compared to using plain format strings is that the result will still be correct in different locales. For example, in US English we would expect the date to be formatted as \"Feb 2,\" whereas in German it should be \"2. Feb.\" `dateFormatFromTemplate:options:locale:` returns the correct format string based on the template we specify and the locale argument. For US English it returns \"MMM d,\" and for German \"d. MMM.\"\n\nFor a full reference of the format specifiers that can be used in the template string, please refer to the date format patterns in the [Unicode locale data markup language](http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns) documentation.\n\n\n### Cache Formatter Objects\n\nSince creating a formatter object is a rather expensive operation, you should cache it for future use:\n\n```objc\nstatic NSDateFormatter *formatter;\n\n- (NSString *)displayDate:(NSDate *)date\n{\n    if (!formatter) {\n        formatter = [[NSDateFormatter alloc] init];\n        formatter.dateStyle = NSDateFormatterShortStyle;\n        formatter.timeStyle = NSDateFormatterNoStyle;\n    }\n    return [formatter stringFromDate:date];\n}\n```\n\nThere's one gotcha with this though: we need to invalidate this cached formatter object if the user changes his or her locale. In order to do this, we have to register for the `NSCurrentLocaleDidChangeNotification`:\n\n```objc\nstatic NSDateFormatter *formatter;\n\n- (void)setup\n{\n    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];\n    [notificationCenter addObserver:self\n                           selector:@selector(localeDidChange)\n                               name:NSCurrentLocaleDidChangeNotification\n                             object:nil];\n}\n\n- (NSString *)displayDate:(NSDate *)date\n{\n    if (!formatter) {\n        formatter = [[NSDateFormatter alloc] init];\n        formatter.dateStyle = NSDateFormatterShortStyle;\n        formatter.timeStyle = NSDateFormatterNoStyle;\n    }\n    return [formatter stringFromDate:date];\n}\n\n- (void)localeDidChange\n{\n    formatter = nil;\n}\n\n- (void)dealloc\n{\n    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];\n    [notificationCenter removeObserver:self\n                                  name:NSCurrentLocaleDidChangeNotification\n                                object:nil];\n}\n```\n\nInteresting side note from Apple's [data formatting guide](https://developer.apple.com/library/mac/documentation/cocoa/conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW10):\n\n> In theory you could use an auto-updating locale (autoupdatingCurrentLocale) to create a locale that automatically accounts for changes in the user's locale settings. In practice this currently does not work with date formatters.\n\nSo we have to use the locale change notification for now. That's quite a bit of code for \"just\" formatting dates. But if you use the formatter object very often, it can be worth the effort. As always, measure and improve.\n\nAlso, remember that formatters are not thread-safe. According to Apple's documentation, you can use them from multiple threads, but you must not mutate them from multiple threads. If you would want to consolidate all formatters you use in a central object in order to make their invalidation on locale change easier, you would have to make sure that you create and update them only on one queue. For example, you could use a concurrent queue and `dispatch_sync` onto it to get the formatters, and use `dispatch_barrier_async` to update the formatters when the locale changes.\n\n\n### Parsing User Input\n\nNumber and date formatters cannot only generate localized strings from number or date objects respectively -- they can also work the other way round. Whenever you need to handle user input of numbers or dates, you should use the appropriate formatter class to parse it. That's the only way to make sure that the input will be parsed correctly according to the user's current locale.\n\n\n#### Parsing Machine Input\n\nWhile formatters are great to parse user input, they're not the best choice to parse machine input where you know the format up front. The power of date and number formatters that are required to parse user input correctly in all locales comes with a performance cost.\n\nFor example, if you receive a lot of date strings from a server and you have to convert them into date objects, a date formatter is not the best tool for the job. As per Apple's [date formatting guide](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1) it's much more efficient to use the UNIX `strptime_l(3)` function for fixed-format, non-localized dates:\n\n```objc\nstruct tm sometime;\nconst char *formatString = \"%Y-%m-%d %H:%M:%S %z\";\n(void) strptime_l(\"2014-02-07 12:00:00 -0700\", formatString, &sometime, NULL);\nNSLog(@\"Issue #9 appeared on %@\", [NSDate dateWithTimeIntervalSince1970: mktime(&sometime)]);\n// Output: Issue #9 appeared on 2014-02-07 12:00:00 -0700\n```\n\nSince even `strptime_l` is aware of the user's locale, make sure to pass in `NULL` as the last parameter to select the standard POSIX locale. For the available format specifiers, please refer to the [`strftime` man page](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/strftime.3.html#//apple_ref/doc/man/3/strftime).\n\n\n## Debugging Localized Strings\n\nMaking sure that everything looks right and works correctly becomes increasingly more difficult the more languages you support in your app. However, there are a few helpful user default options and tools that can help you with that.\n\nYou can use the `NSDoubleLocalizedStrings`, `AppleTextDirection`, and `NSForceRightToLeftWritingDirection` options to make sure your layout doesn't break with longer strings or right-to-left languages. `NSShowNonLocalizedStrings` and `NSShowNonLocalizableStrings` help with finding strings where a localization is missing or which have not been specified using the string localization macros at all. (All these are user defaults options that can be set programmatically or as launch argument in Xcode's Scheme Editor, e.g. `-NSShowNonLocalizedStrings YES`.)\n\nFurthermore, there are two user defaults options that control the preferred language and the locale: `AppleLanguages` and `AppleLocale`. You can use those to start an app using a different preferred language or locale than current system settings, which can save you a lot of switching forth and back during testing. `AppleLanguages` takes an array of [ISO-639](http://www.loc.gov/standards/iso639-2/php/code_list.php) language codes, for example:\n\n```\n-AppleLanguages (de, fr, en)\n```\n\n`AppleLocale` takes a locale identifier as argument as defined by the [International Components for Unicode](http://userguide.icu-project.org/locale), for example:\n\n```\n-AppleLocale en_US\n```\n\nor\n\n```\n-AppleLocale en_GR\n```\n\nIf your translated strings are not showing up, you might want to run the [plutil](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/plutil.1.html) command with its `-lint` option to check your string files for syntax errors. For example, if you forget a semicolon at the end of a line, it will warn you like this:\n\n```\n$ plutil Localizable.strings\n2014-02-04 15:22:40.395 plutil[92263:507] CFPropertyListCreateFromXMLData(): Old-style plist parser: missing semicolon in dictionary on line 6. Parsing will be abandoned. Break on _CFPropertyListMissingSemicolon to debug.\nLocalizable.strings: Unexpected character / at line 1\n```\n\nOnce we fix this error, it signals to us that all is well:\n\n```\n$ plutil Localizable.strings\nLocalizable.strings: OK\n```\n\nAlthough it's not about debugging, there's another really helpful trick for multi-language apps: you can automatically generate your app screenshots (on iOS) for multiple languages. Since the app can be scripted using UIAutomation and the language can be set on launch using the `AppleLanguages` user defaults setting, the whole process can be automized. Check [this project on GitHub](https://github.com/jonathanpenn/ui-screen-shooter) for more details.\n\n\n\n<a name=\"choosing-the-right-locale\"> </a>\n\n## Choosing the Right Locale\n\nWhen using date and number formatters or methods like `-[NSString lowercaseStringWithLocale:]`, it's important that you use the correct locale. If you want to use the user's systemwide preferred language, you can retrieve the corresponding locale with `[NSLocale currentLocale]`. However, be aware that this might be a different locale than the one your app is running in.\n\nLet's say the user's system is set to Chinese, but your app only supports English, German, Spanish, and French. In this case, the string localization will fall back to the default language, e.g. English. If you now use `[NSLocale currentLocale]` or formatter class methods that don't specify any locale, like `+[NSNumberFormatter localizedStringFromNumber:numberStyle:]`, your data will formatted according to the Chinese locale, while the rest of the interface will be in English. \n\nUltimately it comes down to your judgment of what makes the most sense for a specific use case, but you might want the app's interface to be localized consistently in some cases. In order to retrieve the locale your app is currently using instead of the user's systemwide locale, we have to ask the main bundle for its preferred language:\n\n```objc\nNSString *localization = [NSBundle mainBundle].preferredLocalizations.firstObject;\nNSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localization];\n```\n\nWith this locale at hand, we can, for example, localize a date in a way that matches the current string localization of the interface:\n\n```objc\nNSDateFormatter *formatter = [[NSDateFormatter alloc] init];\nformatter.locale = locale;\nformatter.dateStyle = NSDateFormatterShortStyle;\nformatter.timeStyle = NSDateFormatterNoStyle;\nNSString *localizedDate = [formatter stringFromDate:[NSDate date]];\n```\n\n&nbsp;\n\n\n## Conclusion\n\nWhen localizing strings, never assume that anything that works in your native language will work in other languages as well. The frameworks provide a lot of powerful tools to abstract away the complexities of different languages; we just have to use them consistently. It's a little bit of extra work, but it will pay off big time once you start localizing your app into multiple languages.\n\n"
  },
  {
    "path": "2014-02-10-string-parsing.md",
    "content": "---\ntitle:  \"String Parsing\"\ncategory: \"9\"\ndate: \"2014-02-10 08:00:00\"\ntags: article\nauthor:\n  - name: Chris Eidhof\n    url: https://twitter.com/chriseidhof\n---\n\nIn almost every computer program, we have to parse strings. Sometimes these strings follow a very simple format, and sometimes they are very complicated. We will look at multiple ways to convert these strings into something we can reason and work with. We'll discuss regular expressions and scanners and parsers, as well as when to apply them.\n\n### Regular vs. Context-Free Grammars\n\nFirst, a little bit of background theory: if we parse a string, we interpret the string in a specific *language*. For example, if we want to parse the string `@\"42\"` to a number, we interpret the string in the language of natural numbers. A grammar is used to describe a *language*: it is the collection of rules according to which a string can be interpreted. In the case of natural numbers, there can be only one rule: a string can be interpreted, if and only if it is a sequence of digits. This language could be described using some standard C functions, or using a regular expression. If we can describe a language using regular expressions, it is said to have a *regular grammar*.\n\nIf we consider expressions like \"1 + 2 * 3\", parsing becomes a bit more difficult. For these expressions, the language can be described using an inductive grammar. In other words, this is a grammar that has rules that refer to themselves, possibly even in a recursive way. For example, to recognize this language, we could have three rules:\n\n1. Any number is part of the language\n1. If `x` is a member of the language, and `y` is a member, then `x + y` is also a member\n1. If `x` is a member of the language, and `y` is a member, then `x * y` is also a member\n\nThe languages that can be described using these kinds of grammars are called *context-free* grammars, or [CFG][1]. Note that they cannot be parsed using regular expressions (although some regular expression implementations, such as PCRE, can express more than regular grammars). The classical example of languages that can be parsed with CFG but not with regular expressions is that of matching [parentheses][2].\n\n[1]: http://en.wikipedia.org/wiki/Chomsky_hierarchy\n[2]: http://en.wikipedia.org/wiki/Context-free_grammar#Well-formed_parentheses\n\n\nSome examples of things that can be parsed using regular languages are numbers, strings, and dates. This means that you can use a regular expression (or a similar technique) to parse them. \n\nSome things that cannot be parsed using regular languages, are [e-mail][3] [addresses][4], JSON, XML, or most programming languages. To parse these, we need an actual parser. Often times, these parsers are already written for us. Apple provides parsers for XML and JSON, so if we want to parse those, it's easiest to just use Apple's format.\n\n[3]: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html\n[4]: http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx/\n\n## Regular Expressions\n\nWhen you want to recognize simple languages, regular expressions are often the right tool for the job. However, they are also frequently misused for things like HTML parsing, where they are not a good fit. Let's suppose we have a file containing simple variable definitions for colors that designers can use to change the colors in your iPhone app. The format looks like this:\n\n```objc\nbackgroundColor = #ff0000\n```\n\nIf we want to parse a single line using this format, we can use a regular expression, like below. The `pattern` is the most important thing. If you don't know regular expressions, we will quickly go over what this means, but explaining regular expressions completely is way beyond the scope of this article. The first thing to look at is `\\\\w+`, which matches a word character (defined by `\\\\w`), one or more times (defined by the `+`). Then, to make sure we can later use the result of that match, we put it in parentheses, creating a *capture group*. Next, there's a literal space character, followed by an equals sign, another space character, and a pound. Then, we need to match six hexadecimal numbers. The `\\\\p{Hex_Digit}` matches a hexadecimal digit (`Hex_Digit` is a [unicode property name](http://en.wikipedia.org/wiki/Unicode_character_property#Hexadecimal_digits)). The modifier `{6}` means that we expect six of those characters, and again, we put it in parentheses to create a second capture group:\n\n```objc\nNSError *error = nil;\nNSString *pattern = @\"(\\\\w+) = #(\\\\p{Hex_Digit}{6})\";\nNSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern\n                                                                            options:0\n                                                                              error:&error];\nNSTextCheckingResult *result = [expression firstMatchInString:string \n                                                      options:0\n                                                        range:NSMakeRange(0, string.length)];\nNSString *key = [string substringWithRange:[result rangeAtIndex:1]];\nNSString *value = [string substringWithRange:[result rangeAtIndex:2]];\n```\n\nWe create a regular expression and ask the expression to match the string `string`. Then we extract the two groups captured by the parentheses by using `rangeAtIndex`. The entire regular expression is at index 0, the first capture group is at index 1, the second capture group is at index 2, and so on. Now `key` is `backgroundColor` and `value` is `ff0000`. This regular expression parses a single line; a next step would be to parse multiple lines and add some error checking. For example, the input:\n\n```objc\nbackgroundColor = #ff0000\ntextColor = #0000ff\n```\n\nshould produce the following dictionary: `@{@\"backgroundColor\": @\"ff0000\", @\"textColor\": @\"0000ff\"}`. The code to do that is simple.\nWe split the string into lines, iterate over them, and add them to our dictionary:\n\n```objc\nNSString *pattern = @\"(\\\\w+) = #([\\\\da-f]{6})\";\nNSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern\n                                                                            options:0 \n                                                                              error:NULL];\nNSArray *lines = [input componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];\nNSMutableDictionary *result = [NSMutableDictionary dictionary];\nfor (NSString *line in lines) {\n    NSTextCheckingResult *textCheckingResult = [expression firstMatchInString:line \n                                                                      options:0 \n                                                                        range:NSMakeRange(0, line.length)];\n    NSString* key = [line substringWithRange:[textCheckingResult rangeAtIndex:1]];\n    NSString* value = [line substringWithRange:[textCheckingResult rangeAtIndex:2]];\n    result[key] = value;\n}\nreturn result;\n```\n\nAs an aside: to separate the string into components, we could have also used `componentsSeparatedByString:` or enumerated them using `enumerateSubstringsInRange:options:usingBlock:` with the option `NSStringEnumerationByLines`.\n\nTo see if a line doesn't match (for example, if we accidentally forget one of the hexadecimal characters), we can check if `textCheckingResult` is nil, and throw an error:\n\n```objc\n if (!textCheckingResult) {\n     NSString* message = [NSString stringWithFormat:@\"Couldn't parse line: %@\", line]\n     NSDictionary *errorDetail = @{NSLocalizedDescriptionKey: message};\n     *error = [NSError errorWithDomain:MyErrorDomain code:FormatError userInfo:errorDetail];\n     return nil;\n }\n```\n\n## Scanners\n\nThere is a second way of taking this string and turning it into a dictionary, namely using scanners. Conveniently, Foundation provides us with `NSScanner`, which has an easy-to-use object-oriented API. First, we need to create a scanner:\n\n```objc\nNSScanner *scanner = [NSScanner scannerWithString:string];\n```\n\nBy default, a scanner skips all white space and newline characters. For our purposes, we don't want to skip newlines, only white space:\n\n```objc\nscanner.charactersToBeSkipped = [NSCharacterSet whitespaceCharacterSet];\n```\n\nThen, we define the set of hexadecimal characters. A lot of character sets are defined, but the hexadecimal set is not one of them:\n\n```objc\nNSCharacterSet *hexadecimalCharacterSet = \n  [NSCharacterSet characterSetWithCharactersInString:@\"0123456789abcdefABCDEF\"];\n```\n\nFirst, let's write a version without error checking. A scanner works like this: it takes a string and sets its cursor to 0, the beginning of the string. You tell it to scan something specific, like `[scanner scanString:@\"=\" intoString:NULL]`. The method returns `YES` if the scan succeeds, and increases the cursor value to just after the scanned part. The method `scanCharactersFromSet:intoString:` works in a similar way: it keeps scanning characters from the set, and puts the result into the string pointer given by the second argument. Note that we combine the different scanning calls with `&&`. This way, the right-hand side of the `&&` is only scanned if the left-hand side succeeded:\n\n```objc\nNSMutableDictionary *result = [NSMutableDictionary dictionary];\nwhile (!scanner.isAtEnd) {\n    NSString *key = nil;\n    NSString *value = nil;\n    NSCharacterSet *letters = [NSCharacterSet letterCharacterSet];\n    BOOL didScan = [scanner scanCharactersFromSet:letters intoString:&key] &&\n                   [scanner scanString:@\"=\" intoString:NULL] &&\n                   [scanner scanString:@\"#\" intoString:NULL] &&\n                   [scanner scanCharactersFromSet:hexadecimalCharacterSet intoString:&value] &&\n                   value.length == 6;\n    result[key] = value;\n    [scanner scanCharactersFromSet:[NSCharacterSet newlineCharacterSet] \n                        intoString:NULL]; // scan an optional newline\n}\nreturn result;\n```\n\nTo add error handling, we can write this code just after the `didScan` line. If scanning didn't succeed, we just return nil and set the `error` parameter accordingly. When parsing text, it's important to think about what you want to do in case a string is malformed, whether it is to crash, present the error to the user, try to recover from the error, etcetera: \n\n```objc\n    if (!didScan) {\n        NSString *message = [NSString stringWithFormat:@\"Couldn't parse: %u\", scanner.scanLocation];\n        NSDictionary *errorDetail = @{NSLocalizedDescriptionKey: message};\n        *error = [NSError errorWithDomain:MyErrorDomain code:FormatError userInfo:errorDetail];\n        return nil;\n    }\n```\n\nNote that C also provides you with scanner functions, such as `man 3 sscanf`. They follow a similar syntax to `printf`, but operate in the inverse order (parsing a string, rather than generating one).\n\n## Parsers\n\nWhat if our designers would also want to specify RGB colors, like this: `(100,0,255)`? We would have to make our method for parsing colors a bit smarter. As a matter of fact, after we're done here, we will have written a very basic parser.\n\nFirst, we will add a couple of more methods to our class, and store our scanner in a property. The first method we add is called `scanColor:`, and its job is to scan either a hex color (like `ff0000`) or an RGB tuple (e.g. `(255,0,0)`):\n\n```objc\n- (NSDictionary *)parse:(NSString *)string error:(NSError **)error\n{\n    self.scanner = [NSScanner scannerWithString:string];\n    self.scanner.charactersToBeSkipped = [NSCharacterSet whitespaceCharacterSet];\n\n    NSMutableDictionary *result = [NSMutableDictionary dictionary];\n    NSCharacterSet *letters = [NSCharacterSet letterCharacterSet]\n    while (!self.scanner.isAtEnd) {\n        NSString *key = nil;\n        UIColor *value = nil;\n        BOOL didScan = [self.scanner scanCharactersFromSet:letters intoString:&key] &&\n                       [self.scanner scanString:@\"=\" intoString:NULL] &&\n                       [self scanColor:&value];\n        result[key] = value;\n        [self.scanner scanCharactersFromSet:[NSCharacterSet newlineCharacterSet] \n                                 intoString:NULL]; // scan an optional newline\n    }\n}\n```\n\nThe `scanColor:` method itself is very easy. First, it tries to scan a hex color, and if that doesn't work, it tries an RGB tuple:\n\n```objc\n- (BOOL)scanColor:(UIColor **)out\n{\n    return [self scanHexColorIntoColor:out] || [self scanTupleColorIntoColor:out];\n}\n```\n\nScanning a hex color is the same as before. The only difference is that we have now wrapped it in a method, and use the same pattern as `NSScanner` methods. It returns a `BOOL` indicating successful scanning, and stores the result in a pointer to a `UIColor`:\n\n```objc\n- (BOOL)scanHexColorIntoColor:(UIColor **)out\n{\n    NSCharacterSet *hexadecimalCharacterSet = \n       [NSCharacterSet characterSetWithCharactersInString:@\"0123456789abcdefABCDEF\"];\n    NSString *colorString = NULL;\n    if ([self.scanner scanString:@\"#\" intoString:NULL] &&\n        [self.scanner scanCharactersFromSet:hexadecimalCharacterSet \n                                 intoString:&colorString] &&\n        colorString.length == 6) {\n        *out = [UIColor colorWithHexString:colorString];\n        return YES;\n    }\n    return NO;\n}\n```\n\nScanning a tuple-based color is very similar; we have already looked at all the necessary methods for doing this. We scan tokens like `@\"(\"`, interspersed by the integer components. In production code, we would need some more error checking, to, for example, make sure that the integers are in the range `0-255`:\n\n```objc\n- (BOOL)scanTupleColorIntoColor:(UIColor **)out\n{\n    NSInteger red, green, blue = 0;\n    BOOL didScan = [self.scanner scanString:@\"(\" intoString:NULL]\n            && [self.scanner scanInteger:&red]\n            && [self.scanner scanString:@\",\" intoString:NULL]\n            && [self.scanner scanInteger:&green]\n            && [self.scanner scanString:@\",\" intoString:NULL]\n            && [self.scanner scanInteger:&blue]\n            && [self.scanner scanString:@\")\" intoString:NULL];\n    if (didScan) {\n        *out = [UIColor colorWithRed:(CGFloat)red/255. \n                               green:(CGFloat)green/255.\n                                blue:(CGFloat)blue/255. alpha:1];\n        return YES;\n    } else {\n        return NO;\n    }\n}\n```\n\nWhen we start to mix scanning things with logic--such as scanning multiple alternatives, and calling other methods--we are writing a parser. Parsers are a fascinating subject, and a very powerful tool in your arsenal. Once you know how to write a parser, you can invent small languages for anything: specifying style sheets, parsing constraints, querying your data model, describing business logic, and so on. An interesting book on this is Fowler's [Domain Specific Languages](http://martinfowler.com/books/dsl.html).\n\n## Tokenization\n\nWe have a very simple parser that can extract key/value pairs from a string (for example, coming from a file) and we can use those strings to generate `UIColor` objects. But we're not done yet. What if the designers want to specify more things? For example, suppose we have a different file that contains some layout constraints, in the following form:\n\n```objc\nmyView.left = otherView.right * 2 + 10\nviewController.view.centerX + myConstant <= self.view.centerX\n```\n\nHow could we parse this? It turns out that regular expressions are not the best way to do this.\n\nBefore we parse this, it's a good idea to do *tokenization*. This is the process that converts a string (a stream of characters) into a stream of tokens. For example, the result of scanning `myConstant = 100` might be `@[@\"myConstant\", @\"=\", @100]`. For most languages, tokenization removes white spaces and parses related characters into tokens. In our language, tokens could be identifiers (e.g. `myConstant` or `centerX`), operators (e.g. `.`, `+` or `=`), or numbers (e.g. `100`). After tokenization, the tokens are parsed into their final form. \n\nFor tokenization (which is also sometimes called *lexing* or *scanning*), we can reuse our `NSScanner` class.  First, we can focus on parsing strings that only contain operators:\n\n```objc\nNSScanner *scanner = [NSScanner scannerWithString:contents];\nNSMutableArray *tokens = [NSMutableArray array];\nwhile (![scanner isAtEnd]) {\n  for (NSString *operator in @[@\"=\", @\"+\", @\"*\", @\">=\", @\"<=\", @\".\"]) {\n      if ([scanner scanString:operator intoString:NULL]) {\n          [tokens addObject:operator];\n      }\n  }\n}\n```\n\nThe next step is to also recognize identifiers such as `myConstant` and `viewController`. For our simple purposes, identifiers can only consist of letters (no digits). This is how we scan them:\n\n```objc\nNSString *result = nil;\nif ([scanner scanCharactersFromSet:[NSCharacterSet letterCharacterSet] \n                        intoString:&result]) {\n    [tokens addObject:result];\n}\n```\n\nThe method `scanCharactersFromSet:intoString:` will only return `YES` if those characters can be found, and then we add it to our tokens array. We're almost done now; the only thing left is parsing numbers. Luckily, `NSScanner` has some methods for that too. We can just use `scanDouble:` to scan doubles, make an `NSNumber` out of them, and add them to the tokens:\n\n```objc\ndouble doubleResult = 0;\nif ([scanner scanDouble:&doubleResult]) {\n    [tokens addObject:@(doubleResult)];\n}\n```\n\nNow our scanner is complete, and we can test it:\n\n```objc\nNSString* example = @\"myConstant = 100\\n\"\n                    @\"\\nmyView.left = otherView.right * 2 + 10\\n\"\n                    @\"viewController.view.centerX + myConstant <= self.view.centerX\";\nNSArray *result = [self.scanner tokenize:example];\nNSArray *expected = @[@\"myConstant\", @\"=\", @100, @\"myView\", @\".\", @\"left\", \n                      @\"=\", @\"otherView\", @\".\", @\"right\", @\"*\", @2, @\"+\", \n                      @10, @\"viewController\", @\".\", @\"view\", @\".\", \n                      @\"centerX\", @\"+\", @\"myConstant\", @\"<=\", @\"self\", \n                      @\".\", @\"view\", @\".\", @\"centerX\"];\nXCTAssertEqualObjects(result, expected);\n```\n\nOur scanner creates separate tokens for the operators and names and puts the numbers into `NSNumber` objects. Once that's complete, we're ready to take the second step: parsing the array of tokens into something more meaningful.\n\n## Parsing\n\nThere's a reason why we can't solve this problem with regular expressions or just scanners and need a more powerful technique. It's because parsing might fail. For example, consider when we see the token `@\"myConstant\"`. In our parsing function, we don't know if this is the start of a constraint expression or a constant definition. We will need to try both options and see which succeeds. We can implement this by hand (which is not that hard, but gets messy), or use the right tool for the job: a parsing library.\n\nFirst, we need to describe our language in a format that the library can understand. Here's the grammar for our constraint-parsing language. This is written in [EBNF](http://en.wikipedia.org/wiki/Extended_Backus–Naur_Form):\n\n```\nconstraint = expression comparator expression\ncomparator = \"=\" | \">=\" | \"<=\"\nexpression = keyPath \".\" attribute addMultiplier addConstant\nkeyPath = identifier | identifier \".\" keyPath\nattribute = \"left\" | \"right\" | \"top\" | \"bottom\" | \"leading\" | \"trailing\" | \"width\" | \"height\" | \"centerX\" | \"centerY\" | \"baseline\"\naddMultiplier = \"*\" atom\naddConstant = \"+\" atom\natom = number | identifier\n```\n\nThere are multiple Objective-C libraries available for parsing (see [CocoaPods](http://cocoapods.org/?q=parse)). One of them is [CoreParse](https://github.com/beelsebob/CoreParse), which provides us with an API that works really well in Objective-C. \nHowever, we cannot directly feed the grammar above into CoreParse. The library only has parsers with a lookahead of one. This means that whenever the parser has to decide between two rules (for example, in the rule `keyPath`) it will look at the next token and make the decision. If we find out only later that we should have chosen the other rule, we're in trouble. There are other parsers which allow for more ambiguous grammars, but this comes at a large performance penalty.\n\nLuckily, we can do some refactoring on our grammar, in order to make sure it's compatible with the parser library. We can also convert it to [Backus-Naur Form](http://en.wikipedia.org/wiki/Backus–Naur_Form), and now it's exactly in the format that CoreParse expects:\n\n```objc\nNSString* grammarString = [@[\n    @\"Atom ::= num@'Number' | ident@'Identifier';\",\n    @\"Constant ::= name@'Identifier' '=' value@<Atom>;\",\n    @\"Relation ::= '=' | '>=' | '<=';\",\n    @\"Attribute ::= 'left' | 'right' | 'top' | 'bottom' | 'leading' | 'trailing' | 'width' | 'height' | 'centerX' | 'centerY' | 'baseline';\",\n    @\"Multiplier ::= '*' num@'Number';\",\n    @\"AddConstant ::= '+' num@'Number';\",\n    @\"KeypathAndAttribute ::= 'Identifier' '.' <AttributeOrRest>;\",\n    @\"AttributeOrRest ::= att@<Attribute> | 'Identifier' '.' <AttributeOrRest>;\",\n    @\"Expression ::= <KeypathAndAttribute> <Multiplier>? <AddConstant>?;\",\n    @\"LayoutConstraint ::= lhs@<Expression> rel@<Relation> rhs@<Expression>;\",\n    @\"Rule ::= <Atom> | <LayoutConstraint>;\",\n] componentsJoinedByString:@\"\\n\"];\n```\n\nIf a rule is matched, then the library tries to find a class with the same name (for example, `Expression`). If that class implements a method `initWithSyntaxTree:`, then that method is called. Alternatively, \nthe parser has a delegate, which gets callbacks whenever a rule is matched (or when an error occurs). For example, in the case of the relation rule, we get back a `CPSyntaxTree`, and the first child of that tree is a keyword token containing either `@\"=\"`, `@\">=\"`, or `@\"<=\"`. We can map that string to an `NSNumber` containing the layout attribute using a dictionary:\n\n```objc\n- (id)parser:(CPParser *)parser didProduceSyntaxTree:(CPSyntaxTree *)syntaxTree\n    NSString *ruleName = syntaxTree.rule.name;\n    if ([ruleName isEqualToString:@\"Attribute\"]) {\n        return self.layoutAttributes[[[syntaxTree childAtIndex:0] keyword]];\n    }\n    ...\n```\n\nThe full code for our parser is on [GitHub](https://github.com/objcio/issue-9-string-parsing), and in a class of slightly more than 100 lines we can parse complicated layout constraints like:\n\n```objc\nviewController.view.centerX + 20 <= self.view.centerX * 0.5\n```\n\nAnd get a result like below, which can easily be converted into an `NSLayoutConstraint`:\n\n```\n(<Expression: self.keyPath=(viewController, view), \n              self.attribute=9,\n              self.multiplier=1, \n              self.constant=20> \n -1 \n <Expression: self.keyPath=(self, view), \n              self.attribute=9,\n              self.multiplier=0.5,\n              self.constant=0>)\n```\n\n## Other Tools\n\nInstead of Objective-C libraries, another common strategy is to use tools like [Bison](http://www.gnu.org/software/bison/), [Yacc](http://dinosaur.compilertools.net/yacc/), [Ragel](http://www.complang.org/ragel/), or [Lemon](http://www.hwaci.com/sw/lemon/lemon.html), which are all C-level libraries.\n\nAnother thing you could do is use these parsers to generate part of your code at build time. For example, once you have a parser for a language, you can create a simple command-line wrapper around it. Add an Xcode [build rule](/issues/6-build-tools/build-process/), and you have a compiler for your own language that gets executed on each build.\n\n## Parsing Ideas\n\nIt might seem like parsing is a bit weird, and creating string-based languages doesn't feel very Objective-C-like. The opposite is true: Apple is doing this all the time. For example, think about `NSLog` format strings, `NSPredicate` strings, the layout constraint visual formatting language, and even key-value coding. All of these use small internal parsers to parse strings into objects and actions. Often you don't have to write a parser yourself, which will save a lot of work: common languages like JSON and XML are already taken care of. But if you would want to write a calculator, a [graphics language](https://github.com/damelang/nile), or even an embedded Smalltalk, parsers are your friend.\n"
  },
  {
    "path": "2014-02-10-string-rendering.md",
    "content": "---\ntitle:  \"String Rendering\"\ncategory: \"9\"\ndate: \"2014-02-07 07:00:00\"\ntags: article\nauthor:\n  - name: Chris Eidhof\n    url: https://twitter.com/chriseidhof\n  - name: Daniel Eggert\n    url: http://twitter.com/danielboedewadt\n  - name: Florian Kugler\n    url: https://twitter.com/floriankugler\n---\n\n\nWe've talked about many different string topics in this issue, from Unicode over localization to parsing. But in most cases, strings ultimately have to be drawn onto the screen for the user to see and interact with them. This article covers the basics, best practices, and common pitfalls of how to present strings in the user interface.\n\n\n## How to Get Strings onto the Screen\n\nFor the sake of simplicity, we're first going to focus on what UIKit has to offer in terms of string drawing. Afterward, we'll talk about similarities and differences between iOS and OS X.\n\nUIKit comes with multiple classes that can be used to display and edit text on screen. Each is made for specific use cases, and it's important to choose the right tool for the task at hand in order to avoid unnecessary trouble.\n\n\n### `UILabel`\n\n[`UILabel`](https://developer.apple.com/library/ios/documentation/userexperience/conceptual/UIKitUICatalog/UILabel.html) is the simplest way to bring text onto the screen. It's a `UIView` subclass and is made to display small amounts of *read-only* text. The text can be laid out in a single line or in multiple lines, and it can be clipped in different ways if it doesn't fit into the specified space. Although labels are pretty straightforward to use, they have a couple of tricks up their sleeves that are worth mentioning.\n\nBy default, labels only show a single line, but you can change this behavior by setting the `numberOfLines` property to a value other than one. Setting it to a value greater than one will restrict the number of lines to the specified value, while setting it to zero tells the label that it should display the whole text no matter how many lines it takes.\n\nLabels can display simple plain text by setting the `text` property, but they can also display rich text by setting an attributed string on the `attributedText` property. When using plain text, you can style its appearance using the label's `font`, `textColor`, `textAlignment`, `shadowColor`, and `shadowOffset` properties, either by setting them directly, or by using the label's appearance proxy if you want to alter the style of labels in your app in general. Attributed strings offer much more flexible styling options, and different parts of the string can be styled in different ways. See the section about [common layouts](#layout-with-attributed-strings) below for some examples of attributed strings.\n\nBeyond the styling of the label you define via the label's styling properties or the string attributes, you can give `UILabel` permission to adjust your styling if the text wouldn't fit otherwise (`adjustsFontSizeToWidth`, `minimumScaleFactor`, and `adjustsLetterSpacingToFitWidth`). If you care about how your user interface looks, you should strive to never use these options. But sometimes localization in different languages poses challenges that are hard to solve differently. Just try a language like German as your phone's system language, and you'll find all kinds of ugly, shrunk-down text in Apple's apps. It's not pretty, but sometimes useful.\n\nIf you use those options to let UIKit shrink your text to fit, you can use the `baselineAdjustment` property to define if the text stays put on the same baseline when it shrinks, or if it should be aligned to the upper-left corner. However, this option only works for single line labels.\n\n\n### `UITextField`\n\nLike labels, [text fields](https://developer.apple.com/library/ios/documentation/userexperience/conceptual/UIKitUICatalog/UITextField.html#//apple_ref/doc/uid/TP40012857-UITextField-SW1) can handle either plain or attributed strings. But where labels can only *display* plain or attributed strings, text fields can also handle user *input*. However, text fields are limited to a single line of text. Therefore, `UITextField` is a `UIControl` subclass to hook into the responder chain and to deliver action messages when the user starts or ends editing. For more control, you can implement the text field's [delegate](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intf/UITextFieldDelegate).\n\nText fields come with a bunch of options to control the way text input works. They implement the [`UITextInputTraits`](https://developer.apple.com/library/ios/documentation/uikit/reference/UITextInputTraits_Protocol/Reference/UITextInputTraits.html) protocol, which lets you specify all kinds of details regarding how the keyboard should look and work, e.g. what kind of keyboard should be shown and how the return key behaves. \n\nText fields can also show a placeholder text when no text is entered, show the standard clear button on the right-hand side, and host arbitrary left and right auxiliary views. You can also set a background image, which can be used to apply a custom border style to the text field by using a resizable image.\n\nBut whenever you need to input more than a single line of text, you have to switch to `UITextField`'s big brother...\n\n\n### `UITextView`\n\n[Text views](https://developer.apple.com/library/ios/documentation/userexperience/conceptual/UIKitUICatalog/UITextView.html) are the perfect choice for displaying or editing long texts. `UITextView` is a subclass of `UIScrollView`, so it can handle overflowing text by allowing the user to scroll back and forth. As text fields, text views can also handle plain and attributed strings. Text views also implement the [`UITextInputTraits`](https://developer.apple.com/library/ios/documentation/uikit/reference/UITextInputTraits_Protocol/Reference/UITextInputTraits.html) protocol to control keyboard behavior and appearance.\n\nBut apart from text view's ability to edit multiline text, its biggest selling point is that you can access and customize the entire [Text Kit](https://developer.apple.com/Library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/CustomTextProcessing/CustomTextProcessing.html) stack. You can customize the behavior or swap in your own custom subclasses for the [layout manager](https://developer.apple.com/library/ios/documentation/uikit/reference/NSLayoutManager_Class_TextKit/Reference/Reference.html), the [text container](https://developer.apple.com/library/ios/documentation/uikit/reference/NSTextContainer_Class_TextKit/Reference/Reference.html), and the [text storage](https://developer.apple.com/library/ios/documentation/uikit/reference/NSTextStorage_Class_TextKit/Reference/Reference.html). Have a look at Max's [Text Kit article](/issues/5-ios7/getting-to-know-textkit/) in objc.io issue #5.\n\nUnfortunately, `UITextView` still has some issues in iOS 7. It's at version 1.0. It was reimplemented from scratch based on the OS X Text Kit. Before iOS 7, it was based on Webkit and was a lot less powerful. Have a look at [Peter's](http://petersteinberger.com/blog/2014/fixing-uitextview-on-ios-7/) and [Brent's](http://inessential.com/2014/01/07/uitextview_the_solution) articles on this matter to learn how to work around those issues.\n\n\n### What About the Mac?\n\nNow that we have covered the basics of the text classes in UIKit, let us shortly point out some differences in how these classes are structured in AppKit.\n\nFirst of all, AppKit doesn't have an equivalent to `UILabel`. Instead, the most basic class to display text is `NSTextField`. Setting the text field to be not editable and not selectable is the Mac equivalent to using a `UILabel` on iOS. And while `NSTextField` sounds like the equivalent to `UITextField`, it is not limited to single-line text. \n\n`NSTextView`, on the other hand, is the equivalent to `UITextView` in that it exposes the whole stack of the [Cocoa Text System](https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/Introduction/Introduction.html). But it also packs a lot of additional functionality. Some of it comes from the fact that the Mac is a computer with a pointing device. Notable additions include the rulers to set and edit tab stops.\n\n\n### Core Text\n\nAll classes we discussed above ultimately use [Core Text](https://developer.apple.com/library/mac/documentation/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html) to lay out and draw the actual glyphs. Core Text is a very powerful framework that is beyond the scope of this article. But if you ever need to do draw text in a completely custom way (e.g. along a Bézier path), you should definitely look into it.\n\nCore Text gives you full flexibility over any drawing aspect you could possibly imagine. However, Core Text can be quite unwieldy. It's a complex Core Foundation / C API. It gives you full access to all aspects of typesetting.\n\n\n\n## Displaying Dynamic Text in a Table View\n\nPerhaps the most common case where everybody interacts with string drawing methods is when you have to draw variable height table view cells. You will find this in all social media apps. The table view's delegate has a method, `tableView:heightForRowAtIndexPath:`, which is used to calculate the height. Before iOS 7, this used to be rather hard to do in a reliable way.\n\nIn our example, we will display a list of quotes in a table view:\n\n![Table view with quotes](/images/issue-9/uitableview-finished.png)\n\nTo do this, first we'll make sure we have full control of the `UITableViewCell` by creating a custom subclass. In that subclass, we'll do the layout of our label ourselves:\n\n```objc\n- (void)layoutSubviews\n{\n    [super layoutSubviews];\n    self.textLabel.frame = CGRectInset(self.bounds, \n                                       MyTableViewCellInset,\n                                       MyTableViewCellInset);\n}\n```\n\nThe `MyTableViewCellInset` is defined as a constant, so that we can use it for height calculation in the table view's delegate. The easiest and most reliable way to calculate height is by converting the string into an attributed string, and calculating the height of the attributed string. We take the table view's width and subtract twice the `MyTableViewCellInset` constant (for leading and trailing space). To calculate the actual height, we use `boundingRectWithSize:options:context:`. \n\nThe first parameter is the size to which the text should be constrained. We only care about constraining the width, hence we pass in `CGFLOAT_MAX` for the height. The second parameter is very important: if you pass in other values, the bounding rect will almost certainly be wrong. If you want to adjust font scaling and/or tracking, you can use the third parameter. Finally, once we have the `boundingRect`, we have to add the inset again:\n\n```objc\n- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath\n{\n    CGFloat labelWidth = self.tableView.bounds.size.width - MyTableViewCellInset*2;\n    NSAttributedString *text = [self attributedBodyTextAtIndexPath:indexPath];\n    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin |\n                                     NSStringDrawingUsesFontLeading;\n    CGRect boundingRect = [text boundingRectWithSize:CGSizeMake(labelWidth, CGFLOAT_MAX)\n                                             options:options\n                                             context:nil];\n\n    return (CGFloat) (ceil(boundingRect.size.height) + MyTableViewCellInset*2);    \n}\n```\n\nThere are two more subtle things about the resulting bounding rect, which might not be obvious unless you read the documentation: the returned size returns fractional values, and the documentation tells us to round the result up using `ceil`. Finally, the result may actually still be larger than could be contained in the size argument.\n\nNote that, while our text is a plain `NSString`, we created the method `attributedBodyTextAtIndexPath:`, which is also used in the `tableView:cellForRowAtIndexPath:`. This way, we make sure that both stay in sync.\n\nAlso, taking a look at the documentation (see the screenshot below), we can see that a lot of methods have been deprecated since iOS 7. If you browse the internet, or StackOverflow, you'll find a lot of answers and workarounds for measuring string sizes. Because the text system received a major overhaul (internally, everything is rendered using TextKit rather than WebKit), please use the new methods.\n\n![Deprecated string measuring methods](/images/issue-9/deprecated-methods.png)\n    \nAnother option for dynamically sized table view cells is to use Auto Layout, for example, as explained in [this blog post](http://blog.amyworrall.com/post/66085151655/using-auto-layout-to-calculate-table-cell-height\n). You can then piggyback on the `intrinsicContentSize` of the contained labels. However, Auto Layout is currently a lot slower than calculating things manually. For prototyping, however, it is perfect: it allows you to quickly adjust constraints and move things around (this is especially important if you have more than one element on your cell). Once you have finished the design iterations, you can then rewrite it to do the layout manually.\n\n\n<a name=\"layout-with-attributed-strings\"> </a>\n\n## Layout with Text Kit and `NSAttributedString`\n\nWith Text Kit, you have an amazing amount of flexibility to create professional-grade text layout. With this flexibility comes a lot of complexity in figuring out how to combine the multitude of options to get the layout you want.\n\nWe want to give a few examples to highlight a few common layout problems, as well as show solutions to them.\n\n### Classic Text\n\nFirst, let's take a look at some classic text. We'll use [Histoire des nombres et de la numération mécanique](http://www.gutenberg.org/ebooks/27936) by Jacomy-Régnier and set it in [Bodoni](http://www.myfonts.com/fonts/itc/bodoni-seventy-two/). The screenshot of the final result looks like this:\n\n![](/images/issue-9/Layout-Example-1.png)\n\nThis is all done with Text Kit. The ornament between sections is also text, set in the [Bodoni Ornaments](http://www.myfonts.com/fonts/itc/bodoni-ornaments/) font.\n\nWe're using justified text for the body style. The first paragraph starts at the very left, and subsequent paragraphs are inset by an [em space](https://en.wikipedia.org/wiki/Em_space).\n\nWe have three different styles for this: the *body* style, the variation of it with the indent of the first line, and the style for the ornament.\n\nLet's first set up the `body1stAttributes`:\n\n\n```objc\nCGFloat const fontSize = 15;\n\nNSMutableDictionary *body1stAttributes = [NSMutableDictionary dictionary];\nbody1stAttributes[NSFontAttributeName] = [UIFont fontWithName:@\"BodoniSvtyTwoITCTT-Book\" \n                                                         size:fontSize];\nNSMutableParagraphStyle *body1stParagraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];\nbody1stParagraph.alignment = NSTextAlignmentJustified;\nbody1stParagraph.minimumLineHeight = fontSize + 3;\nbody1stParagraph.maximumLineHeight = body1stParagraph.minimumLineHeight;\nbody1stParagraph.hyphenationFactor = 0.97;\nbody1stAttributes[NSParagraphStyleAttributeName] = body1stParagraph;\n```\n\nWe're setting the font to `BodoniSvtyTwoITCTT`. This is the PostScript name of the font. To find the font name, we can use `+[UIFont familyNames]` to first get the available font families. A [font family](https://en.wikipedia.org/wiki/Font_family) is also known as a typeface. Each typeface or font family has one or multiple fonts. To get the names of those, we can use `+[UIFont fontNamesForFamilyName:]`. Note that the `UIFontDescriptor` class can be very helpful if you're working with multiple fonts, e.g. when you need to find out what the italic version of a given font is.\n\nMany settings live inside the `NSParagraphStyle`. We're creating a mutable copy of the default style and adjusting it. In our case, we'll use the font size and add 3[pt](https://en.wikipedia.org/wiki/Point_%28typography%29) to it.\n\nNext up, we'll make a copy of these attributes and modify them to create `bodyAttributes`, which are our attributes for paragraphs that are not the first within a section:\n\n```objc\nNSMutableDictionary *bodyAttributes = [body1stAttributes mutableCopy];\nNSMutableParagraphStyle *bodyParagraph = \n  [bodyAttributes[NSParagraphStyleAttributeName] mutableCopy];\nbodyParagraph.firstLineHeadIndent = fontSize;\nbodyAttributes[NSParagraphStyleAttributeName] = bodyParagraph;\n```\n\nWe're simply making a mutable copy of the attributes dictionary, and then making a mutable copy of the paragraph style in order to change it. Setting the `firstLineHeadIndent` to the same as the font size will give us the desired [em space](https://en.wikipedia.org/wiki/Em_space) indent.\n\nNext up, the ornament paragraph style:\n\n```objc\nNSMutableDictionary *ornamentAttributes = [NSMutableDictionary dictionary];\nornamentAttributes[NSFontAttributeName] = [UIFont fontWithName:@\"BodoniOrnamentsITCTT\"\n                                                          size:36];\nNSMutableParagraphStyle *ornamentParagraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];\nornamentParagraph.alignment = NSTextAlignmentCenter;\nornamentParagraph.paragraphSpacingBefore = fontSize;\nornamentParagraph.paragraphSpacing = fontSize;\nornamentAttributes[NSParagraphStyleAttributeName] = ornamentParagraph;\n```\n\nThis is pretty self-explanatory. We're using the ornaments font and setting the text alignment to center. Additionally, we're adding paragraph space before and after the ornament character.\n\n### Tables with Numbers\n\nNext up, a table of numbers. We want to align fractional numbers on their decimal separators, i.e. “.” in English:\n\n![](/images/issue-9/Layout-Example-2.png)\n\nTo achieve this, we have to specify tab stops that center on the decimal separator.\n\nFor the above example, we're simply doing:\n\n```objc\nNSCharacterSet *decimalTerminator = [NSCharacterSet \n  characterSetWithCharactersInString:decimalFormatter.decimalSeparator];\nNSTextTab *decimalTab = [[NSTextTab alloc] \n   initWithTextAlignment:NSTextAlignmentRight\n                location:100\n                 options:@{NSTabColumnTerminatorsAttributeName:decimalTerminator}];\nNSTextTab *percentTab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentRight\n                                                        location:200\n                                                         options:nil];\nNSMutableParagraphStyle *tableParagraphStyle = \n  [[NSParagraphStyle defaultParagraphStyle] mutableCopy];\ntableParagraphStyle.tabStops = @[decimalTab, percentTab];\n```\n\n### Lists\n\nAnother common use case is a list like this:\n\n![](/images/issue-9/Layout-Example-3.png)\n\n(from [Robert's Rules of Order](http://www.gutenberg.org/ebooks/9097) by Henry M. Robert)\n\nThe hanging indents are relatively simple to set up. We need to make sure there's a tab character between either the list number “(1)” and text or the bullet and the text. Then we'll adjust the paragraph style like so:\n\n```objc\nNSMutableDictionary *listAttributes = [bodyAttributes mutableCopy];\nNSMutableParagraphStyle *listParagraph = \n  [listAttributes[NSParagraphStyleAttributeName] mutableCopy];\nlistParagraph.headIndent = fontSize * 3;\nlistParagraph.firstLineHeadIndent = fontSize;\nNSTextTab *listTab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentNatural\n                                                     location:fontSize * 3 \n                                                      options:nil];\nlistParagraph.tabStops = @[listTab];\nlistAttributes[NSParagraphStyleAttributeName] = listParagraph;\n```\n\nWe're setting the `headIndent` to the indent of the actual text and the `firstLineHeadIndent` to the indent (from the left-hand side) that we want the bullet to have. Finally, we need to add a tab stop at the same position as the `headIndent`. The tab character after the bullet will then make sure the text on that line starts at the correct position.\n\n"
  },
  {
    "path": "2014-02-10-unicode.md",
    "content": "---\ntitle:  \"NSString and Unicode\"\ncategory: \"9\"\ndate: \"2014-02-10 11:00:00\"\ntags: article\nauthor:\n  - name: Ole Begemann\n    url: http://oleb.net\n---\n\nIf you write any non-legacy code that deals with text today and you aren't using [Unicode][WikipediaUnicode] everywhere, you're doing it wrong. Fortunately for us, Apple and NeXT have been among the driving forces behind the creation of the Unicode standard and NeXT's [Foundation Kit][Foundation Kit], [introduced in 1994][Cocoa19thBirthday], was one of the first standard libraries based on Unicode for any programming language. But even though [`NSString`][NSString] fully supports Unicode and does most of the difficult work for you, handling text in hundreds of different languages and writing systems remains a very complex topic, and there are some things you as a programmer should be aware of.\n\nIn this article, I want to give you an overview of the Unicode standard and then explain how the `NSString` class handles it, as well as discuss some common problems you may encounter.\n\n## History\n\nComputers cannot handle text directly; they can only deal with numbers. To represent text (a string of characters) as (a string of) numbers in a computer, we specify a mapping from characters into numbers. This is called an [_encoding_][WikipediaCharacterEncoding].\n\nThe best-known character encoding is [ASCII][WikipediaASCII]. ASCII is a 7-bit code that maps the English alphabet, the digits 0-9, and some punctuation and control characters into the integers 0 to 127. Subsequently, many different 8-bit encodings were created to make computers work with languages other than English. They were mostly based on ASCII and utilized the unused eighth bit to encode additional letters, symbols, or entire alphabets (such as Cyrillic or Greek).\n\nThese encodings were all incompatible with each other, of course — and necessarily so, since eight bits did not provide enough room for all characters used even in the common European scripts, not to mention all of the world's writing systems. This was a big problem for the text-based computer systems of the time because only one encoding (also called a [code page][ListOfCodePages]) could be active at a time; if you wrote a text on one machine and then opened it on another computer that used a different code page, all characters in the 128-255 range would be interpreted incorrectly.\n\nEast Asian scripts such as Chinese, Japanese, and Korean presented another problem. They have so many characters that the mapping requires much more than the 256 slots provided by 8-bit numbers. As a result, wider encodings (usually 16 bits) were developed. And as soon as you're dealing with values that do not fit into one byte, the question of how these numbers should be stored in memory or on disk becomes non-trivial. You have to perform a second mapping that defines rules for [byte order][WikipediaEndianness] and possibly applies a [variable-length][WikipediaVariableWidthEncoding] encoding instead of a simple fixed-width variant. Note that this second mapping step is just another form of encoding, and the fact that we can use the same word for both is a common source of confusion. I'll get back to this in the discussion of UTF-8 and UTF-16.\n\nModern operating systems are no longer limited to using only one code page at a time, so as long as every document correctly reported the encoding it was written in, dealing with dozens or hundreds of different encodings would be entirely possible, if annoying. What is not possible is _mixing_ multiple encodings in one document and thus writing multilingual documents, and this really puts the final nail in the coffin of the pre-Unicode state of the world.\n\n[Beginning in 1987][UnicodeHistory], people from major tech companies, including Apple and NeXT, started working together on a universal character encoding for all the world's writing systems, which resulted in the release of version 1.0.0 of the [Unicode Standard](http://www.unicode.org/standard/standard.html) in October 1991.\n\n## Unicode Overview\n\n### The Basics\n\nAt its most basic level, the Unicode standard defines a unique number for every character or symbol that is used in writing, for nearly all[^1] of the world's writing systems. The numbers are called _code points_ and are written in the form `U+xxxx` where the `xxxx` are four to six hexadecimal digits. For example, the code point U+0041 (65<sub>decimal</sub>) stands for the letter A in the Latin alphabet (same as ASCII) and U+1F61B represents the [emoji][WikipediaEmoji] named FACE WITH STUCK-OUT TONGUE, or 😛. (The names of the characters are an official part of the Unicode standard, by the way.) You can use the [official code charts][UnicodeCodeCharts] or the Character Viewer on OS X (<kbd>Control</kbd> + <kbd>Option</kbd> + <kbd>Space</kbd>) to look up the code points.\n\n[^1]: The current Unicode standard 6.3.0 [supports 100 scripts and 15 symbol collections][UnicodeSupportedScripts], such as mathematical symbols or mahjong tiles. Among the [yet unsupported scripts][UnicodeUnsupportedScripts], it specifically lists 12 <q>scripts in current use in living communities</q> and 31 <q>archaic or ‘dead’ scripts</q>.\n\n![The Character Viewer in OS X showing a table of Emoji and Unicode character information](/images/issue-9/os-x-character-viewer-emoji.png)\n\nLike the other encodings I mentioned above, Unicode represents characters in an abstract way and says nothing about how they should be rendered. This goes so far that Unicode uses identical code points for the Han characters used in Chinese, Japanese, and Korean (CJK) scripts (the so-called [Han unification][WikipediaHanUnification]) although these writing systems have each developed unique glyph variants of the characters — a controversial decision.\n\nUnicode was originally conceived as a 16-bit encoding, providing room for 65,536 characters. This was deemed big enough to encode all scripts and characters used in modern text around the world. Obsolete or rare characters were supposed to go into [Private Use Areas][WikipediaUnicodePrivateUseAreas] when needed — designated regions within the 65,536 character space that organizations could use to define their own mappings (which could potentially conflict with each other). Apple encodes a substantial number of custom symbols and control characters in the Private Use Areas, ([documented here][ApplePrivateUseAreaUsage]), though most of them are deprecated. A notable exception is the Apple logo at U+F8FF:  (depending on the platform you are reading this on, you may see a completely different character here).\n\nThe Unicode code space was [later][UnicodeFAQ16BitEncoding] extended to 21 bits (U+0000 to U+10FFFF) to allow for the encoding of historic scripts and rarely-used Kanji or Chinese characters.[^2] This is an important point: despite what we are going to learn about `NSString`, Unicode is _not a 16-bit encoding!_ It's 21 bits wide. These 21 bits provide room for 1,114,112 code points. Only approximately 10 percent of those are currently in use, so there is plenty of room to grow.\n\n[^2]: Today, Unicode encodes more than 70,000 unified CJK characters, easily blasting the 16-bit boundary with these alone.\n\nThe code space is divided into 17 [planes][WikipediaUnicodePlane] with 65,536 characters each. Plane 0 is called the _Basic Multilingual Plane (BMP)_ and it is where almost all characters you will encounter in the wild reside, with the notable exception of emoji. The other planes are called _supplementary planes_ and are largely empty.\n\n<a name=\"peculiar-unicode-features\"> </a>\n### Peculiar Unicode Features\n\nIt helps to think of Unicode as a unification of existing (mostly 8-bit) encodings, rather than a _universal_ code. Mostly for compatibility reasons with legacy encodings, the standard includes a number of subtleties you have to be aware of to correctly handle Unicode strings in your code.\n\n#### Combining Character Sequences\n\nFor compatibility with preexisting standards, certain characters can be represented either as a single code point or as sequences of two or more code points. For example, the accented letter é can be represented as the precomposed character U+00E9 (LATIN SMALL LETTER E WITH ACUTE), or it can be encoded in a decomposed form as U+0065 (LATIN SMALL LETTER E) followed by U+0301 (COMBINING ACUTE ACCENT). The two forms are variants of a [_combining (or composite) character sequence_][WikipediaPrecomposedCharacter]. Combining character sequences are not only observed in western scripts; in [Hangul][WikipediaHangul], for example, the syllable 가 can be represented as a single code point (U+AC00) or as the sequence ᄀ + ᅡ (U+1100 U+1161).\n\nIn Unicode parlance, the two forms are _not equal_ (because they contain different code points) but [_canonically equivalent_][WikipediaUnicodeEquivalence]: that is, they have the same appearance and meaning.\n\n#### Duplicate Characters\n\nMany seemingly identical characters are encoded multiple times at different code points, representing different meanings. For example, the Latin character A (U+0041) is identical in shape to the [Cyrillic character A][WikipediaCyrillicA] (U+0410) but they are, in fact, different. Encoding these as separate code points not only simplifies conversion from legacy encodings, but also allows Unicode text to retain the characters' meaning.\n\nBut there are also rare instances of “real” duplication, where the same character is defined under multiple code points. As an example, the Unicode Consortium [lists][UnicodeCharacterDuplicates] the letter Å (LATIN CAPITAL LETTER A WITH RING ABOVE, U+00C5) and the character Å (ANGSTROM SIGN, U+212B). Since the Ångström sign is, in fact, defined to be the Swedish capital letter, these characters are truly identical. In Unicode, they too are not equal but canonically equivalent.\n\nMore characters and sequences fall under a broader definition of “duplicate,” called [_compatibility equivalence_][WikipediaUnicodeEquivalence] in the Unicode standard. Compatible sequences represent the same abstract character, but may have a different visual appearance or behavior. Examples include many Greek letters, which are also used as mathematical and technical symbols, and the Roman numerals, which are encoded in addition to the standard Latin letters in the range from U+2160 to U+2183. Other good examples of compatibility equivalence are [ligatures][WikipediaLigature]: the character ﬀ (LATIN SMALL LIGATURE FF, U+FB00) is compatible with (but not canonically equivalent to) the sequence ff (LATIN SMALL LETTER F + LATIN SMALL LETTER F, U+0066 U+0066), although both may be rendered identically, depending on the context, typeface, and the capabilities of the text rendering system.\n\n<a name=\"normalization-forms\"> </a>\n#### Normalization Forms\n\nWe have seen that string equality is not a simple concept in Unicode. Aside from comparing two strings, code point for code point, we also need a way to test for canonical equivalence or compatibility equivalence. Unicode defines several [_normalization_][WikipediaUnicodeNormalization] algorithms for this. Normalizing a string means converting it to a form that guarantees a unique representation of equivalent character sequences, so that it can then be binary-compared to another normalized string.\n\nThe Unicode standard includes four normalization forms labeled C, D, KD, and KC, which can be arranged in a two-by-two matrix (I have also listed the `NSString` methods that perform the normalizations):\n\n<table>\n  <thead>\n    <tr>\n      <th colspan=\"2\" rowspan=\"2\">Unicode Normalization Forms (NF)</th>\n      <th colspan=\"2\">Character Form</th>\n    </tr>\n    <tr>\n      <th>Composed (é)</th>\n      <th>Decomposed (e + ´)</th>\n    </tr>\n  </thead>\n  <tr>\n    <th rowspan=\"2\">Equivalence<br>Class</th>\n    <th>Canonical</th>\n    <td>\n      <p>C</p>\n      <p><a href=\"https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/precomposedStringWithCanonicalMapping\"><code>precomposed​String​With​Canonical​Mapping</code></a></p>\n    </td>\n    <td>\n      <p>D</p>\n      <p><a href=\"https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/decomposedStringWithCanonicalMapping\"><code>decomposed​String​With​Canonical​Mapping</code></a></p>\n    </td>\n  </tr>\n  <tr>\n    <th>Equivalence</th>\n    <td>\n      <p>KC</p>\n      <p><a href=\"https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/precomposedStringWithCompatibilityMapping\"><code>precomposed​String​With​Compatibility​Mapping</code></a></p>\n    </td>\n    <td>\n      <p>KD</p>\n      <p><a href=\"https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/decomposedStringWithCompatibilityMapping\"><code>decomposed​String​With​Compatibility​Mapping</code></a></p>\n    </td>\n  </tr>\n</table>\n\nFor the purpose of comparing strings, it does not matter whether you normalize them all to the decomposed (D) form or to the composed &#40;C) form. Form D should be faster, since the algorithm for form C involves two steps: characters are first decomposed and then recomposed. If one character sequence includes multiple combining marks, the ordering of the combining marks will be unique after decomposition. On the other hand, the Unicode Consortium [recommends form C][UnicodeOrgFAQNormalizationQ2] for storage due to better compatibility with strings converted from legacy encodings.\n\nBoth equivalence classes can be handy for string comparisons, especially in the context of sorting and searching. Keep in mind, though, that you should generally not normalize a string with compatibility equivalence that is supposed to be stored permanently, as [it can alter the text's meaning][UnicodeStandardAnnex15NormalizationForms]:\n\n> Normalization Forms KC and KD must not be blindly applied to arbitrary text.\n> Because they erase many formatting distinctions, they will prevent round-trip\n> conversion to and from many legacy character sets, and unless supplanted by\n> formatting markup, they may remove distinctions that are important to the\n> semantics of the text. It is best to think of these Normalization Forms as\n> being like uppercase or lowercase mappings: useful in certain contexts for\n> identifying core meanings, but also performing modifications to the text that\n> may not always be appropriate.\n\n#### Glyph Variants\n\nSome fonts provide multiple shape variants ([glyphs][WikipediaGlyph]) for a single character, and Unicode provides a mechanism named [variation sequences][WikipediaVariantForm] that allows the author to select a certain variant. It works exactly like combining character sequences: a base character is followed by one of 256 variation selectors (VS1-VS256, U+FE00 to U+FE0F, and U+E0100 to U+E01EF). The standard distinguishes between [Standardized Variation Sequences][UnicodeStandardizedVariants] (defined in the Unicode standard) and [Ideographic Variation Sequences][UnicodeIdeographicVariationDatabase] (submitted by third parties to the Unicode consortium; once registered, they can be used by anyone). From a technical perspective, there is no difference between the two.\n\nAn example of standardized variation sequences is emoji styles. Many emoji and some “normal” characters come in two fashions, a colorful “emoji style” and a black and white, more symbol-like “text style.” For instance, the UMBRELLA WITH RAIN DROPS character (U+2614) can look like this: &#x2614;&#xFE0F; (U+2614 U+FE0F) or this: &#x2614;&#xFE0E; (U+2614 U+FE0E).\n\n### Unicode Transformation Formats\n\nAs we have seen above, mapping characters to code points only gets us half of the way. We have to define another encoding that determines how code point values are to be represented in memory or on disk. The Unicode Standard defines several of these mappings and calls them _transformation formats (UTF)_. In the real world, most people just call them _encodings_ — if something is encoded in a UTF, it uses Unicode by definition, so there is no need to distinguish between the two steps.\n\n#### UTF-32\n\nThe most straightforward UTF is [UTF-32][WikipediaUTF32]: it uses exactly 32 bits for each code point, and since 32 > 21, every UTF-32 value can be a direct representation of its code point. Despite its simplicity, UTF-32 is almost never used in the wild because using four bytes per character is very space inefficient.\n\n#### UTF-16 and the Concept of Surrogate Pairs\n\n[UTF-16][WikipediaUTF16] is a lot more common and, as we will see, very relevant for the discussion of `NSString`'s Unicode implementation. It is defined in terms of so-called _code units_ that have a fixed width of 16 bits. UTF-16 itself is a variable-width encoding. Each code point in the BMP is directly mapped to one code unit. Since the BMP encompasses almost all common characters, UTF-16 typically requires only half the memory of UTF-32. The rarely used code points in other planes are encoded with two 16-bit code units. The two code units that together represent one code point are called a _surrogate pair_.\n\nTo avoid ambiguous byte sequences in a UTF-16-encoded string, and to make detection of surrogate pairs easy, the Unicode standard has reserved the range from U+D800 to U+DFFF for the use of UTF-16. Code point values in this range will never be assigned a character. When a program sees a bit sequence that falls into this range in a UTF-16 string, it knows right away that it has encountered part of a surrogate pair. The actual encoding algorithm is simple, and you can read more about it in the [Wikipedia article for UTF-16][WikipediaUTF16]. The design of UTF-16 is also the reason for the seemingly weird 21-bit range for code points. U+10FFFF is the highest value you can encode with this scheme.\n\nLike all multibyte encoding schemes, UTF-16 (and UTF-32) must also take care about [byte order][WikipediaEndianness]. For strings in memory, most implementations naturally adopt the endianness of the CPU they run on. For storage on disk or transmission over the network, UTF-16 allows implementations to insert a [Byte Order Mark][WikipediaByteOrderMark] (BOM) at the beginning of the string. The BOM is a code unit with the value U+FEFF, and by examining the first two bytes of a file, the decoding machine can recognize its byte order. The BOM is optional, and the standard prescribes big-endian byte order as the default. The complexity introduced by the need to specify byte order is one reason why UTF-16 is not a popular encoding for file formats or transmission over the network, although OS X and Windows both use it internally.\n\n#### UTF-8\n\nBecause the first 256 Unicode code points (U+0000 to U+00FF) are identical to the common [ISO-8859-1][WikipediaISO-8859-1] (Latin 1) encoding, UTF-16 still wastes a lot of space for typical English and western European text: the upper 8 bits of each 16-bit code unit would be 0.[^3] Perhaps more importantly, UTF-16 presented challenges to legacy code that often assumed text to be ASCII-encoded. [UTF-8][WikipediaUTF8] was developed by Ken Thompson (of Unix fame) and Rob Pike to remedy these deficiencies.[^4] It is a great design and you should definitely read [Rob Pike's account of how it was created][RobPikeUTF8].\n\n[^3]: And even documents in other scripts can contain a lot of characters from that range. Consider an HTML document, the content of which is entirely in Chinese. A significant percentage of its total character count will consist of HTML tags, CSS styles, Javascript code, spaces, line terminators, etc.\n\n[^4]: In a 2012 blog post, I wondered whether [making UTF-8 ASCII-compatible was the right decision][OleBegemannOnUTF8]. As I now know, this was, in fact, one of the central goals of the scheme, specifically to avoid problems with non-Unicode-aware file systems. I still think that too much backward compatibility can often turn out to be a hindrance, because this feature still hides bugs in poorly tested Unicode-handling code today.\n\nUTF-8 uses between one and four[^5] bytes to encode a code point. The code points from 0-127 are mapped directly to one byte (making UTF-8 identical to ASCII for texts that only contain these characters). The following 1,920 code points are encoded with two bytes, and all remaining code points in the BMP need three bytes. Code points in other Unicode planes require four bytes. Since UTF-8 is based on 8-bit code units, it does not need to care about byte ordering (some programs add a superfluous BOM to UTF-8 files, though).\n\n[^5]: UTF-8 was originally designed to encode code points up to 31 bits, which required sequences of up to 6 bytes. It was later restricted to 21 bits in order to match the constraints set by UTF-16. The longest UTF-8 byte sequence is now 4 bytes.\n\nThe space efficiency (for western languages) and lack of byte order issues make UTF-8 the best encoding for the storage and exchange of Unicode text. It has become the _de facto_ standard for file formats, network protocols, and Web APIs.\n\n## NSString and Unicode\n\n`NSString` is fully built on Unicode. However, Apple does a bad job explaining this correctly. This is what [Apple's documentation has to say about `CFString` objects][AppleDocCFStringUnicodeBasis] (which provides the implementation for `NSString`, too):\n\n> Conceptually, a CFString object represents an array of Unicode characters\n> (`UniChar`) along with a count of the number of characters. … The \\[Unicode\\]\n> standard defines a universal, uniform encoding scheme that is _16 bits per\n> character_.\n\nEmphasis mine. This is completely and utterly wrong! We have already learned that Unicode is a _21-bit_ encoding scheme, but with documentation like this, it's no wonder so many people believe it's 16 bits.\n\nThe [`NSString` documentation][NSString] is equally misleading:\n\n> A string object presents itself as an array of Unicode characters …. You can\n> determine how many characters a string object contains with the `length` method\n> and can retrieve a specific character with the `characterAtIndex:` method.\n> These two “primitive” methods provide basic access to a string object.\n\nThis sounds better at first glance because it does not repeat the bullshit about Unicode characters being 16 bits wide. But dig a little deeper and you'll see that [`unichar`][AppleDocUnichar], the return type of the [`characterAtIndex:`][AppleDocCharacterAtIndex] method, is just a 16-bit unsigned integer. Obviously, that's not enough to represent 21-bit Unicode characters:\n\n```objc\ntypedef unsigned short unichar;\n```\n\nThe truth is that an `NSString` object actually represents an array of _UTF-16-encoded code units_. Accordingly, the [`length`][AppleDocNSStringLength] method returns the number of code units (not characters) in the string. At the time when `NSString` was developed (it was first published in 1994 as part of Foundation Kit), Unicode was still a 16-bit encoding; the wider range and UTF-16's surrogate character mechanism were introduced with Unicode 2.0 in 1996. From today's perspective, the `unichar` type and the `characterAtIndex:` method are terribly named because they tend to promote any confusion a programmer may have between Unicode characters (code points) and UTF-16 code units. `codeUnitAtIndex:` would be a vastly better method name.\n\nIf you only remember one thing about `NSString`, make it this: `NSString` represents UTF-16-encoded text. Length, indices, and ranges are all based on UTF-16 code units. Methods based on these concepts provide unreliable information unless you know the contents of the string or take appropriate precautions. Whenever the documentation mentions characters or `unichar`s, it really talks about code units. The Apple documentation actually expresses this correctly in a later section of the String Programming Guide, though Apple continues to assign the wrong meaning to the word _character_. I highly recommend you read the section titled [Characters and Grapheme Clusters][StringProgrammingGuideCharactersAndGraphemeClusters], which explains very well what is really going on.\n\nNote that although strings are conceptually based on UTF-16, that does not imply that the class always works with UTF-16-encoded data internally. It makes no promise about the internal implementation (and you could write your own by subclassing `NSString`). As a matter of fact, `CFString` [attempts to be as memory-efficient as possible][CFStringDocStringStorage], depending on the string's content, while still retaining the capability for O(1) conversion to UTF-16 code units. You can read the [CFString source code][CFStringSourceCode] to verify this for yourself.\n\n### Common Pitfalls\n\nKnowing what you now know about `NSString` and Unicode, you should be able to recognize potentially dangerous string operations. Let's check some of them out and see how we can avoid problems. But first, we need to know how to create strings with any Unicode character sequence.\n\nBy default, Clang expects source files to be UTF-8-encoded. As long as you make sure that Xcode saves your files in UTF-8, you can directly insert any character from the Character Viewer. If you prefer to work with code points, you can enter them as `@\"\\u266A\"` (♪) for code points up to U+FFFF or `@\"\\U0001F340\"` (🍀) for code points outside the BMP. Interestingly, [C99 does not allow][C99UniversalCharacterNames] these _universal character names_ for characters in the standard C character set, so this fails:\n\n```objc\nNSString *s = @\"\\u0041\"; // Latin capital letter A\n// error: character 'A' cannot be specified by a universal character name\n```\n\nI think you should avoid using the [format specifier][StringFormatSpecifiers] %C, which takes a `unichar`, for the purpose of creating string variables, as it can easily lead to confusion between code units and code points. It can be useful for log output, though.\n\n#### Length\n\n`-[NSString length]` returns the number of `unichar`s in a string. We have seen three Unicode features why this value may be different than the actual number of (visible) characters:\n\n1. Characters outside the Basic Multilingual Plane: Remember that all characters in the BMP can be expressed as a single code unit in UTF-16. All other characters require two code units (a surrogate pair). Since virtually all characters in modern use reside in the BMP, surrogate pairs were very rare encounters in the real world. However, this has changed a few years ago, with the inclusion of emoji into Unicode, which are in Plane 1. Emoji have become so common that your code must be able to handle them correctly:\n\n```objc\nNSString *s = @\"\\U0001F30D\"; // earth globe emoji 🌍\nNSLog(@\"The length of %@ is %lu\", s, [s length]);\n// => The length of 🌍 is 2\n```\n\nThe simplest solution for this problem is a small hack. You can just ask the string to calculate the number of bytes the string would need in UTF-32 and divide by 4:\n\n```objc\nNSUInteger realLength =\n    [s lengthOfBytesUsingEncoding:NSUTF32StringEncoding] / 4;\nNSLog(@\"The real length of %@ is %lu\", s, realLength);\n// => The real length of 🌍 is 1\n```\n\n2. Combining character sequences: If an é is encoded in its decomposed form (e + ´), it counts as two code units:\n\n```objc\nNSString *s = @\"e\\u0301\"; // e + ´\nNSLog(@\"The length of %@ is %lu\", s, [s length]);\n// => The length of é is 2\n```\n\nThe result of `2` is correct in the sense that the string really contains two Unicode characters, but it does not represent the apparent length as a person would count it. You can use the method `precomposedStringWithCanonicalMapping` to normalize the string to normalization form C (precomposed characters) to get a better result:\n\n```objc\nNSString *n = [s precomposedStringWithCanonicalMapping];\nNSLog(@\"The length of %@ is %lu\", n, [n length]);\n// => The length of é is 1\n```\n\nUnfortunately, this will not work in all cases because only the most common combining character sequences are available in precomposed form — other combinations of base character and combining marks will remain as they are, even after normalization. If you really need to know the apparent length of a string as counted by a person, you must iterate over the string and count it yourself. See the section about looping for details.\n\n3. Variation sequences: These behave like decomposed combining character sequences, so the variation selector counts as a separate character.\n\n#### Random Access\n\nAccessing a `unichar` directly by its index with the `characterAtIndex:` method presents the same problems. The string may contain combining character sequences, surrogate pairs, and/or variation sequences. Apple uses the term _composed character sequence_ to refer to all these features. The terminology gets really confusing here; be careful not to confound composed character sequences (the Apple term) with combining character sequences (the Unicode term). The latter are only a subset of the former. Use the [`rangeOfComposedCharacterSequenceAtIndex:`][AppleDocrangeOfComposedCharacterSequenceAtIndex:] method to find out if the `unichar` at a given index is part of a sequence of code units that represents a single character (which, in turn, can consist of multiple code points). You should do this whenever you need to pass a range of a string with unknown contents to another method to make sure that Unicode characters don't get torn apart.\n\n#### Looping\n\nUsing `rangeOfComposedCharacterSequenceAtIndex:`, you could write a routine that correctly loops over all characters in a string, but it would be quite inconvenient to have to do this every time you want to iterate over a string. Fortunately, `NSString` has a better way in the form of the [`enumerateSubstringsInRange:options:usingBlock:`][AppleDocEnumerateSubstringsInRange] method. This method abstracts away the peculiarities of Unicode and allows you to loop over composed character sequences, words, lines, sentences, or paragraphs in a string very easily. You can even add the `NSStringEnumerationLocalized` option, which takes the user's locale into account for determining the boundaries of words and sentences. To iterate over characters, specify `NSStringEnumerationByComposedCharacterSequences`:\n\n```objc\nNSString *s = @\"The weather on \\U0001F30D is \\U0001F31E today.\";\n    // The weather on 🌍 is 🌞 today.\nNSRange fullRange = NSMakeRange(0, [s length]);\n[s enumerateSubstringsInRange:fullRange\n                      options:NSStringEnumerationByComposedCharacterSequences \n                   usingBlock:^(NSString *substring, NSRange substringRange, \n                                NSRange enclosingRange, BOOL *stop) \n{\n    NSLog(@\"%@ %@\", substring, NSStringFromRange(substringRange));\n}];\n```\n\nThis wonderful method emphasizes that Apple wants us to think of a string as a collection of substrings, rather than characters (in the Apple sense), because (a) a single `unichar` is too small a unit to represent a true Unicode character, and (b) some characters (in the common sense) are composed of multiple Unicode code points. Note that it was only added relatively recently (in OS X 10.6 and iOS 4.0). Before, looping over the characters in a string was a lot less fun.\n\n#### Comparisons\n\nString objects are not normalized unless you perform that step manually. This means that comparing strings that contain combining character sequences can potentially lead to wrong results. Both [`isEqual:`][AppleDocIsEqual] and [`isEqualToString:`][AppleDocIsEqualToString] compare strings byte for byte. If you want precomposed and decomposed variants to match, you must normalize the strings first:\n\n```objc\nNSString *s = @\"\\u00E9\"; // é\nNSString *t = @\"e\\u0301\"; // e + ´\nBOOL isEqual = [s isEqualToString:t];\nNSLog(@\"%@ is %@ to %@\", s, isEqual ? @\"equal\" : @\"not equal\", t);\n// => é is not equal to é\n\n// Normalizing to form C\nNSString *sNorm = [s precomposedStringWithCanonicalMapping];\nNSString *tNorm = [t precomposedStringWithCanonicalMapping];\nBOOL isEqualNorm = [sNorm isEqualToString:tNorm];\nNSLog(@\"%@ is %@ to %@\", sNorm, isEqualNorm ? @\"equal\" : @\"not equal\", tNorm);\n// => é is equal to é\n```\n\nYour other option is to use the [`compare:`][AppleDocCompare] method (or one of its variants like [`localizedCompare:`][AppleDocLocalizedCompare]), which returns a match for strings that are _compatibility equivalent_. This is not documented well by Apple. Note that you will often want to compare for _canonical_ equivalence instead. `compare:` does not give you that choice:\n\n```objc\nNSString *s = @\"ff\"; // ff\nNSString *t = @\"\\uFB00\"; // ﬀ ligature\nNSComparisonResult result = [s localizedCompare:t];\nNSLog(@\"%@ is %@ to %@\", s, result == NSOrderedSame ? @\"equal\" : @\"not equal\", t);\n// => ff is equal to ﬀ\n```\n\nIf you need to use `compare:` but don't want to take equivalence into account, the [`compare:options:`][AppleDocCompareOptions] variants allow you to specify `NSLiteralSearch`, which also speeds things up.\n\n#### Reading Text from Files or the Network\n\nIn general, text data is only useful if you know how that text is encoded. When you download text data from a server, you usually know its encoding or can retrieve it from the HTTP header. It is then trivial to create a string object from the data with the method [`-[NSString initWithData:encoding:]`][AppleDocNSStringInitWithDataEncoding].\n\nText files do not include their encoding in the file data itself, but `NSString` can often determine the encoding of a text file by looking at extended file attributes or by using heuristics (for example, certain binary sequences are guaranteed to not appear in a valid UTF-8 file). To read text from a file with a known encoding, use [`-[NSString initWithContentsOfURL:encoding:error:]`][AppleDocNSStringInitWithContentsOfURL]. To read files with unknown encoding, [Apple provides this guideline][StringProgrammingGuideReadingDataUnknownEncoding]:\n\n> If you are forced to guess the encoding (and note that in the absence of explicit information,\n> it is a guess):\n> \n> 1.  Try `stringWithContentsOfFile:usedEncoding:error:` or `initWithContentsOfFile:usedEncoding:error:` (or the URL-based equivalents).\n> \n>     These methods try to determine the encoding of the resource, and if successful return by reference the encoding used.\n> \n> 1.  If (1) fails, try to read the resource by specifying UTF-8 as the encoding.\n> \n> 1.  If (2) fails, try an appropriate legacy encoding.\n> \n>     “Appropriate” here depends a bit on circumstances; it might be the default C string encoding, it might be ISO or Windows Latin 1, or something else, depending on where your data are coming from.\n> \n> 1.  Finally, you can try `NSAttributedString`'s loading methods from the Application Kit (such as `initWithURL:options:documentAttributes:error:`).\n> \n>     These methods attempt to load plain text files, and return the encoding used. They can be used on more-or-less arbitrary text documents, and are worth considering if your application has no special expertise in text. They might not be as appropriate for Foundation-level tools or documents that are not natural-language text.\n\n#### Writing Text to Files\n\nI have already mentioned that your encoding of choice for plain text files, as well as your own file formats or network protocols, should be UTF-8 unless a specification prescribes something else. To write a string to a file, use the [`writeToURL:​atomically:​encoding:​error:`][AppleDocNSStringWriteToURL] method.\n\nThis method will automatically add a byte order mark to the file for UTF-16 or UTF-32. It will also store the file's encoding in an [extended file attribute][NSHipsterExtendedFileAttributes] under the name `com.apple.TextEncoding`. Since the `initWithContentsOf…:usedEncoding:error:` methods obviously know about this attribute, just using the standard `NSString` methods gets you a long way in making sure you are using the correct encoding when loading text from a file.\n\n## Conclusion\n\nText is complicated. And although Unicode has vastly improved the way software deals with text, it does not absolve developers from knowing how it works. Today, virtually every app has to deal with multilingual text. Even if your app is not localized into Chinese or Arabic, as soon as you accept _any_ input from your users, you must be prepared to deal with the whole spectrum of Unicode.\n\nYou owe the rest of the world to test your string handling routines thoroughly, and that means testing them with inputs other than plain English. Be sure to use lots of emoji and words from non-Latin scripts in your unit tests. If you don't know how to write in a certain script, I have found Wikipedia to be very useful. Just copy words from a random article in the [Wikipedia of your choice][ListOfWikipedias].\n\n## Further Reading\n\n* Joel Spolsky: [The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets][JoelOnSoftwareUnicode]. This is more than 10 years old and not specific to Cocoa, but still a very good overview.\n* [Ross Carter][RossCarter] gave a wonderful talk at NSConference 2012 titled [You too can speak Unicode][RossCarterUnicodeTalk]. It's a very entertaining talk and I highly recommend watching it. I based part of this article on Ross's presentation. Scotty from [NSConference][NSConference] was kind enough to make the video available to all objc.io readers. Thanks!\n* The [Wikipedia article on Unicode][WikipediaUnicode] is great.\n* [unicode.org][UnicodeOrg], the website of the Unicode Consortium, not only has the full standard and code chart references, but also a wealth of other interesting information. The extensive [FAQ section][UnicodeOrgFAQ] is excellent.\n\n[JoelOnSoftwareUnicode]: http://www.joelonsoftware.com/articles/Unicode.html\n[RossCarter]: https://twitter.com/RossT\n[RossCarterUnicodeTalk]: https://vimeo.com/86030221\n[NSConference]: http://nsconference.com\n[UnicodeOrg]: http://www.unicode.org\n[UnicodeOrgFAQ]: http://www.unicode.org/faq/\n[WikipediaUnicode]: http://en.wikipedia.org/wiki/Unicode\n[Foundation Kit]: http://www.cilinder.be/docs/next/NeXTStep/3.3/nd/Foundation/IntroFoundation.htmld/index.html\n[Cocoa19thBirthday]: http://blog.securemacprogramming.com/2013/10/happy-19th-birthday-cocoa/\n[NSString]: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSString_Class/Reference/NSString.html\n[WikipediaCharacterEncoding]: http://en.wikipedia.org/wiki/Character_encoding\n[WikipediaASCII]: http://en.wikipedia.org/wiki/ASCII\n[ListOfCodePages]: http://www.i18nguy.com/unicode/codepages.html\n[UnicodeHistory]: http://www.unicode.org/history/summary.html\n[UnicodeSupportedScripts]: http://www.unicode.org/standard/supported.html\n[UnicodeUnsupportedScripts]: http://www.unicode.org/standard/unsupported.html\n[WikipediaEmoji]: http://en.wikipedia.org/wiki/Emoji\n[UnicodeCodeCharts]: http://www.unicode.org/charts/index.html\n[WikipediaHanUnification]: http://en.wikipedia.org/wiki/Han_unification\n[UnicodeFAQ16BitEncoding]: http://www.unicode.org/faq/utf_bom.html#gen0\n[WikipediaUnicodePlane]: http://en.wikipedia.org/wiki/Plane_%28Unicode%29\n[WikipediaUTF32]: http://en.wikipedia.org/wiki/UTF-32\n[WikipediaUTF16]: http://en.wikipedia.org/wiki/UTF-16\n[WikipediaUTF8]: http://en.wikipedia.org/wiki/UTF-8\n[WikipediaEndianness]: http://en.wikipedia.org/wiki/Endianness\n[WikipediaByteOrderMark]: http://en.wikipedia.org/wiki/Byte_Order_Mark\n[OleBegemannOnUTF8]: http://oleb.net/blog/2012/09/utf-8/\n[WikipediaISO-8859-1]: http://en.wikipedia.org/wiki/ISO/IEC_8859-1\n[RobPikeUTF8]: http://www.cl.cam.ac.uk/~mgk25/ucs/utf-8-history.txt\n[WikipediaCyrillicA]: http://en.wikipedia.org/wiki/A_%28Cyrillic%29\n[WikipediaPrecomposedCharacter]: http://en.wikipedia.org/wiki/Precomposed_character\n[WikipediaHangul]: http://en.wikipedia.org/wiki/Hangul\n[WikipediaUnicodeEquivalence]: http://en.wikipedia.org/wiki/Unicode_equivalence\n[WikipediaHalfwidthFullwidth]: http://en.wikipedia.org/wiki/Halfwidth_and_Fullwidth_Forms_%28Unicode_block%29\n[UnicodeCharacterDuplicates]: http://www.unicode.org/standard/where/#Duplicates\n[WikipediaLigature]: http://en.wikipedia.org/wiki/Typographic_ligature\n[WikipediaUnicodeNormalization]: http://en.wikipedia.org/wiki/Unicode_normalization#Normalization\n[UnicodeStandardAnnex15NormalizationForms]: http://unicode.org/reports/tr15/\n[UnicodeStandard]: http://www.unicode.org/standard/standard.html\n[UnicodeOrgFAQNormalizationQ2]: http://www.unicode.org/faq/normalization.html#2\n[AppleDocCFStringUnicodeBasis]: https://developer.apple.com/library/ios/documentation/CoreFoundation/Conceptual/CFStrings/Articles/UnicodeBasis.html\n[CFStringSourceCode]: http://www.opensource.apple.com/source/CF/CF-855.11/CFString.c\n[CFStringDocStringStorage]: https://developer.apple.com/library/ios/documentation/CoreFoundation/Conceptual/CFStrings/Articles/StringStorage.html\n[StringProgrammingGuideCharactersAndGraphemeClusters]: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Strings/Articles/stringsClusters.html\n[StringFormatSpecifiers]: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html\n[StringProgrammingGuideReadingDataUnknownEncoding]: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/readingFiles.html#//apple_ref/doc/uid/TP40003459-SW4\n[NSHipsterExtendedFileAttributes]: http://nshipster.com/extended-file-attributes/\n[ListOfWikipedias]: http://meta.wikimedia.org/wiki/List_of_Wikipedias\n[WikipediaVariableWidthEncoding]: http://en.wikipedia.org/wiki/Variable-width_encoding\n[WikipediaUnicodePrivateUseAreas]: http://en.wikipedia.org/wiki/Private_Use_Areas\n[ApplePrivateUseAreaUsage]: http://www.unicode.org/Public/MAPPINGS/VENDORS/APPLE/CORPCHAR.TXT\n[AppleDocUnichar]: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/uid/20000154-SW40\n[AppleDocCharacterAtIndex]: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/characterAtIndex:\n[AppleDocNSStringLength]: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/length\n[AppleDocrangeOfComposedCharacterSequenceAtIndex:]: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/rangeOfComposedCharacterSequenceAtIndex:\n[AppleDocEnumerateSubstringsInRange]: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/enumerateSubstringsInRange:options:usingBlock:\n[AppleDocIsEqual]: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/isEqual:\n[AppleDocIsEqualToString]: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/isEqualToString:\n[AppleDocCompare]: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/compare:\n[AppleDocLocalizedCompare]: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/localizedCompare:\n[AppleDocCompareOptions]: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/compare:options:\n[AppleDocNSStringInitWithDataEncoding]: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/initWithData:encoding:\n[AppleDocNSStringInitWithContentsOfURL]: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/initWithContentsOfURL:encoding:error:\n[AppleDocNSStringWriteToURL]: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/writeToURL:atomically:encoding:error:\n[WikipediaGlyph]: http://en.wikipedia.org/wiki/Glyph\n[WikipediaVariantForm]: http://en.wikipedia.org/wiki/Variant_form_%28Unicode%29\n[UnicodeStandardizedVariants]: http://unicode.org/Public/UCD/latest/ucd/StandardizedVariants.html\n[UnicodeIdeographicVariationDatabase]: http://www.unicode.org/ivd/\n[C99UniversalCharacterNames]: http://c0x.coding-guidelines.com/6.4.3.html\n"
  },
  {
    "path": "2014-02-10-working-with-strings.md",
    "content": "---\ntitle:  \"Working with Strings\"\ncategory: \"9\"\ndate: \"2014-02-10 10:00:00\"\ntags: article\nauthor:\n  - name: Daniel Eggert\n    url: http://twitter.com/danielboedewadt\n---\n\n\nWe use strings in various places in every single app. Here we'll quickly take a look at some of the common ways to work with strings; it's a walkthrough of some best practices for common operations.\n\n## Sorting, Comparing, and Searching Strings\n\nSorting and comparing strings is more complex than first meets the eye. Not only can strings contain *surrogate pairs* (see [Ole's article on Unicode](/issues/9-strings/unicode/#peculiar-unicode-features)) but sorting also depends on the locale. The corner cases are quite tricky.\n\nApple's *String Programming Guide* has a section called [“Characters and Grapheme Clusters”](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/stringsClusters.html), which mentions a few of the pitfalls. For example, for sorting purposes, some European languages consider the sequence “ch” a single letter. In some languages, “ä” is considered equal to `a`, while in others it should be sorted after `z`.\n\n`NSString` has methods to help us with this complexity. First off, there's:\n\n```objc\n- (NSComparisonResult)compare:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)range locale:(id)locale\n```\n\nwhich gives us full flexibility. Then there are a slew of convenience functions that all map to the aforementioned method.\n\nThe available options for comparison are:\n\n```\nNSCaseInsensitiveSearch\nNSLiteralSearch\nNSNumericSearch\nNSDiacriticInsensitiveSearch\nNSWidthInsensitiveSearch\nNSForcedOrderingSearch\n```\n\nThese can be or'd together.\n\n`NSCaseInsensitiveSearch`: “A” is the same as “a,” though in some locales more complex things happen. For example, in German, “ß” and “SS” would be equal.\n\n`NSLiteralSearch`: Unicode point for Unicode-point comparison. This will only return equal (`NSOrderedSame`) when all characters are composed in the exact same way. `LATIN CAPITAL LETTER A` and `COMBINING RING ABOVE` is not the same as `LATIN CAPITAL LETTER A WITH RING ABOVE`.\n\n`NSNumericSearch`: This orders numbers inside strings, so that “Section 9” < “Section 20” < “Section 100.”\n\n`NSDiacriticInsensitiveSearch`: “A” is the same as “Å” and the same as “Ä.”\n\n`NSWidthInsensitiveSearch`: Some East Asian scripts (Hiragana and Katakana) have characters in full-width and half-width forms. \n\nIt's worth mentioning `-localizedStandardCompare:`, which sorts items the same way that the Finder does. It corresponds to setting the option to `NSCaseInsensitiveSearch`, `NSNumericSearch`, `NSWidthInsensitiveSearch`, and `NSForcedOrderingSearch`. If we're displaying a list of files in any UI, this is what we should use. \n\nCase-insensitive compare and diacritic-insensitive compare are relatively complicated and expensive operations. If we need to compare strings too many times that it becomes a bottleneck (e.g. sorting large datasets), a common solution is to store both the original string and a folded string. For example, our `Contact` class would have a normal `name` property and internally it would also have a `foldedName` property that would get updated automatically when the name is changed. We can then use `NSLiteralSearch` to compare the folded version of the name. `NSString` has a method to create such a folded version:\n\n```objc\n- (NSString *)stringByFoldingWithOptions:(NSStringCompareOptions)options locale:(NSLocale *)locale\n```\n\n### Searching\n\nWhen searching for a substring inside a string, the method with the most flexibility is:\n\n```objc\n- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange locale:(NSLocale *)locale\n```\n\nAgain, there are quite a few convenience methods, all of which end up calling into this one. We can pass the same options listed above, as well as these additional ones:\n\n```\nNSBackwardsSearch\nNSAnchoredSearch\nNSRegularExpressionSearch\n```\n\n`NSBackwardsSearch`: Start at the end of the string.\n\n`NSAnchoredSearch`: Only consider the start of the string, or (if combined with `NSBackwardsSearch`) only at the end of the string. This can be used to check for a prefix or suffix, as well as use case-insensitive and/or diacritic-insensitive comparison.\n\n`NSRegularExpressionSearch`: Uses regular expression. See Chris's article for more information about using regular expressions.\n\nIn addition, there's also a method called:\n\n```objc\n- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet options:(NSStringCompareOptions)mask range:(NSRange)aRange\n```\n\nInstead of searching for a string, it searches for the first character in the given character set. Even though this only searches for one character, the length of the returned range can be larger than one due to composed character sequences.\n\n\n## Lower and Upper Case\n\nWe must never use `-uppercaseString` or `-lowercaseString` for strings that are supposed to be displayed in the UI. Instead, we must use `-uppercaseStringWithLocale:`, like so:\n\n```objc\nNSString *name = @\"Tómas\";\ncell.text = [name uppercaseStringWithLocale:[NSLocale currentLocale]];\n```\n\n## Formatting Strings\n\nAnalogous to the C function `sprintf()` (part of the ANSI C89 standard), Objective C's `NSString` class has these methods:\n\n```\n-initWithFormat:\n-initWithFormat:arguments:\n+stringWithFormat:\n```\n\nNote that these formatting methods are *non-localized*. They should *not* be used for strings to be shown in the UI. For that we need to use:\n\n```\n-initWithFormat:locale:\n-initWithFormat:locale:arguments:\n+localizedStringWithFormat:\n```\n\nFlorian's article about [string localization](/issues/9-strings/string-localization/#localized-format-strings) talks about this in more detail.\n\n\nThe man page for [printf(3)](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/printf.3.html) has all the gory details on how format strings work. The format string is copied verbatim, except for the so-called conversion specification, which starts with a `%` character:\n\n```objc\ndouble a = 25812.8074434;\nfloat b = 376.730313461;\nNSString *s = [NSString stringWithFormat:@\"%g :: %g\", a, b];\n// \"25812.8 :: 376.73\"\n```\n\nWe're formatting two floating point values. Note that both single-precision `float` and double-precision `double` can be formatted with the same conversion specifications.\n\n### Objects\n\nIn addition to the conversion specifications from `printf(3)`, we can use `%@` to output an object. As noted in [Object Description](#object-description), if the object responds to `-descriptionWithLocale:`, that gets called. Otherwise `-description` gets called. The %@ sequence is then replaced with the result.\n\n### Integer Values\n\nWhen using integer numbers, there are a few things to be aware of. First, there are conversion specifications for signed (`d` and `i`) and unsigned (`o`, `u`, `x`, and `X`). Then there are modifiers that specify what the type of these are.\n\nIf we're using something that's not in the list of types that printf knows about, we have to typecast the value. The same thing goes for types such as `NSUInteger`, which is not the same on 64-bit and 32-bit platforms. Here's an example that works for both 32-bit and 64-bit platforms:\n\n```objc\nuint64_t p = 2305843009213693951;\nNSString *s = [NSString stringWithFormat:@\"The ninth Mersenne prime is %llu\", (unsigned long long) p];\n// \"The ninth Mersenne prime is 2305843009213693951\"\n```\n\n<table>\n  <thead>\n  <tr><th style='text-align: left'>Modifier          </th><th style='text-align: left'>d, i           </th><th style='text-align: left'>o, u, x, X</th></tr>\n  </thead>\n  <tbody>\n  <tr><td>hh                </td><td>signed char    </td><td>unsigned char</td></tr>\n  <tr><td>h                 </td><td>short          </td><td>unsigned short</td></tr>\n  <tr><td>(none)            </td><td>int            </td><td>unsigned int</td></tr>\n  <tr><td>l (ell)           </td><td>long           </td><td>unsigned long</td></tr>\n  <tr><td>ll (ell ell)      </td><td>long long      </td><td>unsigned long long</td></tr>\n  <tr><td>j                 </td><td>intmax_t       </td><td>uintmax_t</td></tr>\n  <tr><td>t                 </td><td>ptrdiff_t      </td><td></td></tr>\n  <tr><td>z                 </td><td>               </td><td>size_t</td></tr>\n  </tbody>\n</table>\n\nThe conversion specifiers for integer numbers work like this:\n\n```objc\nint m = -150004021;\nuint n = 150004021U;\nNSString *s = [NSString stringWithFormat:@\"d:%d i:%i o:%o u:%u x:%x X:%X\", m, m, n, n, n, n];\n// \"d:-150004021 i:-150004021 o:1074160465 u:150004021 x:8f0e135 X:8F0E135\"\n```\n\n`%d` and `%i` both do the same thing. They simply print the signed decimal value. `%o` is slightly obscure: it uses the [octal](https://en.wikipedia.org/wiki/Octal) notation. `%u` gives us the unsigned decimal value -- it's what we usually want. Finally, `%x` and `%X` use hexadecimal notation -- the latter with capital letters.\n\nFor `%x` and `%X`, we can use a `#` flag to prefix `0x` in front of the string to make it more obvious that it's a hexadecimal value.\n\nAnd we can pass a minimum field width and a minimum number of digits (both are zero if omitted), as well as left / right alignment. Check the man page for details. Here are some samples:\n\n```objc\nint m = 42;\nNSString *s = [NSString stringWithFormat:@\"'%4d' '%-4d' '%+4d' '%4.3d' '%04d'\", m, m, m, m, m];\n// \"[  42] [42  ] [ +42] [ 042] [0042]\"\nm = -42;\nNSString *s = [NSString stringWithFormat:@\"'%4d' '%-4d' '%+4d' '%4.3d' '%04d'\", m, m, m, m, m];\n// \"[ -42] [-42 ] [ -42] [-042] [-042]\"\n```\n\n`%p` is what we'd use to print pointer values -- it's similar to `%#x` but does the correct thing on both 32-bit and 64-bit platforms.\n\n\n### Floating Point Values\n\nThere are eight conversion specifiers for floating-point values: `eEfFgGaA`. But we'll hardly ever need anything except for '%f' and '%g'. The uppercase version uses an uppercase `E`, while the the lowercase version uses a lowercase `e` for exponential components.\n\nUsually `%g` is the go-to conversion specifier for floating-point values. The difference to `%f` is best illustrated with this sample:\n\n```objc\ndouble v[5] = {12345, 12, 0.12, 0.12345678901234, 0.0000012345678901234};\nNSString *s = [NSString stringWithFormat:@\"%g %g %g %g %g\", v[0], v[1], v[2], v[3], v[4]];\n// \"12345 12 0.12 0.123457 1.23457e-06\"\nNSString *s = [NSString stringWithFormat:@\"%f %f %f %f %f\", v[0], v[1], v[2], v[3], v[4]];\n// \"12345.000000 12.000000 0.120000 0.123457 0.000001\"\n```\n\nLike with integer values, we can specify a minimum field width and a minimum number of digits.\n\n\n### Specifying Positions\n\nThe format string allows the parameters to be *consumed* in another order:\n\n```objc\n[NSString stringWithFormat:@\"%2$@ %1$@\", @\"1st\", @\"2nd\"];\n// \"2nd 1st\"\n```\n\nWe simply have to put the 1-based index of the parameter and a `$` sign after the `%`. This is mostly relevant for localized strings, because the order in which certain parts occur in the string might be different for other languages.\n\n### NSLog()\n\nThe `NSLog()` function works the same way as `+stringWithFormat:`. When we call:\n\n```objc\nint magic = 42;\nNSLog(@\"The answer is %d\", magic);\n```\n\nthe code will construct the string in the same way as:\n\n```objc\nint magic = 42;\nNSString *output = [NSString stringWithFormat:@\"The answer is %d\", magic];\n```\n\nObviously `NSLog()` will then also output the string. And it prefixes it with a timestamp, process name, process identifier, and thread identifier.\n\n### Implementing Methods that take Format Strings\n\nIt's sometimes convenient to provide a method on our own class that also takes a format string. Let's say we're implementing a To Do app which has an `Item` class. We want to provide:\n\n```objc\n+ (instancetype)itemWithTitleFormat:(NSString *)format, ...\n```\n\nso we can use it with:\n\n```objc\nItem *item = [Item itemWithFormat:@\"Need to buy %@ for %@\", food, pet];\n```\n\nThis kind of method, which takes a variable number of arguments, is called a *variadic* method. We have to use the macros defined in `stdarg.h` to use these. An implementation of the above method would look like this:\n\n```objc\n+ (instancetype)itemWithTitleFormat:(NSString *)format, ...;\n{\n    va_list ap;\n    va_start(ap, format);\n    NSString *title = [[NSString alloc] initWithFormat:format locale:[NSLocale currentLocale] arguments:ap];\n    va_end(ap);\n    return [self itemWithTitle:title];\n}\n```\n\nAdditionally, we should add `NS_FORMAT_FUNCTION` to the method definition (in the header file), like so:\n\n```objc\n+ (instancetype)itemWithTitleFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);\n```\n\nThe `NS_FORMAT_FUNCTION` expands to a method `__attribute__`, which tells the compiler that the argument at index **1** is a format string, and that the arguments start at index **2**. This allows the compiler to check the format string and output warnings in the same way it would do for `NSLog()` and `-[NSString stringWithFormat:]`.\n\n## Characters and String Components\n\nGiven a string like \"bird,\" it is straightforward to know what the individual letters are. The second letter is an \"i\" (Unicode: `LATIN SMALL LETTER I`). For a string like [Åse](https://en.wikipedia.org/wiki/Åse), it's not that simple.\n\nWhat looks like three characters can be represented in several ways, e.g.\n\n```\nA    LATIN CAPITAL LETTER A\n ̊    COMBINING RING ABOVE\ns    LATIN SMALL LETTER S\ne    LATIN SMALL LETTER E\n```\n\nor\n\n```\nÅ    LATIN CAPITAL LETTER A WITH RING ABOVE\ns    LATIN SMALL LETTER S\ne    LATIN SMALL LETTER E\n```\n\nRead more about combining marks in [Ole's article on Unicode](/issues/9-strings/unicode/#peculiar-unicode-features). Other scripts have more complicated surrogate pairs.\n\nIf we need to work on the character level of a string, we need to be careful. Apple's *String Programming Guide* has a section called [“Characters and Grapheme Clusters”](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/stringsClusters.html) that goes into more detail about this. \n\n`NSString` has these two methods:\n\n```\n-rangeOfComposedCharacterSequencesForRange:\n-rangeOfComposedCharacterSequenceAtIndex:\n```\n\nthat help us if we need to, for example, split a string, to make sure that we don't split so-called *surrogate pairs*. The range can then be passed to `-substringWithRange:`.\n\nIf we need to need to work with the characters of a string, `NSString` has a method called:\n\n```\n-enumerateSubstringsInRange:options:usingBlock:\n```\n\nPassing `NSStringEnumerationByComposedCharacterSequences` as the option will allow us to scan through all characters. For example, with the below method, we'd turn the string “International Business Machines” into “IBM”:\n\n```objc\n- (NSString *)initials;\n{\n    NSMutableString *result = [NSMutableString string];\n    [self enumerateSubstringsInRange:NSMakeRange(0, self.length) options:NSStringEnumerationByWords | NSStringEnumerationLocalized usingBlock:^(NSString *word, NSRange wordRange, NSRange enclosingWordRange, BOOL *stop1) {\n        __block NSString *firstLetter = nil;\n          [self enumerateSubstringsInRange:NSMakeRange(0, word.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *letter, NSRange letterRange, NSRange enclosingLetterRange, BOOL *stop2) {\n              firstLetter = letter;\n              *stop2 = YES;\n          }];\n          if (firstLetter != nil) {\n              [result appendString:firstLetter];\n        };\n    }];\n    return result;\n}\n```\n\nAs noted in the documentation, word and sentence boundaries may also change depending on the locale. Hence the `NSStringEnumerationLocalized` option.\n\n\n\n## Multi-Line String Literals\n\nAn admittedly obscure feature of the compiler is that it will join several string literals separated by nothing but white space. What does that mean? These two are identical:\n\n```objc\nNSString *limerick = @\"A lively young damsel named Menzies\\n\"\n@\"Inquired: «Do you know what this thenzies?»\\n\"\n@\"Her aunt, with a gasp,\\n\"\n@\"Replied: \"It's a wasp,\\n\"\n@\"And you're holding the end where the stenzies.\\n\";\n```\n\nand:\n\n```objc\nNSString *limerick = @\"A lively young damsel named Menzies\\nInquired: «Do you know what this thenzies?»\\nHer aunt, with a gasp,\\nReplied: \"It's a wasp,\\nAnd you're holding the end where the stenzies.\\n\";\n```\n\nThe former is easier on the eye. Just be sure not to insert a semicolon or comma at the end of any lines.\n\nWe can also do things like\n\n```objc\nNSString *string = @\"The man \" @\"who knows everything \" @\"learns nothing\" @\".\";\n```\n\nThe pieces are concatenated at compile time. It's merely a convenience provided by our friend, the compiler.\n\n\n\n## Mutable Strings\n\nThere are two common scenarios where mutable strings are useful: (1) when piecing strings together from smaller parts, and (2) when replacing parts of a string.\n\n### Building Strings\n\nMutable strings make your code easier when you need to build up your string from multiple pieces:\n\n```objc\n- (NSString *)magicToken\n{\n    NSMutableString *string = [NSMutableString string];\n    if (usePrefix) {\n        [string appendString:@\">>>\"];\n    }\n    [string appendFormat:@\"%d--%d\", self.foo, self.bar];\n    if (useSuffix) {\n        [string appendString:@\">>>\"];\n    }\n    return string;\n}\n```\n\nAlso note how we're simply returning an instance of `NSMutableString` to the caller.\n\n### Replacing Substrings\n\nAside from appending, `NSMutableString` also has these four methods:\n\n```\n-deleteCharactersInRange:\n-insertString:atIndex:\n-replaceCharactersInRange:withString:\n-replaceOccurrencesOfString:withString:options:range:\n```\n\nThese are similar to the `NSString` methods:\n\n```\n-stringByReplacingOccurrencesOfString:withString:\n-stringByReplacingOccurrencesOfString:withString:options:range:\n-stringByReplacingCharactersInRange:withString:\n```\n\nbut they don't create a new string -- they mutate the string in place. This can make your code easier to read and will most likely also improve performance:\n\n```objc\nNSMutableString *string; // assume we have this\n// Remove prefix string:\nNSString *prefix = @\"WeDon’tWantThisPrefix\"\nNSRange r = [string rangeOfString:prefix options:NSAnchoredSearch range:NSMakeRange(0, string.length) locale:nil];\nif (r.location != NSNotFound) {\n    [string deleteCharactersInRange:r];\n}\n```\n\n## Joining Components\n\nA seemingly trivial, yet common case is joining strings. Let's say we have a few strings:\n\n```\nHildr\nHeidrun\nGerd\nGuðrún\nFreya\nNanna\nSiv\nSkaði\nGróa\n```\n\nand we want to create the string:\n\n```\nHildr, Heidrun, Gerd, Guðrún, Freya, Nanna, Siv, Skaði, Gróa\n```\n\nWe can do this with:\n\n```objc\nNSArray *names = @[\"Hildr\", @\"Heidrun\", @\"Gerd\", @\"Guðrún\", @\"Freya\", @\"Nanna\", @\"Siv\", @\"Skaði\", @\"Gróa\"];\nNSString *result = [names componentsJoinedByString:@\", \"];\n```\n\nIf we were to display this to users, we'd want to use the locale and make sure we replace the last part with “, and”:\n\n```objc\n@implementation NSArray (ObjcIO_GroupedComponents)\n\n- (NSString *)groupedComponentsWithLocale:(NSLocale *)locale;\n{\n    if (self.count < 1) {\n        return @\"\";\n    } else if (self.count < 2) {\n        return self[0];\n    } else if (self.count < 3) {\n        NSString *joiner = NSLocalizedString(@\"joiner.2components\", @\"\");\n        return [NSString stringWithFormat:@\"%@%@%@\", self[0], joiner, self[1]];\n    } else {\n        NSString *joiner = [NSString stringWithFormat:@\"%@ \", [locale objectForKey:NSLocaleGroupingSeparator]];\n        NSArray *first = [self subarrayWithRange:NSMakeRange(0, self.count - 1)];\n        NSMutableString *result = [NSMutableString stringWithString:[first componentsJoinedByString:joiner]];\n        \n        NSString *lastJoiner = NSLocalizedString(@\"joiner.3components\", @\"\");\n        [result appendString:lastJoiner];\n        [result appendString:self.lastObject];\n        return result;\n    }\n}\n\n@end\n```\n\nand then have:\n\n```\n\"joiner.2components\" = \" and \";\n\"joiner.3components\" = \", and \";\n```\n\nfor US English or:\n\n```\n\"joiner.2components\" = \" und \";\n\"joiner.3components\" = \" und \";\n```\n\nfor German.\n\n\nThe inverse of joining components can be done with the `-componentsSeparatedByString:` method, which turns a string into an array, e.g. “12|5|3” into “12,” “5,” and “3.”\n\n\n<a name=\"object-description\"> </a>\n## Object Description\n\nIn many object-oriented programming languages, it's common for objects to have a `toString()` or similarly named method. In Objective C, this method is:\n\n```objc\n- (NSString *)description\n```\n\nalong with its sibling:\n\n```objc\n- (NSString *)debugDescription\n```\n\nIt is good practice to override `-description` for model objects in such a way that the return value can be used to display the object in UI. Let's say we have a `Contact` class. It would make sense to implement:\n\n```objc\n- (NSString *)description\n{\n    return self.name;\n}\n```\n\nwhich would allow us to use format strings, like so:\n\n```objc\nlabel.text = [NSString stringWithFormat:NSLocalizedString(@\"%@ has been added to the group “%@”.\", @\"\"), contact, group];\n```\n\nSince this string is for the UI, we may need access to the locale. If that's the case, we can instead override:\n\n```objc\n- (NSString *)descriptionWithLocale:(NSLocale *)locale;\n```\n\nThe format sequence `%@` looks for `-descriptionWithLocale:` first, and falls back to `-description`.\n\nInside the debugger, we can print and object with `po` (short for print object):\n\n```\n(lldb) po contact\n```\n\nThis will call `-debugDescription` on the object. By default, `-debugDescription` calls `-description`. If we want to output different info, simply override both. In most cases (particularly for non-model objects) simply overriding `-description` will fit the bill.\n\nThe de-facto standard output format for objects is:\n\n```objc\n- (NSString *)description;\n{\n    return [NSString stringWithFormat:@\"<%@: %p>\", self.class, self];\n}\n```\n\nThis is what `NSObject` will return to us. When we override this method, it most likely makes sense to use this as a starting point. If we have a `DetailViewController` that controls UI to display a `contact`, we might want to implement it, like so:\n\n```objc\n- (NSString *)description;\n{\n    return [NSString stringWithFormat:@\"<%@: %p> contact = %@\", self.class, self, self.contact.debugDescription];\n}\n```\n\n### Description of `NSManagedObject` Subclasses\n\nWe should take special care when adding `-description` / `-debugDescription` to subclasses of `NSManagedObject`. Core Data's faulting mechanism allows for objects to be around without their data. We most likely don't want to alter the state of our application when calling `-debugDescription`, hence we should make sure to check `isFault`. For example, we might implement it like this:\n\n```objc\n- (NSString *)debugDescription;\n{\n    NSMutableString *description = [NSMutableString stringWithFormat:@\"<%@: %p>\", self.class, self];\n    if (! self.isFault) {\n        [description appendFormat:@\" %@ \\\"%@\\\" %gL\", self.identifier, self.name, self.metricVolume];\n    }\n    return description;\n}\n```\n\nAgain, since these are model objects, it makes sense to override `-description` to simply return the property that describes the instance such as the `name`.\n\n\n## File Paths\n\nThe short story is that we shouldn't use `NSString` for file paths. As of OS X 10.7 and iOS 5, `NSURL` is just as convenient to use, and is more efficient, as it's able to cache file system properties.\n\nAdditionally, `NSURL` has eight methods for accessing so-called *resource values*, which give a stable interface to get and set various properties of files and directories, such as localized file name (`NSURLLocalizedNameKey`), file size (`NSURLFileSizeKey`), and creation date (`NSURLCreationDateKey`), to name a few.\n\nParticularly when enumerating directory content, using `-[NSFileManager enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:]` with the list of *keys*, and then retrieving them with `-getResourceValue:forKey:error:`, can give substantial performance boosts.\n\nHere's a short example on how to put this together:\n\n```objc\nNSError *error = nil;\nNSFileManager *fm = [[NSFileManager alloc] init];\nNSURL *documents = [fm URLForDirectory:NSDocumentationDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:&error];\nNSArray *properties = @[NSURLLocalizedNameKey, NSURLCreationDateKey];\nNSDirectoryEnumerator *dirEnumerator = [fm enumeratorAtURL:documents\n                                includingPropertiesForKeys:properties\n                                                   options:0\n                                              errorHandler:nil];\nfor (NSURL *fileURL in dirEnumerator) {\n    NSString *name = nil;\n    NSDate *creationDate = nil;\n    if ([fileURL getResourceValue:&name forKey:NSURLLocalizedNameKey error:NULL] &&\n        [fileURL getResourceValue:&creationDate forKey:NSURLCreationDateKey error:NULL])\n    {\n        NSLog(@\"'%@' was created at %@\", name, creationDate);\n    }\n}\n```\n\nWe're passing the keys for properties into the `-enumeratorAtURL:...` method, which will make sure they're fetched in a very efficient manner as we enumerate the directory content. Inside the loop, the calls to `-getSourceValue:...` will then simply get the already cached values from that `NSURL` without having to touch the file system.\n\n## Passing Paths to UNIX APIs\n\nBecause Unicode is very complex and can represent the same letter in multiple ways, we need to be careful when passing paths to UNIX APIs. We must absolutely not use `UTF8String` in these cases. The correct thing is to use the `-fileSystemRepresentation` method, like so:\n\n```objc\nNSURL *documentURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:NULL];\ndocumentURL = [documentURL URLByAppendingPathComponent:name];\nint fd = open(documentURL.fileSystemRepresentation, O_RDONLY);\n```\n\nThe very same thing goes for `NSString` as for `NSURL`. If we fail to do this, we'll see random failure when opening files that have any composed characters in their name or anywhere in their path. On OS X, this is particularly bad when the user's short name happens to contain composed characters, e.g. `tómas`.\n\nCommon cases where we need a `char const *` version of a path are the UNIX `open()` and `close()` commands. But this also occurs with GCD / libdispatch's I/O API:\n    \n```objc\ndispatch_io_t\ndispatch_io_create_with_path(dispatch_io_type_t type,\n    const char *path, int oflag, mode_t mode,\n    dispatch_queue_t queue,\n    void (^cleanup_handler)(int error));\n```\n\nIf we want to use this with an `NSString`, we need to make sure to do it like this:\n\n```objc\nNSString *path = ... // assume we have this\nio = dispatch_io_create_with_path(DISPATCH_IO_STREAM,\n    path.fileSystemRepresentation,\n    O_RDONLY, 0, queue, cleanupHandler);\n```\n\nWhat `-fileSystemRepresentation` does is that it first converts the string to the file system's [normalization form](/issues/9-strings/unicode/#normalization-forms) and then encodes it as UTF-8.\n\n"
  },
  {
    "path": "2014-03-07-data-synchronization.md",
    "content": "---\ntitle:  \"Data Synchronization\"\ncategory: \"10\"\ndate: \"2014-03-07 11:00:00\"\ntags: article\nauthor:\n  - name: Drew McCormack\n    url: https://twitter.com/drewmccormack\n---\n\n<br/>\n\n> Think of sync or all you sync will sink.\n>\n> _Not Dr. Seuss_\n\nSynchronization is a fundamental element of software development. It takes many forms, from forcing clocks on different devices to agree on just how late it is, to serializing access to resources in multithreaded programming with a `@synchronized` block. \n\nIn this article, I want to introduce various approaches to _data synchronization_, which I'll refer to from here on simply as _sync_. In short, the question is: how do you keep two data stores, separated by space and time, mirroring one another as closely as possible?\n\nMy own interest goes back to the early days of the iOS App Store, and sync has played a major role in how I've earned my living ever since. I am the developer of a study card application called [Mental Case](http://mentalcaseapp.com). With versions for Mac, iPad, and iPhone, Mental Case is more a suite than a single app, and one of its distinguishing features has always been the ability to sync up your study material and progress between devices. Originally, in the era of the digital hub, Mental Case would sync a central Mac over a local Wi-Fi network with one or more iOS devices. Currently, the Mental Case apps sync peer-to-peer via iCloud.\n\nProperly implementing sync can be challenging, but it is a problem that is more specialized than developing a general web service, and this allows for more specialized solutions. For example, where a generic web service will invariably require server-side development, it is possible to adopt a synchronization framework with minimal changes to your existing codebase, and no server-side code at all.\n\nIn what follows, I want to introduce the various approaches to sync that have arisen since the early days of mobile devices, explain their workings at a high level, and give some guidance as to which would work best for your app. I'll also delineate the new trends in sync, which point to where we are heading.\n\n## A Brief History\n\nBefore looking at the various approaches to sync in detail, it's worth examining how it has evolved and adapted to the constraints imposed by the technology of the day. \n\nAs far as consumer devices are concerned, sync began with wired connections. In the late 1990s and early 2000s, peripheral devices such as the [Palm Pilot](http://en.wikipedia.org/wiki/PalmPilot) and [iPod](http://en.wikipedia.org/wiki/IPod) would sync with a Mac or PC via Firewire or USB. Apple's Digital Hub strategy built upon this approach. Later, with network speeds increasing, Wi-Fi and Bluetooth supplemented the wire to some extent, but iTunes continues to use this approach today.\n\nAs cloud services took off later in the 2000s, the role played by the central Mac/PC shifted to the cloud. The cloud has the advantage that it is accessible whenever a device has a network, and it's always on. With cloud-based sync, there was no longer a need to be at home in the vicinity of your computer to sync.\n\nEach of the approaches above utilizes what I'll term _Synchronous Communication (SC)_ between devices. An app on your iPhone communicates directly with a Mac or cloud service, and it expects to receive a response in real time. \n\nAt present, we are seeing the rise of a new approach to sync, one that is built upon _Asynchronous Communication (AC)_. Rather than 'talking' directly to the cloud, the app exchanges data with a framework or local file system. The app does not expect an immediate response; instead, data is transferred to and from the cloud in the background. \n\nThis approach decouples the application code from the sync process, freeing the developer from explicitly handling sync operations. Examples of products following this new direction are Apple's [Core Data&mdash;iCloud framework](https://developer.apple.com/library/ios/documentation/General/Conceptual/iCloudDesignGuide/Chapters/DesignForCoreDataIniCloud.html), the [Dropbox Datastore API](https://www.dropbox.com/developers/datastore), and even document stores like [TouchDB](http://labs.couchbase.com/TouchDB-iOS/) (which is based on Apache's [CouchDB project](http://couchdb.apache.org)).\n\nThis history of sync does not follow a single linear path. Each stage overlaps with those that follow, continuing to be utilized even as new approaches evolve. Today, all of these techniques still exist and are in active use, and each may be an appropriate solution to your particular problem.\n\n## The Sync Grid\n\nWe've already seen that approaches to sync can be categorized according to whether they involve synchronous communications, but it is useful to break things down even further according to whether a 'smart' server is involved, or whether the process is essentially peer-to-peer, with the client apps handling all of the complexities. This leads to a simple grid into which all sync technologies fall:\n\n<table>\n<tr><td></td> <td><strong>Synchronous</strong></td> <td><strong>Asynchronous</strong></td> </tr>\n<tr>\n\t<td style=\"margin-right:1em\"><strong>Client-Server</strong></td> \n\t\t<td style=\"margin-right:1em\">\n\t\t<a href=\"https://parse.com\">Parse</a><br/>\n\t\tStackMob<br/>\n\t\t<a href=\"http://www.windowsazure.com/en-us/services/mobile-services/\">Windows Azure Mobile Services</a><br/>\n\t\t<a href=\"http://helios.io\">Helios</a><br/>\n\t\tCustom Web Service\n\t\t</td> \n\t\t<td>\n\t\t<a href=\"https://www.dropbox.com/developers/datastore\">Dropbox Datastore</a><br/>\n\t\t<a href=\"http://labs.couchbase.com/TouchDB-iOS/\">TouchDB</a><br />\n\t\t<a href=\"http://www.wasabisync.com\">Wasabi Sync</a><br/>\n\t\t<a href=\"http://zumero.com\">Zumero</a>\n\t\t</td>\n</tr>\n<tr>\n\t<td><strong>Peer-to-Peer</strong></td> \n\t\t<td>\n\t\t\tiTunes/iPod<br />\n\t\t\tPalm Pilot<br />\n\t\t</td>\n\t\t<td>\n\t\t<a href=\"https://developer.apple.com/library/ios/documentation/General/Conceptual/iCloudDesignGuide/Chapters/DesignForCoreDataIniCloud.html\">Core Data with iCloud</a><br/>\n\t\t<a href=\"https://github.com/nothirst/TICoreDataSync\">TICoreDataSync</a><br/>\n\t\t<a href=\"https://github.com/drewmccormack/ensembles\">Core Data Ensembles</a><br />\n\t\t</td>\n</tr>\n</table>\n\nThe Synchronous Peer-to-Peer (S&ndash;P2P) approach was actually the first to be broadly adopted, and used for peripheral devices like iPods and PDAs. S-P2P tends to be simpler to implement, and local networks are fast. iTunes still uses this approach, due to the large quantities of media transfer involved.\n\n![Synchronous Peer-to-Peer](/images/issue-10/sp2p.png)\n*Synchronous Peer-to-Peer (S&ndash;P2P)*\n\nThe Synchronous Client-Server (S-CS) approach grew in popularity as networks improved and cloud services like [Amazon Web Services](http://aws.amazon.com) (AWS) became popular. S-CS is probably the most common approach to sync in use today. From a implementation standpoint, it is much the same as developing any other web service. Typically a custom cloud app is developed in a language, and with a programming stack unrelated to the client app, such as [Ruby on Rails](http://rubyonrails.org), [Django](https://www.djangoproject.com), or [Node.js](http://nodejs.org). Communication with the cloud is slower than using a local network, but S-CS has the advantage of being 'always on,' and the client can sync from any location with network connectivity.\n\n![Synchronous Client-Server](/images/issue-10/scs.png)<br />\n*Synchronous Client-Server (S&ndash;CS)*\n\nWith Asynchronous Client-Server (A-CS), the developer adopts an API for data storage, which gives access to a local copy of the data. Sync occurs transparently in the background, with the application code being informed of changes via a callback mechanism. Examples of this approach include the [Dropbox Datastore API](https://www.dropbox.com/developers/datastore), and -- for Core Data developers -- the [Wasabi Sync](http://www.wasabisync.com) service. \n\nOne advantage of the asynchronous _replicate and sync_ approach is that apps continue to work and have access to the user's data when the network is unavailable. Another is that the developer is less burdened with the details of communications and sync, and can focus on other aspects of the app, treating data storage almost as if it were local to the device. \n\n![Asynchronous Client-Server](/images/issue-10/acs.png)<br />\n*Asynchronous Client-Server (A&ndash;CS)*\n\nThe Asynchronous Peer-to-Peer (A-P2P) approach is still in its infancy, and has not seen widespread use. A-P2P places the full burden of piecing together the 'truth' on the client app, without any recourse to direct communication. Developing an A&ndash;P2P framework is complex, and that has led to some well-publicized failures, including early attempts by Apple to add iCloud support to Core Data (recent attempts are much improved). As with A&ndash;CS, each device has a full copy of the data store. The stores are kept in sync by communicating changes between devices via a series of files, typically referred to as _transaction logs_. The logs are moved to the cloud, and from there to other devices by a basic file handling server (e.g.\niCloud, Dropbox), which has no insight into the file content.\n\n![Asynchronous Peer-to-Peer](/images/issue-10/ap2p.png)<br />\n*Asynchronous Peer-to-Peer (A&ndash;P2P)*\n\nGiven the complexities of developing an A-P2P system, you might ask why we should even bother. One major advantage of A-P2P frameworks is that they abstract away the need for an intelligent server. The developer can avoid all server-side development, and can take advantage of the multitude of file transfer services available, many of which are free. And because A-P2P systems are not coupled to a particular service, there is no danger of being locked in to a single vendor.\n\n## The Elements of Sync\n\nHaving introduced the different families of sync algorithms, I now want to take a look at the common components of these algorithms. What do you have to consider over and above what you would need to handle in an isolated app?\n\nThere are a few elements that all sync methods have in common. These include:\n* Identifying corresponding objects across stores\n* Determining what has changed since the last sync \n* Resolving conflicts due to concurrent changes\n\nIn the sections that follow, I want to address each of these, before moving on to explain in more detail how you would go about implementing the algorithms.\n\n### Identity\n\nIn standalone apps, with a single store, objects are typically identified by a row index in a database table, or something equivalent like an `NSManagedObjectID` in Core Data. These identities are specific to the store, and not suitable for identifying corresponding objects on different devices. When an app syncs, it's important that objects in different stores can be correlated with one another, hence the need for _global identifiers_. \n\nGlobal identifiers are often just [Universally Unique Identifiers (UUIDs)](http://en.wikipedia.org/wiki/Universally_unique_identifier); objects in different stores with the same global identifier are considered to be logically representative of a single instance. Changes to one object should eventually result in the corresponding object also being updated. (UUIDs can be created in Cocoa with the recently added `NSUUID` class, or the oft-forgotten `globallyUniqueString` method of the `NSProcessInfo` class.)\n\nUUIDs are not appropriate for all objects. For example, it is not unusual to have certain classes of objects for which there are a fixed set of members to choose from. A common example is a singleton object, for which only one possible object is allowed. Another example is that of tag-like objects, where uniqueness is determined by a string. \n\nHowever a class determines object identity, it is important that it is reflected in the global identifiers. Logically equivalent objects in different stores should have the same identifier, and objects that are not equivalent should have different identifiers.\n\n### Change Tracking\n\n_Change Tracking_ is a term used to describe how a sync algorithm determines what has changed since the last synchronization event, and thereby what should be changed locally. Each change to an object (often called a _delta_) is usually handled as a [CRUD](http://en.wikipedia.org/wiki/Create,_read,_update_and_delete) operation: a creation, read, update, or deletion. \n\nOne of the first choices that needs to be made is the granularity that the recorded changes will take. Should all properties in an entity be updated if any single property changes? Or should only the changed property be recorded? The right path may vary; I'll discuss this more as we delve into details.\n\nIn either case, you need a means to record a change. In the simplest case, this could just be a Boolean attribute in the local store, indicating whether the object is new or has been updated since the last sync. In a more advanced algorithm, a change could be stored outside the main store as a dictionary of changed properties and with an associated timestamp.\n\n### Conflict Resolution\n\nWhen you have two or more stores representing the same logical set of data, the potential for _conflicts_ exists. A change to an object in one store could occur at about the same time as a change to the corresponding object in a second store, with no intervening sync. These changes are said to have occurred _concurrently_, and some action may be necessary to leave the conflicting objects in a consistent and valid state across all stores once they sync up.\n\nIn the simplest of all worlds, reading and writing a store can be considered an atomic operation, and resolving conflicts simply involves choosing which version of the store to keep. This is actually more common than you might think. For example, the document-syncing capabilities of iCloud are handled this way: when a conflict arises, the user is asked to choose the version he or she wishes to keep -- changes from the conflicting stores are not merged.\n\nThere are many ways to decide which changes take precedence when resolving conflicts. If a central server is involved, the most straightforward approach is just to assume the latest sync operation takes priority. Any change present in the operation overwrites previously stored values. More complex systems involve comparing the creation timestamps of conflicting changes and keeping the most recent.\n\nConflict resolution can get tricky, and if you have the choice, you should avoid it altogether by devising a model that simply cannot become invalid due to concurrent changes. In a new project, this is much easier than trying to think of all the possible invalid states that could arise.\n\nRelationships can be particularly troublesome (and that's not a commentary on human interactions). Take a simple one-to-one relationship between entities `A` and `B`. Imagine `Device 1` and `Device 2` both begin with object `A[1]` related to object `B[1]`. `Device 1` creates an object `B[2]`, relates `A[1]` to `B[2]`, and deletes `B[1]`. Concurrently, `Device 2` also deletes `B[1]`, but creates `B[3]` and relates that to `A[1]`. \n\n![Orphaned object](/images/issue-10/orphan.png)<br />\n*Orphaned object arising from conflicting changes to a one-to-one relationship.*\n\nAfter synchronizing, there will be an extra, orphaned `B` object that is unrelated to any `A`. If there were to be a validation rule requiring the relationship, you would now have an invalid object graph. And this is about the simplest type of relationship you can imagine. There are many other twists and turns possible when relationships are involved.\n\nHowever conflicts like this are resolved, it is important that the resolution be deterministic. If the same scenario occurs on two different devices, they should end up taking the same action. \n\nThis may seem obvious, but it is very easy to get wrong. Take the example above. If your resolution involves randomly picking one of the `B` objects to delete, at some point the two devices are going to delete different objects, and you will end up with no `B` objects at all. You should strive to delete corresponding `B` objects from each device. This can be achieved by sorting the objects first, and always picking the same object.\n\n## Synchronous Communication, Peer-to-Peer (S-P2P)\n\nNow that we have covered common elements of all sync algorithms, let's finish off by looking at the specific approaches introduced earlier in more detail, beginning with SC methods. \n\nLet's start with the simplest workable S&ndash;P2P solution imaginable. Assume we have a Mac app like iTunes, which communicates synchronously with an iPhone via USB, Bluetooth, or Wi-Fi. With a fast local network, we don't have to be so concerned about restricting data transfer, so we can be lazy in that respect.\n\nThe first time a particular iPhone syncs, the two apps discover each other via Bonjour, and the Mac app zips up its entire store, sending the resulting file to the iPhone app via a socket, which would unzip and install it.\n\nNow imagine the user takes the iPhone and makes changes to an existing object (e.g. gives a star rating to a song). The app on the device sets a Boolean flag on the object to indicate it is new or updated (e.g. `changedSinceSync`).\n\nWhen the next sync occurs, the iPhone app zips and sends its entire data store back to the Mac. The Mac loads the store, looks for modified instances, and updates its own data accordingly. The Mac then sends back a full copy of its updated store, replacing the existing iPhone store, and the whole process starts over again. \n\nThere are many variations and improvements possible, but this is a working solution, and will be sufficient for many apps. To summarize, a sync operation involves one device transferring its store to another device, which determines what has changed, merges, and sends back the resulting store. You are guaranteed that both devices have the same data after a sync, so it is very robust.\n\n## Synchronous Communication, Client-Server (S-CS)\n\nThings get more subtle when a server is added to the equation. The server offers flexibility, in terms of where and when a sync can occur, but it has a cost in terms of data transfer and storage. We need to reduce the communications overhead as much as possible, so copying whole stores back and forth is not going to fly.\n\nAgain, I'll aim for the simplest viable solution. Assume data is stored in a database on the server, with a last-modified timestamp for each object. When a client app first syncs, it downloads all the data in a serialized form (e.g. JSON), and builds a local store from it. It also records the timestamp of the sync locally.\n\nAs changes are made in the client app, it updates the last-modified timestamps of the objects involved. The server does the same thing, should another device sync in the interim.\n\nWhen the next sync takes place, the client determines which objects have changed since the last sync, and only sends those objects to the server. The server merges in these changes. Where the server's copy of an object has been modified by another client, it keeps the change with the most recent timestamp. \n\nThe server then sends back any changes it has that are newer than the last sync timestamp sent by the client. This set should take account of the merge, with any overridden changes removed.\n\nThere are many variations possible. For example, you could include a timestamp for each individual property, and track changes at that level of granularity. Or you could do all merging of data on the client, and push merge changes back to the server, effectively switching roles. But fundamentally, one device sends changes to the other, and the receiver merges and sends back a set of changes incorporating the results of the merge.\n\nDeletions require a little more thought, because once you delete an object, you have no way to track it. One option is to use _soft deletions_, where the object is not really deleted, but marked for deletion (e.g. using a Boolean property). (This is analogous to trashing a file in the Finder. It only gets permanently removed when you empty the trash.)\n\n## Asynchronous Communication, Client-Server Sync (A-CS)\n\nThe attraction of asynchronous sync frameworks and services is that they offer an off-the-shelf solution. The synchronous solutions discussed above are bespoke &mdash; you have to write lots of custom code for each app. What's more, with an S-CS architecture, you have to duplicate similar functionality across all platforms, and maintain operation of a server. This requires a skill set that most Objective-C developers don't possess. \n\nAsynchronous services (e.g. [Dropbox Datastore API](https://www.dropbox.com/developers/datastore) and [Wasabi Sync](http://www.wasabisync.com)) typically provide a framework, which the app developer uses as if it were a local data store. The framework stores its changes locally, and then handles syncing with a server in the background. \n\nThe main difference between A&ndash;CS and S-CS is that the extra layer of abstraction provided by the framework in A&ndash;CS shields the client code from direct involvement in syncing. It also means that the same service can be used for all data models, not just one particular model.\n\n## Asynchronous Communication, Peer-to-Peer Sync (A-P2P)\n\nA-P2P is the most underdeveloped approach, because it is also the most difficult to implement. But its promise is great, as it goes a step beyond A-CS, abstracting away the backend so that a single app can sync via a multitude of different services.\n\nUnderdeveloped as it is, there are apps already using this approach. For example, the popular To-Do list [Clear](http://realmacsoftware.com/clear) has a custom implementation of A-P2P, which syncs via iCloud, and has been [documented online](http://blog.helftone.com/clear-in-the-icloud/). And frameworks like Apple's Core Data&mdash;iCloud integration, [TICoreDataSync](https://github.com/nothirst/TICoreDataSync), and [Core Data Ensembles](https://github.com/drewmccormack/ensembles) all take this approach and are gradually finding adoption.\n\nAs an app developer, you shouldn't have to concern yourself too much with how an A-P2P system works &mdash; the complexities should remain largely hidden &mdash; but it is worth understanding how things work at a basic level, as well as the challenges involved.\n\nIn the simplest case, each device writes its CRUD change sets to transaction log files, and uploads them to the cloud. Each change set includes an ordering parameter, such as a timestamp, and when a device receives new changes from other devices, it replays them to build up a local copy of the store.\n\nIf each device just kept writing transaction logs, data in the cloud would increase _ad infinitum_. A rebasing technique can be employed to compress old change sets and set a new _baseline_. Effectively, all old changes are reduced to a set of object creations representing the initial state of the store. This reduces the number of redundant changes stored in the history. For example, if an object gets deleted, all changes related to that object can be removed.\n\n## A-P2P is Hard\n\nThis brief description probably makes it seem like a straightforward algorithm, but it hides many, many complexities. A-P2P is hard -- even harder than other forms of sync.\n\nOne of the biggest risks of A-P2P is divergence. With no central truth, and no direct communications between devices, a poor implementation can easily introduce small discrepancies which grow over time. (Bet you never expected to have to deal with [The Butterfly Effect](http://en.wikipedia.org/wiki/The_Butterfly_Effect) as an app developer.)\n\nA-P2P wouldn't be as difficult if you could keep the latest copy of the whole store permanently in the cloud. But copying the store every save would require much too much data transfer, so A-P2P apps have to be content with receiving data in chunks, and they never know for sure what other data or devices exist at any point in time. Changes can even arrive out of order, or get changes from one device that are predicated on changes from a different device that haven't arrived yet. You can literally expect to see updates to an object that hasn't been created yet.\n\nNot only can changes arrive out of order, but even determining what that order is can be challenging. Timestamps usually can't be trusted, especially on client devices like iPhones. If you aren't careful, and accept a timestamp way into the future, it could prevent new changes ever being incorporated again. More robust approaches to ordering events in time are available (e.g. [Lamport Timestamps](http://en.wikipedia.org/wiki/Lamport_timestamps) and [Vector Clocks](http://en.wikipedia.org/wiki/Vector_clock)), but at a cost: ordering of events in time is only approximate.\n\nDetails like these, and many others, make A-P2P sync a challenge to implement. But that doesn't mean we shouldn't try. The payoff &mdash; a backend-agnostic synchronizing store &mdash; is a worthy goal, and would make the barrier to implementing sync in apps much lower.\n\n## A Solved Problem?\n\nI sometimes hear people say sync is a solved problem. I wish it were as easy as that makes it sound, because then every app would support sync out of the box. In reality, very few actually do. It would perhaps be more accurate to say that sync has solutions, most of which are challenging to adopt, expensive, or limiting in some way. \n\nWe've seen that data synchronization algorithms take many different forms, and there really is no ideal one-size-fits-all approach. The solution you adopt will depend on the needs of your app, your resources, and your skills as a developer. \n\nDoes your app work with very large quantities of media data? Unless you are a cash-rich startup, you will probably be best served by good old-fashioned S-P2P over a local network, like iTunes.\n\nHave a simple data model and ambitions to extend into social or go cross platform? S-CS with a custom web service is probably the way to go. \n\nDeveloping a new app, where the ability to sync anywhere is paramount, but you don't want to waste too much time on it? Adopt an A-CS solution like the [Dropbox Datastore API](http://www.dropbox.com/developers/datastore). \n\nOr do you have an existing Core Data app, don't want to mess with servers, and don't want to get locked in to one vendor? An A-P2P solution like [Ensembles](https://github.com/drewmccormack/ensembles) may be your best option. (Admission: I am the founder and principle developer of the Ensembles project.)\n\nChoose wisely.\n"
  },
  {
    "path": "2014-03-07-editorial.md",
    "content": "---\r\ntitle:  \"Editorial\"\r\ncategory: \"10\"\r\ndate: \"2014-03-07 12:00:00\"\r\ntags: editorial\r\n---\r\n\r\nWelcome to objc.io issue #10!\r\n\r\nThis issue is all about data synchronization and network communication. It's a world of connected devices out there. We all own multiple devices, and making our data available on all of them has become very important. Yet syncing is inherently difficult to solve well.\r\n\r\nHere, we will try to help you get a better grasp of the problems involved. First off, to get you started and help you understand the domain, we have an [overview of the possible approaches and their challenges](/issues/10-syncing-data/data-synchronization/) by [Drew McCormack](https://twitter.com/drewmccormack). After that, we take a closer look at Apple's iCloud syncing solutions. In particular, iCloud Core Data sync has received a lot of attention and criticism, and it was deemed unusable by many developers. [Matthew Bischoff](https://twitter.com/mb) and [Brian Capps](https://twitter.com/bcapps) give us an update of the state of [iCloud Core Data](/issues/10-syncing-data/icloud-core-data/), and [Friedrich Gräter](https://twitter.com/hdrxs) and [Max Seelemann](http://twitter.com/macguru17) take a closer look at [iCloud Documents](/issues/10-syncing-data/icloud-document-store/).\r\n\r\nWe also have an example of a [custom syncing solution](/issues/10-syncing-data/sync-case-study/) on top of Core Data, which goes into detail of a specific solution, and an article on how to structure a [simple networking application with Core Data](/issues/10-syncing-data/networked-core-data-application/), which helps by pointing out how to get some of the basics right. If you're up for something more low level, this issue also has a thorough fundamentals article on [TCP/IP and HTTP](/issues/10-syncing-data/ip-tcp-http/) -- the technology that most of our network communication relies on.\r\n\r\nWe've created a new public repository on [GitHub](https://github.com/objcio/articles) that contains all current and past objc.io articles. If you find any mistakes or have suggestions for improvements, please don't hesitate to [file issues](https://github.com/objcio/articles/issues), or even better: submit a [pull request](https://github.com/objcio/articles/pulls)!\r\n\r\n\r\nAll the best from Berlin,\r\n\r\nChris, Daniel, and Florian.\r\n"
  },
  {
    "path": "2014-03-07-icloud-core-data.md",
    "content": "---\ntitle:  \"iCloud and Core Data\"\ncategory: \"10\"\ndate: \"2014-03-07 10:00:00\"\ntags: article\nauthor:\n  - name: Matthew Bischoff\n    url: https://twitter.com/mb\n  - name: Brian Capps\n    url: https://twitter.com/bcapps\n---\n\n\nWhen Steve Jobs first introduced [iCloud](http://en.wikipedia.org/wiki/ICloud) at WWDC 2011, the promise of seamless syncing seemed too good to be true. And if you tried to implement iCloud [Core Data](/issues/4-core-data/core-data-overview/) syncing in [iOS 5](http://adcdownload.apple.com//videos/wwdc_2011__hd/session_303__whats_new_in_core_data_on_ios.m4v) or [iOS 6](http://adcdownload.apple.com//videos/wwdc_2012__hd/session_227__using_icloud_with_core_data.mov), you know very well that it was.\n\nProblems with syncing [library-style applications](https://developer.apple.com/library/mac/documentation/General/Conceptual/MOSXAppProgrammingGuide/CoreAppDesign/CoreAppDesign.html#//apple_ref/doc/uid/TP40010543-CH3-SW3) continued as [many](http://www.macworld.com/article/1167742/developers_dish_on_iclouds_challenges.html) [developers](http://blog.caffeine.lu/problems-with-core-data-icloud-storage.html) [abandoned](http://web.archive.org/web/20130926051046/http://www.jumsoft.com/2013/01/response-to-sync-issues/) iCloud in favor of alternatives like [Simperium](http://simperium.com), [TICoreDataSync](https://github.com/nothirst/TICoreDataSync), and [WasabiSync](http://www.wasabisync.com).\n\nIn early 2013, after years of struggling with Apple’s opaque and buggy implementation of iCloud Core Data sync, the issues reached a breaking point when developers [called out](http://arstechnica.com/apple/2013/03/frustrated-with-icloud-apples-developer-community-speaks-up-en-masse/) the service’s shortcomings, culminating in a [pointed article](http://www.theverge.com/2013/3/26/4148628/why-doesnt-icloud-just-work) by Ellis Hamburger at The Verge.\n\n## WWDC\n\nIt was clear something had to change, and Apple took notice. At WWDC 2013, [Nick Gillett](http://about.me/nickgillett) announced that the Core Data team had spent a year focusing on fixing some of the biggest frustrations with iCloud in iOS 7, promising a vastly improved and simpler implementation for developers. “We’ve significantly reduced the amount of complex code developers have to write,” Nick said on stage at the [“What’s New in Core Data and iCloud”](http://asciiwwdc.com/2013/sessions/207) session. With iOS 7, Apple focused on the speed, reliability, and performance of iCloud, and it shows.\n\nLet’s take a look at what’s changed and how you can implement iCloud Core Data in your iOS 7 app.\n\n## Setup\n\nTo set up an iCloud Core Data app, you must first request access to iCloud in your application’s [entitlements](https://developer.apple.com/library/mac/documentation/General/Conceptual/iCloudDesignGuide/Chapters/iCloudFundametals.html), which will give your app the ability to read and write to one or more ubiquity containers. You can do this easily from the new [“Capabilities”](https://developer.apple.com/xcode/) screen in Xcode 5, accessible from your application’s target.\n\nInside a ubiquity container, the Core Data framework will store transaction logs -- records of changes made to your persistent store -- in order to sync data across devices. Core Data uses a technique called [multi-master replication](http://en.wikipedia.org/wiki/Multi-master_replication) to sync data across multiple iOS devices and/or Macs. The persistent store file itself is stored on each device in a Core Data-managed directory called `CoreDataUbiquitySupport`, inside your application sandbox. As a user changes iCloud accounts, the Core Data framework will manage multiple stores within this directory without your application having to observe the [`NSUbiquityIdentityDidChangeNotification`](https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/nsfilemanager_class/Reference/Reference.html#//apple_ref/doc/uid/20000305-SW81). \n\nEach transaction log is a `plist` file that keeps track of insertions, deletions, and updates to your entities. These logs are occasionally automatically coalesced by the system in a process known as [baselining](http://mentalfaculty.tumblr.com/post/23788055417/under-the-sheets-with-icloud-and-core-data-seeding).\n\nTo set up your persistent store for iCloud, there are a few [options](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSPersistentStoreCoordinator_Class/NSPersistentStoreCoordinator.html#//apple_ref/doc/constant_group/Store_Options) you need to be aware of to pass when calling [`addPersistentStoreWithType:configuration:URL:options:error:`](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSPersistentStoreCoordinator_Class/NSPersistentStoreCoordinator.html#//apple_ref/occ/instm/NSPersistentStoreCoordinator/addPersistentStoreWithType:configuration:URL:options:error:) or [`migratePersistentStore:toURL:options:withType:error:`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSPersistentStoreCoordinator_Class/NSPersistentStoreCoordinator.html#//apple_ref/doc/uid/TP30001180-BBCFDEGA):\n\n- `NSPersistentStoreUbiquitousContentNameKey` (`NSString`)  \nSpecifies the name of the store in iCloud (e.g. `@\"MyAppStore\"`)\n- `NSPersistentStoreUbiquitousContentURLKey` (`NSString`, optional in iOS 7)  \nSpecifies the subdirectory path for the transaction logs (e.g. `@\"Logs\"`)\n- `NSPersistentStoreUbiquitousPeerTokenOption` (`NSString`, optional)  \nA per-application salt to allow multiple apps on the same device to share a Core Data store integrated with iCloud (e.g. `@\"d70548e8a24c11e3bbec425861b86ab6\"`)\n- `NSPersistentStoreRemoveUbiquitousMetadataOption` (`NSNumber` (Boolean), optional)  \nUsed whenever you need to back up or migrate an iCloud store to strip the iCloud metadata (e.g. `@YES`)\n- `NSPersistentStoreUbiquitousContainerIdentifierKey` (`NSString`)  \nSpecifies a container if your app has multiple ubiquity container identifiers in its entitlements (e.g. `@\"com.company.MyApp.anothercontainer\"`)\n- `NSPersistentStoreRebuildFromUbiquitousContentOption` (`NSNumber` (Boolean), optional)  \nTells Core Data to erase the local store file and rebuild it from the iCloud data (e.g. `@YES`)\n\n\nThe only required option for an iOS 7-only application is the content name key, which lets Core Data know where to put its logs and metadata. As of iOS 7, the string value that you pass for `NSPersistentStoreUbiquitousContentNameKey` may not contain periods. If your application already uses Core Data for persistence but you haven't implemented iCloud syncing, simply adding the content name key will prepare your store for iCloud, whether or not there is an active iCloud account.\n\nSetting up a managed object context for your application is as simple as allocating an instance of `NSManagedObjectContext` and telling it about your persistent store, as well as including a merge policy. Apple recommends `NSMergeByPropertyObjectTrumpMergePolicy`, which will merge conflicts, giving priority to in-memory changes over the changes on disk.\n\nWhile Apple hasn’t released official sample code for iCloud Core Data in iOS 7, an Apple engineer on the Core Data team provided this basic template [on the Developer Forums](https://devforums.apple.com/message/828503#828503). We’ve edited it slightly for clarity:\n\n```objc\n#pragma mark - Notification Observers\n- (void)registerForiCloudNotifications {\n    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];\n    \n    [notificationCenter addObserver:self \n                           selector:@selector(storesWillChange:) \n                               name:NSPersistentStoreCoordinatorStoresWillChangeNotification \n                             object:self.persistentStoreCoordinator];\n\n    [notificationCenter addObserver:self \n                           selector:@selector(storesDidChange:) \n                               name:NSPersistentStoreCoordinatorStoresDidChangeNotification \n                             object:self.persistentStoreCoordinator];\n\n    [notificationCenter addObserver:self \n                           selector:@selector(persistentStoreDidImportUbiquitousContentChanges:) \n                               name:NSPersistentStoreDidImportUbiquitousContentChangesNotification \n                             object:self.persistentStoreCoordinator];\n}\n\n# pragma mark - iCloud Support\n\n/// Use these options in your call to -addPersistentStore:\n- (NSDictionary *)iCloudPersistentStoreOptions {\n    return @{NSPersistentStoreUbiquitousContentNameKey: @\"MyAppStore\"};\n}\n\n- (void) persistentStoreDidImportUbiquitousContentChanges:(NSNotification *)changeNotification {\n    NSManagedObjectContext *context = self.managedObjectContext;\n    \n    [context performBlock:^{\n        [context mergeChangesFromContextDidSaveNotification:changeNotification];\n    }];\n}\n\n- (void)storesWillChange:(NSNotification *)notification {\n    NSManagedObjectContext *context = self.managedObjectContext;\n    \n    [context performBlockAndWait:^{\n        NSError *error;\n        \n        if ([context hasChanges]) {\n            BOOL success = [context save:&error];\n            \n            if (!success && error) {\n                // perform error handling\n                NSLog(@\"%@\",[error localizedDescription]);\n            }\n        }\n        \n        [context reset];\n    }];\n\n    // Refresh your User Interface.\n}\n\n- (void)storesDidChange:(NSNotification *)notification {\n    // Refresh your User Interface.\n}\n```\n\n### Asynchronous Persistent Store Setup\n\nIn iOS 7, calling `addPersistentStoreWithType:configuration:URL:options:error:` with iCloud options now returns a store almost immediately.[^1] It does this by first setting up an internal 'fallback' store, which is a local store that serves as a placeholder, while the iCloud store is being asynchronously built from transaction logs and ubiquitous metadata. Changes made in the fallback store will be migrated to the iCloud store when it’s added to the coordinator. `Using local storage: 1` will be logged to the console when the fallback store is set up, and after the iCloud store is fully set up you should see `Using local storage: 0`. This means that the store is iCloud enabled, and you’ll begin seeing imports of content from iCloud via the `NSPersistentStoreDidImportUbiquitousContentChangesNotification`.\n\nIf your application is interested in when this transition between stores occurs, observe the new `NSPersistentStoreCoordinatorStoresWillChangeNotification` and/or `NSPersistentStoreCoordinatorStoresDidChangeNotification` (scoped to your coordinator in order to filter out internal notification noise) and inspect the `NSPersistentStoreUbiquitousTransitionTypeKey` value in its `userInfo` dictionary. The value will be an NSNumber boxing an `enum` value of type [`NSPersistentStoreUbiquitousTransitionType`](https://developer.apple.com/library/ios/documentation/cocoa/Reference/CoreDataFramework/Classes/NSPersistentStoreCoordinator_Class/NSPersistentStoreCoordinator.html#//apple_ref/c/tdef/NSPersistentStoreUbiquitousTransitionType), which will be `NSPersistentStoreUbiquitousTransitionTypeInitialImportCompleted` when this transition has occurred.\n\n## Edge Cases\n\n### Churn\n\nOne of the worst problems with testing iCloud on iOS 5 and 6 was when heavily used accounts would encounter 'churn' and become unusable. Syncing would completely stop, and even removing all ubiquitous data wouldn’t make it work. At [Lickability](http://lickability.com), we affectionately dubbed this state “f\\*\\*\\*ing the bucket.”\n\nIn iOS 7, there is a system-supported way to truly remove all ubiquitous content: `+removeUbiquitousContentAndPersistentStoreAtURL:options:error:`. This method is great for testing, and may even be relevant for your application if the user gets into an inconsistent state and needs to remove all content and start over. There are a few caveats, though. First, this method is synchronous. Even while performing network operations and possibly taking a significant length of time, this method will not return until it is finished. Second, there should be absolutely no persistent store coordinators active when performing this operation. There are serious issues that can put your app into an unrecoverable state, and the official guidance is that all active persistent store coordinators should be completely deallocated beforehand.\n\n### Account Changes\n\nIn iOS 5, when a user switched iCloud accounts or disabled iCloud, the data in the `NSPersistentStoreCoordinator` would completely vanish without letting the application know. In fact, the only way to check if an account had changed was to call `URLForUbiquityContainerIdentifier` on `NSFileManager` -- a method that can set up the ubiquitous content folder as a side effect, and take seconds to return. In iOS 6, this was remedied with the introduction of `ubiquityIdentityToken` and its corresponding `NSUbiquityIdentityDidChangeNotification`, which is posted when there is a change to the ubiquity identity. This effectively notifies the app of account changes.\n\nIn iOS 7, however, this transition is even simpler. Account changes are handled by the Core Data framework, so as long as your application responds appropriately to both `NSPersistentStoreCoordinatorStoresWillChangeNotification` and `NSPersistentStoreCoordinatorStoresDidChangeNotification`, it will seamlessly transition when the user’s account changes. Inspecting the `NSPersistentStoreUbiquitousTransitionType`key in the `userInfo` dictionary of this notification will give more detail as to the type of transition.\n\nThe framework will manage one persistent store file per account inside the application sandbox, so if the user comes back to a previous account later, his or her data will still be available as it was left. Core Data now also manages the cleanup of these files when the user’s device is running low on disk space.\n\n### iCloud On / Off Switch\n\nImplementing a switch to enable or disable iCloud in your app is also much easier in iOS 7, although it probably isn’t necessary for most applications. Because the API now automatically creates a separate file structure when iCloud options are passed to the `NSPersistentStore` upon creation, we can have the same store URL and many of the same options between both local and iCloud stores. This means that switching from an iCloud store to a local store can be done by migrating the iCloud persistent store to the same URL with the same options, plus the `NSPersistentStoreRemoveUbiquitousMetadataOption`. This option will disassociate the ubiquitous metadata from the store, and is specifically designed for these kinds of migration or copying scenarios. Here's a sample:\n\n```objc\n- (void)migrateiCloudStoreToLocalStore {\n    // assuming you only have one store.\n    NSPersistentStore *store = [[_coordinator persistentStores] firstObject]; \n    \n    NSMutableDictionary *localStoreOptions = [[self storeOptions] mutableCopy];\n    [localStoreOptions setObject:@YES forKey:NSPersistentStoreRemoveUbiquitousMetadataOption];\n    \n    NSPersistentStore *newStore =  [_coordinator migratePersistentStore:store \n                                                                  toURL:[self storeURL] \n                                                                options:localStoreOptions \n                                                               withType:NSSQLiteStoreType error:nil];\n    \n    [self reloadStore:newStore];\n}\n\n- (void)reloadStore:(NSPersistentStore *)store {\n    if (store) {\n        [_coordinator removePersistentStore:store error:nil];\n    }\n\n    [_coordinator addPersistentStoreWithType:NSSQLiteStoreType \n                               configuration:nil \n                                         URL:[self storeURL] \n                                     options:[self storeOptions] \n                                       error:nil];\n}\n```\n\nSwitching from a local store back to iCloud is just as easy; simply migrate with iCloud-enabled options, and add a persistent store with same options to the coordinator.\n\n### External File References\n\nExternal file references is a feature introduced for Core Data in iOS 5 that allows large binary data properties to be automatically stored outside the SQLite database on the file system. In our testing, when this occurs, iCloud does not always know how to resolve the relationship and can throw exceptions. If you plan to use iCloud syncing, consider unchecking this box in your iCloud entities:\n  \n![Core Data Modeler Checkbox](/images/issue-10/allows-external-storage.png)\n\n### Model Versioning\n\nIf you are using iCloud, the contents of a store can only be migrated if the store is compatible with automatic [lightweight migration](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreDataVersioning/Articles/vmLightweightMigration.html). This means that Core Data must be able to infer the mapping and you cannot provide your own mapping model. Only simple changes to your model, like adding and renaming attributes, are supported. When considering whether to use Core Data syncing, be sure to think about how much your model may change in future versions of your app.\n\n### Merge Conflicts\n\nWith any syncing system, conflicts between the server and the client are inevitable. Unlike [iCloud Data Document Syncing](https://developer.apple.com/library/ios/documentation/General/Conceptual/iCloudDesignGuide/Chapters/DesigningForDocumentsIniCloud.html#//apple_ref/doc/uid/TP40012094-CH2-SW1) APIs, iCloud Core Data integration does not specifically allow handling of conflicts between the local store and the transaction logs. That’s likely because Core Data already supports custom merge policies through subclassing of `NSMergePolicy`. To handle conflicts yourself, create a subclass of `NSMergePolicy` and override `resolveConflicts:error:` to determine what to do in the case of a conflict. Then, in your `NSManagedObjectContext` subclass, return an instance of your custom merge policy from the `mergePolicy` method.\n\n### UI Updates\n\nMany library-style applications display both collections of objects and detail views which display a single object. Views that are managed by `NSFetchedResultsController` instances will automatically update as the Core Data ubiquity system imports changes from the network. However, you should ensure that each detail view is properly observing changes to its object and keeping itself up to date. If you don't, you risk accidentally showing stale data, or worse, saving edited values on top of newer changes from other peers.\n\n## Testing\n\n### Local Networks vs. Internet Syncing\n\nThe iCloud daemon can synchronize data across devices in one of two ways: on your local network or over the Internet. When the daemon detects that two devices, also known as peers, are on the same local area network, it will transfer the data over that presumably faster connection. If, however, the peers are on separate networks, the system will fall back to transferring transaction logs over the Internet. This is important to know, as you must test both cases heavily in development to make sure your application is functioning properly. In either scenario, syncing changes or transitioning from a fallback store to an iCloud store sometimes takes longer than expected, so if something isn't working right away, try giving it time.\n\n### iCloud in the Simulator\n\nOne of the most helpful changes to iCloud in iOS 7 is the ability to _finally_ [use iCloud in the iOS Simulator](https://developer.apple.com/library/mac/documentation/General/Conceptual/iCloudDesignGuide/Chapters/TestingandDebuggingforiCloud.html). In previous versions, you could only test on a device, which limited how easy it was to observe the syncing process during development. Now, you can even sync data between your Mac and the simulator as two separate peers.\n\nWith the addition of the iCloud Debug Gauge in Xcode 5, you can see all files in your app's ubiquity containers and examine their file transfer statuses, such as \"Current,\" \"Excluded,\" and \"Stored in Cloud.\" For more hardcore debugging, enable verbose console logging by passing `-com.apple.coredata.ubiquity.logLevel 3` as a launch argument or setting it as a user default. And consider installing the [iCloud Storage Debug Logging Profile](http://developer.apple.com/downloads) on iOS and the new [`ubcontrol`](https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man1/ubcontrol.1.html) command-line tool on OS X to provide high-quality bug reports to Apple. You can retrieve the logs that these tools generate from `~/Library/Logs/CrashReporter/MobileDevice/device-name/DiagnosticLogs` after syncing your device with iTunes.\n\nHowever, iCloud Core Data is not fully supported in the simulator. When testing across devices and the simulator, it seems that iCloud Core Data on the simulator only uploads changes, and never pulls them back down. This is still a big improvement over needing separate test devices, and a nice convenience, but iCloud Core Data support on the iOS Simulator is definitely not fully baked yet.\n\n## Moving On\n\nWith the clear improvements in both APIs and functionality in iOS 7, the fate of apps currently shipping iCloud Core Data on iOS 5 and iOS 6 is uncertain. Since the sync systems are so different from an API perspective (and, we’ve found out, from a functionality perspective), Apple's recommendations haven't been kind to apps that need legacy syncing. **Apple explicitly recommends [on the Developer Forums](https://devforums.apple.com/thread/199983?start=0&tstart=0*) that you sync absolutely no data between iOS 7 and any prior version of iOS**.\n\nIn fact, “at no time should your iOS 7 devices be allowed to communicate with iOS 6 peers. The iOS 6 peers will continue to exhibit bugs and issues that have been fixed in iOS 7 but in doing so will pollute the iCloud account.” The easiest way to guarantee this separation is by simply changing the `NSPersistentStoreUbiquitousContentNameKey` of your store, hopefully to comply with new guidance on periods within the name. This difference guarantees that the data from older versions is siloed from the new methods of syncing, and allows developers to make a completely clean break from old implementations.\n\n## Shipping\n\nShipping an iCloud Core Data application is still risky. You need to perform tests for everything: account switching, running out of iCloud storage, many devices, model upgrades, and device restores. Although the iCloud Debug Gauge and [developer.icloud.com](http://developer.icloud.com) can help, it’s still quite a leap of faith to ship an app relying on a service that’s completely out of your control. \n\nAs Brent Simmons [pointed out](http://inessential.com/2013/03/27/why_developers_shouldnt_use_icloud_sy), shipping an app with any kind of iCloud syncing can be limiting, so be sure to understand the costs up front. Apps like [Day One](http://dayoneapp.com) and [1Password](https://agilebits.com/onepassword) have opted to let users sync their data with either iCloud or Dropbox. For many users, nothing beats the simplicity of a single account, but some power users still demand full control over their data. And for developers, maintaining disparate [database syncing systems](https://www.dropbox.com/developers/datastore) can be quite taxing during development and testing.\n\n## Bugs\n\nOnce you’ve tested and shipped an iCloud Core Data application, you will likely encounter bugs in the framework. The best way to report these bugs to Apple is to file detailed [Radars](http://bugreport.apple.com), which contain the following information:\n\n1. Complete steps to reproduce.\n2. The output to the console with [iCloud debug logging](http://www.freelancemadscience.com/fmslabs_blog/2012/3/28/debug-settings-for-core-data-and-icloud.html) on level three and the iCloud debugging profile installed.\n3. The full contents of the ubiquity container as a zip archive.\n\n## Conclusion\n\nIt’s no secret that iCloud Core Data was fundamentally broken in iOS 5 and 6, with an Apple employee acknowledging that “there were significant stability and long term reliability issues with iOS 5 / 6 when using Core Data + iCloud…The way forward is iOS 7 only, really really really really.” High-profile developers like [Agile Tortoise](http://agiletortoise.com) and [Realmac Software](http://realmacsoftware.com) are now comfortable trusting the iCloud Core Data integration in their applications, and with enough [consideration](https://developer.apple.com/library/ios/documentation/General/Conceptual/iCloudDesignGuide/Chapters/Introduction.html) and testing, you should be too.\n\n\n*Special thanks to Andrew Harrison, Greg Pierce, and Paul Bruneau for their help with this article.*\n\n\n[^1]: In previous OS versions, this method wouldn’t return until iCloud data was downloaded and merged into the persistent store. This would cause significant delays, meaning that any calls to the method would need to be dispatched to a background queue. Thankfully, this is no longer necessary.\n"
  },
  {
    "path": "2014-03-07-icloud-document-store.md",
    "content": "---\ntitle:  \"Mastering the iCloud Document Store\"\ncategory: \"10\"\ndate: \"2014-03-07 09:00:00\"\ntags: article\nauthor:\n  - name: Friedrich Gräter\n    url: https://twitter.com/hdrxs\n  - name: Max Seelemann\n    url: http://twitter.com/macguru17\n---\n\nEven three years after its introduction, the iCloud document store is still a topic full of myth, misunderstanding, and resentment. iCloud synching has often been criticized for being unreliable or slow. And while there were imminent bugs in the early days of iCloud, application developers had to learn their lessons about file synchronization, too. File synchronization is non-trivial and brings new aspects to application development — aspects which are often underestimated, such as the requirement to handle asynchronous file changes while being cooperative regarding synchronization services.\n\nThis article will give an overview of several common stumbling stones you may find when creating an iCloud-ready application. In case you are not already familiar with the iCloud document store, we strongly recommend reading the [Apple iCloud companion guide][1] first, since this article provides only a rough overview.\n\n## The Document Store in a Nutshell\n\nThe core idea of the iCloud document store is pretty simple: every application has access to at least one 'magic folder' where it can store files that are then being synchronized across all devices subscribing to the same iCloud account.\n\nIn contrast to other file-based syncing services, the iCloud document store benefits from a deep integration with OS X and iOS. Many system frameworks have been extended to support iCloud. Classes like `NSDocument` and `UIDocument` have been designed to deal with external changes. Synchronization conflicts are handled by the Version Store and `NSFileVersion`. Spotlight is employed to provide synchronization metadata like the progress of file transfers or the availability of documents in the cloud.\n\nIt does not take much to write a simple, document-based, iCloud-enabled application on OS X. Actually, you don’t need to care about any of the inner workings of iCloud, as `NSDocument` delivers almost everything for free: it coordinates document accesses with iCloud, automatically watches for external changes, triggers downloads, and handles conflicts. It even provides a simple user interface for managing cloud documents through the default open panel. All you need to do is to be a good `NSDocument` subclass citizen and to implement the required methods for reading and writing file contents.\n\n![NSDocument provides a simple user interface for managing the synchronized documents of an app.][image-1]\n\nHowever, as soon as you leave the predefined path, you have to know a lot more. For example, everything beyond the single-level folder hierarchy provided by the default open panel has to be done manually. Maybe your application needs to manage its documents beside the documents contents, like it is done in Mail, iPhoto, or [Ulysses][2] (our own app). In these cases, you cannot rely on `NSDocument` and need to implement its functionality on your own. But for that you have to have a deep understanding of the locking and notification mechanisms employed by iCloud.\n\nDeveloping iCloud-ready apps for iOS also requires more work and knowledge; while `UIDocument` still manages file access with iCloud and handles synchronization conflicts, it lacks any user interface for managing documents and folders. For performance and storage reasons, iOS does also not automatically download new documents from the cloud. Instead, you need to query for recent changes of the directory using Spotlight and trigger downloads manually.\n\n## What’s in a Ubiquity Container?\n\nAny application that is eligible for App Store provisioning can use the iCloud document store. After setting the correct [entitlements][3], it gains access to one or multiple so-called 'ubiquity containers.' This is Apple slang for “a directory that is managed and synced by iCloud.” Each ubiquity container is bound to an application identifier, resulting in one shared storage per user per app. Developers who have multiple apps may specify multiple app identifiers (of the same team), and by that gain access to multiple containers.\n\n`NSFileManager` provides the URL of each container through [`URLForUbiquityContainerIdentifier:`][4]. On OS X, it is possible to inspect all available ubiquity containers by opening the directory `~/Library/Mobile Documents`.\n\n![The contents of ”~/Library/Mobile Documents.“ It contains a separate folder for each ubiquity container of each application.][image-2]\n\nTypically, there are two processes concurrently accessing each ubiquity container. First, there is the application that is presenting and manipulating the documents inside the container. Second, there is the infrastructure of iCloud, which is mostly represented through the Ubiquity Daemon (*ubd*). The iCloud infrastructure waits for changes performed by the application and uploads them to Apple’s cloud servers. It also waits for incoming changes on iCloud and may modify the contents of the ubiquity container accordingly.\n\nSince both entities are working completely independent from one another, some kind of arbitration is needed to prevent race conditions or lost updates on files inside the container. To guarantee access to an isolated file, applications need to use a concept called [*file coordination*][5] for every access. This access is provided by the [`NSFileCoordinator`][6] class. In a nutshell, it provides a simple reader-writer lock for files. This lock is extended by a notification mechanism that is supposed to improve cooperation across different processes accessing the same files.\n\nThis notification mechanism is an essential advantage over simple file locks and allows for a seamless user experience. iCloud may replace a document by a new version from another device at any time. If an application is currently showing the same document, it must load the new version from disk and show the updated contents to the user. During the update, the application may need to lock the user interface for a while and enable it again afterward. Even worse scenarios may happen: the application may hold unsaved contents, which need to be saved to disk *first* in order to detect synchronization conflicts. Finally, iCloud may be interested in uploading the most recent version of a file if good network conditions are available. Thus, it must be able to query an application to flush all unsaved changes immediately.\n\nFor these negotiations, file coordination is accompanied by another mechanism called *file presentation*. Whenever an application opens a file and shows it to the user, it is said to be *presenting the document* and should register an object implementing the [`NSFilePresenter`][7] protocol. The file presenter receives notifications about the presented file whenever another process accesses it through a file coordinator. These notifications are delivered as method calls, which are performed asynchronously on an operation queue specified by the presenter ([`presentedItemOperationQueue`][8]).\n\nFor example, before any other process is allowed to start a coordinated read operation, the file presenter will be asked to persist any unsaved changes. This is done by dispatching a block on its presentation queue calling the method [`savePresentedItemChangesWithCompletionHandler:`][9]. The presenter may then save the file and confirm the notification by executing a block that has been passed as argument to the notification handler. Aside from change notifications, file presenters are also used to notify the application on synchronization conflicts. Whenever a conflicting version of a file has been downloaded, a new file version is added to the Versions store. All presenters are notified that a new version has been created through [`presentedItemDidGainVersion:`][10]. This callback receives an `NSFileVersion` instance referencing potential conflicts.\n\nFile presenters can be also used if your application needs to monitor folder contents. For instance, whenever iCloud is changing the contents of a folder, e.g. by creating, deleting, or moving files, the application should be notified to update its documents overview. For this purpose, the application can register an instance implementing the `NSFilePresenter` protocol for a presented directory. A file presenter on a directory will be notified of any changes in the folder or any files nested to it or to its subfolders. For example, if a file inside the folder has been modified, the presenter will receive a `presentedSubitemDidChangeAtURL:` notification referencing the URL of the modified file. \n\nSince bandwidth and battery life are much more limited on mobile devices, iOS will not download new files automatically from iCloud. Instead, applications must decide manually when to trigger downloads of new files to the ubiquity container. To continue providing the application an overview of which files are available, as well as their current synchronization status, iCloud also synchronizes metadata for files inside the ubiquity container. An application may query this metadata by using an `NSMetadataQuery` or by accessing the ubiquity resource attributes of `NSURL`. Whenever the application wants to get access to a file’s contents, it must trigger a download explicitly through `NSFileManager`’s  [`startDownloadingUbiquitousItemAtURL:error:`][11].\n\n## Inside the Depths of iCloud\n\nInstead of continuing to explain how to implement file coordination and observation, we will now dive into common problems we have encountered over the last few years. Again, please make sure you have read and understood the [Apple iCloud companion guide][12] for documents in the cloud.\n\nWhile the description of those file mechanisms makes their use sound pretty straightforward, there are many hidden pitfalls. And some of these pitfalls originate from bugs inside the underlying frameworks. Since iCloud syncing is spread on quite a few levels of the operating system, one can expect that Apple will be fixing bugs very carefully. Actually, Apple even seems to prefer deprecating broken APIs over providing bug fixes.\n\nEven so, it’s our experience that it is also very, very easy to make mistakes. The asynchronous, cooperative, and lock-based nature of file coordination and file presentation has implications that are often not easy to grasp. In the following, we would like to share our experiences in the form of a couple of general rules to follow when manually integrating iCloud document syncing.\n\n### Use Presenters only when Necessary\n\nFile presenters can be very expensive objects. They should only be used if your application needs to be able to react to or intervene in external file accesses immediately.\n\nIf your application is currently presenting something like a document editor to the user, file presentation is adequate. In this case, your application may need to lock the editor while other processes are writing, or it may need to flush any unsaved changes to disk. However, if only temporary access is needed and notifications may be processed lazily, your application should not use file presentation. For instance, when indexing a file or creating a thumbnail, watching change dates and using simple file coordination will probably be sufficient. Also, if you are presenting the contents of a directory tree, it may be completely sufficient to register a *single* presenter at the root of the tree or to use an `NSMetadataQuery` to be lazily notified of any changes.\n\nWhat makes file presentation so expensive? Well, it requires a lot of interprocess communication: each file presenter registered to a file must be asked to relinquish the presented file before other processes get access to that file. For example, if another process tries to read a certain file, its presenters will be asked to save all unsaved changes ([`savePresentedItemChangesWithCompletionHandler:`][13]). They are also asked to relinquish the file to the reader ([`relinquishPresentedItemToReader:`][14]), e.g. to temporary lock editors while the file is read.\n\nEach of these notifications need to be dispatched, processed, and confirmed by their respective receivers. And since only the implementing process knows which notifications will be handled, interprocess communication will happen for every possible notification, even if a presenter does not implement any of those methods.\n\nAdditionally, multiple context switches between the reading process, the presenting process, and the file coordination daemon (`filecoordinationd`) are required for each step. As a result, a simple file access can quickly become a very expensive operation.\n\nOn top of all that, the file coordination daemon can deplete critical system resources if too many presenters have been registered. For each presenter, it needs to open and observe every folder on the path of the presented item. Especially on OS X Lion and iOS 5, these resources were very scarce, and an overuse could easily have led to a full lockdown or crash of the file coordination daemon.\n\nFor these reasons, we strongly recommend not adding file presenters on every node inside a directory tree, rather only using as few file presenters as needed.\n\n### Use Coordination only if Necessary\n\nWhile file coordination is way cheaper than file presentation, it still adds an additional overhead to your application and to the entire system.\n\nWhenever your application is coordinating a file access, other processes trying to access the same file at the same time may need to wait. Therefore, you should never perform any lengthy task while coordinating a file. If you are, for instance, saving large files, you may consider saving them to a temporary folder first and then just swizzling hard links during a coordinated access. Keep in mind that every coordinated access may trigger a file presenter inside another process — a presenter that may need time to update the file in advance to your access. Always consider the usage of flags like `NSFileCoordinatorReadingWithoutChanges` if it’s not required to read the most recent version of a file. \n\nWhile the ubiquity container of your app will probably not be accessed by other applications, exaggerated file coordination may still become a problem with iCloud, and performing many coordination requests may lead to a starvation of system processes like `ubd`. During the startup phase of an application, `ubd` seems to scan through all files inside your ubiquity container. If your application is performing the same scan during program startup, both processes may often collide, which may lead to a high coordination overhead. It’s wise to consider more optimistic approaches in this case. For example, when scanning directory contents, isolated access to a file’s contents may not be required at all. Instead, defer the coordination until the file’s contents are actually being presented.\n\nFinally, never coordinate a file that has not been downloaded yet. File coordination will trigger the downloading of files. Unfortunately, the coordination will wait until the download has been completed, which may block an application for an incalculable period of time. Before accessing a file, an app should verify the file’s download state. You can do this by querying the URL’s resource value `NSURLUbiquitousItemDownloadingStatusKey` or using an `NSMetadataQuery`.\n\n### Some Remarks on Coordination Methods\n\nReading the documentation of `NSFileCoordinator`, you may notice that many method calls have a lengthy and complicated description. While the API is generally very conclusive, it has a high complexity due to the variety of interactions with other coordinators and file presenters, as well as the differing semantics for folder and file locking. Throughout these lengthy descriptions there are several details and issues that may be easily missed:\n\n1. Take coordination options seriously. They really influence the behavior of file coordinators and file presenters. For example, if the flag `NSFileCoordinatorWritingForDeleting` is not provided, file presenters will not be able to influence the deletion of a file through `accommodatePresentedItemDeletionWithCompletionHandler:`. If `NSFileCoordinatorWritingForMoving` is not used when moving directories, the move operation will not wait for ongoing coordinations on subitems to be finished.\n\n2. Always expect that coordination calls may fail and return errors. Since file coordination interacts with iCloud, a coordination call may fail with an error message if the coordinated file cannot be downloaded, and your actual file operation may not be performed. If error handling is not correctly implemented, your application may not notice problems like this.\n\n3. Verify a file’s state after entering coordination blocks. A lot of time may pass after the request for the coordination. In the meantime, preconditions that lead an application to perform a file operation may have become false. Information you are going to write could have become stale until the lock is granted. It could also be possible that your file has been deleted while you’ve waited to get write access. In this case, you might accidentally recreate the deleted file.\n\n### Notification Deadlocks\n\nImplementing notification handlers of `NSFilePresenter` requires special attention. Some notifications, such as `relinquishPresentedItemToReader:`, must be confirmed to signal to other processes that a file is ready for access. Typically, this is done by executing a confirmation block passed as an argument to the notification handler. It is important to know that, until the confirmation block is called, the other process has to wait. If the confirmation is delayed due to slow notification processing, the coordinating process may stall. If it is never executed, it will probably hang forever.\n\nUnfortunately, notifications that need to be confirmed can also be slowed down by other, completely independent notifications. To ensure notifications are being processed in the correct order, the `presentedItemOperationQueue` is usually configured as a sequential queue. However, using a sequential queue means that slowly processed notifications will delay their succeeding notifications. In particular, they may slow down succeeding notifications that require a confirmation, and by that, any process waiting for them.\n\nFor example, assume a notification like `presentedItemDidChange` has been enqueued first. A lengthy processing of this callback may stall other notifications, like `relinquishPresentedItemToReader:`, that have been enqueued shortly after. As a consequence, the confirmation of this notification will also be delayed, which in turn stalls the process waiting for it.\n\nAbove all, *never* perform any file coordination while inside a presentation queue. In fact, even simple notifications without any confirmation needs (e.g. `presentedItemDidChange`) can cause deadlocks. Just imagine two file presenters presenting the same file. Both presenters are handling the notification `presentedItemDidChange` by performing a coordinated read operation on the presented file. If the file has been changed, this notification is sent to both presenters and both presenters perform a coordinated read on the same file. As a consequence, both presenters query each other to relinquish the file by enqueuing a `relinquishPresentedItemToReader:` and wait for each other to confirm this notification. Unfortunately, both presenters are unable to confirm the notification since both are blocking their presentation queues by the coordination request waiting forever on the other's confirmation. We've prepared a small example exploiting this deadlock on [GitHub][15].\n\n### Defective Notifications\n\nDrawing the correct conclusions from notifications is not always easy. There are bugs inside file presentation causing some notification handlers to *never be called*. Here is a short glimpse of known misbehaving notifications:\n\n1. Aside from `presentedSubitemDidChangeAtURL:` and `presentedSubitemAtURL:didMoveToURL:`, all subitem notifications are either never called or called in a very unpredictable way. Don’t rely on them at all — in particular, `presentedSubitemDidAppearAtURL:` and `accommodatePresentedSubitemDeletionAtURL:completionHandler:` will never be called.\n2. `accommodatePresentedItemDeletionWithCompletionHandler:` will only work if the deletion was performed through a file coordination that used the `NSFileCoordinatorWritingForDeleting` flag. Otherwise, you may not even receive a change notification.\n3. `presentedItemDidMoveToURL:` and `presentedSubitemAtURL:didMoveToURL:` will only be sent if `itemAtURL:didMoveToURL:` was called by the moving file coordinator. If not, items will not receive any useful notifications. Subitems may still receive two separate `presentedSubitemDidChange` notifications for the old and new URLs.\n4. Even if files have been moved correctly and a `presentedSubitemAtURL:didMoveToURL:` notification was sent, you will still receive two additional `presentedSubitemDidChangeAtURL:` notifications for the old and new URL. Be prepared for that.\n\nGenerally, you have to be aware that notifications may be outdated. You should also not rely on any specific ordering of notifications. For example, when presenting a directory tree, you may not expect that notifications regarding a parent folder will appear before or after notifications on one of its subitems.\n\n### Be Aware of URL Changes\n\nThere are several situations where you need to be prepared in case file coordinators and file presenters deliver multiple variants of the same URL referencing the same file. You should never compare URLs using `isEqual:`, because two different URLs may still reference the same file. You should always standardize URLs before comparing them. This is especially important on iOS, where ubiquity containers are stored in `/var/mobile/Library/Mobile Documents/`, which is a symbolically linked folder for `/private/var/mobile/Library/Mobile Documents/`. You will receive presenter notifications with URLs based on *both path variants* that still reference the same file. This issue can also occur on OS X if you are using file coordination code for iCloud and local documents.\n\nBeyond that, there are also several issues on case-insensitive file systems. You should always make sure that you perform a case-insensitive comparison of filenames *if* the file system requires it. File coordination blocks and presenter notifications may deliver variants of the same URL using different casings. In particular, this an important issue when renaming files using file coordinators. To understand this issue, you need to recall how files are actually renamed:\n\n```objc\n[coordinator coordinateWritingItemAtURL:sourceURL \n                                options:NSFileCoordinatorWritingForMoving \n                       writingItemAtURL:destURL \n                                options:0 \n                                  error:NULL \n                             byAccessor:^(NSURL *oldURL, NSURL *newURL) \n{\n    [NSFileManager.defaultManager moveItemAtURL:oldURL toURL:newURL error:NULL];\n    [coordinator itemAtURL:oldURL didMoveToURL:newURL];\n}];\n```\n\nAssume `sourceURL` references a file named `~/Desktop/my text` and `destURL` references the new filename written in upper case: `~/Desktop/My Text`. By design, the coordination block will be passed the most recent version of both URLs in order to accommodate move operations that happened while waiting for file access. Now, unfortunately, when changing a filename’s case, the URL's validation performed by file coordination will find an existing valid file for both the old and the new URL, which is the lowercase variant `~/Desktop/my text`. The access block will receive the same *lowercase* URL as `oldURL` and `newURL`, leading to a failure of the move operation.\n\n### Requesting Downloads\n\nOn iOS, it’s the application's responsibility to trigger downloads from iCloud. Downloads can be triggered through the method  [`startDownloadingUbiquitousItemAtURL:error:`][16] of `NSFileManager`. If your application is designed to download files automatically (i.e. not triggered by the user), you should always perform those download requests from a sequential background queue. On the one hand, each single download request involves quite a bit of interprocess communication and may take up to a second. On the other hand, triggering too many downloads at once seems to overload the *ubd* daemon at times. A common mistake is to wait for new files in iCloud using an `NSMetadataQuery` and automatically trigger a download for them. Since the query result is always delivered on the main queue and it can contain updates for dozens of files, directly triggering downloads will block an application for a long time.\n\nTo query the download or upload status of a certain file, you can use resource values of `NSURL`. Before iOS 7 / OS X 10.9, the download status of a file was made available through `NSURLUbiquitousItemIsDownloadedKey`. According to its header documentation, this resource value never worked correctly, and so it was deprecated in iOS 7 and Mavericks. Apple now recommends to use `NSURLUbiquitousItemDownloadingStatusKey`. On older systems, you should use an `NSMetadataQuery` and query for `NSMetadataUbiquitousItemIsDownloadedKey` to get the correct download status.\n\n## General Considerations\n\nAdding support for iCloud to your application is not just another feature you’re adding. Instead, it is a decision that has far-reaching consequences on the design and implementation of your application. It influences your data model as well as the user interface. So don’t underestimate the efforts of properly supporting iCloud. \n\nMost importantly, adding iCloud will introduce a new level of asynchrony to an application. The application must be able to deal with changes on documents and metadata at any time. Notifications on those changes may be received by different threads, raising the need for synchronization primitives across your entire application. You need to be aware of issues in code that are critical for the integrity of a user's documents, like lost updates, race conditions, and deadlocks.\n\nAlways keep in mind that synchronization guarantees of iCloud are very weak. You can only assume that files and packages are synchronized atomically. But you cannot expect that multiple files modified simultaneously are also synchronized at once. For example, if your application stores meta information separate from the actual files, it must be able to cope with the fact that this metadata will be downloaded earlier or later than the actual files.\n\nUsing the iCloud document sync also means that you’re writing a distributed application. Your documents will be processed on different devices running different versions of your application. You may want to be forward-compatible with different versions of your file format. At the very least, you must ensure your application will not crash or fail if it faces a file generated by a newer version of your application installed on a different device. Users may not update all devices at once, so be prepared for that.\n\nFinally, your user interface needs to reflect synchronization, even though it may kill some of the magic. Especially on iOS, connection failures and slow file transfers are a reality. Your users need to be informed about the synchronization status of documents. You should consider showing whether files are currently uploading or downloading, in order to give users an idea of the availability of their documents. When using large files, you may need to show progress of file transfers. Your user interface should be graceful; if iCloud can’t serve you a certain document in time, your application should still be responsive and let the user retry or at least abort the operation.\n\n## Debugging\n\nDue to the involvement of multiple system services and external servers, debugging iCloud issues is quite difficult. The iCloud debugging capabilities provided by Xcode 5 are limited and mostly just give a glimpse of whether iCloud sync is happening or not. Fortunately, there are some more or less official ways of debugging the iCloud document store.\n\n### Debugging on OS X\n\nEvery now and then, you may experience iCloud stopping syncing of a certain file or even stopping to work completely. In particular, this happens easily when using debug breakpoints inside file coordinators or when killing a process during an ongoing file operation. It may even happen to your customers if your application crashed at such critical points. Often, neither rebooting nor logging out and back in to iCloud fixes the issue.\n\nTo fix these lockdowns, one command-line utility can be very beneficial: `ubcontrol`. This utility is part of every OS X release since 10.7. Using the command `ubcontrol -x`, you are able to reset the local state of document syncing. It will revive stalled synchronizations by resetting some private databases and caches and restarting all involved system daemons. It also stores some kind of post-mortem information inside `~/Library/Application Support/Ubiquity-backups`.\n\nWhile there are already very extensive log files written to `~/Library/Logs/Ubiquity`, you may also increase the logging level through `ubcontrol -k 7`. You are usually asked by Apple engineers to do this for collecting information on an iCloud-related bug report.\n\nFor debugging file coordination issues, you can also directly retrieve lock status information from inside the file coordination daemon. This enables you to understand file coordination deadlocks that may occur inside your application or between multiple processes. To access this information you need to execute the following commands in Terminal:\n\n```\nsudo heap filecoordinationd -addresses NSFileAccessArbiter\nsudo lldb -n filecoordinationd\npo [<address> valueForKey: @\"rootNode\"]\n```\n\nThe first command will return you the address of an internal singleton object of the file coordination daemon. Afterward, you attach *lldb* to the running daemon. By using the retrieved address from the first step, you will get an overview on the state of all active locks and file presenters. The debugger command will show you the entire tree of files that are currently being presented or coordinated. For example, if TextEdit is presenting a file called `example.txt` you will get the following trace:\n\n```\nexample.txt\n    <NSFileAccessNode 0x…> parent: 0x…, name: \"example.txt\"\n    presenters:\n        <NSFilePresenterProxy …> client: TextEdit …>\n        location: 0x7f9f4060b940\n    access claims: <none>\n    progress subscribers: <none>\n    progress publishers: <none>\n    children: <none>\n```\n\nIf you create such traces while a file coordination is going on (e.g. by setting a break point inside a file coordination block), you will also get a list of all processes waiting for file coordinators.\n\nIf you’re inspecting file coordination through *lldb*, you should always remember to execute the `detach` command as soon as possible. Otherwise, the global root process file coordination daemon will stay stopped, which will stall almost any application in your system.\n\n### Debugging on iOS\n\nOn iOS, debugging is more complicated, because you can’t inspect running system processes and you can’t use command-line tools like `ubcontrol`.\n\nLockdowns of iCloud seem to occur even more often on iOS. Neither restarts of the application nor simple device reboots help. The only effective way to fix such issues is a *cold boot*. During a cold boot, iOS seems to perform a reset of iCloud’s internal databases. A device can be cold-booted by pressing the power and home button at the same time for 10 seconds. \n\nTo activate extensive logging on iOS, there exists a special iCloud logging profile on Apple’s [developer downloads page][17]. If you’re searching for “Bug Reporter Logging Profiles (iOS),” you will find a mobile device profile called “iCloud Logging Profile.” Install this profile on your iOS device to activate extensive logging. You can access these logs by syncing your device with iTunes. Afterward, you will find it inside the folder `Library/Logs/CrashReporter/Mobile Device/<Device Name>/DiagnosticLogs/Ubiquity`. To deactivate intensive logging, just delete the profile from the device. Apple recommends you reboot your device before activation and after deactivation of the profile.\n\n### Debugging on iCloud Servers\n\nAside from debugging on your own devices, it might also be helpful to consider the debugging services on Apple’s servers. A particular web application is located at [developer.icloud.com][18], and it allows you to browse all information stored inside your ubiquity container, as well as the current transfer status. \n\nFor the past few months, Apple has also offered a safe server-side reset of iCloud syncing on all connected devices. For details, please have a look at this [support document][19].\n\n[1]: https://developer.apple.com/library/ios/documentation/General/Conceptual/iCloudDesignGuide/Chapters/DesigningForDocumentsIniCloud.html \"Designing For Documents in the Cloud\"\n[2]:http://www.ulyssesapp.com\n[3]:https://developer.apple.com/library/ios/documentation/General/Conceptual/iCloudDesignGuide/Chapters/iCloudFundametals.html#//apple_ref/doc/uid/TP40012094-CH6-SW13\n[4]:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/index.html#//apple_ref/occ/instm/NSFileManager/URLForUbiquityContainerIdentifier:\n[5]:https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileCoordinators/FileCoordinators.html#//apple_ref/doc/uid/TP40010672-CH11-SW1\n[6]:https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSFileCoordinator_class/Reference/Reference.html\n[7]:https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSFilePresenter_protocol/Reference/Reference.html\n[8]:https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSFilePresenter_protocol/Reference/Reference.html#//apple_ref/occ/intfp/NSFilePresenter/presentedItemOperationQueue\n[9]:https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSFilePresenter_protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSFilePresenter/savePresentedItemChangesWithCompletionHandler:\n[10]:https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSFilePresenter_protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSFilePresenter/presentedItemDidGainVersion:\n[11]:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/index.html#//apple_ref/occ/instm/NSFileManager/URLForUbiquityContainerIdentifier:#//apple_ref/occ/instm/NSFileManager/startDownloadingUbiquitousItemAtURL:error:\n[12]:https://developer.apple.com/library/ios/documentation/General/Conceptual/iCloudDesignGuide/Chapters/DesigningForDocumentsIniCloud.html \"Designing For Documents in the Cloud\"\n[13]:https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSFilePresenter_protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSFilePresenter/savePresentedItemChangesWithCompletionHandler:\n[14]:https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSFilePresenter_protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSFilePresenter/relinquishPresentedItemToReader:\n[15]:https://github.com/hydrixos/DeadlockExample\n[16]:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/index.html#//apple_ref/occ/instm/NSFileManager/URLForUbiquityContainerIdentifier:#//apple_ref/occ/instm/NSFileManager/startDownloadingUbiquitousItemAtURL:error:\n[17]:https://developer.apple.com/downloads\n[18]:https://developer.icloud.com/\n[19]:http://support.apple.com/kb/HT5824\n\n[image-1]:/images/issue-10/Open.png\n[image-2]:/images/issue-10/Mobile%20Documents.png\n"
  },
  {
    "path": "2014-03-07-ip-tcp-http.markdown",
    "content": "---\ntitle: \"IP, TCP, and HTTP\"\ncategory: \"10\"\ndate: \"2014-03-07 06:00:00\"\ntags: article\nauthor:\n  - name: Daniel Eggert\n    url: http://twitter.com/danielboedewadt\n---\n\nWhen an app communicates with a server, more often than not, that communication happens over HTTP. HTTP was developed for web browsers: when you enter http://www.objc.io into your browser, the browser talks to the server named www.objc.io using HTTP.\n\nHTTP is an application protocol running at the application layer. There are several protocols layered on top of each other. The stack of layers is often depicted like this:\n\n\n```\nApplication Layer -- e.g. HTTP\n----\nTransport Layer -- e.g. TCP\n----\nInternet Layer -- e.g. IP\n----\nLink Layer -- e.g. IEEE 802.2\n```\n\nThe so-called [OSI (Open Systems Interconnection) model](https://en.wikipedia.org/wiki/OSI_model) defines seven layers. We'll take a look at the application, transport, and Internet layers for the typical HTTP usage: HTTP, TCP, and IP. The layers below IP are the data link and physical layers. These are the layers that, e.g. implement Ethernet (Ethernet has a data link part and a physical part).\n\nWe will only look at the application, transport, and Internet layers, and in fact only look at one particular combination: HTTP running on top of TCP, which in turn runs on top of IP. This is the typical setup most of us use for our apps, day in and day out.\n\nWe hope that this will give you a more detailed understanding of how HTTP works under the hood, as well as what some common problems are, and how you can avoid them.\n\nThere are ways to send data through the Internet other than HTTP. One reason that HTTP has become so popular is that it will almost always work, even when the machine is behind a firewall.\n\nLet's start out at the lowest layer and take a look at IP, the Internet Protocol.\n\n## IP — Internet Protocol\n\nThe **IP** in TCP/IP is short for [Internet Protocol](https://en.wikipedia.org/wiki/Internet_Protocol). As the name suggests, it is one of the fundamental protocols of the Internet.\n\nIP implements [packet-switched networking](https://en.wikipedia.org/wiki/Packet_switching). It has a concept of *hosts*, which are machines. The IP protocol specifies how *datagrams* (packets) are sent between these hosts.\n\nA packet is a chunk of binary data that has a source host and a destination host. An IP network will then simply transmit the packet from the source to the destination. One important aspect of IP is that packets are delivered using *best effort*. A packet may be lost along the way and never reach the destination. Or it may get duplicated and arrive multiple times at the destination.\n\nEach host in an IP network has an address -- the so-called *IP address*. Each packet contains the source and destination hosts' addresses. The IP is responsible for routing datagrams -- as the IP packet travels through the network, each node (host) that it travels through looks at the destination address in the packet to figure out in which direction the packet should be forwarded.\n\nToday, most packages are still IPv4 (Internet Protocol version 4), where each IPv4 address is 32 bits long. They're most often written in [dotted-decimal](https://en.wikipedia.org/wiki/Dotted_decimal) notation, like so: 198.51.100.42\n\nThe newer IPv6 standard is slowly gaining traction. It has a larger address space: its addresses are 128 bits long. This allows for easier routing as the packets travel through the network. And since there are more available addresses, tricks such as [network address translation](https://en.wikipedia.org/wiki/Network_address_translation) are no longer necessary. IPv6 addresses are represented in the hexadecimal system and divided into eight groups separated by colons, e.g. `2001:0db8:85a3:0042:1000:8a2e:0370:7334`.\n\n### The IP Header\n\nAn IP packet consists of a header and a payload.\n\nThe payload contains the actual data to be transmitted, while the header is metadata.\n\n#### IPv4 Header\n\nAn IPv4 header looks like this:\n\n```\nIPv4 Header Format\nOffsets  Octet    0                       1                       2                       3\nOctet    Bit      0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31|\n 0         0     |Version    |IHL        |DSCP            |ECN  |Total Length                                   |\n 4        32     |Identification                                |Flags   |Fragment Offset                       |\n 8        64     |Time To Live           |Protocol              |Header Checksum                                |\n12        96     |Source IP Address                                                                             |\n16       128     |Destination IP Address                                                                        |\n20       160     |Options (if IHL > 5)                                                                          |\n```\n\nThe header is 20 bytes long (without options, which are rarely used).\n\nThe most interesting parts of the header are the source and destination IP addresses. Aside from that, the version field will be set to 4 -- it's IPv4. And the *protocol* field specifies which protocol the payload is using. TCP's protocol number is 6. The total length field specified is the length of the entire packet -- header plus payload.\n\nCheck Wikipedia's [article on IPv4](https://en.wikipedia.org/wiki/IPv4_header) for all the details about the header and its fields.\n\n#### IPv6 Header\n\n\nIPv6 uses addresses that are 128 bits long. The IPv6 header looks like this:\n\n```\nOffsets  Octet    0                       1                       2                       3\nOctet    Bit      0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31|\n 0         0     |Version    |Traffic Class         |Flow Label                                                 |\n 4        32     |Payload Length                                |Next Header            |Hop Limit              |\n 8        64     |Source Address                                                                                |\n12        96     |                                                                                              |\n16       128     |                                                                                              |\n20       160     |                                                                                              |\n24       192     |Destination Address                                                                           |\n28       224     |                                                                                              |\n32       256     |                                                                                              |\n36       288     |                                                                                              |\n```\n\nThe IPv6 header has a fixed length of 40 bytes. It's a lot simpler than IPv4 -- a few lessons were learned in the years that have passed since IPv4.\n\nThe source and destination addresses are again the most interesting fields. In IPv6, the *next header* field specifies what data follows the header. IPv6 allows chaining of headers inside the packet. Each subsequent IPv6 header will also have a *next header* field until the actual payload is reached. When the *next header* field, for example, is 6 (TCP's protocol number), the rest of the packet will be TCP data.\n\nAgain: Wikipedia's [article on IPv6 packets](https://en.wikipedia.org/wiki/IPv6_packet) has a lot more detail.\n\n### Fragmentation\n\nIn IPv4, packets (datagrams) can get [fragmented](https://en.wikipedia.org/wiki/IP_fragmentation). The underlying transport layer will have an upper limit to the length of packet it can support. In IPv4, a router may fragment a packet if it gets routed onto an underlying data link for which the packet would otherwise be too big. These packets will then get reassembled at the destination host. The sender can decide to disallow routers to fragment packets, in which case they'll send a *Packet Too Big* [ICMP](https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol) back to the sender.\n\nIn IPv6, a router will always drop the packet and send back a *Packet Too Big* [ICMP6](https://en.wikipedia.org/wiki/ICMPv6) message to the sender. The end points use this to do a [path MTU discovery](https://en.wikipedia.org/wiki/Path_MTU) to figure out what the optimal so-called *maximum transfer unit* (MTU) along the path between the two hosts is. Only when the upper layer has a minimum payload size that is too big for this MTU will IPv6 use [fragmentation](https://en.wikipedia.org/wiki/IPv6_packet#Fragmentation). With TCP over IPv6, this is not the case.\n\n## TCP — Transmission Control Protocol\n\nOne of the most common protocols to run on top of IP is, by far, TCP. It's so common that the entire suite of protocols is often referred to as TCP/IP.\n\nThe IP protocol allows for sending single packets (datagrams) between two hosts. Packets are delivered *best effort* and may: reach the destination in a different order than the one in which they were sent, reach the destination multiple times, or never reach the destination at all.\n\nTCP is built on top of IP. The Transmission Control Protocol provides reliable, ordered, error-checked delivery of a stream of data between programs. With TCP, an application running on one device can send data to an application on another device and be sure that the data arrives there in the same way that it was sent. This may seem trivial, but it's really a stark contrast to how the raw IP layer works.\n\nWith TCP, applications establish connections between each other. A TCP connection is duplex and allows data to flow in both directions. The applications on either end do not have to worry about the data being split up into packets, or the fact that the packet transport is *best effort*. TCP guarantees that the data will arrive at the other end in pristine condition.\n\nA typical use case of TCP is HTTP. Our web browser (application 1) connects to a web server (application 2). Once the connection is made, the browser can send a request through the connection, and the web server can send a response back through the same connection.\n\nMultiple applications on the same host can use TCP simultaneously. To uniquely identify an application, TCP has a concept of *ports*. A connection between two applications has a source IP address and a source port on one end, and a destination IP address and a destination port at the other end. This pair of addresses, plus a port for either end, uniquely identifies the connection.\n\nA web server using HTTPS will *listen* on port 443. The browser will use a so-called *ephemeral port* as the source port and then use TCP to establish a connection between the two address-port pairs.\n\nTCP runs unmodified on top of both IPv4 and IPv6. The *Protocol* (IPv4) or *Next Header* (IPv6) field will be set to 6, which is the protocol number for TCP.\n\n### TCP Segments\n\nThe data stream that flows between hosts is cut up into chunks, which are turned into TCP segments. The TCP segment then becomes the payload of an IP packet.\n\nEach TCP segment has a header and a payload. The payload is the actual data chunk to be transmitted. The TCP segment header first and foremost contains the source and destination port number -- the source and destination addresses are already present in the IP header.\n\nThe header also contains sequence and acknowledgement numbers and quite a few other fields which are all used by TCP to manage the connection.\n\nWe'll go into more detail about sequence number in a bit. It's basically a mechanism to give each segment a unique number. The first segment has a random number, e.g. 1721092979, and subsequent segments increase this number by 1: 1721092980, 1721092981, and so on. The acknowledgement numbers allow the other end to communicate back to the sender regarding which segments it has received so far. Since TCP is duplex, this happens in both directions.\n\n### TCP Connections\n\nConnection management is a central component of TCP. The protocol needs to pull a lot of tricks to hide the complexities of the unreliable IP layer. We'll take a quick look at connection setup, the actual data flow, and connection termination.\n\nThe state transitions that a connection can go through are quite complex (c.f. [TCP state diagram](https://upload.wikimedia.org/wikipedia/commons/f/f6/Tcp_state_diagram_fixed_new.svg)). But in most cases, things are relatively simple.\n\n#### Connection Setup\n\nIn TCP, a connection is always established from one host to another. Hence, there are two different roles in connection setup: one end (e.g. the web server) is listening for connections, while the other end (e.g. our app) connects to the listening application (e.g. the web server). The server performs a so-called *passive open* -- it starts listening. The client performs a so-called *active open* toward the server.\n\nConnection setup happens through a three-way handshake. It works like this:\n\n 1. The client sends a **SYN** to the server with a random sequence number, `A`\n 2. The server replies with a **SYN-ACK** with an acknowledgment number of `A+1` and a random sequence number, `B`\n 3. The client sends an **ACK** to the server with an acknowledgement number of `B+1` and a sequence number of `A+1`\n\n**SYN** is short for *synchronize sequence numbers*. Once data flows between both ends, each TCP segment has a sequence number. This is how TCP makes sure that all parts arrive at the other end, and that they're put together in the right order. Before communication can start, both ends need to synchronize the sequence number of the first segments.\n\n**ACK** is short for *acknowledgment*. When a segment arrives at one of the ends, that end will acknowledge the receipt of that segment by sending an acknowledgment for the sequence number of the received segment.\n\nIf we run´:\n\n```\ncurl -4 http://www.apple.com/contact/\n```\n\nthis will cause `curl` to create a TCP connection to www.apple.com on port 80.\n\nThe server www.apple.com / 23.63.125.15 is listening on port 80. Our own address in the output is `10.0.1.6`, and our *ephemeral port* is `52181` (this is a random, available port). The output from `tcpdump(1)` for the three-way handshake looks like this:\n\n```\n% sudo tcpdump -c 3 -i en3 -nS host 23.63.125.15\n18:31:29.140787 IP 10.0.1.6.52181 > 23.63.125.15.80: Flags [S], seq 1721092979, win 65535, options [mss 1460,nop,wscale 4,nop,nop,TS val 743929763 ecr 0,sackOK,eol], length 0\n18:31:29.150866 IP 23.63.125.15.80 > 10.0.1.6.52181: Flags [S.], seq 673593777, ack 1721092980, win 14480, options [mss 1460,sackOK,TS val 1433256622 ecr 743929763,nop,wscale 1], length 0\n18:31:29.150908 IP 10.0.1.6.52181 > 23.63.125.15.80: Flags [.], ack 673593778, win 8235, options [nop,nop,TS val 743929773 ecr 1433256622], length 0\n```\n\nThat's a lot of information right there. Let's step through this bit by bit.\n\nOn the very left-hand side we see the system time. This was run in the evening at 18:31. Next, `IP` tells us that these are IP packets.\n\nNext we see `10.0.1.6.52181 > 23.63.125.15.80`. This is the source and destination address-port pair. The first and third lines are from the client to the server, the second from the server to the client. `tcpdump` will simply append the port number to the IP address. `10.0.1.6.52181` means IP address 10.0.1.6, port 52181.\n\nThe `Flags` are flags in the TCP segment header: `S` for **SYN**, `.` for **ACK**, `P` for **PUSH**, and `F` for **FIN**. There are a few more we won't see here. Note how these three lines have **SYN**, then **SYN-ACK**, then **ACK**: this is the three-way handshake.\n\nThe first line shows the client sending the random sequence number 1721092979 (A) to the server. The second line shows the server sending an acknowledgement for 1721092980 (A+1) and its random sequence number 673593777 (B). Finally, the third line shows the client acknowledging 673593778 (B+1).\n\n#### Options\n\nAnother thing that happens during connection setup is for both ends to exchange additional options. In the first line, we see the client sending:\n\n```\n[mss 1460,nop,wscale 4,nop,nop,TS val 743929763 ecr 0,sackOK,eol]\n```\n\nand on the second line, the server is sending:\n\n```\n[mss 1460,sackOK,TS val 1433256622 ecr 743929763,nop,wscale 1]\n```\n\nThe `TS val` / `ecr` are used by TCP to estimate the round-trip time (RTT). The `TS val` part is the *time stamp* of the sender, and the (`ecr`) is the timestamp *echo reply*, which is (generally) the last timestamp that the sender has received. TCP uses the round-trip time for its congestion-control algorithms.\n\nBoth ends are sending `sackOK`. This will enable *Selective Acknowledgement*. It allows both ends to acknowledge receipt of byte ranges. Normally, the acknowledgement mechanism only allows acknowledging that the receiver has all data up to a specific byte count. SACK is outlined in [section 3 of RFC 2018](http://tools.ietf.org/html/rfc2018#section-3).\n\nThe `mss` option specified the *Maximum Segment Size*, which is the maximum number of bytes that this end is willing to receive in a single segment. `wscale` is the *window scale factor* that we'll talk about in a bit.\n\n#### Connection Data Flow\n\nWhen the connection is created, both ends can send data to the other end. Each segment that is sent has a sequence number corresponding to the number of bytes sent so far. The receiving end will acknowledge packets as they are received by sending back segments with the corresponding **ACK** in the header.\n\nIf we were transmitting 10 bytes per segment and 5 bytes in the last segment, this may looks like:\n\n```\nhost A sends segment with seq 10\nhost A sends segment with seq 20\nhost A sends segment with seq 30    host B sends segment with ack 10\nhost A sends segment with seq 35    host B sends segment with ack 20\n                                    host B sends segment with ack 30\n                                    host B sends segment with ack 35\n```\n\nThis mechanism happens in both directions. Host A will keep sending packets. As they arrive at host B, host B will send acknowledgements for these packets back to host A. But host A will keep sending packets without waiting for host B to acknowledge them.\n\nTCP incorporates flow control and a lot of sophisticated mechanisms for congestion control. These are all about figuring out (A) if segments got lost and need to be retransmitted, and (B) if the rate at which segments are sent needs to be adjusted.\n\nFlow control is about making sure that the sending side doesn't send data faster than the receiver (at either end) can process it. The receiver sends a so-called *receive window*, which tells the sender how much more data the receiver can buffer. There are some subtle details we'll skip, but in the above `tcpdump` output, we see a `win 65535` and a `wscale 4`. The first is the window size, the latter a scale factor. As a result, the host at `10.0.1.6` says it has a receive window of 4·64 kB = 256 kB and the host at `23.63.125.15` advertises `win 14480` and `wscale 1`, i.e. roughly 14 kB. As either side receives data, it will send an updated receive window to the other end.\n\nCongestion control is quite complex. The various mechanisms are all about figuring out at which rate data can be sent through the network. It's a very delicate balance. On one hand, there's the obvious desire to send data as fast as possible, but on the other hand, the performance will collapse dramatically when sending too much data. This is called [congestive collapse](https://en.wikipedia.org/wiki/Congestive_collapse#Congestive_collapse) and it's a property inherent to packet-switched networks. When too many packets are sent, packets will collide with other packets and the packet loss rate will climb dramatically.\n\nThe congestion control mechanisms need to also make sure that they play well with other flows. Today's congestion control mechanisms in TCP are outlined in detail in about 6,000 words in [RFC 5681](https://www.rfc-editor.org/rfc/rfc5681.txt). The basic idea is that the sender side looks at the acknowledgments it gets back. This is a very tricky business and there are various tradeoffs to be made. Remember that the underlying IP packets can arrive out of order, not at all, or twice. The sender needs to estimate what the round-trip time is and use that to figure out if it should have received an acknowledgement already. Retransmitting packets is obviously costly, but not retransmitting causes the connection to stall for a while, and the load on the network is likely to be very dynamic. The TCP algorithms need to constantly adapt to the current situation.\n\nThe important thing to note is that a TCP connection is a very lively and flexible thing. Aside from the actual data flow, both ends constantly send hints and updates back and forth to continuously fine-tune the connection.\n\nBecause of this tuning, TCP connections that are short-lived can be very inefficient. When a connection is first created, the TCP algorithms still don't know what the conditions of the network are. And toward the end of the connection lifetime, there's less information flowing back to the sender, which therefore has a harder time estimating how things are moving along.\n\nAbove, we saw the first three segments between the client and the server. If we look at the remainder of the connection, it looks like this:\n\n```\n18:31:29.150955 IP 10.0.1.6.52181 > 23.63.125.15.80: Flags [P.], seq 1721092980:1721093065, ack 673593778, win 8235, options [nop,nop,TS val 743929773 ecr 1433256622], length 85\n18:31:29.161213 IP 23.63.125.15.80 > 10.0.1.6.52181: Flags [.], ack 1721093065, win 7240, options [nop,nop,TS val 1433256633 ecr 743929773], length 0\n```\n\nThe client at `10.0.1.6` sends the first segment with data `length 85` (the HTTP request, 85 bytes). The **ACK** number is left at the same value, because no data has been received from the other end since the last segment.\n\nThe server at `23.63.125.15` then acknowledges the receipt of that data (but doesn't send any data): `length 0`. Since the connection is using *Selective acknowledgments*, the sequence number and acknowledgment numbers are byte ranges: 1721092980 to 1721093065 is 85 bytes. When the other end sends `ack 1721093065`, that means: I have everything up to byte 1721093065. The reason for these numbers being so large is because we're starting out at a random number. The byte ranges are relative to that initial number.\n\nThis pattern continues until all data has been sent:\n\n```\n18:31:29.189335 IP 23.63.125.15.80 > 10.0.1.6.52181: Flags [.], seq 673593778:673595226, ack 1721093065, win 7240, options [nop,nop,TS val 1433256660 ecr 743929773], length 1448\n18:31:29.190280 IP 23.63.125.15.80 > 10.0.1.6.52181: Flags [.], seq 673595226:673596674, ack 1721093065, win 7240, options [nop,nop,TS val 1433256660 ecr 743929773], length 1448\n18:31:29.190350 IP 10.0.1.6.52181 > 23.63.125.15.80: Flags [.], ack 673596674, win 8101, options [nop,nop,TS val 743929811 ecr 1433256660], length 0\n18:31:29.190597 IP 23.63.125.15.80 > 10.0.1.6.52181: Flags [.], seq 673596674:673598122, ack 1721093065, win 7240, options [nop,nop,TS val 1433256660 ecr 743929773], length 1448\n18:31:29.190601 IP 23.63.125.15.80 > 10.0.1.6.52181: Flags [.], seq 673598122:673599570, ack 1721093065, win 7240, options [nop,nop,TS val 1433256660 ecr 743929773], length 1448\n18:31:29.190614 IP 23.63.125.15.80 > 10.0.1.6.52181: Flags [.], seq 673599570:673601018, ack 1721093065, win 7240, options [nop,nop,TS val 1433256660 ecr 743929773], length 1448\n18:31:29.190616 IP 23.63.125.15.80 > 10.0.1.6.52181: Flags [.], seq 673601018:673602466, ack 1721093065, win 7240, options [nop,nop,TS val 1433256660 ecr 743929773], length 1448\n18:31:29.190617 IP 23.63.125.15.80 > 10.0.1.6.52181: Flags [.], seq 673602466:673603914, ack 1721093065, win 7240, options [nop,nop,TS val 1433256660 ecr 743929773], length 1448\n18:31:29.190619 IP 23.63.125.15.80 > 10.0.1.6.52181: Flags [.], seq 673603914:673605362, ack 1721093065, win 7240, options [nop,nop,TS val 1433256660 ecr 743929773], length 1448\n18:31:29.190621 IP 23.63.125.15.80 > 10.0.1.6.52181: Flags [.], seq 673605362:673606810, ack 1721093065, win 7240, options [nop,nop,TS val 1433256660 ecr 743929773], length 1448\n18:31:29.190679 IP 10.0.1.6.52181 > 23.63.125.15.80: Flags [.], ack 673599570, win 8011, options [nop,nop,TS val 743929812 ecr 1433256660], length 0\n18:31:29.190683 IP 10.0.1.6.52181 > 23.63.125.15.80: Flags [.], ack 673602466, win 7830, options [nop,nop,TS val 743929812 ecr 1433256660], length 0\n18:31:29.190688 IP 10.0.1.6.52181 > 23.63.125.15.80: Flags [.], ack 673605362, win 7830, options [nop,nop,TS val 743929812 ecr 1433256660], length 0\n18:31:29.190703 IP 10.0.1.6.52181 > 23.63.125.15.80: Flags [.], ack 673605362, win 8192, options [nop,nop,TS val 743929812 ecr 1433256660], length 0\n18:31:29.190743 IP 10.0.1.6.52181 > 23.63.125.15.80: Flags [.], ack 673606810, win 8192, options [nop,nop,TS val 743929812 ecr 1433256660], length 0\n18:31:29.190870 IP 23.63.125.15.80 > 10.0.1.6.52181: Flags [.], seq 673606810:673608258, ack 1721093065, win 7240, options [nop,nop,TS val 1433256660 ecr 743929773], length 1448\n18:31:29.198582 IP 23.63.125.15.80 > 10.0.1.6.52181: Flags [P.], seq 673608258:673608401, ack 1721093065, win 7240, options [nop,nop,TS val 1433256670 ecr 743929811], length 143\n18:31:29.198672 IP 10.0.1.6.52181 > 23.63.125.15.80: Flags [.], ack 673608401, win 8183, options [nop,nop,TS val 743929819 ecr 1433256660], length 0\n```\n\n#### Connection Termination\n\nFinally the connection is torn down (or terminated). Each end will send a **FIN** flag to the other end to tell it that it's done sending. This **FIN** is then acknowledged. When both ends have sent their **FIN** flags and they have been acknowledged, the connection is fully closed:\n\n```\n18:31:29.199029 IP 10.0.1.6.52181 > 23.63.125.15.80: Flags [F.], seq 1721093065, ack 673608401, win 8192, options [nop,nop,TS val 743929819 ecr 1433256660], length 0\n18:31:29.208416 IP 23.63.125.15.80 > 10.0.1.6.52181: Flags [F.], seq 673608401, ack 1721093066, win 7240, options [nop,nop,TS val 1433256680 ecr 743929819], length 0\n18:31:29.208493 IP 10.0.1.6.52181 > 23.63.125.15.80: Flags [.], ack 673608402, win 8192, options [nop,nop,TS val 743929828 ecr 1433256680], length 0\n```\n\nNote how on the second line, `23.63.125.15` sends its **FIN**, and at the same time acknowledges the other end's **FIN** with an **ACK** (dot), all in a single segment.\n\n## HTTP — Hypertext Transfer Protocol\n\nThe [World Wide Web](https://en.wikipedia.org/wiki/World_Wide_Web) of interlinked hypertext documents and a browser to browse this web started as an idea set forward in 1989 at [CERN](https://en.wikipedia.org/wiki/CERN). The protocol to be used for data communication was the *Hypertext Transfer Protocol*, or HTTP. Today's version is *HTTP/1.1*, which is defined in [RFC 2616](https://tools.ietf.org/html/rfc2616).\n\n### Request and Response\n\nHTTP uses a simple request and response mechanism. When we enter http://www.apple.com/ into Safari, it sends an HTTP request to the server at `www.apple.com`. The server in turn sends back a (single) response which contains the document that was requested.\n\nThere's always a single request and a single response. And both requests and responses follow the same format. The first line is the *request line* (request) or *status line* (response). This line is followed by headers. The headers end with an empty line. After that, an empty line follows the optional message body.\n\n### A Simple Request\n\nWhen [Safari](https://en.wikipedia.org/wiki/Safari_%28web_browser%29) loads the HTML for http://www.objc.io/about.html, it sends an HTTP request to `www.objc.io` with the following content:\n\n```\nGET /about.html HTTP/1.1\nHost: www.objc.io\nAccept-Encoding: gzip, deflate\nConnection: keep-alive\nIf-None-Match: \"a54907f38b306fe3ae4f32c003ddd507\"\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\nIf-Modified-Since: Mon, 10 Feb 2014 18:08:48 GMT\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.74.9 (KHTML, like Gecko) Version/7.0.2 Safari/537.74.9\nReferer: http://www.objc.io/\nDNT: 1\nAccept-Language: en-us\n```\n\nThe first line is the **request line**. It has three parts: the action, the resource, and the HTTP version. \n\nIn our example, the action is `GET`. The action is also often referred to as the [HTTP method](https://en.wikipedia.org/wiki/HTTP_method#Request_methods). The resources specify which resource the given action should be applied to. In our case it is `/about.html`, which tells the server that we want to *get* the document at `/about.html`. The HTTP version will be `HTTP/1.1`.\n\nNext, we have 10 lines with 10 HTTP headers. These are followed by an empty line. There's no message body in this request.\n\nThe headers have very varying purposes. They convey additional information to the web server. Wikipedia has a nice list of [common HTTP header fields](https://en.wikipedia.org/wiki/List_of_HTTP_header_fields). The first `Host: www.objc.io` header tells the server what server name the request is meant for. This mandatory request header allows the same physical server to serve multiple [domain names](https://en.wikipedia.org/wiki/Domain_names).\n\nLet's look at a few common ones:\n\n```\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\nAccept-Language: en-us\n```\n\nThis tells the server what media type Safari would like to receive. A server may be able to send a response in various formats. The `text/html` strings are [Internet media types](https://en.wikipedia.org/wiki/Mime_type), sometimes also known as MIME types or Content-types. The `q=0.9` allows Safari to convey a quality factor which it associates with the given media types. `Accept-Language` tells the server which languages Safari would prefer. Again, this lets the server pick the matching languages, if available.\n\n```\nAccept-Encoding: gzip, deflate\n```\n\nWith this header, Safari tells the server that the response body can be sent compressed. If this header is not set, the server must send the data uncompressed. Particularly for text (such as HTML), compression rates can dramatically reduce the amount of data that has to be sent.\n\n```\nIf-Modified-Since: Mon, 10 Feb 2014 18:08:48 GMT\nIf-None-Match: \"a54907f38b306fe3ae4f32c003ddd507\"\n```\n\nThese two are due to the fact that Safari already has the resulting document in its cache. Safari tells the server only to send it if it has either changed since February 10, or if its so-called ETag doesn't match `a54907f38b306fe3ae4f32c003ddd507`.\n\nThe `User-Agent` header tells the server what kind of client is making the request.\n\n### A Simple Response\n\nIn response to the above, the server responds with:\n\n```\nHTTP/1.1 304 Not Modified\nConnection: keep-alive\nDate: Mon, 03 Mar 2014 21:09:45 GMT\nCache-Control: max-age=3600\nETag: \"a54907f38b306fe3ae4f32c003ddd507\"\nLast-Modified: Mon, 10 Feb 2014 18:08:48 GMT\nAge: 6\nX-Cache: Hit from cloudfront\nVia: 1.1 eb67cb25620df959ba21a943fbc49ef6.cloudfront.net (CloudFront)\nX-Amz-Cf-Id: dDSBgR86EKBemW6el-pBI9kAnuYJEaPQYEqGmBnilD12CbixCuZYVQ==\n```\n\nThe first line is the so-called *status line*. It contains the HTTP version, followed by a [status code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes) (304) and a status message.\n\nHTTP defines a [list of status codes](https://en.wikipedia.org/wiki/Http_status_codes) and their meanings. In this case, we're receiving **304**, which means that the resource we requested hasn't been modified.\n\nThe response doesn't contain any body message. It simply tells the receiver: *Your version is up to date.*\n\n### Caching Turned Off\n\nLet's do another request with `curl`:\n\n```\n% curl http://www.apple.com/hotnews/ > /dev/null\n```\n\n`curl` doesn't use a local cache. The entire request will now look like this:\n\n```\nGET /hotnews/ HTTP/1.1\nUser-Agent: curl/7.30.0\nHost: www.apple.com\nAccept: */*\n```\n\nThis is quite similar to what Safari was requesting. This time, there's no `If-None-Match` header, and the server will have to send the document.\n\nNote how `curl` announces that it will accept any kind of media format: (`Accept: */*`).\n\nThe response from www.apple.com looks like this:\n\n```\nHTTP/1.1 200 OK\nServer: Apache\nContent-Type: text/html; charset=UTF-8\nCache-Control: max-age=424\nExpires: Mon, 03 Mar 2014 21:57:55 GMT\nDate: Mon, 03 Mar 2014 21:50:51 GMT\nContent-Length: 12342\nConnection: keep-alive\n\n<!DOCTYPE html>\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\" lang=\"en-US\">\n<head>\n    <meta charset=\"utf-8\" />\n```\n\nand continues on for quite a while. It now has a response body which contains the HTML document.\n\nThe response from Apple's server contains a [status code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes) of *200*, which is the standard response for successful HTTP requests.\n\nApple's server also tells us that the response's media type is `text/html; charset=UTF-8`. The `Content-Length: 12342` tells us what the length of the message body is.\n\n## HTTPS — HTTP Secure\n\n[Transport Layer Security](https://en.wikipedia.org/wiki/Transport_Layer_Security) is a cryptographic protocol that runs on top of TCP. It allows for two things: both ends can verify the identity of the other end, and the data sent between both ends is encrypted. Using HTTP on top of TLS gives you HTTP Secure, or simply, HTTPS.\n\nSimply using HTTPS instead of HTTP will give you a huge improvement in security. There are some additional steps that you may want to take, though, both of which will additionally improve the security of your communication.\n\n### TLS 1.2\n\nYou should set the `TLSMinimumSupportedProtocol` to `kTLSProtocol12` to require [TLS version 1.2](https://en.wikipedia.org/wiki/Transport_Layer_Security#TLS_1.2) if your server supports that. This will make [man-in-the-middle attacks](https://en.wikipedia.org/wiki/Man-in-the-middle_attack) more difficult.\n\n### Certificate Pinning\n\nThere's little point in the data we send being encrypted if we can't be certain that the other end we're talking to is actually who we think it is. The server's certificate tells us who the server is. Only allowing a connection to a very specific certificate is called [certificate pinning](https://en.wikipedia.org/wiki/Certificate_pinning#Certificate_pinning).\n\nWhen a client makes a connection over TLS to a server, the operating system will decide if it thinks the server's certificate is valid. There are a few ways this can be circumvented, most notably by installing certificates onto the iOS device and marking them as trusted. Once that's been done, it's trivial to perform a [man-in-the-middle attack](https://en.wikipedia.org/wiki/Man-in-the-middle_attack) against your app.\n\nTo prevent this from happening (or at least make it extremely difficult), we can use a method called certificate pinning. When then TLS connection is set up, we inspect the server's certificate and check not only that it's to be considered valid, but also that it is the certificate that we expect our server to have. This only works if we're connecting to our own server and hence can coordinate an update to the server's certificate with an update to the app.\n\nTo do this, we need to inspect the so-called *server trust* during connection. When an `NSURLSession` creates a connection, the delegate receives a `-URLSession:didReceiveChallenge:completionHandler:` call. The passed `NSURLAuthenticationChallenge` has a `protectionSpace` property, which is an instance of `NSURLProtectionSpace`. This, in turn, has a `serverTrust` property.\n\nThe `serverTrust` is a `SecTrustRef` object. The Security framework has various methods to query the `SecTrustRef` object. The [`AFSecurityPolicy`](https://github.com/AFNetworking/AFNetworking/blob/7f2c395ba185b586468557b22977ccd2b79fae66/AFNetworking/AFSecurityPolicy.m) from the AFNetworking project is a good starting point. As always, when you build your own security-related code, have someone review it carefully. You don't want to have a [`goto fail;`](https://www.imperialviolet.org/2014/02/22/applebug.html) bug in this part of your code.\n\n## Putting the Pieces Together\n\nNow that we know a bit about how all the pieces (IP, TCP, and HTTP) work, there are a few things we can do and be aware of.\n\n\n### Efficiently Using Connections\n\nThere are two aspects of a TCP connection that are problematic: the initial setup, and the last few segments that are pushed across the connection.\n\n#### Setup\n\nThe connection setup can be very time consuming. As mentioned above, TCP needs to do a three-way handshake. There's not a lot of data that needs to be sent back and forth. But, particularly when on a mobile network, the time a packet takes to travel from one host (a user's iPhone) to another host (a web server) can easily be around 250 ms -- a quarter of a second. For a three-way handshake, we'll often spend 750 ms just to establish the connection, before any payload data has been sent.\n\nIn the case of HTTPS, things are even more dramatic. With HTTPS we have HTTP running on top of TLS, which in turn runs on top of TCP. The TCP connection will still do its three-way handshake. Next up, the TLS layer does another three-way handshake. Roughly speaking, an HTTPS connection will thus take twice as long as a normal HTTP connection before sending any data . If the round-trip time is 500 ms (250 ms end-to-end), that adds up to 1.5 seconds.\n\nThis setup time is costly, regardless of if the connection will transfer a lot or just a small amount of data.\n\nAnother aspect of TCP affects connections on which we expect to transfer a larger amount of data. When sending segments into a network with unknown conditions, TCP needs to probe the network to determine the available capacity. In other words: it takes TCP a while to figure out how fast it can send data over the network. Only once it has figured this out can it send the data at the optimal speed. The algorithm that is used for this is called [slow-start](https://en.wikipedia.org/wiki/Slow-start). On a side note, it's worth pointing out that the slow-start algorithm doesn't perform well on networks with poor data link layer transmission quality, as is often the case for wireless networks.\n\n#### Tear Down\n\nAnother problem arises toward the end of the data transfer. When we do an HTTP request for a resource, the server will keep sending TCP segments to our host, and the host will respond with **ACK** messages as it receives the data. If a single packet is lost along the way, the server will not receive an **ACK** for that packet. It can therefore notice that the packet was lost and do what's called a [fast retransmit](https://en.wikipedia.org/wiki/Fast_retransmit).\n\nWhen a packet is lost, the following packet will cause the receiver to send an **ACK** identical to the last **ACK** it sent. The receiver will hence receive a *duplicate ACK*. There are several network conditions that can cause a duplicate ACK even without packet loss. The sender therefore only performs a fast retransmit when it receives three duplicate ACKs.\n\nThe problem with this is at the end of the data transmission. When the sender stops sending segments, the receiver stops sending ACKs back. And there's no way for the fast-retransmit algorithm to detect if a packet is lost within the last four segments being sent. On a typical network, that's equivalent to 5.7 kB of data. Within the last 5.7 kB, the fast retransmit can't do its job. If a packet is lost, TCP has to fall back to a more patient algorithm to detect packet loss. It's not uncommon for a retransmit to take several seconds in such a case.\n\n#### Keep-Alive and Pipelining\n\nHTTP has two strategies to counter these problems. The simplest is called [HTTP persistent connection](https://en.wikipedia.org/wiki/HTTP_persistent_connection), sometimes also known as *keep-alive*. HTTP will simply reuse the same TCP connection once a single request-response pair is done. In the case of HTTPS, the same TLS connection will be reused:\n\n```\nopen connection\nclient sends HTTP request 1 ->\n                            <- server sends HTTP response 1\nclient sends HTTP request 2 ->\n                            <- server sends HTTP response 2\nclient sends HTTP request 3 ->\n                            <- server sends HTTP response 3\nclose connection\n```\n\nThe second step is to use [HTTP pipelining](https://en.wikipedia.org/wiki/Http_pipelining), which allows the client to send multiple requests over the same connection without waiting for the response from previous requests. The requests can be sent in parallel with responses being received, but the responses are still sent back to the client in the order that the requests were sent -- it's still a [first-in-first-out](https://en.wikipedia.org/wiki/FIFO) principle. \n\nSlightly simplified, this would look like:\n\n```\nopen connection\nclient sends HTTP request 1 ->\nclient sends HTTP request 2 ->\nclient sends HTTP request 3 ->\nclient sends HTTP request 4 ->\n                            <- server sends HTTP response 1\n                            <- server sends HTTP response 2\n                            <- server sends HTTP response 3\n                            <- server sends HTTP response 4\nclose connection\n```\n\nNote, however, that the server may start sending the response at any time, and doesn't have to wait until all requests have been received.\n\nWith this, we're able to use TCP in a more efficient way. We're now only doing the handshake at the very beginning, and since we keep using the same connection, TCP can do a much better job of using the available bandwidth. The TCP congestion control algorithms will also do a much better job. Finally, the problem with the fast retransmit failing to work on the last four segments will only affect the last four segments of the entire connection, and not the last four segments of each request-response, as before.\n\nThe improvements of using HTTP pipelining can be quite dramatic over high-latency connections -- which is what you have when your iPhone is not on Wi-Fi. In fact, there's been some [research](http://research.microsoft.com/pubs/170059/A%20comparison%20of%20SPDY%20and%20HTTP%20performance.pdf) that suggests that there's no additional performance benefit to using [SPDY](https://en.wikipedia.org/wiki/SPDY) over HTTP pipelining on mobile networks.\n\nWhen communicating with a server, [RFC 2616](https://tools.ietf.org/html/rfc2616) recommends using two connections when HTTP pipelining is enabled. According to these guidelines, this will result in the best response times and avoid congestion. There's no performance benefit to additional connections.\n\nSadly, there are still some servers that don't support pipelining. You should try to enable it, however, and check if your particular server does support it. Chances are, it does. But because some servers don't, HTTP pipelining is turned off by default for `NSURLSession`. Make sure to set `HTTPShouldUsePipelining` to `YES` on the `NSURLSessionConfiguration` if you're able to use pipelining.\n\n### Timeout\n\nWe have all used apps while being on a slow network. Many of these apps give up after a timeout, say 15 seconds. That is really bad app design. It's probably good to give the user feedback and say, \"Hey, your network seems to be slow. This may take some time.\" But as long as there's a connection, even if it's bad, TCP ensure that the request will make it to the other end and that the response will make it back. It may just take a while.\n\nOr look at it another way: If we're on a slow network, the request-response round-trip time may take 17 seconds to complete. If we're stopping after 15 seconds, the user will be unable to perform the desired action if he or she is patient. If the user is on a slow network, he or she will know that things will take a while (we'll send a notification), and if the user is willing to be patient, we shouldn't prevent him or her from using the app.\n\nThere's a misconception that restarting the (HTTP) request will fix the problem. That is not the case. Again, TCP will resend those packets that need resending on its own.\n\nThe correct approach is this: When we send off a URL request, we set a timer for 10 seconds. Once we get the response back, we invalidate the timer. If the timer fires before we get the response back, we'll show some UI: \"Please be patient, the network appears to be slow right now.\" Depending on the app, we might want to give the user an option to cancel the action. But we shouldn't make that decision for the user.\n\nAs long as both sides have the same IP address, the connection stays 'alive.' On an iPhone, you will lose connection when you switch from Wi-Fi to 3G because the other end can no longer route its packets to the IP address that was used to create the connection.\n\n### Caching\n\nNote how in our first example, we send a:\n\n```\nIf-None-Match: \"a54907f38b306fe3ae4f32c003ddd507\"\n```\n\nline to the server, letting it know that we already have a resource locally and only want the server to send the resource to us if it has a newer version. If you're building your own communication with your server, try to make use of this mechanism. It can dramatically speed up communication if used correctly. The mechanism is called [HTTP ETag](https://en.wikipedia.org/wiki/HTTP_ETag).\n\nGoing to extremes, remember: \"The fastest request is the one never made.\" When sending a request to a server, even on a very good network, requesting a small amount of data, and a very fast server, you're unlikely to have a response back in less than 50 ms. And that's just one request. If there's a way for you to create that data locally in 50 ms or less, don't make that request.\n\nCache resources locally whenever you think that they'll stay valid for some time. Check the `Expires` header or rely on the caching mechanisms of `NSURLSession` by using the default `NSURLRequestUseProtocolCachePolicy`.\n\n\n## Summary\n\nSending a request over HTTP with `NSURLSession` is very straightforward. But there are a lot of technologies involved in making that request. Knowing how those pieces work will allow you to optimize the way you're using HTTP requests. Our users expect apps that perform well under any condition. Understanding how IP, TCP, and HTTP work makes it easier to deliver on those expectations.\n"
  },
  {
    "path": "2014-03-07-networked-core-data-application.md",
    "content": "---\ntitle:  \"A Networked Core Data Application\"\ncategory: \"10\"\ndate: \"2014-03-07 07:00:00\"\ntags: article\nauthor:\n  - name: Chris Eidhof\n    url: https://twitter.com/chriseidhof\n---\n\n\nOne of the things almost every app developer has to do in his or her life is import things from a web service into Core Data. This article describes how to do that. Everything we discuss here has been described before in previous articles, and by Apple in its documentation. However, it is still instructive to have a look at how to do from start to finish. \n\nThe full source of the app is [available on GitHub](https://github.com/objcio/issue-10-core-data-network-application).\n\n## Plan\n\nWe will build a small read-only app that shows a list of all the CocoaPods specifications. They are displayed in a table view, and all the pod specifications are fetched from a web service that returns them as JSON objects, in a paginated fashion.\n\nWe proceed as follows:\n\n1. First, we create a `PodsWebservice` class that fetches all the specs from the web service.\n1. Next, we create an `Importer` object that takes the specs, and imports them into Core Data.\n1. Finally, we show how to make the import work on a background thread.\n\n## Getting Objects from the Web Service\n\nFirst, it is nice to create a separate class that imports things from the web service. We have written a [small example web server](https://gist.github.com/chriseidhof/725946f0d02b17ced209) that takes the CocoaPods specs repository and generates JSON from that; getting the URL `/specs` returns a list of pod specifications in alphabetic order. The web service is paginated, so we need to request each page separately. An example response looks like this:\n\n```json\n{ \n  \"number_of_pages\": 559,\n  \"result\": [{\n    \"authors\": { \"Ash Furrow\": \"ash@ashfurrow.com\" },\n    \"homepage\": \"https://github.com/500px/500px-iOS-api\",\n    \"license\": \"MIT\",\n    \"name\": \"500px-iOS-api\",\n  ...\n```\n\nWe want to create a class that has only one method, `fetchAllPods:`, that takes a callback block, which gets called for every page. It could also have been done using delegation; why we chose to have a block is something you can read in the article on [communication patterns](/issues/7-foundation/communication-patterns/):\n\n```objc\n@interface PodsWebservice : NSObject\n- (void)fetchAllPods:(void (^)(NSArray *pods))callback;\n@end\n```\n\nThis callback gets called for every page. Implementing this method is easy. We create a helper method, ` fetchAllPods:page:`, that fetches all pods for a page, and then calls itself once it has loaded a page. Note that, for brevity, we've left out the error handling here, but if you look at the full project on GitHub, you'll see that we added it there. It's important to always check for errors, and at least log them so that you can quickly see if something isn't working as expected:\n\n```objc\n- (void)fetchAllPods:(void (^)(NSArray *pods))callback page:(NSUInteger)page\n{\n    NSString *urlString = [NSString stringWithFormat:@\"http://localhost:4567/specs?page=%d\", page];\n    NSURL *url = [NSURL URLWithString:urlString];\n    [[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:\n      ^(NSData *data, NSURLResponse *response, NSError *error) {\n        id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];\n        if ([result isKindOfClass:[NSDictionary class]]) {\n            NSArray *pods = result[@\"result\"];\n            callback(pods);\n            NSNumber* numberOfPages = result[@\"number_of_pages\"];\n            NSUInteger nextPage = page + 1;\n            if (nextPage < numberOfPages.unsignedIntegerValue) {\n                [self fetchAllPods:callback page:nextPage];\n            }\n        }\n    }] resume];\n}\n```\n\nThat's all there is to it. We parse the JSON, do some very rough checking (verifying that the result is a dictionary), and then call our callback.\n\n## Putting Objects into Core Data\n\nNow we can load the JSON results into our Core Data store. To separate things, we create an `Importer` object that calls the web service and creates or updates objects. It's nice to have this in a separate class, because that way our web service and the Core Data parts are completely decoupled. If we would ever want to feed the store with a different web service, or reuse the web service somewhere, we now don't have to manually detangle the two. Also, not having this logic in a view controller makes it easier to reuse the components in a different app.\n\nOur importer has two methods:\n\n```objc\n@interface Importer : NSObject\n- (id)initWithContext:(NSManagedObjectContext *)context \n           webservice:(PodsWebservice *)webservice;\n- (void)import;\n@end\n```\n\nInjecting the context into the object via the constructor is a powerful trick. When writing tests, we could easily inject a different context. The same holds for the web service: we could easily have a different object mock the web service. \n\nThe `import` method is the one that has the logic. We call the `fetchAllPods:` method, and for each batch of pod specifications, we import them into the context. By wrapping the logic into a `performBlock:`, the context makes sure that everything happens on the correct thread. We then iterate over the specs, and for each one, we generate a unique identifier (this can be anything that uniquely determines a model object, as also explained in [Drew's article](/issues/10-syncing-data/data-synchronization/)). We then try to find the model object, or create it if it doesn't exist. The method `loadFromDictionary:` takes the JSON dictionary and updates the model object with the values contained in the dictionary:\n\n```objc\n- (void)import\n{\n    [self.webservice fetchAllPods:^(NSArray *pods)\n    {\n        [self.context performBlock:^\n        {\n            for(NSDictionary *podSpec in pods) {\n                NSString *identifier = [podSpec[@\"name\"] stringByAppendingString:podSpec[@\"version\"]];\n                Pod *pod = [Pod findOrCreatePodWithIdentifier:identifier inContext:self.context];\n                [pod loadFromDictionary:podSpec];\n            }\n        }];\n    }];\n}\n```\n\nThere are some more things to note about the code above. First of all, the find-or-create method is very inefficient. In production code, you would batch up the pods and find all of them at the same time, as explained in the section [Efficiently Importing Data](/issues/4-core-data/importing-large-data-sets-into-core-data/) in the Core Data Programming Guide.\n\nSecond, we created the `loadFromDictionary:` directly in the `Pod` class (which is the managed object subclass). This means that now our model object knows about the web service. In real code, we would probably put this into a category so that the two are nicely separated. For this example, it doesn't matter.\n\n## Creating a Separate Background Stack\n\nIn writing the code above, we started by having everything on the main managed object context. Our app displays a list of all the pods in a table view controller, using a fetched results controller. The fetched results controller automatically updates the data model when things change in the managed object context. However, also having the importing being done on the main managed object context is not optimal. The main thread might get blocked, and the UI unresponsive. Most of the time, the work being done on the main thread is so minimal that it is hardly noticeable. If this is the case in your situation, it might be fine to leave it like that. However, if we put in some extra effort, we can make the import happen in a background thread.\n\nIn the WWDC sessions Apple recommends two options for concurrent Core Data. Both involve separate managed object contexts, and they can either share the same persistent store coordinator, or not. Having separate persistent store coordinators provides the best performance when doing a lot of changes, because the only locks needed are on the sqlite level. Having a shared persistent store coordinator also means having a shared cache. When you're not making a lot of changes, this can be much faster. So, depending on your use case, you should measure what's best and then choose whether you want a shared persistent store coordinator or not.\nIn the case where the main context is read-only (such as in the app described so far), no locking is required at all, because sqlite in iOS7 has write-ahead logging enabled and supports multiple readers and a single writer. However, for our demonstration purposes, we'll use the approach with completely separate stacks. To set up a managed object context, we use the following code:\n\n\n```objc\n- (NSManagedObjectContext *)setupManagedObjectContextWithConcurrencyType:(NSManagedObjectContextConcurrencyType)concurrencyType\n{\n    NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:concurrencyType];\n    managedObjectContext.persistentStoreCoordinator =\n            [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];\n    NSError* error;\n    [managedObjectContext.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType \n                                                                  configuration:nil \n                                                                            URL:self.storeURL \n                                                                        options:nil \n                                                                          error:&error];\n    if (error) {\n        NSLog(@\"error: %@\", error.localizedDescription);\n    }\n    return managedObjectContext;\n}\n```\n\nThen we call this method twice — once for the main managed object context, and once for the background managed object context:\n\n```objc\nself.managedObjectContext = [self setupManagedObjectContextWithConcurrencyType:NSMainQueueConcurrencyType];\nself.backgroundManagedObjectContext = [self setupManagedObjectContextWithConcurrencyType:NSPrivateQueueConcurrencyType];\n```\n\nNote that passing in the parameter `NSPrivateQueueConcurrencyType` tells Core Data to create a separate queue, which ensures that the background managed object context operations happen on a separate thread.\n\nNow there's only one more step left: whenever the background context is saved, we need to update the main thread. We described how to do this in a [previous article](/issues/2-concurrency/common-background-practices/) in issue #2. We register to get a notification whenever a context saves, and if it's the background context, call the method `mergeChangesFromContextDidSaveNotification:`. That's all there is to it:\n\n```objc\n[[NSNotificationCenter defaultCenter]\n        addObserverForName:NSManagedObjectContextDidSaveNotification\n                    object:nil\n                     queue:nil\n                usingBlock:^(NSNotification* note) {\n    NSManagedObjectContext *moc = self.managedObjectContext;\n    if (note.object != moc) {\n        [moc performBlock:^(){\n            [moc mergeChangesFromContextDidSaveNotification:note];\n        }];\n    }\n }];\n```\n\nAgain, there is a small caveat: the `mergeChangesFromContextDidSaveNotification:` happens inside the `performBlock:`. In our case, the `moc` is the main managed object context, and hence, this will block the main thread.\n\nNote that your UI (even if it's read-only) has to be able to deal with changes to objects, or even deletions. Brent Simmons recently wrote about [why to use a custom notification for deletion](http://inessential.com/2014/02/25/why_use_a_custom_notification_for_note_d) and [deleting objects in Core Data](http://inessential.com/2014/02/25/more_about_deleting_objects_in_core_data). These explanations show how to deal with the fact that, if you're displaying an object in your UI, changes might happen or the object might get deleted as you're displaying it.\n\n\n## Implementing Writes from the UI\n\n\nYou might think the above looks very simple, and that's because the only writing is done in the background thread. In our current application, we don't deal with merging in the other direction; there are no changes coming from the main managed object context. In order to add this, you could take multiple strategies. This is best described in [Drew's article](/issues/10-syncing-data/data-synchronization/). \n\nDepending on your requirements, one very simple pattern that might work is this: whenever the user changes something in the UI, you don't change the managed object context. Instead, you call the web service. If this succeeds, you get the diff from the web service, and update your background context. The changes will then propagate to the main context. There are two drawbacks with this: it might take a while before the user sees the changes in the UI, and if the user is not online, he or she can't change anything. In [Florian's article](/issues/10-syncing-data/sync-case-study/), we describe how we used a different strategy that also works offline.\n\nIf you're dealing with merges, you will also need to define a merge policy. This is again something very specific to your use case. You may want to throw an error if a merge fails, or always give priority to one managed object context. The [NSMergePolicy](https://developer.apple.com/library/mac/documentation/CoreData/Reference/NSMergePolicy_Class/Reference/Reference.html) class describes the possibilities.\n\n\n## Conclusion\n\nWe've seen how to implement a simple read-only application that imports a large set of data from a web service into Core Data. By using a background managed object context, we've built an app that doesn't block the main UI (except when merging the changes).\n"
  },
  {
    "path": "2014-03-07-sync-case-study.md",
    "content": "---\ntitle:  \"A Sync Case Study\"\ncategory: \"10\"\ndate: \"2014-03-07 08:00:00\"\ntags: article\nauthor:\n  - name: Florian Kugler\n    url: https://twitter.com/floriankugler\n---\n\nA while ago I was working together with [Chris](https://twitter.com/chriseidhof) on an enterprise iPad application that was to be deployed in a large youth sports organization. We chose to use Core Data for our persistency needs and built a custom data synchronization solution around it that fit our needs. In the syncing grid explained in [Drew's article](/issues/10-syncing-data/data-synchronization/), this solution uses the asynchronous client-server approach.\n\nIn this article, I will lay out the decision and implementation process as a case study for rolling out your own syncing solution. It's not a perfect or a universally applicable one, but it fit our needs at the time.\n\nBefore we dive into it: if you're interested in the topic of data syncing solutions, (which you probably are if you're reading this), you should definitely also head over to [Brent's blog](http://www.inessential.com) and follow his Vesper sync series. It's a great read, following along his thinking while implementing sync for Vesper.\n\n\n## Use Case\n\nMost syncing solutions today focus on the problem of syncing a user's data across multiple personal devices, e.g. [iCloud Core Data sync](/issues/10-syncing-data/icloud-core-data/) or the [Dropbox Datastore API](https://www.dropbox.com/developers/datastore). Our syncing needs were a bit different, though. The app we were building was to be deployed within an organization with approximately 50 devices in total. We needed to sync the data between all those devices, each device belonging to a different staff member, with everybody working off the same data set.\n\nThe data itself had a moderately complex structure with approximately a dozen entities and many relationships between them. But we needed to handle quite a bit of data. In production, the number of records would quickly grow into the six figures.\n\nWhile Wi-Fi access would be available for most of the staff members most of the time, the quality of the network connection was pretty poor. But being able to access and work with the app most of the time wouldn't have been good enough anyway, so we needed to make sure that people could also interact with the data offline. \n\n\n## Requirements\n\nWith this usage scenario in mind, the requirements for our syncing architecture were pretty clear:\n\n1. Each device has to have the whole data set available, with and without an Internet connection.\n2. Due to the mediocre network connection, syncing has to happen with as few requests as possible.\n3. Changes are only accepted if they were made based off the most recent data, because nobody should be able to override somebody else's prior changes without being aware of them.\n\n\n## Design\n\n### API\n\nDue to the nested structure of the data model and potential high latency in the network connection, a traditional REST-style API wouldn't have been a very good fit. For example, to show a typical dashboard view in the application, several layers of the data hierarchy would have to have been traversed in order to collect all the necessary data: teams, team-player associations, players, screens, and screen items. If we were to query all these entity types separately, we would have ended up with many requests until all the data was up to date.\n\nInstead, we chose to use something more atomic, with a much higher data-to-request ratio. The client interacts with the sync server via a single API endpoint: `/sync`. \n\nIn order to achieve this, we needed a custom format of exchanging data between the client and the server that would transport all the necessary information for the sync process and handle it in one request.\n\n\n### Data Format\n\nThe client and the server exchange data via a custom JSON format. The same format is used in both directions -- the client talks to the server in the same way the server talks back to the client. A simple example of this format looks like this:\n\n```json\n{\n    \"maxRevision\": 17382,\n    \"changeSets: [\n        ...\n    ]\n}\n```\n\nOn the top level, the JSON data has two keys: `maxRevision` and `changeSets`. `maxRevision` is a simple revision number that unambiguously identifies the revision of the data set currently available on the client that sends this request. The `changeSets` key holds an array of change set objects that look something like this:\n\n```json\n{\n    \"types\": [ \"users\" ],\n    \"users\": {\n        \"create\": [],\n        \"update\": [ 1013 ],\n        \"delete\": [],\n        \"attributes\": {\n            \"1013\": {\n                \"first_name\": \"Florian\",\n                \"last_name\": \"Kugler\",\n                \"date_of_birth\": \"1979-09-12 00:00:00.000+00\"\n                \"revision\": 355\n            }\n        }\n    }\n}\n```\n\nThe top level, `types`, key lists all the entity types that are contained in this change set. Each entity type then is described by its own change object, which contains the keys `create`, `update`, and `delete`, which are arrays of record IDs -- as well as `attributes`, which actually holds the new or updated data for each changed record.\n\nThis data format carries a little bit of legacy cruft from a previously existing web application where this particular structure was beneficial for processing in the client-side framework used at the time. But it serves the purpose of the syncing solution described here equally well.\n\nLet's have a look at a slightly more complex example. We have entered some new screening data for one of the players on a device, which now should be synced up to the server. The request would look something like this:\n\n```json\n{\n    \"maxRevision\": 1000,\n    \"changeSets\": [\n        {\n            \"types\": [ \"screen_instances\", \"screen_instance_items\" ],\n            \"screen_instances\": {\n                \"create\": [ -10 ],\n                \"update\": [],\n                \"delete\": [],\n                \"attributes\": {\n                    \"-10\": {\n                        \"screen_id\": 749,\n                        \"date\": \"2014-02-01 13:15:23.487+01\",\n                        \"comment\": \"\"\n                    }\n                }\n            },\n            \"screen_instance_items: {\n                \"create\": [ -11, -12 ],\n                \"update\": [],\n                \"delete\": [],\n                \"attributes\": {\n                    \"-11\": {\n                        \"screen_instance_id\": -10,\n                        \"numeric_value\": 2\n                    },\n                    \"-12\": {\n                        ...\n                    }\n                }\n            }\n        }\n    ]\n}\n```\n\nNotice how the records being sent have negative IDs. That's because they are newly created records. The new `screen_instance` record has the ID `-10`, and the `screen_instance_items` records reference this record by their foreign keys. \n\nOnce the server has processed this request (let's assume there was no conflict or permission problem), it would respond with JSON data like this:\n\n```objc\n{\n    \"maxRevision\": 1001,\n    \"changeSets\": [\n        {\n            \"conflict\": false,\n            \"types\": [ \"screen_instances\", \"screen_instance_items\" ],\n            \"screen_instances\": {\n                \"create\": [ 321 ],\n                \"update\": [],\n                \"delete\": [],\n                \"attributes\": {\n                    \"321\": {\n                        \"__oldId__\": -10\n                        \"revision\": 1001\n                        \"screen_id\": 749,\n                        \"date\": \"2014-02-01 13:15:23.487+01\",\n                        \"comment\": \"\",\n                    }\n                }\n            },\n            \"screen_instance_items: {\n                \"create\": [ 412, 413 ],\n                \"update\": [],\n                \"delete\": [],\n                \"attributes\": {\n                    \"412\": {\n                        \"__oldId__\": -11,\n                        \"revision\": 1001,\n                        \"screen_instance_id\": 321,\n                        \"numeric_value\": 2\n                    },\n                    \"413\": {\n                        \"__oldId__\": -12,\n                        \"revision\": 1001,\n                        ...\n                    }\n                }\n            }\n        }\n    ]\n}\n```\n\nThe client sent the request with the revision number `1000`, and the server now responds with a revision number, `1001`, which is also assigned to all of the newly created records. (The fact that it is only incremented by one tells us that the client's data set was up to date before this request was issued.)\n\nThe negative IDs have now been swapped by the sync server for the real ones. To preserve the relations between the records, the negative foreign keys have also been updated accordingly. However, the client can still map the previous temporary IDs to the permanent IDs, because the server sends the temporary IDs back as part of each record's attributes. \n\nIf the client's data set would not have been up to date at the time of the request (for example, the client's revision number was `995`), then the server would respond with multiple change sets to bring the client up to date. The server would send back the change sets necessary to go from `995` to `1000`, plus the change set with the new revision number `1001` that represents the changes the client has just sent.\n\n\n### Conflict Resolution\n\nAs stated above, in this scenario of many people working off the same data set, nobody should be able to override prior changes without being aware of them. The policy here is that whenever you have not seen the latest changes your colleagues have made to the data, you're not allowed to override their changes unknowingly. \n\nWith the system of revision numbers in place, this policy is very straightforward to implement. Whenever the client commits updated records to the sync server, it includes the revision number of each record in the change set. Since revision numbers are never modified on the client side, they represent the state of the record when the client last talked to the server. The server can now look up the current revision number of the record the client is trying to apply a change to, and block the change if it's not based on the latest revision.\n\nThe beauty of this system is that this data exchange format allows transactional changes. One change set in the JSON data can include several changes to different entity types. On the server side, a transaction is started for this change set, and if any of the change set's records result in a conflict, the transaction is rolled back and the server marks the whole change set with a `conflict` flag when sending it back to the client.\n\nA problem that arises on the client side whenever a conflict happens is the question of how to restore the correct state of the data. Since the changes could have been made while the client was offline and only were committed the next day, we'd have to keep an exact transaction log around (and persist it). This allows us to revert any change in case it creates a conflict during syncing.\n\nIn our case, we chose a different route: since the server is the ultimate authority for the 'truth' of the data, it just sends the correct data back in case a conflict occurs. On the server side, this turned out to be implemented very easily, whereas it would have required a major effort for the client. \n\nIf a client now, for example, deletes a record it is not allowed to delete, the server will respond with a change set marked with the `conflict` flag, and this change set contains the data that has been erroneously deleted, including related records to which the delete has cascaded. This way, the client can easily restore the data that has been deleted without keeping track of all transactions itself.\n\n\n## Implementation\n\nNow that we have discussed the basics of the syncing concept, let's have a closer look at the actual implementation. \n\n\n### Backend\n\nThe backend is a very lightweight application built with node.js, and it uses PostgreSQL to store structured data, as well as a Redis key-value store that caches all the change sets representing each database transaction. (In other words, each change set represents the changes that get you from revision number `x` to `x + 1`.) These change sets are used to be able to quickly respond with the last few change sets a client is missing when it makes a sync request, rather than having to query all different database tables for records with a revision number greater than `x`. \n\nThe implementation details of the backend are beyond the scope of this article. But honestly, there really isn't too much exciting stuff there. The server simply goes through the change sets it receives, starts a database transaction for each of them, and tries to apply the changes to the database. If a conflict occurs, the transaction gets rolled back and a change set with the true state of the data is constructed. If everything goes smoothly, the server confirms the change with a change set containing the new revision number of the changed records.\n\nAfter processing the changes the client has sent, it checks if the client's highest revision number is trailing behind the server's, and, if that's the case, adds the change sets to the response, which enables the client to catch up.\n\n\n### Core Data\n\nThe client application uses Core Data, so we need to hook into that to catch the changes the user is making and commit them to the sync server behind the scenes. Similarly, we need to process the incoming data from the sync server and merge it with the local data.\n\nTo achieve this, we use a main queue managed object context for everything UI related (this includes data input by the user), and an independent private queue context for importing data from the sync server. \n\nWhenever the user makes a change to the data, the main context gets saved and we listen for its save notifications. From the save notification, we extract the inserted, updated, and deleted objects and construct a change set object that then gets added to a queue of change sets that are waiting to be synced with the server. This queue is persisted (the queue itself and the objects it holds implement `NSCoding`) so that we don't lose any changes in the case that the app gets killed before it has a chance to talk to the sync server.\n\nOnce the client can establish a connection to the sync server, it takes all the change set objects from the queue, converts them into the JSON format described above, and sends them off to the server together with its most current revision number.\n\nOnce the response comes in, the client goes through all change sets it received from the server and updates the local data accordingly in the private queue context. Only if this process has completed successfully and without errors, the client stores the current revision number it received from the server in a Core Data entity reserved especially for this purpose. \n\nLast but not least, the changes made in the private queue context are now merged into the main context, so that the UI can update accordingly.\n\nOnce all that is complete, we can start over and send the next sync request if something new is in the sync queue. \n\n\n#### Merge Policy\n\nWe have to safeguard against potential conflicts between the private queue context used to import data from the sync server and the main context. The user could easily make some edits in the main context while data is being imported in the background.\n\nSince the data received from the server represents the 'truth,' the merge policy is set up so that changes in the persistent store trump in-memory changes when merging from the private to the main context. \n\nThis merge policy could, of course, lead to cases where a background change from the sync server, for example, deletes an object that is currently being edited in the user interface. We can give the UI a chance to react to this change before it happens by sending a custom notification about such events when the private queue has saved, but before the changes are merged into the main context.\n\n\n#### Initial Data Import\n\nSince we're dealing with substantial amounts of data for mobile devices (in the six figures), it would take quite a bit of time to download all the data from the server and import it on an iOS device. Therefore, we're shipping a recent snapshot of the data set with the app. These snapshots are simply generated by running the Simulator with a special flag that enables the download of all data from the server if it's not present yet. \n\nThen we take the SQLite database generated in this process, and run the following two commands on it:\n\n```objc\nsqlite> PRAGMA wal_checkpoint;\nsqlite> VACUUM;\n```\n\nThe first one makes sure that all changes from the write-ahead logging file are transferred to the main `.sqlite` file, while the second command makes sure that the file is not unnecessarily bloated.\n\nOnce the app is started the first time, the database is copied from the app bundle to its final location. For more information on this process and other ways to import data into Core Data, see [this article in objc.io #4](/issues/4-core-data/importing-large-data-sets-into-core-data/).\n\nSince the Core Data data model includes a special entity that stores the revision number, the database shipped with the app automatically includes the correct revision number of the data set used to seed the client. \n\n\n### Compression\n\nSince JSON is a pretty verbose data format, it is important to enable gzip compression for the requests to the server. Adding the `Accept-Encoding: gzip` header to the request allows the server to gzip its response. However, this only enables compression from the server to the client, but not the other way around. \n\nThe client including the `Accept-Encoding` header only signals to the server that it supports gzip compression and that the server should send the response compressed if the server supports it too. Usually the client doesn't know at the time of the request if the server supports gzip or not, therefore it cannot send the request body in a compressed form by default.\n\nIn our case though we control the server and we can make sure that it supports gzip compression. Then we can simply gzip the data that should be sent to the server ourselves and add the `Content-Encoding: gzip` header, since we know that the server will be able to handle it. See [this `NSData` category](https://github.com/nicklockwood/GZIP) for an example of gzip compression.\n\n\n### Temporary and Permanent IDs\n\nWhen creating new records, the client assigns temporary IDs to those records so that it is able to express relations between them when sending them to the server. We simply use negative numbers as temporary IDs, starting with -1 and decreasing on each insert the clients make. The current temporary ID gets persisted in the standard user defaults.\n\nBecause of the way we're handling temporary IDs, it's very important that we're only processing one sync request at a time, and also that we maintain a mapping of the client's temporary IDs to the real IDs received back from the server. \n\nBefore sending a sync request, we check if we already have received permanent IDs from the server for records that are waiting to be committed in the queue of pending changes. If that's the case, we swap out those IDs for their real counterparts and also update any foreign keys that might have used those temporary IDs. If we wouldn't do this or instead send multiple requests in parallel, it could happen that we accidentally create a record multiple times instead of updating an existing one, because we're sending it to the server multiple times with a temporary ID.\n\nSince both the private queue context (when importing changes) as well as the main context (when committing changes) have to access this mapping, access to it is wrapped in a serial queue to make it thread-safe.\n\n\n## Conclusion\n\nBuilding your own syncing solution is not an easy task and probably will take longer than you think. At least, it took a while to iron out all the edge cases of the syncing system described here. But in return you gain a lot of flexibility and control. For example, it would be very easy to use the same backend for a web interface, or to do data analysis on the backend side.\n\nIf you're dealing with less common syncing scenarios, like the use case described here -- where we needed to sync the data set between the personal devices of many people -- you might not even have a choice except to roll out your own custom solution. And while it might be painful from time to time to wrap your head around all the edge cases, it's actually a very interesting project to work on.\n\n\n\n\n\n\n\n\n\n"
  },
  {
    "path": "2014-04-01-android-intents.md",
    "content": "---\ntitle:  \"Android Intents\"\ncategory: \"11\"\ndate: \"2014-04-01 10:00:00\"\ntags: article\nauthor:\n  - name: Martin Marconcini\n    url: https://twitter.com/Gryzor\n---\n\n\n## Introduction\n\nPerhaps a very distinctive thing about Android is the ability for applications to launch other apps or easily share content. Back in the days of iOS 1.0, it quickly became obvious that applications couldn't really talk to each other (at least non-Apple applications), even after the first iOS SDK was released.\n\nBefore iOS 6, attaching a photo or a video to an email you were already composing was definitely a chore. It was not until Apple added the ability in iOS 6 that this was really possible. Android, on the other hand, was designed to support this behavior since day one.\n\nThere are other simple examples where it really becomes clear how different both platforms behave. Imagine the following scenario: you take a picture and want to retouch it with some image editing app, and later share it on Instagram.\n\n*Please note: this is just a example to illustrate a point.*\n\nThis is how you do it on iOS:\n\n1. Open Camera App, and take the picture. \n2. Go to the Home Screen, find your *EditPhoto* app, launch it, open existing photo, find it in the Camera Roll, make your edits. \n3. If *EditPhoto* supports sharing **and** Instagram is on the list, you're good to go!\n4. Otherwise, you will have to Save the image in the Photo Library.\n5. Go to the Home Screen again, find *Instagram*, launch it…\n6. Import the recently saved photo, and then share it on Instagram with your hipster friends. ;)\n\nOn Android, things are a lot easier:\n\n1. Open Camera App, and take the picture. \n2. Swipe Right to see the 'Gallery,' and click the Share button. Pick your *EditPhoto* app and make your edits.\n3. If *EditPhoto* supports sharing (I haven't seen a photo editing app that doesn't), tap it and select Instagram. If it doesn't, remove *EditPhoto* app and get a decent photo editor or use the built-in editor, which has gotten really good in KitKat. \n\nNotice that if iOS apps support sharing between them, the flow is similar. The biggest difference is that if the app is not supported, you just can't do it directly. Instagram is an easy and popular one, just like Facebook or Twitter, but there are dozens of other not-so-supported apps out there.\n\nLet's say you have a picture in Instagram and you want to share it to Path (I know, not a lot of people use Path, but still…). In Android, you would likely find Path in the *chooser dialog*. As simple as that.\n\nLet's get back on topic. *Intents*. \n\n## What is an Android Intent?\n\nThe English dictionary defines an Intent as:\n\n```\nnoun\nintention or purpose\n```\n\nAccording to the official Android [documentation](http://developer.android.com/guide/components/intents-filters.html), an `Intent` *is a messaging object you can use to request an action from another app component*. In truth, an Intent is an abstract description of an operation to be performed. \n\nThis sounds interesting, but there's more than meets the eye. Intents are used everywhere, no matter how simple your app is; even your Hello World app will use an Intent. That's because the most common case for an Intent is to start an `Activity`.[^1]\n\n## Activities and Fragments, What are You Talking About?\n\n*The closest thing to an `Activity` in iOS would be a `UIViewController`. Don't go around looking for an Android equivalent of an `ApplicationDelegate`; there is none. Perhaps the closest thing would be the `Application` class in Android, but there are a lot architecture differences between them.*\n\nAs screens in devices grew bigger, the Android team added the concept of `Fragments`.[^2]  The typical example is the News Reader app. On a phone with a small screen, you only see the list of articles. When the user selects one, the article opens in fullscreen.\n\nBefore `Fragments`, you would have had to create two activities (one for the list, and one for the fullscreen article) and switch between them. \n\nThis worked well, until tablets with big screens came. Since you can only have **one** activity visible at a time (by design), the Android team invented the concept of `Fragments`, where a hosting `Activity` can display more than one `Fragment` at the same time. \n\nNow, instead of having two different `Activities`, you can have one that will display **two** `Fragments` -- one for the list of articles and one that is capable of showing the selected article fullscreen. In phones or devices with small screens, you would simply swap the `Fragment` when the user selected an article, but on tablets, the same activity would host both at the same time. To visualize this, think of the Mail app on an iPad, where you see the inbox on the left and the mail list on the right. \n\n### Starting Activities\n\nIntents are commonly used to start activities (and to pass data between them). An `Intent` will glue the two activities by defining an operation to be performed: launch an `Activity`.\n\nSince starting an `Activity` is not a simple thing, Android has a system component called `ActivityManager` that is responsible for creating, destroying, and managing activities. I won't go into much more detail about the `ActivityManager`, but it's important to understand that it keeps track of all the open activities and delivers broadcasts across the system; for example, it notifies the rest of the Android system once the booting process is finished.\n\nIt's an important piece of the Android system and it relies on `Intents` to do much of its work.\n\nSo how does Android use an `Intent` to start an `Activity`?\n\nIf you dig through the `Activity` class hierarchy, you will find that it extends from a `Context`, which, in turn, contains a method called `startActivity()`, defined as:\n\n```java\npublic abstract void startActivity(Intent intent, Bundle options);\n```\n\nThis abstract method is implemented in `Activity`. This means you can start activities from any activity, but you need to pass an `Intent` to do so. How?\n\nLet's imagine we want to launch an `Activity` called `ImageActivity`.\n\nThe `Intent` constructor is defined as:\n\n```java\npublic Intent(Context packageContext, Class<?> cls)\n```\n\nSo we need a `Context` (remember, any `Activity` is a valid `Context`) and a `Class` type. \n\nWith that in mind:\n\n```java\nIntent i = new Intent(this, ImageActivity.class);\nstartActivity(i);\n```\n\nThis triggers a lot of code behind the scenes, but the end result is that if everything went well, your `Activity` will start its lifecycle and the current one will likely be paused and stopped. \n\nSince Intents can also be used to pass certain data between activities, we could use them to pass *Extras*. For example:\n\n```java\nIntent i = new Intent(this, ImageActivity.class);\ni.putExtra(\"A_BOOLEAN_EXTRA\", true); //boolean extra\ni.putExtra(\"AN_INTEGER_EXTRA\", 3); //integer extra\ni.putExtra(\"A_STRING_EXTRA\", \"three\"); //string extra\nstartActivity(i);\n```\n\nBehind the scenes, the *extras* are stored in an Android `Bundle`,[^3] which is pretty much a glorified serializable container. \n\nThe nice thing is that our `ImageActivity` will receive these values in the `Intent` and can easily do:\n\n```java\n int value = getIntent().getIntExtra(\"AN_INTEGER_EXTRA\", 0); //name, default value\n```\n\nThis is how you pass data between activities. If you can serialize it, you can pass it. \n\nImagine you have an object that implements `Serializable`. You could then do this:\n\n```java\nYourComplexObject obj = new YourComplexObject();\nIntent i = new Intent(this, ImageActivity.class);\ni.putSerializable(\"SOME_FANCY_NAME\", obj); //using the serializable constructor here\nstartActivity(i);\n```\n\nAnd it would work the same way on the other `Activity`:\n\n```java\nYourComplexObject obj = (YourComplexObject) getIntent().getSerializableExtra(\"SOME_FANCY_NAME\");\n```\n\nAs a side note, *always check for null when retrieving the Intent*:\n\n```java\nif (getIntent() != null ) {\n         // you have an intent, so go ahead and get the extras…\n}\n```\n\nThis is Java, and Java doesn't like null references. Get used to it. ;)\n\nWhen you start an activity with this method (`startActivity()`), your current activity is paused, stopped (in that order) and put in the task stack, so if the user presses the *back* button, it can be restored. This is usually OK, but there are certain *Flags* you can pass to the Intent to indicate the `ActivityManager` that you'd like to change this behavior. \n\nAlthough I will not go into detail because it's a rather extensive subject, you should take a look at the [Tasks and Back Stack official docs](http://developer.android.com/guide/components/tasks-and-back-stack.html) to understand what else *Intent Flags* can do for you.\n\nSo far, we've only used Intents to open other activities in our application, but what else can an `Intent` do?\n\nThere are two more things that are possible thanks to `Intents`: \n\n* Start (or send a command to) a `Service`.[^4]\n* Deliver a `Broadcast`.\n\n### Starting a Service\n    \nSince `Activities` cannot be put in the background (because they would be paused, stopped, and maybe destroyed), the alternative -- if you need to run a background process while there's no visible UI -- is to use a `Service`. Services are also a big subject, but the short version is they can perform tasks in the background, regardless of whether or not the UI is visible.\n\nThey are prone to be destroyed if memory is needed and they run on the UI thread, so any long-time running operation should spawn a thread, usually through an [AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html). If a `Service` needs to do something like media playback, it can request a *Foreground* status, which **forces** the application to show a permanent notification in the Notification Bar to indicate to the user that something is happening in the background. The app can cancel the foreground status (and therefore dismiss the notification), but by doing so, the `Service` loses its higher-priority status. \n\n`Services` are very powerful mechanisms that allow Android applications to perform the 'real multitasking' that so controversially affected battery life in the past. Back when iOS had virtually no multitasking, Android was already dancing with the stars. When correctly used, `Services` are an integral part of the platform. \n\nIn the past, the biggest problem was that there were ways to request a `Service` foreground status **without** showing a notification. This behavior was abused by developers who left tasks running in the background without the user knowing about it. In Android 4.0 (Ice Cream Sandwich), Google finally fixed the 'hidden' notification, and now if your app is doing something in the background, the user **will see** the notification alongside your app's name and icon. You can even access the application information directly from the notification bar (and kill it!). Yes, Android's battery life is nowhere near as good as with iOS, but it's no longer because of hidden `Services`. ;)\n\nHow are `Intents` and `Services` related?\n\nIn order to start a service, you need to use an `Intent`. Once a `Service` is started, you can keep sending commands to the service, until it's stopped (in which case it will restart).\n\nThe easiest way to understand it is to see some code:\n\nIn some `Activity`, you could do:\n\n```java\nIntent i = new Intent(this, YourService.class);\ni.setAction(\"SOME_COMMAND\");\nstartService(i);\n```\n\nWhat happens next will depend on whether or not this was the first time you did that. If so, the service will be started (it's constructor, and `onCreate()` methods will be executed first). If it was already running, the `onStartCommand()` method will be directly called.\n\nThe signature is: `public int onStartCommand(Intent intent, int flags, int startId);`\n\nLet's ignore the `flags` and `startId`, as they have nothing to do with the topic at hand, and concentrate on the `Intent`. \n\nWe set an `Action` earlier with `setAction(\"SOME_COMMAND\")`. This action is passed to the `Service` and we can retrieve it from the `onStartCommand()`. For example, in our `Service`, we could do:\n\n```java\n@Override\npublic int onStartCommand(Intent intent, int flags, int startId) {\n    String action = intent.getAction();\n    if (action.equals(\"SOME_COMMAND\")) {\n        // Do SOME COMMAND ;)\n    }\n    return START_NOT_STICKY; // Don't restart the Service if it's killed.\n}\n```\n\nIf you are wondering what that `START_NOT_STICKY` thing is, the [Android docs](http://developer.android.com/reference/android/app/Service.html) are an excellent source of information.\n\n**TL;DR:** if this `Service` gets killed, don't attempt to restart it. The opposite is `START_STICKY`, which means restart the `Service` should its process die. \n\nAs you can see from the snippet above, you can retrieve the `Action` from the `Intent`. This is how you usually communicate with `Services`.\n\nLet's imagine we are developing an application that can reproduce YouTube videos and stream them to a Chromecast (*the stock YouTube app already does this, but this is Android, so we want to make our own*).\n\nThe streaming would be implemented in a `Service` so the streaming doesn't stop if the user goes to another application while he or she is playing a video. You could have different actions defined, like:\n\n```\nACTION_PLAY, ACTION_PAUSE, ACTION_SKIP.\n```\n\nYou could also have a `switch` or `if` statement in the `onStartCommand()` to deal with each case. \n\nThe names can be anything you want, but you will usually want to use constants (as we will see later) and better names to avoid conflicts with other apps, usually full package names like: '`com.yourapp.somepackage.yourservice.SOME_ACTION_NAME`'. This can also be made private if you only want your own app to be able to communicate with your service, but it can be public, meaning you could let other apps use your `Service`. \n\n### Sending and Receiving Broadcasts\n\nPart of the strength of the Android platform is that any application can broadcast an `Intent` and anyone can define a `BroadcastReceiver` to receive one. In fact, Android itself makes use of this mechanism to inform apps and the system about events. For example, if the network goes down, an Android component will broadcast an `Intent`. If you were interested in this, you could create a `BroadcastReceiver` with the right **filter** to intercept that and act accordingly. \n\nThink of this as a global channel you can subscribe to, add the filters you care for, and receive notifications when those broadcasts occur. You can define them privately if you want, meaning only your app will be able to receive them.\n\nTo continue with the previous example of our YouTube streaming service, if there were a problem with video playback, the service could *broadcast* an `Intent` saying, \"Hey, there was a problem and I will now stop playback.\"\n\nYour application could register a `BroadcastReceiver` to listen to your `Service` so it can react to that.\n\nLet's see some code to illustrate. \n\nYou have an `Activity` that is displaying the currently playing music track alongside with the media buttons (play, pause, stop, etc.). You are interested in knowing what your service is doing; if there's an error, you want to know (so you can show an error message, etc.). \n\nIn your activity (or in its own .java file) you would create your broadcast receiver:\n\n```java\nprivate final class ServiceReceiver extends BroadcastReceiver {\n    public IntentFilter intentFilter;\n    public ServiceReceiver() {\n        super();\n        intentFilter = new IntentFilter();\n        intentFilter.addAction(\"ACTION_PLAY\");\n        intentFilter.addAction(\"ACTION_STOP\");\n        intentFilter.addAction(\"ACTION_ERROR\");\n    }\n    @Override\n    public void onReceive(final Context context, final Intent intent) {\n        if (intent.getAction().equals(\"ACTION_ERROR\")) {\n           // THERE HAS BEEN AN ERROR, PLAYBACK HAS STOPPED\n        } else if (intent.getAction().equals(\"ACTION_PLAY\")){\n           // Playback has started\n        }\n        // etc…\n    }\n }\n```\n\nThat's your basic receiver. Notice how we added an `IntentFilter` with the `Actions` that we're interested in. We called them `ACTION_PLAY`, `ACTION_STOP`, and `ACTION_ERROR`.\n\nSince we use Java and Android has some conventions, we'd call this:\n\n`private ServiceReceiver mServiceReceiver;` as a field *member* of our `Activity`. In our `onCreate()` method we instantiate it with: `mServiceReceiver = new ServiceReceiver();`\n\nBut creating this object is not enough. We also need to register it somewhere. Initially, you may think that a good place to do it would be the `onStart()` method of our `Activity`. When the `onStart()` method is executed, that means our `Activity` is visible to the user.\n\nThe signature for the method is (in `Context`):\n\n```java\npublic abstract Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter);\n```\n\n \n*`Activities` and `Services` are also `Contexts`, so both implement this method. This means that either can register one or more `BroadcastReceivers`.*\n\nThe method needs a `BroadcastReceiver` and an `IntentFilter`. We've created both, so we pass them:\n\n```java\n@Override\npublic void onStart() {\n    onStart();\n      registerReceiver(mServiceReceiver, mServiceReceiver.intentFilter);\n}\n```\n\nIn order to be good Java/Android citizens, we want to unregister if our `Activity` is stopping:\n    \n```java\n@Override\npublic void onStop() {\n    super.onStop();\n    unregisterReceiver(mServiceReceiver);\n}\n```\n\nThis approach is not incorrect, but you have to keep in mind that if the user navigates outside of your application, you will never receive the broadcast. This is because your `Activity` will be stopped, and because you are unregistering during `onStop()`. When designing `BroadcastReceivers`, you have to keep in mind whether or not this makes sense. There are other ways to implement them (outside an `Activity`) to act as independent objects.\n\nWhen the `Service` detects an error, it can dispatch a broadcast that our `BroadcastReceiver` will receive in its `onReceive()` method.\n\nBroadcast receivers are very powerful mechanisms and are core mechanisms in Android.\n\nAstute readers may be wondering how *global* these broadcasts are and how to make them private or restricted to their own apps. \n\nThere are two types of Intents: *explicit* and *implicit*.\n\nThe former will specify the component to start by the fully qualified name, something that you will always know for your own application. The latter declares a general action to perform, which allows a component from another app to handle it. And here is where things start to get interesting. \n\nLet's focus on *implicit Intents*, since we have already seen *explicit Intents* in action with our example above. \n\nThe best way to see the power of *implicit intents* is by using a simple example. There are two ways to use a filter. The first approach is more iOS friendly, because iOS can define a custom URI scheme, for example: yourapp://some.example.com\n\nIf you have to support the same URI from both iOS and Android, then this will be your only choice. On the other hand, if you are able to use a regular URL (`http://your.domain.com/yourparams`) then you should try to do it this way on Android. This raises the big argument of whether using a custom URI is good or bad, and I'm not going to dive into that at this point, suffice to say that (and I quote): \n\n> This goes against the web standards for URI schemes, which attempts to rigidly control those names for good reason -- to avoid name conflicts between different entities. Once you put a link to your scheme on a web site, you have put that little name into the entire Internet's namespace, and should be following those standards.\n\nSource: [StackOverflow](http://stackoverflow.com/a/2449500/2684)\n\nArguments aside, let's take a look at two examples, one for YouTube using a regular URL, and then define our own custom URI scheme for our own app.\n\nIt's simpler than it looks because Android has a configuration file called `AndroidManifest.xml`, where it stores metadata about your `Activities`, `Services`, `BroadcastReceivers`, versions, Intent filters, and more. Every application has this file -- you can read more about it [here](http://developer.android.com/guide/topics/manifest/manifest-intro.html).\n\nThe idea behind an Intent filter is that the system will check for installed apps to see if there's one (or more) that can handle a particular URI.\n\nIf your app matches and it's the only one, it will be automatically open. Otherwise, you will see a dialog like this:\n\n![image](/images/issue-11/android-dialog-choser.jpg)\n\nSo how did the official YouTube app end up in that list?\n\nI tapped on a YouTube link in the Facebook App. How did Android know that it was YouTube? *What kind of sorcery is this?*\n\nIf we had access to YouTube's `AndroidManifest.xml`, we would likely see something like this: \n\n```xml\n1 <activity android:name=\".YouTubeActivity\">\n2     <intent-filter>\n3        <action android:name=\"android.intent.action.VIEW\" />\n4       <category android:name=\"android.intent.category.DEFAULT\" />\n5         <category android:name=\"android.intent.category.BROWSABLE\" />\n6       <data\n7        android:scheme=\"http\"\n8        android:host=\"www.youtube.com\"\n9        android:pathPrefix=\"/\" />\n10   </intent-filter>\n11 </activity>\n```\n\nLet's examine this simple XML line by line.\n\nLine 1 declares the activity (you must declare each `Activity` in Android, regardless of the Intent filters).\n\nLine 3 declares the action. In this case, `VIEW` is the most common action, indicating that data will be displayed to the user. Some actions can only be sent by the system because they are protected.\n\nLines 4-5 declare the categories. Implicit Intents require at least one action and one category. Categories provide additional detail about the action the Intent performs. When resolving an Intent, only activities that provide all of the requested categories will be used. `android.intent.category.DEFAULT` is applied to every `Activity` by Android when you use `startActivity()`, so if you want your activity to receive implicit Intents, it must include it.\n\n`android.intent.category.BROWSABLE` is a different beast:\n\n> Activities that can be safely invoked from a browser must support this category. For example, if the user is viewing a web page or an e-mail and clicks on a link in the text, the Intent generated execute that link will require the BROWSABLE category, so that only activities supporting this category will be considered as possible actions. By supporting this category, you are promising that there is nothing damaging (without user intervention) that can happen by invoking any matching Intent.\n\nSource: [Android Documentation](http://developer.android.com/reference/android/content/Intent.html#CATEGORY_BROWSEABLE)\n\nThis is an interesting point, and this gives Android a very powerful mechanism for apps to respond to any link. You could create your own web browser and it will respond to any URL; the user could set it as default if he or she wishes.\n\nLines 6-9 declare the data to operate on. This is part of the *type*. In this simple example, we're filtering by scheme and host, so any http://www.youtube.com/ link will work, even if tapped on a WebBrowser.\n\nBy adding these lines to YouTube's `AndroidManifest.xml`, when it's time to perform an *Intent resolution*, Android performs a matching of an Intent against all of the `<intent-filter>` descriptions in the installed application packages (or `BroadcastReceivers` registered via code, like our example above).\n\nThe Android `PackageManager`[^6] will be queried using the `Intent` information (the action, type, and category), for a component that can handle it. If there's one, it will be automatically invoked, otherwise the above dialog will be presented to the user, so he or she can chose (and maybe set as default) a particular app or package to handle the type of Intent.\n\nThis works well for many apps, but sometimes you need to use the same iOS link (where your only choice is to use a custom URI). In Android, you could support both, since you can add more filters to the same activity. To continue with the YouTubeActivity, let's add now an imaginary YouTube URI scheme:\n\n```xml\n<activity android:name=\".YouTubeActivity\">\n  <intent-filter>\n    <action android:name=\"android.intent.action.VIEW\" />\n    <category android:name=\"android.intent.category.DEFAULT\" />\n      <category android:name=\"android.intent.category.BROWSABLE\" />\n    <data\n     android:scheme=\"http\"\n     android:host=\"www.youtube.com\"\n     android:pathPrefix=\"/\" />\n    <data android:scheme=\"youtube\" android:host=\"path\" />\n  </intent-filter>\n</activity>\n```\n\nThe filter is almost the same, except we added a new line 10, specifying our own scheme. \n\nThe app can now open links like: `youtube://path.to.video.` and normal HTTP links. You can add as many filters and types to an `Activity` as you wish.\n\n#### How Bad is it to Use my Custom URI Scheme?\n\nThe problem is that it doesn't follow the standard rules for URIs defined by the W3C, at least according to purists. The truth is that this is not entirely true or a real problem. You are OK to use custom URI schemes, as long as you restrict them to your own internal packages. The biggest problem with a custom (public) URI scheme is name conflict. If I define a `myapp://`, nothing stops the next app from doing the same, and we have a problem. Domains, on the other hand, are never going to clash, unless I'm trying to create my own YouTube player, in which case, it's fine for Android to give me the choice to use my own YouTube player or the official Android app. \n\nMeanwhile, a custom URL like `yourapp://some.data` may not be understood by a web browser and it can lead to 404 errors. You're *bending* the rules and standard conventions. \n\n\n### Sharing Data\n\n`Intents` are used when you have something you want to *share* with other apps, such as a post in a social network, sending a picture to an image editor, or sending an email, an SMS, or something via any other instant messaging service. So far, we have seen how to create intent filters and register our app to be notified when we are capable of handling certain types of data. In this final section, we'll see how to tell Android that we have something to *share*. Remember what an `Intent` is: *an abstract description of an operation to be performed*. \n\n#### Posting to Social Networks\n\nIn the following example, we're going to share a text and let the user make the final decision:\n\n```java\n1  Intent shareIntent = new Intent(Intent.ACTION_SEND);\n2  shareIntent.setType(\"text/plain\");\n3  shareIntent.putExtra(Intent.EXTRA_TEXT, \"Super Awesome Text!\");\n4  startActivity(Intent.createChooser(shareIntent, \"Share this text using…\"));\n```\n\nLine 1 creates an `Intent` and passes an action using the constructor: `public Intent(String action);`\n\n`ACTION_SEND` is used when you want to *deliver some data to someone else*. In this case, the data is our \"Super Awesome Text!\" But we don't know who that 'someone else' is yet. It will be up to the user to decide that. \n\nLine 2 sets an explicit MIME data type of `text/plain`.\n\nLine 3 adds the data (the text) to this Intent using an extra.\n\nLine 4 is where the magic happens. `Intent.createChooser` is a convenience function that wraps your original Intent in a new one with an action, `ACTION_CHOOSER`.\n\nThere's no rocket science going on here. The action is designed so an activity chooser is displayed, allowing the user to pick what he or she wants before proceeding. Sometimes you want to be explicit (so if the user is sending an email, you may want to use the default email client directly), but in this case, we want the user to select any app to handle this text. \n\nThis is what I see when I use it (the list is longer -- it's a scrollable list):\n\n![image](/images/issue-11/android-chooser.gif)\n\nI have decided to send it to Google Translate. Here's the result:\n\n![image](/images/issue-11/android-translate.jpg)\n\nThe results attempting to do it in Google Translate speak in Italian. \n\n## An Extra Example\n\nBefore wrapping up, let's see another example. This time, we'll see how to share and receive an image. We want the app to appear in the chooser when the user shares an image.\n\nWe need to do something like this in our `AndroidManifest`:\n\n```xml\n1 <activity android:name=\"ImageActivity\">\n2   <intent-filter>\n3     <action android:name=\"android.intent.action.SEND\"/>\n4     <category android:name=\"android.intent.category.DEFAULT\"/>\n5     <data android:mimeType=\"image/*\"/>\n6   </intent-filter>\n7 </activity>\n```\n\nRemember, we need at least one action and one category. \n\nLine 3 sets the action as `SEND`, so we will match `SEND` actions.\n\nLine 4 declares the `DEFAULT` category. This category gets added by default when you use `startActivity()`. \n\nLine 5 is they key that sets the MIME type as *any type of image*.\n\nNow, in our `ImageActivity`, we handle the Intent like this:\n\n```java\n1    @Override\n2    protected void onCreate(Bundle savedInstanceState) {\n3        super.onCreate(savedInstanceState);\n4        setContentView(R.layout.main);\n5        \n6        // Deal with the intent (if any)\n7        Intent intent = getIntent();\n8        if ( intent != null ) {\n9            if (intent.getType().indexOf(\"image/\") != -1) {\n10                 Uri data = intent.getData();\n11                 // handle the image…\n12            } \n13        }\n14    }\n```\n\nThe relevant code is in line 9, where we're actually checking if the Intent contains image data.\n\nNow, let's do the opposite. This is how we *share* an image:\n\n```\n1    Uri imageUri = Uri.parse(\"/path/to/image.png\");\n2    Intent intent = new Intent(Intent.ACTION_SEND);\n3    intent.setType(\"image/png\");    \n4    intent.putExtra(Intent.EXTRA_STREAM, imageUri);\n5    startActivity(Intent.createChooser(intent , \"Share\"));\n```\n\nThe interesting code is in line 3, where we define the MIME type (so only `IntentFilters` capable of dealing with this type will be shown), and in line 4, where we actually place the data that will be shared.\n\nFinally, line 5 creates the *chooser* dialog we've seen before, but only containing apps that can handle `image/png`.\n\n## Summary\n\nWe have scratched the surface regarding what Intents can do and how information can be shared in Android, but there's a lot more to see. It's a very powerful mechanism and one aspect that makes Android users frown when they use iOS devices. They (myself included) find the process of always going home and/or using the Task Switcher in iOS very inefficient. \n\nThis doesn't really mean Android is technically better or that the Android method is superior when it comes to sharing data between applications. In the end, everything is a matter of preference, just like the *back* button some iOS users loathe when they grab an Android device. On the other hand, Android users love that button. It's standard and efficient and it's always in the same place, next to the *home* button. \n\nWhen I lived in Spain, I remember they had a very good saying: \"Colors were created so we can all have different tastes\" (or something like that). ;) \n\n## Further Reading\n\n* [Intents and Filters](http://developer.android.com/guide/components/intents-filters.html)\n* [Intents](http://developer.android.com/reference/android/content/Intent.html) \n* [Common Intents](http://developer.android.com/guide/components/intents-common.html)\n* [Integrating Application with Intents](http://android-developers.blogspot.com.es/2009/11/integrating-application-with-intents.html)\n* [Sharing Simple Data](http://developer.android.com/training/sharing/index.html)\n\n\n\n\n\n[^1]: Activities are the components that provide a user interface for a single screen in your application.\n[^2]: A fragment represents a behavior or a portion of user interface in an activity. \n[^3]: A mapping from string values to various Parcelable types.\n[^4]: A service is an application component representing an application's desire to either perform a longer-running operation while not interacting with the user, or to supply functionality for other applications to use.\n[^6]: [PackageManager](http://developer.android.com/reference/android/content/pm/PackageManager.html): class for retrieving various kinds of information related to the application packages that are currently installed on the device.\n"
  },
  {
    "path": "2014-04-01-android-notifications.md",
    "content": "---\ntitle:  \"Android’s Notification Center\"\ncategory: \"11\"\ndate: \"2014-04-01 08:00:00\"\ntags: article\nauthor:\n  - name: Kevin Grant\n    url: https://twitter.com/kevingrant5\n---\n\n\nNotifications from our devices are almost second nature for us these days. Hardly an hour goes by that we aren’t pulling out our phones, checking our status bars, and then putting our phones back in our pockets. For Android users, this is especially true, as it is one of the primary ways of interacting with their devices. Unlock your screen, read a few emails, approve some friend requests, and like your buddy’s check-in, across three different applications, all directly from the notification bar.\n\nBut this is an entirely different world for some. Particularly, iOS has a long history of not getting notifications quite right, and iOS developers didn’t have the same kind of fine-grained control over their apps' notifications. It wasn’t possible to receive silent notifications, to possibly wait and post them later. Things have changed in iOS 7, but the bad taste still remains in the mouths of some, and notifications are still lacking some key features that Android developers have been enjoying for years.\n\nIt’s been long touted that Android 'got' notifications right from the beginning. All of your notifications were centralized in one logical place on your phone, right in the system bar, next to your battery and signal strength settings. But to understand what Android’s notification system is capable of, it’s important to understand its roots, and how the system evolved.\n\nSince Android let developers fully control their own background processes, they were able to create and show notifications at any time, for any reason. There was never a notion of delivering a notification to the application or to the status bar. It was delivered wherever you wanted it.\n\nYou could access this from anywhere, at any time. Since the majority of applications didn’t force a fullscreen design, users could pull down the notification 'drawer' whenever they wanted. For many people, Android was their first smartphone, and this type of notification system deviated from the notification paradigm that existed before, one where you had to arduously open every single application that had information for you, whether it be missed calls, SMSes, or emails.\n\nNotifications in Android 1.6 (Donut): \n\n![Notifications in Android 1.6](/images/issue-11/android-g1-50.jpg) \n\nNotifications in Android 4.4 (KitKat):\n\n![Notifications in Android 4.4](/images/issue-11/modern_notes.png)\n\n\n## A Brief History\n\nNotifications on Android today have come a long way since their debut in 2008.\n\n### Android 1.5 - 2.3\n\nThis is where Android began for most of us (including me). We had a few options available to us, which consisted mainly of an icon, a title, a description, and the time. If you wanted to implement your own custom control, for example, for a music player, you could. The system maintained the desired width and height constraints, but you could put whatever views in there you wanted. Using these custom layouts is how the first versions of many custom music players implemented their custom controls in the notification:\n\n```java\nprivate void showNotification() {\n  // Create the base notification (the R.drawable is a reference fo a png file)\n  Notification notification = new Notification(R.drawable.stat_notify_missed_call,\n      \"Ticket text\", System.currentTimeMillis());\n\n  // The action you want to perform on click\n  Intent intent = new Intent(this, Main.class);\n\n  // Holds the intent in waiting until it’s ready to be used\n  PendingIntent pi = PendingIntent.getActivity(this, 1, intent, 0);\n\n  // Set the latest event info\n  notification.setLatestEventInfo(this, \"Content title\", \"Content subtext\", pi);\n\n  // Get an instance of the notification manager\n  NotificationManager noteManager = (NotificationManager)\n      getSystemService(Context.NOTIFICATION_SERVICE);\n\n  // Post to the system bar\n  noteManager.notify(1, notification);\n}\n```\n\nCode: Function on how notifications were created in 1.5-2.3.\n\n\nWhat the code looks like run on Android 1.6:\n\n![Notifications in Donut 1.6](/images/issue-11/gb/donut.png) \n\nWhat the code looks like run on Android 2.3:\n\n![Notifications in Gingerbread 2.3](/images/issue-11/gb/gingerbread_resized.png)\n\n\n### Android 3.0 - 3.2\n\nNotifications in Android 3.0 actually took a slight turn for the worse. Android’s tablet version, in response to Apple’s iPad, was a fresh take on how to run Android on a large screen. Instead of a single unified drawer, Android tried to make use of its extra space and provide a separate notification experience, one where you still had a drawer, but you would also receive 'growl-like' notifications. Fortunately for developers, this also came with a brand new API, the `NotificationBuilder`, which allowed us to utilize a [builder pattern](http://en.wikipedia.org/wiki/Builder_pattern) to create our notifications. Even though it’s slightly more involved, the builder abstracts away the complexity of creating notification objects that differ ever so slightly with every new version of the operating system:\n\n```java\n// The action you want to perform on click\nIntent intent = new Intent(this, Main.class);\n\n// Holds the intent in waiting until it’s ready to be used\nPendingIntent pi = PendingIntent.getActivity(this, 1, intent, 0);\n\nNotification noti = new Notification.Builder(getContext())\n  .setContentTitle(\"Honeycomb\")\n  .setContentText(\"Notifications in Honeycomb\")\n  .setTicker(\"Ticker text\")\n  .setSmallIcon(R.drawable.stat_notify_missed_call)\n  .setContentIntent(pi)\n  .build();\n\n// Get an instance of the notification manager\nNotificationManager noteManager = (NotificationManager)\n    getSystemService(Context.NOTIFICATION_SERVICE);\n\n// Post to the system bar\nnoteManager.notify(1, notification);\n```\n\nWhat a notification looks like when initially received in Honeycomb:\n\n![Honeycomb notifications ticket text](/images/issue-11/hc/initially-received-hc.png)\n\n\nWhat a notification looks like when you click on it in the navigation bar:\n\n![Honeycomb notifications tapping notification](/images/issue-11/hc/selecting_notification_hc.png)\n\nWhat a notification looks like when you select the clock:\n\n![Honeycomb notifications tapping clock](/images/issue-11/hc/selecting_clock_hc.png)\n\n\nThese redundant notifications led to user confusion about what notifications were representing, and presented many design challenges for the developer, who was trying to get to the right information to the user at the right time.\n\n### Finally, 4.0-4.4\n\nAs with the rest of the operating system, Android began to really flesh out and unify its notification experience in 4.0 and beyond. While 4.0 in particular didn’t bring anything exciting to the table, 4.1 brought us roll-up notifications (a way to visualize more than one notification in a single cell), expandable notifications (for example, reading the first paragraph of an email), picture notifications, and actionable notifications. Needless to say, this created an entirely new way of enriching a user’s out-of-app experience. If someone ‘friended’ me on Facebook, I could simply press an 'accept friend request' button right from the notification bar, without ever opening the application. If I received an email I didn’t actually have to read, I could archive it immediately without ever opening my email.\n\nHere are a few examples of the 4.0+ API’s that are utilized in the [Tumblr application for Android](https://play.google.com/store/apps/details?id=com.tumblr). Using these notifications is incredibly simple; it only requires adding an extra notification style onto the `NotificationBuilder`.\n\n#### Big Text Notifications\n\nIf the text is short enough, why do I have to open the app to read it? Big text solves that problem by giving you some more room to read. No wasted application opens for no reason:\n\n```java\nNotification noti = new Notification.Builder()\n  ... // The same notification properties as the others\n  .setStyle(new Notification.BigTextStyle().bigText(\"theblogofinfinite replied...\"))\n  .build();\n```\n\nBig text notification contracted:\n\n![Notifications in Cupcake 1.5](/images/issue-11/ics/shrunk_text.png)\n\nBig text notification expanded:\n\n![Notifications in Cupcake 1.5](/images/issue-11/ics/bigtext.png)\n\n\n#### Big Picture Notifications\n\nThese wonderful notifications offer a content-first experience without ever requiring the user to open an application. This provides an immense amount of context, and is a beautiful way to interact with your notifications:\n\n```java\nNotification noti = new Notification.Builder()\n  ... // The same notification properties as the others\n  .setStyle(new Notification.BigPictureStyle().bigPicture(mBitmap))\n  .build();\n```\n\n![Big picture notification](/images/issue-11/ics/big_pic.png)\n\n\n#### Roll-Up Notifications\n\nRoll-up notification is bringing multiple notifications into one. The rollup cheats a little bit because it doesn’t actually stack existing notifications. You’re still responsible for building it yourself, so really it’s just more of a nice way of presenting it:\n\n```java\nNotification noti = new Notification.Builder()\n  ... // The same notification properties as the others\n  .setStyle(new Notification.InboxStyle()\n     .addLine(\"Soandso likes your post\")\n     .addLine(\"Soandso reblogged your post\")\n     .setContentTitle(\"3 new notes\")\n     .setSummaryText(\"+3 more\"))\n  .build();\n```\n\n![Rollup notification](/images/issue-11/ics/rollup.png)\n\n\n#### Action Notifications\n\nAdding actions to a notification is just as easy as you’d imagine. The builder pattern ensures that it will use whatever default styles are suggested by the system, ensuring that the user always feels at home in his or her notification drawer:\n\n```java\nNotification noti = new Notification.Builder()\n  ... // The same notification properties as the others\n  .addAction(R.drawable.ic_person, \"Visit blog\", mPendingBlogIntent)\n  .addAction(R.drawable.ic_follow, \"Follow\", mPendingFollowIntent)\n  .build();\n```\n\n![Action notification](/images/issue-11/ics/actions.png)\n\nThese sorts of interactions lent to an application design that put the user in charge, and made performing simple actions incredibly easier, and faster. At a time when Android had suffered from sluggish performance, these sorts of quick actions were greatly welcomed, since you didn’t actually have to open an application to still be able to use it.\n\n### Android Wear\n\nIt’s no secret to anyone in the tech world right now that Android wear is a fascinating introduction into the wearables space. Whether or not it will succeed as a consumer product is certainly up for debate. What isn’t up for debate is the barrier to entry for developers who want to support Android Wear. Living up to its legacy, Android Wear appears to have gotten notifications correct, in regards to syncing with your device. As a matter of fact, if you phone is connected to an Android Wear device, it will push any notifications created with a builder directly to the device, with no code modification necessary. The ongoing simplicity of the `NotificationBuilder` pattern will ensure that whatever devices that come out and support Android or Android Wear will almost immediately have an breadth of app developers who are already comfortable using the APIs to send and receive data.\n\n![Action notification](/images/issue-11/watch/picture.png)\n![Action notification](/images/issue-11/watch/hunkosis.png)\n\nNotificationBuilder provides out-of-the-box support for Android Wear, no code required!\n\n## Custom Notifications\n\nEven though Android’s `NotificationBuilder` provides an enormous level of customizability, sometimes that just isn’t enough, and that's where custom notification layouts come in. It’s hard to imagine what you would do if you had complete control over a notification. How would you change it, what would it really do beyond a normal notification? Thinking creatively within these constraints can be difficult, but many Android developers have stepped up to the plate.\n\nCustom music player notification:\n\n![Custom music player notification](/images/issue-11/custom/music_player.png) \n\nCustom weather notification:\n\n![Custom weather notification](/images/issue-11/custom/weather.jpg) \n\nCustom battery notification:\n\n![Custom battery notification](/images/issue-11/custom/battery_widget.png)\n\nCustom notifications are limited to a subset of view components that are supported by [Remote Views](http://developer.android.com/reference/android/widget/RemoteViews.html), and those view components themselves cannot be extended or overridden too heavily. Regardless of this slight limitation, you can see that you can still create sophisticated notifications using these basic components.\n\nCreating these custom views takes a bit more work however. Custom notification views are created using Android's XML layout system, and you are responsible for making sure your notifications look decent on all the different versions of Android. It’s a pain, but when you see some of these beautiful notifications, you can instantly understand their value:\n\n```xml\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:orientation=\"horizontal\">\n\n    <ImageView\n        android:id=\"@+id/avatar\"\n        android:layout_width=\"32dp\"\n        android:layout_height=\"32dp\"\n        android:layout_gravity=\"center_vertical\" />\n\n    <TextView\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_gravity=\"center_vertical\"\n        android:text=\"You received a notification\" />\n</LinearLayout>\n```\nAn extremely basic custom-notification layout that shows an image, with some text beside it.\n\n## Notification Behavior\n\n### Push Notifications\n\nNow that we’ve had our extensive history lesson, let's get into some interesting behavior about how notifications work. As it might be apparent from the information we’ve already covered, developers have *complete* control over this notification system. That means notifications can be shown or dismissed at any time, for any reason. There is no need for this notification to be received from Google through a push notification service. In fact, even when receiving push notifications, they aren’t just shown in the status bar by default -- you have to catch that push notification and decide what to do with it.\n\nFor example, a common notification interaction looks like this:\n\n1. Receive push notification from remote server\n2. Inspect payload, fire off a background service to fetch data instructed by payload\n3. Receive / parse response\n4. Build and show notification\n\nWhat is interesting, however, is that for steps two and three, there is no time limit that is imposed on this background service. If the push notification told you to download a 1GB file, then that's OK! For most use cases, there is no requirement by the system to show you relatively short running services in the background. Long-running background services (think music player), however, do require an icon to be shown in the status bar. This was great forethought from the Android engineers to make sure that the user would know about anything that was doing background work for too long.\n\nBut even these four steps are more than an average developer would like to handle. Wouldn’t it be great if you could just send the whole payload? [GCM (Google Cloud Messaging)](http://developer.android.com/google/gcm/index.html) allows payloads of up to 4KB. On average, that's between 1,024 and 4,096 UTF-8 characters (depending on the characters). Unless you're pushing down images, you could probably fit whatever you wanted into a single push. Sounds great!\n\n### Notification Callbacks\n\nSo what kind of control do we have as developers over how the user is interacting with the notifications? Sure, we’ve seen that there is a possibility to add custom controls and buttons onto them, and we’ve already seen how to interact with a general click, but is there anything else? Actually, there is! There is a 'delete' action, `setDeleteIntent`, that gets fired when the user dismisses the notification from the drawer. Hooking into delete is a great way to make sure we don’t ever show the user this information again:\n\n```java\n// In Android, we can create arbitrary names of actions, and let\n// individual components decide if they want to receive these actions.\nIntent clearIntent = new Intent(\"clear_all_notifications\");\nPendingIntent clearNotesFromDb = PendingIntent.getBroadcast(aContext, 1, clearIntent, 0)\n\nNotification noti = new Notification.Builder(getContext())\n  ...\n  .setDeleteIntent(clearNotesFromDb)\n  .build();\n```\n\n### Recreating the Navigation Hierarchy\n\nLet’s talk a little more about the default notification click. Now, you could certainly perform some sort of default behavior when clicking on a notification. You could just open the application, and be done with it. The user can figure out where to go from there. But it would be so much nicer if we opened up directly to the relevant screen. If we receive an email notification, let's jump directly to that email. If one of my friends checks in on Foursquare, let's open right to that restaurant and see where he or she is. This is a great feature because it allows your notifications to act as deep links into the content that they are referring to. But often, when deep linking into these parts of your application, you run into a problem where your navigation hierarchy is all out of order. You have no way of actually navigating 'back.' Android helps you solve this problem by allowing you to create a stack of screens before you start anything. This is accomplished via the help of the TaskStackBuilder class. Using it is a little magical and requires some prior knowledge to how applications are structured, but feel free to take a look at Google’s developer site for a\n[brief implementation](http://developer.android.com/guide/topics/ui/notifiers/notifications.html#SimpleNotification).\n\nFor our Gmail example, instead of just telling our application that we want to open an email, we tell it, \"Open the email app, and then open this specific email.\" The user will never see all of the screens being created; instead, he or she will only see the end result. This is fantastic, because now, when selecting back, the user doesn’t leave the application. He or she simply ends up returning to the apps home screen.\n\n## What’s Missing\n\nI’ve detailed quite a bit about what notifications in Android have to offer, and I’ve even demonstrated how powerful they can be. But no system is perfect, and Android’s notification system is not without its shortcomings.\n\n### Standards\n\nOne of the unfortunate problems Android users face is that there is no centralized control for how notifications work. This means that if there is an application prompting you with a notification, short of uninstalling the application, there isn’t much you can do. Starting in Android 4.1, users received a buried binary setting to 'Turn off notifications' for a specific app. This prevents this application from placing *any* notification in the status bar. While it may seem helpful, the user case is actually fairly limited, since rarely do you want to disable all of an application's notifications completely, but rather a single element of it, for instance the LED or the annoying sound.\n\n![Turn off notifications](/images/issue-11/disable_notifications.png)\nStarting in Android 4.1, users received a binary setting to 'Turn off notifications,' but there is still no centralized way to disable LEDs or sounds unless provided explicitly by the developer.\n\n### What to Display\n\nYou might think that we’re taking for granted all of the control that we have over notifications already, but certainly there is always room for more. While the current system offers a lot of functionality and customizability, I’d like to see it taken a step further. The `NotificationBuilder`, as we saw earlier, forces your notification into a certain structure that encourages all notifications to look and feel the same. And if you use a custom layout and build the notification yourself, there are only a handful of supported components that you are allowed to use. If you have a complex component that needs to be custom drawn, it’s probably safe to assume that you can’t do it. And if you wanted to do something next level, like incorporating frame animations, or even a video, forget about it.\n\n## Wrapping Up\n\nAndroid has quite a bit to offer its users and developers in terms of notifications. Right from the get-go, Android made a conscious effort to support notifications in a big and bold way, something that remains unrivaled, even today. Looking at how Android has approached Android Wear, it’s easy to see that there is a huge emphasis on easily accessible APIs for working with the notification manager. While there are some shortcomings around fine-grained notification management and lack of complete UI control, it’s seemingly safe to say that if you are looking for a notifications-first ecosystem, Android might be worth a shot.\n\n#### References\n\n- [A Visual History of Android](http://www.theverge.com/2011/12/7/2585779/android-history)\n- [Android Notifications Docs](http://developer.android.com/guide/topics/ui/notifiers/notifications.html)\n- [Creating Notifications for Android Wear](http://developer.android.com/wear/notifications/creating.html)\n"
  },
  {
    "path": "2014-04-01-android_101_for_ios_developers.md",
    "content": "---\ntitle:  \"Android 101 for iOS Developers\"\ncategory: \"11\"\ndate: \"2014-04-01 11:00:00\"\ntags: article\nauthor:\n  - name: Stephen Barnes\n    url: https://twitter.com/smbarne\n---\n\n\nAs the mobile software industry evolves, it is becoming increasingly impractical to target only iOS for a mobile product. Android market share is approaching 80 percent for smartphones,[^1] and the number of potential users that it can bring to a product can hardly be ignored.\n\nIn this article, I will introduce the core concepts of Android development within the context of iOS development. Android and iOS work on similar problem sets, but they approach many of these problems in different ways. Throughout the article, I will be using a companion project (available on [GitHub](https://github.com/objcio/issue-11-android-101)) to illustrate how to accomplish the same tasks when developing for both platforms.\n\nIn addition to a working knowledge of iOS development, I assume that you have a working knowledge of Java and are able to install and use the [Android Development Tools](http://developer.android.com/tools/index.html). Furthermore, if you are new to Android development, reading through the tutorial by Google about [building your first app](http://developer.android.com/training/basics/firstapp/index.html) could be very helpful.\n\n### A Brief Word on UI Design\n\nThis article will not delve deeply into the user experience and design pattern differences between iOS and Android. However, it would be beneficial to understand some of the key UI paradigms in use on Android today: the action bar, the overflow menu, the back button, the share action, and more. If you are seriously considering Android development, I highly recommending looking into the [Nexus 5](https://play.google.com/store/devices/details?id=nexus_5_white_16gb) from the Google Play Store. Make it your full-time device for a week and force yourself to try the operating system to its fullest extent. A developer who doesn't know the key use patterns of his or her operating system is a liability to the product.\n\n## Language Application Structure \n\n### Java\n\nThere are many differences between Objective-C and Java, and while it may be tempting to bring some of Objective-C's styling into Java, it can lead to a codebase that heavily clashes with the primary framework that drives it. In brief, here are a few gotchas to watch for:\n\n- Leave class prefixes at home on Objective-C. Java has actual namespacing and package management, so there is no need for class prefixes here.\n- Instance variables are prefixed with `m`, not `_`.\n- Take advantage of JavaDoc to write method and class descriptions for as much of your code as possible. It will make your life and the lives of others better.\n- Null check! Objective-C gracefully handles message sending to nil objects, but Java does not.\n- Say goodbye to properties. If you want setters and getters, you have to remember to actually create a getVariableName() method and call it explicitly. Referencing `this.object` will **not** call your custom getter. You must use `this.getObject`.\n- Similarly, prefix method names with `get` and `set` to indicate getters and setters. Java methods are typically written as actions or queries, such as `getCell()`, instead of `cellForRowAtIndexPath:`.\n   \n### Project Structure\n\nAndroid applications are primarily broken into two sections, the first of which is the Java source code. The source code is structured via the Java package hierarchy, and it can be structured as you please. However, a common practice is to use top-level categories for activities, fragments, views, adapters, and data (models and managers).\n\nThe second major section is the `res` folder, short for 'resource' folder. The `res` folder is a collection of images, XML layout files, and XML value files that make up the bulk of the non-code assets. On iOS, images are either `@2x` or not, but on Android there are a number of screen density folders to consider.[^2] Android uses folders to arrange images, strings, and other values for screen density. The `res` folder also contains XML layout files that can be thought of as `xib` files. Lastly, there are other XML files that store resources for string, integer, and style resources.\n\nOne last correlation in project structure is the `AndroidManifest.xml` file. This file is the equivalent of the `Project-Info.plist` file on iOS, and it stores information for activities, application names, and set Intents[^3] (system-level events) that the application can handle.\n\nFor more information about Intents, keep on reading, or head over to the [Intents](/issues/11-android/android-intents/) article.\n\n## Activities\n\nActivities are the basic visual unit of an Android app, just as `UIViewControllers` are the basic visual component on iOS. Instead of a `UINavigationController`, the Android OS keeps an activity stack that it manages. When an app is launched, the OS pushes the app's main activity onto the stack. Note that you can launch other apps' activities and have them placed onto the stack. By default, the back button on Android pops from the OS activity stack, so when a user presses back, he or she can go through multiple apps that have been launched.\n\n\nActivities can also initialize other activities with [Intents](http://developer.android.com/reference/android/content/Intent.html) that contain extra data.  Starting Activities with Intents is somewhat similar to creating a new `UIViewController` with a custom `init` method. Because the most common way to launch new activities is to create an Intent with data, a great way to expose custom initializers on Android is to create static Intent getter methods. Activities can also return results when finished (goodbye modal delegates!) by placing extra data on an Intent when the activity is finished.\n\nOne large difference between Android apps and iOS apps is that any activity can be an entrance point into your application if it registers correctly in the `AndroidManifest` file. Setting an Intent filter in the AndroidManifest.xml file for a `media intent` on an activity effectively states to the OS that this activity is able to be launched as an entry point with media data inside of the Intent. A good example might be a photo-editing activity that opens a photo, modifies it, and returns the modified image when the activity finishes.\n\nAs a side note, model objects must implement the `Parcelable` interface if you want to send them between activities and fragments. Implementing the `Parcelable` interface is similar to conforming to the `<NSCopying>` protocol on iOS. Also note that `Parcelable` objects are able to be stored in an activity's or fragment's savedInstanceState, in order to more easily restore their states after they have been destroyed.\n\nLet's next look at one activity launching another activity, and also responding to when the second activity finishes.\n\n### Launching Another Activity for a Result\n\n```java\n// A request code is a unique value for returning activities\nprivate static final int REQUEST_CODE_NEXT_ACTIVITY = 1234;\n\nprotected void startNextActivity() {\n    // Intents need a context, so give this current activity as the context\n    Intent nextActivityIntent = new Intent(this, NextActivity.class);\n       startActivityForResult(nextActivityResult, REQUEST_CODE_NEXT_ACTIVITY);\n}\n\n@Override\nprotected void onActivityResult(int requestCode, int resultCode, Intent data) {\n    switch (requestCode) {\n    case REQUEST_CODE_NEXT_ACTIVITY:\n        if (resultCode == RESULT_OK) {\n            // This means our Activity returned successfully. For now, Toast this text.  \n            // This just creates a simple pop-up message on the screen.\n                Toast.makeText(this, \"Result OK!\", Toast.LENGTH_SHORT).show();\n            }\n            return;\n        }    \n        super.onActivityResult(requestCode, resultCode, data);\n}\n```\n\n### Returning a Result on Activity Finish()\n\n```java\npublic static final String activityResultString = \"activityResultString\";\n\n/*\n * On completion, place the object ID in the intent and finish with OK.\n * @param returnObject that was processed\n */\nprivate void onActivityResult(Object returnObject) {\n        Intent data = new Intent();\n        if (returnObject != null) {\n            data.putExtra(activityResultString, returnObject.uniqueId);\n        }\n    \n        setResult(RESULT_OK, data);\n        finish();        \n}\n```\n\n## Fragments\n\nThe [Fragment](http://developer.android.com/guide/components/fragments.html) concept is unique to Android and came around somewhat recently in Android 3.0. Fragments are mini controllers that can be instantiated to fill activities. They store state information and may contain view logic, but there may be multiple fragments on the screen at the same time -- putting the activity in a fragment controller role. Also note that fragments do not have their own contexts and they rely heavily on activities for their connection to the application's state.\n\nTablets are a great fragment use case example: you can place a list fragment on the left and a detail fragment on the right.[^4] Fragments allow you to break up your UI and controller logic into smaller, reusable chunks. But beware! The fragment lifecycle, detailed below, is more nuanced.\n\n![A multi-pane activity with two fragments](/images/issue-11/multipane_view_tablet.png)\n \nFragments are the new way of structuring apps on Android, just like `UICollectionView` is the new way of structuring list data instead of `UITableview` for iOS.[^5]  While it is initially easier to avoid using fragments and instead use nothing but activities, you could regret this decision later on. That said, resist the urge to give up on activities entirely by swapping fragments on a single activity -- this can leave you in a bind when wanting to take advantage of intents and using multiple fragments on the same activity.\n\nLet's look at a sample `UITableViewController` and a sample `ListFragment` that show a list of prediction times for a subway trip, courtesy of the [MBTA](http://www.mbta.com/rider_tools/developers/default.asp?id=21898).\n\n### Table View Controller Implementation\n\n&nbsp;\n\n![TripDetailsTableViewController](/images/issue-11/IMG_0095.PNG)\n\n&nbsp;\n\n```objc\n@interface MBTASubwayTripTableTableViewController ()\n\n@property (assign, nonatomic) MBTATrip *trip;\n\n@end\n\n@implementation MBTASubwayTripTableTableViewController\n\n- (instancetype)initWithTrip:(MBTATrip *)trip\n{\n    self = [super initWithStyle:UITableViewStylePlain];\n    if (self) {\n        _trip = trip;\n        [self setTitle:trip.destination];\n    }\n    return self;\n}\n\n- (void)viewDidLoad\n{\n    [super viewDidLoad];\n    \n    [self.tableView registerClass:[MBTAPredictionCell class] forCellReuseIdentifier:[MBTAPredictionCell reuseId]];\n    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MBTATripHeaderView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:[MBTATripHeaderView reuseId]];\n}\n\n#pragma mark - UITableViewDataSource\n\n- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView\n{\n    return 1;\n}\n\n- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section\n{\n    return [self.trip.predictions count];\n}\n\n#pragma mark - UITableViewDelegate\n\n- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section\n{\n    return [MBTATripHeaderView heightWithTrip:self.trip];\n}\n\n- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section\n{\n    MBTATripHeaderView *headerView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:[MBTATripHeaderView reuseId]];\n    [headerView setFromTrip:self.trip];\n    return headerView;\n}\n\n- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath\n{\n    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[MBTAPredictionCell reuseId] forIndexPath:indexPath];\n    \n    MBTAPrediction *prediction = [self.trip.predictions objectAtIndex:indexPath.row];\n    [(MBTAPredictionCell *)cell setFromPrediction:prediction];\n    \n    return cell;\n}\n\n- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath\n{\n    return NO;\n}\n\n- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath\n{\n    [tableView deselectRowAtIndexPath:indexPath animated:YES];\n}\n\n@end\n```\n\n### List Fragment Implementation\n\n&nbsp;\n\n![TripDetailFragment](/images/issue-11/Screenshot_2014-03-25-11-42-16.png)\n\n&nbsp;\n\n```java\npublic class TripDetailFragment extends ListFragment {\n\n    /**\n     * The configuration flags for the Trip Detail Fragment.\n     */\n    public static final class TripDetailFragmentState {\n        public static final String KEY_FRAGMENT_TRIP_DETAIL = \"KEY_FRAGMENT_TRIP_DETAIL\";\n    }\n\n    protected Trip mTrip;\n\n    /**\n     * Use this factory method to create a new instance of\n     * this fragment using the provided parameters.\n     *\n     * @param trip the trip to show details\n     * @return A new instance of fragment TripDetailFragment.\n     */\n    public static TripDetailFragment newInstance(Trip trip) {\n        TripDetailFragment fragment = new TripDetailFragment();\n        Bundle args = new Bundle();\n        args.putParcelable(TripDetailFragmentState.KEY_FRAGMENT_TRIP_DETAIL, trip);\n        fragment.setArguments(args);\n        return fragment;\n    }\n\n    public TripDetailFragment() { }\n\n    @Override\n    public View onCreateView(LayoutInflater inflater, ViewGroup container,\n                             Bundle savedInstanceState) {\n        Prediction[] predictions= mTrip.predictions.toArray(new Prediction[mTrip.predictions.size()]);\n        PredictionArrayAdapter predictionArrayAdapter = new PredictionArrayAdapter(getActivity(), predictions);\n        setListAdapter(predictionArrayAdapter);\n        return super.onCreateView(inflater,container, savedInstanceState);\n    }\n\n    @Override\n    public void onViewCreated(View view, Bundle savedInstanceState) {\n        super.onViewCreated(view, savedInstanceState);\n        TripDetailsView headerView = new TripDetailsView(getActivity());\n        headerView.updateFromTripObject(mTrip);\n        getListView().addHeaderView(headerView);\n    }\n}\n```\n\nIn the next section, let's decipher some of the unique Android components.\n\n## Common Android Components\n\n### List Views and Adapters\n\n`ListViews` are the closest approximation to `UITableView` on Android, and they are one of the most common components that you will use. Just like `UITableView` has a helper view controller, `UITableViewController`, ListView also has a helper activity, `ListActivity`, and a helper fragment, `ListFragment`. Similar to `UITableViewController`, these helpers take care of the layout (similar to the xib) for you and provide convenience methods for managing adapters, which we'll discuss below. Our example above uses a `ListFragment` to display data from a list of `Prediction` model objects, similar to how the table view's datasource uses an array of `Prediction` model objects to populate the `UITableView`.\n\nSpeaking of datasources, on Android we don't have datasources and delegates for `ListView`. Instead, we have adapters. Adapters come in many forms, but their primary goal is similar to a datasource and table view delegate all in one. Adapters take data and adapt it to populate a `ListView` by instantiating views the `ListView` will display. Let's have a look at the array adapter used above:\n     \n```java\npublic class PredictionArrayAdapter extends ArrayAdapter<Prediction> {\n\n    int LAYOUT_RESOURCE_ID = R.layout.view_three_item_list_view;\n\n    public PredictionArrayAdapter(Context context) {\n        super(context, R.layout.view_three_item_list_view);\n    }\n\n    public PredictionArrayAdapter(Context context, Prediction[] objects) {\n        super(context, R.layout.view_three_item_list_view, objects);\n    }\n\n    @Override\n    public View getView(int position, View convertView, ViewGroup parent)\n    {\n        Prediction prediction = this.getItem(position);\n        View inflatedView = convertView;\n        if(convertView==null)\n        {\n            LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);\n            inflatedView = inflater.inflate(LAYOUT_RESOURCE_ID, parent, false);\n        }\n\n        TextView stopNameTextView = (TextView)inflatedView.findViewById(R.id.view_three_item_list_view_left_text_view);\n        TextView middleTextView = (TextView)inflatedView.findViewById(R.id.view_three_item_list_view_middle_text_view);\n        TextView stopSecondsTextView = (TextView)inflatedView.findViewById(R.id.view_three_item_list_view_right_text_view);\n\n        stopNameTextView.setText(prediction.stopName);\n        middleTextView.setText(\"\");\n        stopSecondsTextView.setText(prediction.stopSeconds.toString());\n\n        return inflatedView;\n    }\n}\n```\n\nYou'll note that the adapter has an important method named `getView`, which is very similar to `cellForRowAtIndexPath:`. Another similarity you'll notice is a pattern for reusing views, similar to iOS. Reusing views are just as important as on iOS, and this substantially helps performance! This adapter is rather simple, because it uses a built-in superclass, `ArrayAdapter<T>`, for adapters working with array data, but it illustrates how to populate a `ListView` from a dataset.\n     \n### AsyncTasks\n\nIn place of Grand Central Dispatch on iOS, on Android we have access to `AsyncTasks`. `AsyncTasks` is a different take on exposing asynchronous tools in a more friendly way. `AsyncTasks` is a bit out of scope for this article, but I highly recommend looking over some of the [documentation](http://developer.android.com/reference/android/os/AsyncTask.html).\n \n## Activity Lifecycle\n\nOne of the primary things to watch out for coming from iOS development is the Android lifecycle. Let's start by looking at the [Activity Lifecycle Documentation](http://developer.android.com/training/basics/activity-lifecycle/index.html):\n\n![Android Activity Lifecycle](/images/issue-11/Android-Activity-Lifecycle.png)\n\nIn essence, the activity lifecycle is very similar to the UIViewController lifecycle. The primary difference is that the Android OS can be ruthless with destroying activities, and it is very important to make sure that the data and the state of the activity are saved, so that they can be restored from the saved state if they exist in the `onCreate()`. The best way to do this is by using bundled data and restoring from the savedInstanceState and/or Intents. For example, here is the part of the `TripListActivity` from our sample project that is keeping track of the currently shown subway line:\n\n \n```java\npublic static Intent getTripListActivityIntent(Context context, TripList.LineType lineType) {\n    Intent intent = new Intent(context, TripListActivity.class);\n    intent.putExtra(TripListActivityState.KEY_ACTIVITY_TRIP_LIST_LINE_TYPE, lineType.getLineName());\n    return intent;\n}\n\npublic static final class TripListActivityState {\n    public static final String KEY_ACTIVITY_TRIP_LIST_LINE_TYPE = \"KEY_ACTIVITY_TRIP_LIST_LINE_TYPE\";\n}\n    \nTripList.LineType mLineType;    \n    \n@Override\nprotected void onCreate(Bundle savedInstanceState) {\n   super.onCreate(savedInstanceState);\n   mLineType = TripList.LineType.getLineType(getIntent().getStringExtra(TripListActivityState.KEY_ACTIVITY_TRIP_LIST_LINE_TYPE));\n}    \n```\n\nA note on rotation: the lifecycle **completely** resets the view on rotation. That is, your activity will be destroyed and recreated when a rotation occurs. If data is properly saved in the saved instance state and the activity restores the state correctly after its creation, then the rotation will work seamlessly. Many app developers have issues with app stability when the app rotates, because an activity does not handle state changes properly. Beware! Do not lock your app's rotation to solve these issues, as this only hides the lifecycle bugs that will still occur at another point in time when the activity is destroyed by the OS.\n\n## Fragment Lifecycle\n\nThe [Fragment Lifecycle](http://developer.android.com/training/basics/fragments/index.html) is similar to the activity lifecycle, with a few additions. \n\n![Android Fragment Lifecycle](/images/issue-11/fragment_lifecycle.png)\n\nOne of the problems that can catch developers off guard is regarding issues communicating between fragments and activities. Note that the `onAttach()` happens **before** `onActivityCreated()`. This means that the activity is not guaranteed to exist before the fragment is created. The `onActivityCreated()` method should be used when you set interfaces (delegates) to the parent activity, if needed.\n\nFragments are also created and destroyed aggressively by the needs of the operating system, and to keep their state, require the same amount of diligence as activities. Here is an example from our sample project, where the trip list fragment keeps track of the `TripList` data, as well as the subway line type:\n \n```java\n/**\n * The configuration flags for the Trip List Fragment.\n */\npublic static final class TripListFragmentState {\n    public static final String KEY_FRAGMENT_TRIP_LIST_LINE_TYPE = \"KEY_FRAGMENT_TRIP_LIST_LINE_TYPE\";\n    public static final String KEY_FRAGMENT_TRIP_LIST_DATA = \"KEY_FRAGMENT_TRIP_LIST_DATA\";\n}\n\n/**\n * Use this factory method to create a new instance of\n * this fragment using the provided parameters.\n *\n * @param lineType the subway line to show trips for.\n * @return A new instance of fragment TripListFragment.\n */\npublic static TripListFragment newInstance(TripList.LineType lineType) {\n    TripListFragment fragment = new TripListFragment();\n    Bundle args = new Bundle();\n    args.putString(TripListFragmentState.KEY_FRAGMENT_TRIP_LIST_LINE_TYPE, lineType.getLineName());\n    fragment.setArguments(args);\n    return fragment;\n}\n\nprotected TripList mTripList;\nprotected void setTripList(TripList tripList) {\n    Bundle arguments = this.getArguments();\n    arguments.putParcelable(TripListFragmentState.KEY_FRAGMENT_TRIP_LIST_DATA, tripList);\n    mTripList = tripList;\n    if (mTripArrayAdapter != null) {\n        mTripArrayAdapter.clear();\n        mTripArrayAdapter.addAll(mTripList.trips);\n    }\n}\n\n@Override\npublic void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    if (getArguments() != null) {\n        mLineType = TripList.LineType.getLineType(getArguments().getString(TripListFragmentState.KEY_FRAGMENT_TRIP_LIST_LINE_TYPE));\n        mTripList = getArguments().getParcelable(TripListFragmentState.KEY_FRAGMENT_TRIP_LIST_DATA);\n    }\n}    \n```\n\nNotice that the fragment always restores its state from the bundled arguments in `onCreate`, and that the custom setter for the `TripList` model object adds the object to the bundled arguments as well. This ensures that if the fragment is destroyed and recreated, such as when the device is rotated, the fragment always has the latest data to restore from.\n\n## Layouts\n\nSimilar to other parts of Android development, there are pros and cons to specifying layouts in Android versus iOS. [Layouts](http://developer.android.com/guide/topics/ui/declaring-layout.html) are stored as human-readable XML files in the `res/layouts` folder.  \n\n\n### Subway List View Layout\n\n![Subway ListView](/images/issue-11/Screenshot_2014-03-24-13-12-00.png)\n\n```xml\n<RelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    xmlns:tools=\"http://schemas.android.com/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    tools:context=\"com.example.androidforios.app.activities.MainActivity$PlaceholderFragment\">\n\n    <ListView\n        android:id=\"@+id/fragment_subway_list_listview\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"match_parent\"\n        android:paddingBottom=\"@dimen/Button.Default.Height\"/>\n\n    <Button\n        android:id=\"@+id/fragment_subway_list_Button\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"@dimen/Button.Default.Height\"\n        android:minHeight=\"@dimen/Button.Default.Height\"\n        android:background=\"@drawable/button_red_selector\"\n        android:text=\"@string/hello_world\"\n        android:textColor=\"@color/Button.Text\"\n        android:layout_alignParentBottom=\"true\"\n        android:gravity=\"center\"/>\n\n</RelativeLayout>\n```\n\nHere is the same view on iOS with a `UITableView` and a `UIButton` pinned to the bottom via Auto Layout in Interface Builder:\n\n![iOS Subway Lines UIViewController](/images/issue-11/iOS_Screen1.png)\n\n![Interface Builder Constraints](/images/issue-11/iOSConstraints.png)\n\nYou'll notice that the Android layout file is much easier to **read** and understand what is going on. There are many parts to laying out views in Android, but we'll cover just a few of the important ones.\n\nThe primary structure that you will deal with will be subclasses of [ViewGroup](http://developer.android.com/reference/android/view/ViewGroup.html) -- [RelativeLayout](http://developer.android.com/reference/android/widget/RelativeLayout.html), [LinearLayout](http://developer.android.com/reference/android/widget/LinearLayout.html), and [FrameLayout](http://developer.android.com/reference/android/widget/FrameLayout.html) are the most common. These ViewGroups contain other views and expose properties to arrange them on screen.\n\nA good example is the use of a `RelativeLayout` above. A relative layout allows us to use `android:layout_alignParentBottom=\"true\"` in our layout above to pin the button to the bottom.\n\nLastly, to link layouts to fragments or activities, simply use that layout's resource ID during the `onCreateView`:\n \n```java\n@Override\npublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {\n    return inflater.inflate(R.layout.fragment_subway_listview, container, false);\n}\n```\n\n### Layout Tips\n\n- Always work in dp ([density-independent pixels](http://developer.android.com/training/multiscreen/screendensities.html)) instead of pixels directly.\n- Don't bother nudging items for layouts in the visual editor -- often the visual editor will put individual points of spacing on objects instead of adjusting the height and width as you might like. Your best bet is to adjust the XML directly.\n- If you ever see the `fill_parent` value for a layout height or width, this value was deprecated years ago in API 8 and replaced with `match_parent`.\n\nSee the the [responsive android applications](/issues/11-android/responsive-android-applications/) article for more tips on this.\n \n\n## Data\n\nThe [Data Storage Options](http://developer.android.com/guide/topics/data/data-storage.html) available on Android are also very similar to what is available on iOS:\n\n- [Shared Preferences](http://developer.android.com/guide/topics/data/data-storage.html#pref) <-> NSUserDefaults\n- In-memory objects\n- Saving to and fetching from file structure via the [internal](http://developer.android.com/guide/topics/data/data-storage.html#filesInternal) or [external](http://developer.android.com/guide/topics/data/data-storage.html#filesExternal) file storage <-> saving to the documents directory\n- [SQLite](http://developer.android.com/guide/topics/data/data-storage.html#db) <-> Core Data\n \nThe primary difference is the lack of Core Data. Instead, Android offers straight access to the SQLite database and returns [cursor](http://developer.android.com/reference/android/database/Cursor.html) objects for results. Head over to the article in this issue about [using SQLite on Android](/issues/11-android/sqlite-database-support-in-android/) for more details.\n\n\n## Android Homework\n\nWhat we've discussed so far barely scratches the surface. To really take advantage of some of the things that make Android special, I recommend checking out some of these features:\n\n- [Action Bar, Overflow Menu, and the Menu Button](http://developer.android.com/guide/topics/ui/actionbar.html)\n- [Cross-App Data Sharing](https://developer.android.com/training/sharing/index.html)\n- [Respond to common OS actions](http://developer.android.com/guide/components/intents-common.html)\n- Take advantage of Java's features: generics, virtual methods and classes, etc.\n- [Google Compatibility Libraries](http://developer.android.com/tools/support-library/index.html)\n- The Android Emulator: install the [x86 HAXM plugin](http://software.intel.com/en-us/android/articles/intel-hardware-accelerated-execution-manager) to make the emulator buttery smooth.\n \n## Final Words\n\nMuch of what was discussed in this article is implemented in the MBTA subway transit [sample project](https://github.com/objcio/issue-11-android-101) on GitHub. The project was built as a way to illustrate similar concepts such as application structure, handling data, and building UI on the same application for both platforms.\n\nWhile some of the pure **implementation** details are very different on Android, it is very easy to bring problem-solving skills and patterns learned on iOS to bear. Who knows? Maybe understanding how Android works just a little bit better might prepare you for the next version of iOS.\n\n[^1]: [Source](http://www.prnewswire.com/news-releases/strategy-analytics-android-captures-79-percent-share-of-global-smartphone-shipments-in-2013-242563381.html)\n\n[^2]: See Google's documentation for supporting multiple screen sizes [here](http://developer.android.com/guide/practices/screens_support.html).\n    \n[^3]: [Intents documentation](http://developer.android.com/reference/android/content/Intent.html)\n\n[^4]: See Google's documentation for [multi-pane tablet view](http://developer.android.com/design/patterns/multi-pane-layouts.html) for more information.\n    \n[^5]: Thanks, [NSHipster](http://nshipster.com/uicollectionview/).\n"
  },
  {
    "path": "2014-04-01-dependency-injection-in-java.md",
    "content": "---\ntitle: \"Dependency Injection, Annotations, and why Java is Better Than you Think it is\"\ncategory: \"11\"\ndate: \"2014-04-01 06:00:00\"\ntags: article\nauthor:\n  - name: Bill Phillips\n    url: http://twitter.com/billjings\n---\n\n\nI have a confession to make\\: I like Java.\n\nReally! I do!\n\nThat may not be shocking to you. I did help write a book full of Java code, after all. It’s shocking to me, though. I wasn’t a fan when I started writing Android apps, I wasn’t a fan when we began the [Big Nerd Ranch Guide](http://www.bignerdranch.com/book/android_the_big_nerd_ranch_guide), and I still wasn’t a huge fan when we finished it. \n\nMy beef was not original or well thought out, but here are my issues, roughly:\n\n*    It’s verbose. There’s no shortened syntax for implementing callbacks, like blocks or lambdas, so you have to write a lot of boilerplate to implement even a simple interface. If you need an object that holds four things, you have to create a class with four named fields. \n*    It’s rigid. Writing sensible Java constantly requires you to specify exactly which exception you’re catching, to specify which type you’re taking in, to check and make sure that your references aren’t null, and to import every class you need to use. And while there is some flexibility at runtime, it’s nowhere close to what you get in the Objective-C runtime, much less something like Ruby or Python.\n\nThat was essentially my view of Java. It was this kind of Java:\n\n```java\npublic class NumberStack {\n    List<Integer> mNumbers = new ArrayList<Integer>();\n\n    public void pushNumber(int number) {\n        mNumbers.add(number);\n    }\n\n    public Integer popNumber() {\n        if (mNumber.size() == 0) {\n            return null;\n        } else {\n            return mNumber.remove(mNumber.size() - 1);\n        }\n    }\n}\n```\n\nAdd some inner classes and interfaces to the mix, and that is what I learned and worked with. Not the worst thing in the world to be writing, but other languages had features and flexibility that I wished that I had in Java. Never did I find myself writing code in another language and saying, “Man, I wish this were more like Java.”\n\nMy opinion has changed.\n\n## Something Peculiar to Java\n\nOddly enough, the tool that changed my mind is only popular because of problems that are peculiar to Java. Consider the following code:\n\n```java\npublic class Payroll {\n    ...\n\n    public long getWithholding(long payInDollars) {\n        ...\n        return withholding;\n   }\n\n    public long getAfterTaxPay(Employee employee) {\n        long basePay = EmployeeDatabase.getInstance()\n           .getBasePay(employee);\n        long withholding = getWithholding(basePay);\n\n        return basePay - withholding;\n    }\n}\n```\n\nThis class has a dependency in `getAfterTaxPay()` called `EmployeeDatabase`. There are a variety of ways that we could create this object, but in this example, I’ve used a typical singleton pattern of having a static getInstance method.\n\nDependencies in Java are surprisingly strict things. Whenever I write a line of code like this:\n\n```java\n        long basePay = EmployeeDatabase.getInstance()\n           .getBasePay(employee);\n```\n\nI create a strict dependency on the `EmployeeDatabase` class. Not only that, but I also create a strict dependency on a particular method in `EmployeeDatabase`: the `getInstance()` method. In other languages, I might be able to swizzle or monkey patch this kind of thing. Not that that’s a great idea, necessarily, but it is at least possible. Not so in Java.\n\nOther ways of creating a dependency are even more strict than that. Let’s say that instead, I wrote that line like this:\n\n```java\n        long basePay = new EmployeeDatabase()\n           .getBasePay(employee);\n```\n\nWhen I use the new keyword, I tie myself down in all the same ways I did with the static method, but I also add one more: calling `new EmployeeDatabase()` must always yield an instance of the `EmployeeDatabase` class. You can’t rewrite that constructor to return a mock subclass, no matter what you do.\n\n## Dependency Injection\n\nThe way we usually solve this problem is to use a technique called dependency injection. It’s not a technique unique to Java, but because of the aforementioned issues, Java is in particularly dire need of it. \n\nDependency injection simply means receiving collaborators as constructor parameters instead of fetching them ourselves. So `Payroll` would look like this instead:\n\n```java\npublic class Payroll {\n    ...\n\n    EmployeeDatabase mEmployeeDatabase;\n\n    public Payroll(EmployeeDatabase employeeDatabase) {\n        mEmployeeDatabase = employeeDatabase;\n    }\n\n    public long getWithholding(long payInDollars) {\n        ...\n        return withholding;\n   }\n\n    public long getAfterTaxPay(Employee employee) {\n        long basePay = mEmployeeDatabase.getBasePay(employee);\n        long withholding = getWithholding(basePay);\n\n        return basePay - withholding;\n    }\n}\n```\n\nIs `EmployeeDatabase` a singleton? A mocked-out subclass? A context-specific implementation? `Payroll` no longer needs to know.\n\n## Declarative Dependency Programming\n\nAll of that is just background for what I really want to talk about: dependency injectors.\n\n(An aside: I know it’s a little odd to be two problems deep before actually discussing something nifty, but I hope you’ll bear with me. Understanding what Java gets right just requires more work than it does with other languages. It’s the nature of the beast.)\n\nSee, now that we are passing in dependencies through our constructors, our objects are more difficult to use and more difficult to change. Before I used dependency injection, I could use `Payroll` like this:\n\n```java\n    new Payroll().getAfterTaxPay(employee);\n```\n\nNow, though, I have to write this:\n\n```java\n    new Payroll(EmployeeDatabase.getInstance())\n        .getAfterTaxPay(employee);\n```\n\nPlus, anytime I change `Payroll`’s dependencies, I have to change every place I write `new Payroll`, too.\n\nA dependency injector allows me to forget about writing code to explicitly supply dependencies. Instead, I declaratively say what my dependencies are, and the tool worries about supplying them when they’re needed. There are a variety of dependency injection tools out there; for these examples, I’ll be using RoboGuice.\n\nTo do this, we use Java’s tool for describing code: the annotation. We declare our dependencies by simply annotating our constructor:\n\n```java\n    @Inject\n    public Payroll(EmployeeDatabase employeeDatabase) {\n        mEmployeeDatabase = employeeDatabase;\n    }\n```\n\nThe `@Inject` annotation says, “To build an instance of `Payroll`, execute this constructor, passing in values for all of its parameters.” Then when I actually need a `Payroll` instance, I ask the dependency injector to build me one, like so:\n\n```java\n    Payroll payroll = RoboGuice.getInjector(getContext())\n        .getInstance(Payroll.class);\n\n    long afterTaxPay = payroll.getAfterTaxPay(employee);\n```\n\nOnce I’m constructing instances in this way, I can use the injector itself to configure how dependencies are satisfied. Do I want `EmployeeDatabase` to be a singleton? Do I want to use a customized subclass? All of this can be specified in one place. \n\n## The Wider World of Declarative Java\n\nIt’s an easily described tool, but it’s hard to overestimate how fundamental the gap is between Java with and without a dependency injector. Without a dependency injector, aggressive refactoring and test-driven development are laborious. With one, they are effortless. The only thing more indispensable to a Java developer than a dependency injector is a good IDE.\n\nStill, it’s just the first taste of a wider set of possibilities. Most of the exciting new stuff for Android developers originating outside Google revolves around annotation-based APIs. \n\nTake ButterKnife, for example. We spend a lot of time in Android wiring up listeners to view objects, like this:\n\n```java\npublic void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.activity_content);\n\n    View okButton = findViewById(R.id.ok_button);\n    okButton.setOnClickListener(new View.OnClickListener() {\n        public void onClick(View v) {\n            onOkButtonClicked();\n        }\n    });\n}\n\npublic void onOkButtonClicked() {\n    // handle button click\n}\n```\n\nButterKnife allows us to instead provide a little bit of metadata that says, “Call `onOkButtonClicked` when the view with the id `R.id.ok_button` is clicked.” Like this:\n\n```java\npublic void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.activity_content);\n\n    ButterKnife.inject(this);\n}\n\n@OnClick(R.id.ok_button);\npublic void onOkButtonClicked() {\n    // handle button click\n}\n```\n\nI could go on and on with more examples. There are libraries that use annotations to serialize and deserialize JSON, to stash fields in `savedInstanceState`, to generate code to interface with RESTful web services, and on and on and on.\n\n## Compile Time vs. Runtime Annotation Processing\n\nWhile some tools may achieve similar effects with annotations, Java allows them to achieve these effects in different ways. Take RoboGuice and Dagger, for example. Both are dependency injectors; both use the `@Inject` annotation. But where RoboGuice reads your code annotations at runtime, Dagger reads them at compile time and generates code.\n\nThis has a few important benefits. It means that errors in your annotation semantics can be detected early. Dagger can tell you at compile time when you have a circular dependency; RoboGuice cannot.\n\nIt can also improve performance. Generated code can reduce startup time and eliminate the need to read annotations at runtime. Reading annotations requires the use of Java’s reflection APIs, which can be expensive on some Android devices. \n\n### An Example of Runtime Annotation Processing\n\nI’d like to finish up by showing a simple example of how one might define and process a runtime annotation. Let’s say that you were an exceptionally impatient person and were tired of typing out fully qualified static constants in your Android codebase, constants like these:\n\n```java\npublic class CrimeActivity {\n    public static final String ACTION_VIEW_CRIME = \n        “com.bignerdranch.android.criminalintent.CrimeActivity.ACTION_VIEW_CRIME”;\n}\n```\n\nYou could use a runtime annotation to do this work for you. First, you’d create the annotation class:\n\n```java\n@Retention(RetentionPolicy.RUNTIME)\n@Target( { ElementType.FIELD })\npublic @interface ServiceConstant { }\n```\n\nThis code declares an annotation named `ServiceConstant`. The code is itself annotated with two annotations: `@Retention`, and `@Target`. `@Retention` says how long the annotation will stick around. Here, we say that we want to see it at runtime. If we wanted this annotation to be processed at compile time only, we could have specified `RetentionPolicy.SOURCE`.\n\nThe other annotation, `@Target`, says where you can put the annotation in your source code. Any number of values can be provided. Our annotation is only valid for fields, so we have just provided `ElementType.FIELD`.\n\nOnce the annotation is defined, we write some code to look for it and populate the annotated field automatically:\n\n```java\npublic static void populateConstants(Class<?> klass) {\n    String packageName = klass.getPackage().getName();\n    for (Field field : klass.getDeclaredFields()) {\n        if (Modifier.isStatic(field.getModifiers()) && \n                field.isAnnotationPresent(ServiceConstant.class)) {\n            String value = packageName + \".\" + field.getName();\n            try {\n                field.set(null, value);\n                Log.i(TAG, \"Setup service constant: \" + value + \"\");\n            } catch (IllegalAccessException iae) {\n                Log.e(TAG, \"Unable to setup constant for field \" + \n                        field.getName() +\n                        \" in class \" + klass.getName());\n            }\n        }\n    }\n}\n```\n\nFinally, we add the annotation to our code, and call our magic method:\n\n```java\npublic class CrimeActivity {\n    @ServiceConstant\n    public static final String ACTION_VIEW_CRIME;\n\n    static {\n        ServiceUtils.populateConstants(CrimeActivity.class);\n    }\n}\n```\n\n## Conclusion\n\nWell, that’s all I’ve got. So much for annotations in Java. I can’t say that I’m sure that all this has made you feel the same way as I do about Java, but I hope that you’ve seen some interesting stuff. While day-to-day Java may be lacking a bit in expressivity, there are a few basic building blocks in the Java kit that make it possible for advanced developers to create powerful tools that amplify the productivity of the entire community. \n\nIf you’re interested in diving in deeper, you will find the topic of driving code generation with annotations very interesting. It’s not necessarily pretty to read or write, but folks are doing some nifty work out there with the tools as they are. The source for ButterKnife is reasonably simple, if you’re interested in an example of how it’s done in the real world.\n"
  },
  {
    "path": "2014-04-01-editorial.md",
    "content": "---\ntitle:  \"Editorial\"\ncategory: \"11\"\ndate: \"2014-04-01 12:00:00\"\ntags: editorial\n---\n\nHey everyone,\n\nobjc.io goes Android. \n\nAdmittedly, this started out as an April Fools' joke. But we quickly realized that we actually could make a really good issue about this. After all, it's interesting to Objective-C developers to learn something about what development on the other major mobile platform is like, as well as what we can learn from it.\n\nSince we have been very busy last month finishing [one of our own projects](http://www.decksetapp.com), we were delighted to see that many great Android developers were willing to chip in and fill this issue with their knowledge of the Android platform.\n\nWe tried to cover all the important differences. To get an overview, you can read Stephen's [Android 101](/issues/1-view-controllers/android_101_for_ios_developers.html), which shows differences in the application structure on a high level. One of the things that's really cool about Android is [Intents](/issues/11-android/android-intents/), and Martin shows us how they can be used to structure your application and communicate between applications. Because Android has a plethora of varied devices, developers also need to deal with lots of different screen sizes. Chris tells us all about how to write [responsive apps](/issues/11-android/responsive-android-applications/) on Android.\n\nOn Android, there's no such thing as Core Data. People who like working with SQL will be pleased to read James's article on Android's [database support](/issues/11-android/sqlite-database-support-in-android/). He shows us how to work with the built-in libraries and details which third-party libraries are available. Then Kevin tells us about [notifications on Android](/issues/11-android/android-notifications/), which can do a lot more than their iOS counterparts. Finally, Bill tells us about [dependency injection](/issues/11-android/dependency-injection-in-java/) in Java, which is, of course, just as useful for iOS projects. It's also a great technique to make your objects easier to test, as well as to increase reusability.\n\nHave a great April,\n\nChris, Daniel, and Florian.\n\n"
  },
  {
    "path": "2014-04-01-responsive-android-applications.md",
    "content": "---\ntitle:  \"Responsive Android Applications\"\ncategory: \"11\"\ndate: \"2014-04-01 09:00:00\"\ntags: article\nauthor:\n  - name: Chris Stewart\n    url: https://twitter.com/Cstew\n---\n\n## Introduction\n\nDeveloping a mobile application is a creative process. You want to build something beautiful and functional. Something that works well on any device. Something that delights your users. Something that you’re proud of. I want to show you how I develop these kinds of applications on Android. \n\nOne common misconception about Android development is that it’s hard to write these kinds of applications when screen properties vary so widely. You’ve no doubt seen [Android Fragmentation Visualized](http://opensignal.com/reports/fragmentation.php), which lists a daunting number of Android devices.\n\nThe truth is, you will have to put some thought into the design, but not significantly more than you would on other platforms. Android developers have excellent tools available to support this variation in device configuration and to ensure that their applications perform beautifully on all devices. \n\nIn this article, I will focus on three areas of variability in Android devices and how those variations affect the development and design of Android applications. I will cover these areas at a high level and from an iOS developer’s perspective: \n\n* How do Android developers optimize for minor variations in screen sizes? How are differences in width and height between devices managed? \n* How do Android developers account for screen density variations?\n* How are applications optimized to work well on different device categories? How can I make one app that works well on phone and tablet devices? \n\n## Screen Size\n\nLet’s review screen sizes on iOS. There are effectively three: 3.5-inch iPhone, 4-inch iPhone, and iPad. Although the iPad mini is, of course, smaller than the iPad, from the developer’s perspective, it is simply scaled. For many applications, the 3.5-inch versus 4-inch iPhone screen size variance has little impact, since only the height changes.\n\nThe iOS drawing system uses points and not pixels, so the screen’s retina or non-retina status does not impact layout. Layout is either static (designed down to the point for each device, programmatically, or using device-specific XIB files) or dynamic (using Auto Layout or autoresizing masks).\n\nIn contrast, on Android, there are orders of a magnitude of more screen sizes that we must support. How can an Android developer possibly ensure that his or her app looks good on all of those devices? \n\nIn many ways, design for Android is similar to design for the web. Web designs must support any possible browser size. In the same way, Android designs are best built to anticipate changes in screen size. We design our views so that they will flow to fill the space and content that they are given.\n\nSince you must design your application with different screen sizes in mind, supporting devices in landscape comes naturally. When an application is designed to support any screen size, landscape orientation is really just a wider configuration of your device. \n\n### Layout Files\n\nLet’s dive in to the layout system in more detail. Layout files are XML files that describe your user interface. \n\nWe create a sample layout file below. This file is used as the view for a login screen in our application: \n\n```xml\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n              android:orientation=\"vertical\"\n              android:layout_width=\"match_parent\"\n              android:layout_height=\"match_parent\"\n              android:padding=\"14dp\">\n    <EditText\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:hint=\"username\"/>\n    <EditText\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:hint=\"password\"/>\n    <Button\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:hint=\"Login\"/>\n</LinearLayout>\n```\n\n![](/images/issue-11/image02.png) \n\n![](/images/issue-11/image01.png)\n\nIn the above layout file, `LinearLayout` has been used to align `Views` linearly. We’ve specified three views in the `LinearLayout`: a username `EditText`, a password `EditText`, and a login button. \n\nNotice the `layout_width` and `layout_height` attributes on each view in the layout file. These attributes are used to specify the width and height of a view. We used two constants in each of these parameters: `wrap_content` and `match_parent`. If `wrap_content` is specified as the height of a view, that view will be exactly as tall as it needs to be to display its contents. If a view specifies `match_parent` as its width attribute, that view will be as wide as the view that contains it. \n\nBy making use of the `wrap_content` and `match_parent` values, we have designed a view that stretches to fill any screen size. \n\nThe most important distinction here from iOS is that this layout XML file and the views specified inside of it do not have a size. In fact, the views in this layout file will not have any size value associated with them until just before they are placed on the screen.\n\n## Screen Density\n\nAnother aspect of variability in Views on Android is screen density. How do you write an app that works on any density screen?\n\nAs you know, iOS developers are concerned with two sizes: normal and retina. If the `@2x` suffix on the filename is used, the system will automatically choose the appropriate image, depending on the device. \n\nAndroid screen density works in a similar way but with more variability. Rather than two image buckets, Android developers have many. Our standard image bucket size is `mdpi`, or medium dpi. This `mdpi` bucket is the same as iOS’s normal image size. Then, `hdpi`, or high dpi, is 1.5 times the size of `mdpi`. Finally, `xhdpi`, or extra high dpi, is 2 times the normal size, the same as iOS’s retina size. Android developers can take advantage of other image buckets, including `xxhdpi` and `xxxhdpi`.\n\n## Resource Qualifiers\n\nThe addition of many buckets may seem overwhelming, but Android makes use of a robust resource qualification system to specify how a particular resource can be used. \n\nBelow, you see an example of resource qualifiers for images. In an Android project, we have a `res` folder, which is where we store any resources that the app is going to use. This includes images, but also our layout files, as well as a few other project resources. \n\n![](/images/issue-11/image00.png)\n\nHere, `ic_launcher.png` is duplicated in the following three folders: `drawable-hdpi`, `drawable-mdpi`, and `drawable-xhdpi`. We can ask for the image named `ic_launcher` and the system will automatically choose the appropriate image at runtime, depending on the device configuration.\n\nThis allows us to optimize these images for multiple screen sizes but can be somewhat wasteful since the image will be duplicated multiple times. \n\nThese screen density buckets are fuzzy qualifiers. If you’re using a device with an `xxhdpi` screen in the example above, the system will automatically choose the `xhdpi` version of the image and scale the image for your screen density. This feature allows us to create one version of our images and optimize them for other screen densities as needed. \n\nA common pattern is to supply a high density image and allow Android to downscale that image for devices with a lower screen density. \n\n## DIPs\n\nOne final adjustment to consider for screen density variation is specification of exact dimensions in your layout files. Imagine that you want to supply padding to the outside of a screen in your app. How can we specify dimension values that also scale relative to the device’s screen density?\n\nWell, iOS developers would specify this padding in point values. On a non-retina device, the raw pixel value would be used, and on retina devices, the system will automatically double that pixel size. \n\nOn Android, you can specify this padding in raw pixel values as well, but those values will not scale on devices with high-density screens. Instead, Android developers specify dimension units in density-independent pixels (typically called dip, or dp units). These units will scale relative to the device's density in the same way that iOS automatically performs the scaling. \n\n## Device Category\n\nA final detail to consider is how device categories are managed on Android. Note that iOS has two distinct categories: iPhone and iPad. However, Android is very different, as it has a spectrum of device categories, and the distinction between a phone and tablet can be arbitrary. The resource qualification system mentioned earlier is used heavily to support this spectrum of screen sizes. \n\nFor simple screens, padding can be adjusted around content, based on the size of the device. For example, let’s examine dimension resources. We can define a dimension value in a common location and reference it in our layout files: \n\n```xml\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:layout_margin=\"@dimen/container_margin\" >\n    ...\n</LinearLayout>\n```\n\nNotice the `@dimen/container_margin` value. This refers to a named value stored in the resources system. We can define a base margin-dimension value that is used by default:\n\nIn `res/values/dimens.xml`:\n\n```xml\n<resouces>\n    <dimen name=”container_margin”>4dp</dimen>\n</resources>\n```\n\nThen, we create a qualified version of this padding for tablets:\n\nIn `res/values-w600dp/dimens.xml`:\n\n```xml\n<resouces>\n   <dimen name=”container_margin”>96dp</dimen>\n</resources>\n```\n\nNow, on devices that have a minimum width of 600 dp units, the larger container margin value will be selected by the system. This additional margin will tweak our user interface so that the app is not just a stretched-out version of the application that looks great on phones.\n\n## Split Views\n\nThe above dimension example is a useful tool for some situations, but it is often the case that an application will become more useful on a tablet because there is more space for additional application components. \n\nA common pattern in universal iOS applications is the use of a split view controller. UISplitViewController allows you to host two view controllers that would typically each be visible by themselves in a single screen of your app on an iPad. \n\nOn Android, we have a similar system, but with more control and additional options for expansion. Core pieces of your application can be abstracted into reusable components called fragments, which are similar to view controllers in iOS. All of the controller logic for a single screen of your application can be specified in a fragment. When on a phone, we present one fragment to the user. When on a tablet, we can present two (or more) fragments to the user. \n\nWe can rely again on the resource qualification system to supply a distinct layout file for phones and tablets, which will allow us to host two fragments on a tablet and one on a phone. \n\nFor example, the layout file defined below is intended to be used on phone devices:\n\nIn `res/layout/activity_home.xml`:\n\n```xml\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<FrameLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    android:id=\"@+id/container\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\" />\n```\n\nThis `FrameLayout` defined by the `container` ID will contain the master view for our application and will host the view for our master fragment. \n\nWe can create a qualified version of this same file for tablet devices: \n\nIn `res/layout-sw600dp/activity_home.xml`:\n\n```xml\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    android:orientation=\"horizontal\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\">\n        <FrameLayout \n        android:id=\"@+id/container\"\n        android:layout_width=\"250dp\"\n        android:layout_height=\"match_parent\" />\n        <FrameLayout \n        android:id=\"@+id/detail_container\"\n        android:layout_weight=\"1\"\n        android:layout_width=\"0dp\"\n        android:layout_height=\"match_parent\" />\n    </LinearLayout>\n```\n\nNow, when we use the activity_home layout file on a tablet, we will have two panes in our layout instead of one, which means we can host two fragment views. We can now display the master and the detail view in the same screen with very little code modification. At runtime, the system will decide which version of the layout file to use based on the configuration of the device. \n\n## Conclusion\n\nWith the exception of the `sw600dp` resource qualifier, all of the tools in this article are available on any Android device that you would support. There is an older and less granular resource qualifier that existed prior to the addition of `sw600dp`, available for those older devices. \n\nAs described above, Android developers have the tools needed to optimize for any type of device. You will see some Android applications that don’t adapt very well to many devices (I wouldn’t call this uncommon). I want to stress that shoehorning a design from an existing platform just won’t work on Android. I challenge you to rethink your design and provide a delightful experience for your users.\n"
  },
  {
    "path": "2014-04-01-sqlite-database-support-in-android.md",
    "content": "---\ntitle:  \"SQLite Database Support in Android\"\ncategory: \"11\"\ndate: \"2014-04-01 07:00:00\"\ntags: article\nauthor:\n  - name: James Kelso\n    url: https://twitter.com/jwkelso\n---\n\n\n## Out of the Box\n\nMost of us are familiar with at least some of the persistence features Core Data offers us out of the box. Unfortunately, many of those things aren't automatic on the Android platform. For instance, Core Data abstracts away most of the SQL syntax and database normalization concerns facing database engineers every day. Since Android only provides a thin client to SQLite, you'll still need to write SQL and ensure your database tables are appropriately normalized.\n\nCore Data allows us to think in terms of objects. In fact, it handles marshaling and unmarshaling objects automatically. It manages to perform very well on mobile devices because it provides record-level caching. It doesn't create a separate instance of an object each time the same piece of data is requested from the store. Observation of changes to an object are possible without requiring a refresh each time the object is inspected. \n\nThis isn't the case for Android. You are completely responsible for writing objects into and reading them from the database. This means you must also implement object caching (if desired), manage object instantiation, and manually perform dirty checking of any objects already in existence.\n\nWith Android, you'll need to watch out for version-specific functionality. Different versions of Android ship with different implementations of SQLite. This means the exact same database instructions may give wildly different results across platform versions. A query may perform much differently based on which version of SQLite is executing it.\n\n## Bridging the Gap\n\nMany Android developers come from the enterprise world. For many years, [object-relational mapping](http://en.wikipedia.org/wiki/Object-relational_mapping) libraries have been available on server platforms to ease the pain of interfacing with databases. Sadly, these libraries are much too performance intensive to be used out of the box in a mobile setting. Recognizing this, a few developers set out to solve this issue by creating mobile-friendly ORM libraries.\n\nOne popular option for adding ORM support to SQLite on Android is [OrmLite](http://ormlite.com). OrmLite proffers automatic marshaling and unmarshaling of your persistent objects. It removes the need to write most SQL and provides a programmatic interface for querying, updating, and deleting objects. Another option in the ORM arena is [greenDAO](http://greendao-orm.com). It provides many of the same features as OrmLite, but promises better performance (according to its [website](http://greendao-orm.com/features/#performance)) at the cost of functionality, such as annotation-based setup.\n\nA common complaint about third-party libraries is the extra layer of complexity and performance bloat they can add to a project. One developer felt this pain and decided to write [Cupboard](https://bitbucket.org/qbusict/cupboard), a thin wrapper around the Android SQLite framework. Its stated goals are to provide persistence of Java objects without using ContentValues and parsing Cursors, to be simple and lightweight, and to integrate with core Android classes without any hassle. You'll still need to manage creation of the database, but querying objects becomes a lot simpler.\n\nAnother developer decided to scrap SQLite entirely and created [Perst](http://www.mcobject.com/perst). It was designed from the beginning to interface with object-oriented languages. It's good at marshaling and unmarshaling objects and performs well in benchmarks. The concern for a solution like this is the fact that it's completely replacing a portion of the Android framework. This means you wouldn't be able to replace it in the future with a different solution.\n\nWith these options and many more available, why would anyone choose to develop with the plain vanilla Android database framework? Well, frameworks and wrappers can sometimes introduce more problems than they solve. For instance, in one project, we were simultaneously writing to the database and instantiating so many objects that it caused our ORM library to slow to a crawl. It wasn't designed to handle the kind of punishment we were putting it through. \n\nWhen evaluating frameworks and libraries, check to see whether they make use of Java reflection. Reflection in Java is comparatively expensive and should be used judiciously. Additionally, if your project is pre-Ice Cream Sandwich, evaluate whether your library is using Java annotations. A recently fixed [bug](https://code.google.com/p/android/issues/detail?id=7811) was present in the runtime that caused annotations to be a drag on performance. \n\nFinally, evaluate whether the addition of a framework will significantly increase the complexity level of your project. If you collaborate with other developers, remember that they'll have to work to learn the complexities of the library. It's extremely important to understand how stock Android handles data persistence before you decide whether or not to use a third-party solution.\n\n### Opening the Database\n\nAndroid has made creating and opening a database relatively easy. It provides this through the [SQLiteOpenHelper](http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html) class, which you must subclass. In the default constructor, you'll specify a database name. If a file with the specified name already exists, it's opened. If not, it's created. An application may have any number of separate database files. Each should be represented by a separate subclass of `SQLiteOpenHelper`.\n\nDatabase files are private to your application. They are stored in a subfolder of your application's section of the file system, and are protected by Linux file system permissions. Regrettably, the database files aren't encrypted.\n\nCreating the database file isn't enough, though. In your `SQLiteOpenHelper` subclass, you'll have to override the `onCreate()` method to execute an SQL statement to create your database tables, views, and anything else in your database schema. You can override other methods such as `onConfigure()` to enable/disable database features like write-ahead logging or foreign key support.\n\n### Changing the Schema\n\nIn addition to specifying database name in the constructor of your `SQLiteOpenHelper` subclass, you'll need to specify a database version number. This version number must be constant for any given release, and it's required by the framework to be monotonically increasing.\n\n`SQLiteOpenHelper` will use the version number of your database to decide if it needs to be upgraded or downgraded. In the hooks for upgrade or downgrade, you'll use the provided `oldVersion` and `newVersion` arguments to determine which [ALTER](http://www.w3schools.com/sql/sql_alter.asp) statements need to be run to update your schema. It's good practice to provide a separate statement for each new database version, in order to handle upgrading across multiple database versions at the same time.\n\n### Connecting to the Database\n\nDatabase queries are managed by the  [SQLiteDatabase](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html) class. Calling `getReadableDatabase()` or `getWritableDatabase()` on your `SQLiteOpenHelper` subclass will return an instance of `SQLiteDatabase`. Note that both of these methods usually return the exact same object. The only exception is `getReadableDatabase()`, which will return a read-only database if there's a problem, such as a full disk, that would prevent writing to the database. Since disk problems are a rare occurrence, some developers only call `getWritableDatabase()` in their implementation. \n\nDatabase creation and database schema changes are lazy and don't occur until you obtain an `SQLiteDatabase` instance for the first time. Because of this, it's important you never request an instance of `SQLiteDatabase` on the main thread. Your `SQLiteOpenHelper` subclass will almost always return the exact same instance of `SQLiteDatabase`. This means a call to `SQLiteDatabase.close()` on any thread will close all `SQLiteDatabase` instances throughout your application. This can cause a number of difficult-to-diagnose bugs. In fact, some developers choose to open their `SQLiteDatabase` during application startup and only call `close()` when the application terminates.\n\n### Querying the Data\n\n`SQLiteDatabase` provides methods for querying, inserting, updating, and deleting from your database. For simple queries, this means you don't have to write any SQL. For more advanced queries, though, you'll find yourself writing SQL. SQLiteDatabase exposes `rawQuery()` and `execSQL()` methods, which take raw SQL as an argument to perform advanced queries, such as unions and joins. You can use an [SQLiteQueryBuilder](http://developer.android.com/reference/android/database/sqlite/SQLiteQueryBuilder.html) to assist in constructing the appropriate queries.\n\nBoth `query()` and `rawQuery()` return [Cursor](http://developer.android.com/reference/android/database/Cursor.html) objects. It's tempting to keep references to your Cursor objects and pass them around your application, but Cursor objects take many more system resources to keep around than a Plain Old Java Object (POJO). Because of this, Cursor objects should be unmarshaled into POJOs as soon as possible. After they are unmarshaled, you should call the `close()` method to free up the resources.\n\nDatabase transactions are supported in SQLite. You can start a transaction by calling `SQLiteDatabase.beginTransaction()`. Transactions can be nested by calling `beginTransaction()` while inside a transaction. When the outer transaction has ended, all work done in the transaction and all the nested transactions will be committed or rolled back. Changes are rolled back if any transaction ends without being marked as clean, using `setTransactionSuccessful()`.\n\n### Data Access Objects\n\nAs mentioned earlier, Android doesn't provide any method of marshaling or unmarshaling objects. This means we are responsible for writing the logic to take data from a Cursor to a POJO. This logic should be encapsulated by a [Data Access Object](http://en.wikipedia.org/wiki/Data_access_object) (DAO).\n\nThe DAO pattern is very familiar to practitioners of Java and, by extension, Android developers. Its main purpose is to abstract the application's interaction with the persistence layer without exposing the details of how persistence is implemented. This insulates the application from database schema changes. It also makes moving to a third-party database library less risky to the core application logic. All of your application's interaction with the database should be performed through a DAO.\n\n### Loading Data Asynchronously\n\nAcquiring a reference to `SQLiteDatabase` can be an expensive operation and should never be performed on the main thread. By extension, database queries shouldn't be performed on the main thread either. To assist with this, Android provides [Loaders](http://developer.android.com/guide/components/loaders.html). They allow an activity or a fragment to load data asynchronously. Loaders solve the issue of data persistence across configuration changes, and also monitor the data source to deliver new results when content changes. Android provides the [CursorLoader](http://developer.android.com/reference/android/content/CursorLoader.html) to provide for loading data from a database.\n\n### Sharing Data with Other Applications\n\nThe database is private to the application that created it. Android, however, provides a method of sharing data with other applications. [Content Providers](http://developer.android.com/guide/topics/providers/content-providers.html) provide a structured interface with which other applications can read and possibly even modify your data. Much like `SQLiteDatabase`, Content Providers expose methods such as `query()`, `insert()`, `update()`, and `delete()` to work with the data. Data are returned in the form of a `Cursor`, and access to the Content Provider is synchronized by default to make access thread-safe.\n\n## Conclusion\n\nAndroid databases are much more implementation-heavy than their iOS counterparts. It's important, though, to avoid using a third-party library solely to avoid the boilerplate. A thorough understanding of the Android database framework will guide you in your choice of whether or not to use a third-party library, and if so, which library to choose. The [Android Developer Site](http://developer.android.com) provides two sample projects for working with SQLite databases. Check out the [NotePad](http://developer.android.com/resources/samples/NotePad/index.html) and [SearchableDictionary](http://developer.android.com/resources/samples/SearchableDictionary/index.html) projects for more information.\n"
  },
  {
    "path": "2014-05-08-animating-custom-layer-properties.md",
    "content": "---\ntitle:  \"Animating Custom Layer Properties\"\ncategory: \"12\"\ndate: \"2014-05-08 10:00:00\"\ntags: article\nauthor:\n  - name: Nick Lockwood\n    url: http://twitter.com/nicklockwood\n---\n\nBy default, almost every standard property of `CALayer` and its subclasses can be animated, either by adding a `CAAnimation` to the layer (explicit animation), or by specifying an action for the property and then modifying it (implicit animation).\n\nBut sometimes we may wish to animate several properties in concert as if they were a single animation, or we may need to perform an animation that cannot be implemented by applying animations to standard layer properties.\n\nIn this article, we will discuss how to subclass `CALayer` and add our own properties to easily create animations that would be cumbersome to perform any other way.\n\nGenerally speaking, there are three types of animatable property that we might wish to add to a subclass of `CALayer`:\n\n* A property that indirectly animates one or more standard properties of the layer (or one of its sublayers).\n* A property that triggers redrawing of the layer's backing image (the `contents` property).\n* A property that doesn't involve redrawing the layer or animating any existing properties.\n\n## Indirect Property Animation\n\nCustom properties that indirectly modify other standard layer properties are the simplest of these options. These are really just custom setter methods that convert their input into one or more different values suitable for creating the animation.\n\nWe don't actually need to write any animation code at all if the properties we are setting already have standard animation actions set up, because if we modify those properties, they will inherit whatever animation settings are configured in the current `CATransaction`, and will animate automatically.\n\nIn other words, even if `CALayer` doesn't know how to animate our custom property, it can already animate all of the visible side effects that are caused by changing our property, and that's all we care about.\n\nTo demonstrate this approach, let's create a simple analog clock where we can set the time using a `time` property of type `NSDate`. We'll start by creating our static clock face. The clock consists of three `CAShapeLayer` instances -- a circular layer for the face and two rectangular sublayers for the hour and minute hands:\n\n```objc\n@interface ClockFace: CAShapeLayer\n\n@property (nonatomic, strong) NSDate *time;\n\n@end\n\n@interface ClockFace ()\n\n//private properties\n@property (nonatomic, strong) CAShapeLayer *hourHand;\n@property (nonatomic, strong) CAShapeLayer *minuteHand;\n\n@end\n\n@implementation ClockFace\n\n- (id)init\n{\n    if ((self = [super init]))\n    {\n        self.bounds = CGRectMake(0, 0, 200, 200);\n        self.path = [UIBezierPath bezierPathWithOvalInRect:self.bounds].CGPath;\n        self.fillColor = [UIColor whiteColor].CGColor;\n        self.strokeColor = [UIColor blackColor].CGColor;\n        self.lineWidth = 4;\n        \n        self.hourHand = [CAShapeLayer layer];\n        self.hourHand.path = [UIBezierPath bezierPathWithRect:CGRectMake(-2, -70, 4, 70)].CGPath;\n        self.hourHand.fillColor = [UIColor blackColor].CGColor;\n        self.hourHand.position = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);\n        [self addSublayer:self.hourHand];\n        \n        self.minuteHand = [CAShapeLayer layer];\n        self.minuteHand.path = [UIBezierPath bezierPathWithRect:CGRectMake(-1, -90, 2, 90)].CGPath;\n        self.minuteHand.fillColor = [UIColor blackColor].CGColor;\n        self.minuteHand.position = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);\n        [self addSublayer:self.minuteHand];\n    }\n    return self;\n}\n      \n@end\n```\n\nWe'll also set up a basic view controller with a `UIDatePicker` so we can test our layer (the date picker itself is set up in the Storyboard):\n\n```objc\n@interface ViewController ()\n\n@property (nonatomic, weak) IBOutlet UIDatePicker *datePicker;\n@property (nonatomic, strong) ClockFace *clockFace;\n\n@end\n\n\n@implementation ViewController\n\n- (void)viewDidLoad\n{\n    [super viewDidLoad];\n    \n    //add clock face layer\n    self.clockFace = [[ClockFace alloc] init];\n    self.clockFace.position = CGPointMake(self.view.bounds.size.width / 2, 150);\n    [self.view.layer addSublayer:self.clockFace];\n    \n    //set default time\n    self.clockFace.time = [NSDate date];\n}\n\n- (IBAction)setTime\n{\n    self.clockFace.time = self.datePicker.date;\n}\n\n@end\n```\n\nNow we just need to implement the setter method for our `time` property. This method uses `NSCalendar` to break the time down into hours and minutes, which we then convert into angular coordinates. We then use these angles to generate a `CGAffineTransform` to rotate the hands:\n\n```objc\n- (void)setTime:(NSDate *)time\n{\n    _time = time;\n    \n    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];\n    NSDateComponents *components = [calendar components:NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:time];\n    self.hourHand.affineTransform = CGAffineTransformMakeRotation(components.hour / 12.0 * 2.0 * M_PI);\n    self.minuteHand.affineTransform = CGAffineTransformMakeRotation(components.minute / 60.0 * 2.0 * M_PI);\n}\n```\n\nThe result looks like this:\n\n![](/images/issue-12/clock.gif)\n\nYou can check out the project for yourself [on GitHub](https://github.com/objcio/issue-12-custom-layer-property-animations).\n\nAs you can see, this is really not doing anything clever; we are not actually creating a new animated property, but merely setting several standard animatable layer properties from a single method. So what if we want to create an animation that doesn't map to any existing layer properties?\n\n## Animating Layer Contents\n\nSuppose that instead of implementing our clock face using individual layers, we wanted to draw the clock using Core Graphics. (In general this will have inferior performance, but it's possible to imagine that there are complex drawing operations that we might want to implement that would be hard to replicate using ordinary layer properties and transforms.) How would we do that?\n\nMuch like `NSManagedObject`, `CALayer` has the ability to generate dynamic setters and getters for any declared property. In our current implementation, we've allowed the compiler to synthesize the `time` property's ivar and getter method for us, and we've provided our own implementation for the setter method. But let's change that now by getting rid of our setter and marking the property as `@dynamic`. We'll also get rid of the individual hand layers since we'll now be drawing those ourselves:\n\n```objc\n@interface ClockFace ()\n\n@end\n\n\n@implementation ClockFace\n\n@dynamic time;\n\n- (id)init\n{\n    if ((self = [super init]))\n    {\n        self.bounds = CGRectMake(0, 0, 200, 200);\n    }\n    return self;\n}\n\n@end\n```\n\nBefore we do anything else, we need to make one other slight adjustment: Unfortunately, `CALayer` doesn't know how to interpolate `NSDate` properties (i.e. it cannot automatically generate intermediate values between `NSDate` instances, as it can with numeric types and others such as `CGColor` and `CGAffineTransform`). We could keep our custom setter method and have it set another dynamic property representing the equivalent `NSTimeInterval` (which is a numeric value, and can be interpolated), but to keep the example simple, we'll replace our `NSDate` property with a floating-point value that represents hours on the clock, and update the user interface so it uses a simple `UITextField` to set the value instead of a date picker:\n\n```objc\n@interface ViewController () <UITextFieldDelegate>\n\n@property (nonatomic, strong) IBOutlet UITextField *textField;\n@property (nonatomic, strong) ClockFace *clockFace;\n\n@end\n\n\n@implementation ViewController\n\n- (void)viewDidLoad\n{\n    [super viewDidLoad];\n    \n    //add clock face layer\n    self.clockFace = [[ClockFace alloc] init];\n    self.clockFace.position = CGPointMake(self.view.bounds.size.width / 2, 150);\n    [self.view.layer addSublayer:self.clockFace];\n}\n\n- (BOOL)textFieldShouldReturn:(UITextField *)textField\n{\n    [textField resignFirstResponder];\n    return YES;\n}\n\n- (void)textFieldDidEndEditing:(UITextField *)textField\n{\n    self.clockFace.time = [textField.text floatValue];\n}\n\n@end\n```\n\nNow that we've removed our custom setter method, how are we going to know when our `time` property changes? We need a way to automatically notify the `CALayer` whenever the `time` property changes, so that it can redraw its contents. We do that by overriding the `+needsDisplayForKey:` method, as follows:\n\n```objc\n+ (BOOL)needsDisplayForKey:(NSString *)key\n{\n    if ([@\"time\" isEqualToString:key])\n    {\n        return YES;\n    }\n    return [super needsDisplayForKey:key];\n}\n```\n\nThis tells the layer that whenever the `time` property is modified, it needs to call the `-display` method. We'll now override the `-display` method as well, and add an `NSLog` statement to print out the value of `time`:\n\n```objc\n- (void)display\n{\n    NSLog(@\"time: %f\", self.time);\n}\n```\n\nIf we set the `time` property to 1.5, we'll see that display is called with the new value:\n\n```objc\n2014-04-28 22:37:04.253 ClockFace[49145:60b] time: 1.500000\n```\n\nThat isn't really what we want though; we want the `time` property to animate smoothly between its old and new values over several frames. To make that happen, we need to specify an animation (or \"action\") for our time property, which we can do by overriding the `-actionForKey:` method:\n\n```objc\n- (id<CAAction>)actionForKey:(NSString *)key\n{\n    if ([key isEqualToString:@\"time\"])\n    {\n        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:key];\n        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];\n        animation.fromValue = @(self.time);\n        return animation;\n    }\n    return [super actionForKey:key];\n}\n```\n\nNow, if we set the `time` property again, we see that `-display` is called multiple times. The number of times should equate to approximately 60 times per second, for the duration of the animation (which defaults to 0.25 seconds, or about 15 frames):\n\n```\n2014-04-28 22:37:04.253 ClockFace[49145:60b] time: 1.500000\n2014-04-28 22:37:04.255 ClockFace[49145:60b] time: 1.500000\n2014-04-28 22:37:04.351 ClockFace[49145:60b] time: 1.500000\n2014-04-28 22:37:04.370 ClockFace[49145:60b] time: 1.500000\n2014-04-28 22:37:04.388 ClockFace[49145:60b] time: 1.500000\n2014-04-28 22:37:04.407 ClockFace[49145:60b] time: 1.500000\n2014-04-28 22:37:04.425 ClockFace[49145:60b] time: 1.500000\n2014-04-28 22:37:04.443 ClockFace[49145:60b] time: 1.500000\n2014-04-28 22:37:04.461 ClockFace[49145:60b] time: 1.500000\n2014-04-28 22:37:04.479 ClockFace[49145:60b] time: 1.500000\n2014-04-28 22:37:04.497 ClockFace[49145:60b] time: 1.500000\n2014-04-28 22:37:04.515 ClockFace[49145:60b] time: 1.500000\n2014-04-28 22:37:04.755 ClockFace[49145:60b] time: 1.500000\n```\n\nBut for some reason when we log the `time` value at each of these intermediate points, we are still seeing the final value. Why aren't we getting the interpolated values? The reason is that we are looking at the wrong `time` property.\n\nWhen you set a property of a `CALayer`, you are really setting the value of the *model* layer -- the layer that represents the final state of the layer when any ongoing animations have finished. If you ask the model layer for its values, it will always tell you the last value that it was set to.\n\nBut attached to the model layer is the *presentation* layer -- a copy of the model layer with values that represent the *current*, mid-animation state. If we modify our `-display` method to log the `time` property of the layer's `presentationLayer`, we will see the interpolated values we were expecting. (We'll also use the `presentationLayer`'s `time` property to get the starting value for our animation action, instead of `self.time`):\n\n```objc\n- (id<CAAction>)actionForKey:(NSString *)key\n{\n    if ([key isEqualToString:@\"time\"])\n    {\n        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:key];\n        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];\n        animation.fromValue = @([[self presentationLayer] time]);\n        return animation;\n    }\n    return [super actionForKey:key];\n}\n\n- (void)display\n{\n    NSLog(@\"time: %f\", [[self presentationLayer] time]);\n}\n```\n\nAnd here are the values:\n\n```\n2014-04-28 22:43:31.200 ClockFace[49176:60b] time: 0.000000\n2014-04-28 22:43:31.203 ClockFace[49176:60b] time: 0.002894\n2014-04-28 22:43:31.263 ClockFace[49176:60b] time: 0.363371\n2014-04-28 22:43:31.300 ClockFace[49176:60b] time: 0.586421\n2014-04-28 22:43:31.318 ClockFace[49176:60b] time: 0.695179\n2014-04-28 22:43:31.336 ClockFace[49176:60b] time: 0.803713\n2014-04-28 22:43:31.354 ClockFace[49176:60b] time: 0.912598\n2014-04-28 22:43:31.372 ClockFace[49176:60b] time: 1.021573\n2014-04-28 22:43:31.391 ClockFace[49176:60b] time: 1.134173\n2014-04-28 22:43:31.409 ClockFace[49176:60b] time: 1.242892\n2014-04-28 22:43:31.427 ClockFace[49176:60b] time: 1.352016\n2014-04-28 22:43:31.446 ClockFace[49176:60b] time: 1.460729\n2014-04-28 22:43:31.464 ClockFace[49176:60b] time: 1.500000\n2014-04-28 22:43:31.636 ClockFace[49176:60b] time: 1.500000\n```\n\nSo now, all we have to do is draw our clock. We do this by using ordinary Core Graphics functions to draw to a Graphics Context, and then set the resultant image as our layer's `contents`. Here is the updated `-display` method:\n\n```objc\n- (void)display\n{\n    //get interpolated time value\n    float time = [self.presentationLayer time];\n    \n    //create drawing context\n    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);\n    CGContextRef ctx = UIGraphicsGetCurrentContext();\n    \n    //draw clock face\n    CGContextSetLineWidth(ctx, 4);\n    CGContextStrokeEllipseInRect(ctx, CGRectInset(self.bounds, 2, 2));\n    \n    //draw hour hand\n    CGFloat angle = time / 12.0 * 2.0 * M_PI;\n    CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);\n    CGContextSetLineWidth(ctx, 4);\n    CGContextMoveToPoint(ctx, center.x, center.y);\n    CGContextAddLineToPoint(ctx, center.x + sin(angle) * 80, center.y - cos(angle) * 80);\n    CGContextStrokePath(ctx);\n    \n    //draw minute hand\n    angle = (time - floor(time)) * 2.0 * M_PI;\n    CGContextSetLineWidth(ctx, 2);\n    CGContextMoveToPoint(ctx, center.x, center.y);\n    CGContextAddLineToPoint(ctx, center.x + sin(angle) * 90, center.y - cos(angle) * 90);\n    CGContextStrokePath(ctx);\n    \n    //set backing image\n    self.contents = (id)UIGraphicsGetImageFromCurrentImageContext().CGImage;\n    UIGraphicsEndImageContext();\n}\n```\n\nThe result looks like this:\n\n![](/images/issue-12/clock2.gif)\n\nAs you can see, unlike the first clock animation, the minute hand actually cycles through a full revolution for each hour that the hour hand moves (like a real clock would), instead of just moving to its final position via the shortest path. That's an advantage of animating in this way; because we are animating the `time` value itself instead of just the positions of the hands, the contextual information is preserved. \n\nDrawing the clock in this way is not ideal because Core Graphics functions are not hardware accelerated, and may cause the frame rate of our animation to drop. An alternative to redrawing the `contents` image 60 times per second would be to store a number of pre-drawn images in an array and simply select the correct image based on the interpolated value. The code to do that might look like this:\n\n```objc\nconst NSInteger hoursOnAClockFace = 12;\n\n- (void)display\n{\n    //get interpolated time value\n    float time = [self.presentationLayer time] / hoursOnAClockFace;\n    \n    //fetch frame from a previously defined array of images\n    NSInteger numberOfFrames = [self.frames count];\n    NSInteger index = round(time * numberOfFrames) % numberOfFrames;\n    UIImage *frame = self.frames[index];\n    self.contents = (id)frame.CGImage;\n}\n```\n\nThis improves animation performance by avoiding the need for costly software drawing during each frame, but the tradeoff is that we need to store all of the pre-drawn animation frame images in memory, which -- for a complex animation -- might be prohibitively wasteful of RAM.\n    \nBut this raises an interesting possibility. What happens if we don't update the `contents` image in our `-display` method at all? What if we do something else?\n\n## Animating Non-Visual Properties\n\nThere would be no point in updating any other layer property from within `-display`, because we could simply animate any such property directly, as we did in the first clock-face example. But what if we set something else, perhaps something entirely unrelated to the layer?\n\nThe following code uses a `CALayer` combined with `AVAudioPlayer` to create an animated volume control. By tying the volume to a dynamic layer property, we can use Core Animation's property interpolation to smoothly ramp between different volume levels in the same way we might animate any cosmetic property of the layer:\n\n```objc\n@interface AudioLayer : CALayer\n\n- (id)initWithAudioFileURL:(NSURL *)URL;\n\n@property (nonatomic, assign) float volume;\n\n- (void)play;\n- (void)stop;\n- (BOOL)isPlaying;\n\n@end\n\n\n@interface AudioLayer ()\n\n@property (nonatomic, strong) AVAudioPlayer *player;\n\n@end\n\n\n@implementation AudioLayer\n\n@dynamic volume;\n\n- (id)initWithAudioFileURL:(NSURL *)URL\n{\n    if ((self = [self init]))\n    {\n        self.volume = 1.0;\n        self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:URL error:NULL];\n    }\n    return self;\n}\n\n- (void)play\n{\n    [self.player play];\n}\n\n- (void)stop\n{\n    [self.player stop];\n}\n\n- (BOOL)isPlaying\n{\n    return self.player.playing;\n}\n\n+ (BOOL)needsDisplayForKey:(NSString *)key\n{\n    if ([@\"volume\" isEqualToString:key])\n    {\n        return YES;\n    }\n    return [super needsDisplayForKey:key];\n}\n\n- (id<CAAction>)actionForKey:(NSString *)key\n{\n    if ([key isEqualToString:@\"volume\"])\n    {\n        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:key];\n        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];\n        animation.fromValue = @([[self presentationLayer] volume]);\n        return animation;\n    }\n    return [super actionForKey:key];\n}\n\n- (void)display\n{\n    //set audio volume to interpolated volume value\n    self.player.volume = [self.presentationLayer volume];\n}\n\n@end\n```\n\nWe can test this using a simple view controller with play, stop, volume up, and volume down buttons:\n\n```objc\n@interface ViewController ()\n\n@property (nonatomic, strong) AudioLayer *audioLayer;\n\n@end\n\n\n@implementation ViewController\n\n- (void)viewDidLoad\n{\n    [super viewDidLoad];\n    \n    NSURL *musicURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@\"music\" ofType:@\"caf\"]];\n    self.audioLayer = [[AudioLayer alloc] initWithAudioFileURL:musicURL];\n    [self.view.layer addSublayer:self.audioLayer];\n}\n\n- (IBAction)playPauseMusic:(UIButton *)sender\n{\n    if ([self.audioLayer isPlaying])\n    {\n        [self.audioLayer stop];\n        [sender setTitle:@\"Play Music\" forState:UIControlStateNormal];\n    }\n    else\n    {\n        [self.audioLayer play];\n        [sender setTitle:@\"Pause Music\" forState:UIControlStateNormal];\n    }\n}\n\n- (IBAction)fadeIn\n{\n    self.audioLayer.volume = 1;\n}\n\n- (IBAction)fadeOut\n{\n    self.audioLayer.volume = 0;\n}\n\n@end\n```\n\nNote: even though our layer has no visual appearance, it still needs to be added to the onscreen view hierarchy in order for the animations to work correctly.\n    \n## Conclusion\n    \n`CALayer`'s dynamic properties provide a simple mechanism to implement any sort of animation\u001f-- not just the built-in ones -- and by overriding the `-display` method, we can use those properties to control anything we like, even something like sound volume.\n\nBy using these properties, we not only avoid reinventing the wheel, but we ensure that our custom animations work with the standard animation timing and control functions, and can easily be synchronized with other animated properties.\n"
  },
  {
    "path": "2014-05-08-animations-explained.md",
    "content": "---\ntitle:  \"Animations Explained\"\ncategory: \"12\"\ndate: \"2014-05-08 11:00:00\"\ntags: article\nauthor:\n  - name: Robert Böhnke\n    url: https://twitter.com/dlx\n---\n\nThe applications we write are rarely a static experience, as they adapt to the user's needs and change states to perform a multitude of tasks.\n\nWhen transitioning between these states, it is important to communicate what is going on. Rather than jumping between screens, animations help us explain where the user is coming from and where he or she going.\n\nThe keyboard slides in and out of view to give the illusion that it is a natural part of the phone that was simply hidden below the screen. View controller transitions reinforce the navigational structure of our apps and give the user hints in which direction he or she is moving. Subtle bounces and collisions make interfaces life-like and evoke physical qualities in what is otherwise an environment without visual embellishments.\n\nAnimations are a great way to tell the story of your application, and by understanding the basic principles behind animation, designing them will be a lot easier.\n\n## First Things First\n\nIn this article (and for most of the rest of this issue), we will look at Core Animation specifically. While a lot of what you will see can also be accomplished using higher-level UIKit methods, Core Animation will give you a better understanding of what is going on. It also allows for a more explicit way of describing animations, which is useful for readers of this article, as well as readers of your code.\n\nBefore we can have a look at how animations interact with what we see on the screen, we need to take a quick look at Core Animation's `CALayer`, which is what the animations operate on.\n\nYou probably know that `UIView` instances, as well as layer-backed `NSView`s, modify their `layer` to delegate rendering to the powerful Core Animation framework. However, it is important to understand that animations, when added to a layer, don't modify its properties directly.\n\nInstead, Core Animation maintains two parallel layer hierarchies: the _model layer tree_ and the _presentation layer tree_.[^1] Layers in the former reflect the well-known state of the layers, whereas only layers in the latter approximate the in-flight values of animations.\n\n[^1]: There is actually a third layer tree called the _rendering tree_. Since it's private to Core Animation, we won't cover it here.\n\nConsider adding a fade-out animation to a view. If you, at any point during the animation, inspect the layer's `opacity` value, you most likely won't get an opacity that corresponds to what is onscreen. Instead, you need to inspect the presentation layer to get the correct result.\n\nWhile you may not set properties of the presentation layer directly, it can be useful to use its current values to create new animations or to interact with layers while an animation is taking place.\n\nBy using `-[CALayer presentationLayer]` and `-[CALayer modelLayer]`, you can switch between the two layer hierarchies with ease.\n\n## A Basic Animation\n\nProbably the most common case is to animate a view's property from one value to another. Consider this example:\n\n![](/images/issue-12/rocket-linear@2x.gif)\n\nHere, we animate our little red rocket from an x-position of `77.0` to one of `455.0`, which is just beyond the edge of its parent view. In order to fill in all the steps along the way, we need to determine where our rocket is going to be at any given point in time. This is commonly done using linear interpolation:\n\n![](/images/issue-12/lerp.png)\n\nThat is, for a given fraction of the animation `t`, the x-coordinate of the rocket is the x-coordinate of the starting point `77`, plus the distance to the end point `∆x = 378`, multiplied with said fraction.\n\nUsing `CABasicAnimation`, we can implement this animation as follows:\n\n```objc\nCABasicAnimation *animation = [CABasicAnimation animation];\nanimation.keyPath = @\"position.x\";\nanimation.fromValue = @77;\nanimation.toValue = @455;\nanimation.duration = 1;\n\n[rocket.layer addAnimation:animation forKey:@\"basic\"];\n```\n\nNote that the key path we animate, `position.x`, actually contains a member of the `CGPoint` struct stored in the `position` property. This is a very convenient feature of Core Animation. Make sure to check [the complete list of supported key paths](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreAnimation_guide/Key-ValueCodingExtensions/Key-ValueCodingExtensions.html).\n\nHowever, when we run this code, we realize that our rocket jumps back to its initial position as soon as the animation is complete. This is because, by default, the animation will not modify the presentation layer beyond its duration. In fact, it will even be removed completely at this point.\n\nOnce the animation is removed, the presentation layer will fall back to the values of the model layer, and since we've never modified that layer's `position`, our spaceship reappears right where it started.\n\nThere are two ways to deal with this issue:\n\nThe first approach is to update the property directly on the model layer. _This is the recommended approach_, since it makes the animation completely optional.\n\nOnce the animation completes and is removed from the layer, the presentation layer will fall through to the value that is set on the model, which matches the last step of the animation:\n\n```objc\nCABasicAnimation *animation = [CABasicAnimation animation];\nanimation.keyPath = @\"position.x\";\nanimation.fromValue = @77;\nanimation.toValue = @455;\nanimation.duration = 1;\n\n[rocket.layer addAnimation:animation forKey:@\"basic\"];\n\nrocket.layer.position = CGPointMake(455, 61);\n```\n\nAlternatively, you can tell the animation to remain in its final state by setting its `fillMode` property to ` kCAFillModeForwards` and prevent it from being automatically removed by setting `removedOnCompletion` to `NO`. However, it's a good practice to keep the model and presentation layers in sync, so _this approach should be used carefully_.\n\n[Andy Matuschak also pointed out](https://twitter.com/andy_matuschak/status/464799423785336832), that keeping completed animations around adds additional overhead and may cause the renderer to draw unnecessary frames.\n\n```objc\nCABasicAnimation *animation = [CABasicAnimation animation];\nanimation.keyPath = @\"position.x\";\nanimation.fromValue = @77;\nanimation.toValue = @455;\nanimation.duration = 1;\n\nanimation.fillMode = kCAFillModeForward;\nanimation.removedOnCompletion = NO;\n\n[rectangle.layer addAnimation:animation forKey:@\"basic\"];\n```\n\nIt's worth pointing out that the animation object we create is actually copied as soon as it is added to the layer. This is useful to keep in mind when reusing animations for multiple views. Let's say we have a second rocket that we want to take off shortly after the first one:\n\n```objc\nCABasicAnimation *animation = [CABasicAnimation animation];\nanimation.keyPath = @\"position.x\";\nanimation.byValue = @378;\nanimation.duration = 1;\n\n[rocket1.layer addAnimation:animation forKey:@\"basic\"];\nrocket1.layer.position = CGPointMake(455, 61);\n\nanimation.beginTime = CACurrentMediaTime() + 0.5;\n\n[rocket2.layer addAnimation:animation forKey:@\"basic\"];\nrocket2.layer.position = CGPointMake(455, 111);\n```\n\nSetting the `beginTime` of the animation 0.5 seconds into the future will only affect `rocket2`, since the animation was copied by `[rocket1.layer addAnimation:animation forKey:@\"basic\"];`, and further changes to the animation object are not taken into account by `rocket1`.\n\nCheck out David's [excellent article on animation timing](http://ronnqvi.st/controlling-animation-timing/) to learn how to have even more fine-grained control over your animations.\n\nI've also decided to use `CABasicAnimation`'s `byValue` property, which creates an animation that starts from the current value of the presentation layer and ends at that value plus `byValue`. This makes the animation easier to reuse, since you don't need to specify the precise `from-` and `toValue` that you may not know ahead of time.\n\nDifferent combinations of `fromValue`, `byValue`, and `toValue` can be used to achieve different effects, and it's worth [consulting the documentation](https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CABasicAnimation_class/Introduction/Introduction.html#//apple_ref/doc/uid/TP40004496-CH1-SW4) if you need to create animations that can be reused across your app.\n\n## A Multi-Stage Animation\n\nIt's easy to imagine a situation in which you would want to define more than two steps for your animation, yet instead of chaining multiple `CABasicAnimation` instances, we can use the more generic `CAKeyframeAnimation`.\n\nKeyframes allow us to define an arbitrary number of points during the animation, and then let Core Animation fill in the so-called in-betweens.\n\nLet's say we are working on a log-in form for our next iPhone application and want to shake the form whenever the user enters his or her password incorrectly. Using keyframe animations, this could look a little like so:\n\n![](/images/issue-12/form.gif)\n\n```objc\nCAKeyframeAnimation *animation = [CAKeyframeAnimation animation];\nanimation.keyPath = @\"position.x\";\nanimation.values = @[ @0, @10, @-10, @10, @0 ];\nanimation.keyTimes = @[ @0, @(1 / 6.0), @(3 / 6.0), @(5 / 6.0), @1 ];\nanimation.duration = 0.4;\n\nanimation.additive = YES;\n\n[form.layer addAnimation:animation forKey:@\"shake\"];\n```\n\nThe `values` array defines which positions the form should have.\n\nSetting the `keyTimes` property allows us to specify at which point in time the keyframes occur. They are specified as fractions of the total duration of the keyframe animation.[^2]\n\n[^2]: Note how I chose different values for transitions from 0 to 30 and from 30 to -30 to maintain a constant velocity.\n\nSetting the `additive` property to `YES` tells Core Animation to add the values specified by the animation to the value of the current render tree. This allows us to reuse the same animation for all form elements that need updating without having to know their positions in advance. Since this property is inherited from `CAPropertyAnimation`, you can also make use of it when employing `CABasicAnimation`.\n\n## Animation Along a Path\n\nWhile a simple horizontal shake is not hard to specify in code, animations along complex paths would require us to store a large amount of boxed `CGPoint`s in the keyframe animation's `values` array.  \nThankfully, `CAKeyframeAnimation` offers the more convenient `path` property as an alternative.\n\nFor instance, this is how we would animate a view in a circle:\n\n![](/images/issue-12/planets.gif)\n\n```objc\nCGRect boundingRect = CGRectMake(-150, -150, 300, 300);\n\nCAKeyframeAnimation *orbit = [CAKeyframeAnimation animation];\norbit.keyPath = @\"position\";\norbit.path = CFAutorelease(CGPathCreateWithEllipseInRect(boundingRect, NULL));\norbit.duration = 4;\norbit.additive = YES;\norbit.repeatCount = HUGE_VALF;\norbit.calculationMode = kCAAnimationPaced;\norbit.rotationMode = kCAAnimationRotateAuto;\n\n[satellite.layer addAnimation:orbit forKey:@\"orbit\"];\n```\n\nUsing `CGPathCreateWithEllipseInRect()`, we create a circular `CGPath` that we use as the `path` of our keyframe animation. \n\nUsing `calculationMode` is another way to control the timing of keyframe animations. By setting it to `kCAAnimationPaced`, we let Core Animation apply a constant velocity to the animated object, regardless of how long the individual line segments of our path are.  \nSetting it to `kCAAnimationPaced` also disregards any `keyTimes` we would've set.\n\nSetting the `rotationMode` property to `kCAAnimationRotateAuto` ensures that the satellite follows the rotation along the path. By contrast, this is what the animation would look like had we left the property `nil`:\n\n![](/images/issue-12/planets-incorrect.gif)\n\nYou can achieve a couple of interesting effects using animations with paths;\nfellow objc.io author [Ole Begemann](https://twitter.com/olebegemann) wrote [a great post](http://oleb.net/blog/2010/12/animating-drawing-of-cgpath-with-cashapelayer) about how you can combine path-based animations with `CAShapeLayer` to create cool drawing animations with only a couple of lines of code.\n\n## Timing Functions\n\nLet's look at our first example again:\n\n![](/images/issue-12/rocket-linear@2x.gif)\n\nYou'll notice that there is something very artificial about the animation of our rocket. That is because most movements we see in the real world take time to accelerate or decelerate. Objects that instantly reach their top speed and then stop immediately tend to look very unnatural. Unless you're [dancing the robot](https://www.youtube.com/watch?v=o8HkEprSaAs&t=1m2s), that's rarely a desired effect.\n\nIn order to give our animation an illusion of inertia, we could factor this into our interpolation function that we saw above. However, we then would have to create a new interpolation function for every desired acceleration or deceleration behavior, an approach that would hardly scale.\n\nInstead, it's common practice to decouple the interpolation of the animated properties from the speed of the animation. Thus, speeding up the animation will give us an effect of an accelerating rocket without affecting our interpolation function.\n\nWe can achieve this by introducing a _timing function_ (also sometimes referred to as an easing function). This function controls the speed of the animation by modifying the fraction of the duration:\n\n![](/images/issue-12/lerp-with-easing.png)\n\nThe simplest easing function is _linear_. It maintains a constant speed throughout the animation and is effectively what we see above.\nIn Core Animation, this function is represented by the `CAMediaTimingFunction`\nclass:\n\n![](/images/issue-12/rect-linear@2x.gif)\n\n```objc\nCABasicAnimation *animation = [CABasicAnimation animation];\nanimation.keyPath = @\"position.x\";\nanimation.fromValue = @50;\nanimation.toValue = @150;\nanimation.duration = 1;\n\nanimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];\n\n[rectangle.layer addAnimation:animation forKey:@\"basic\"];\n\nrectangle.layer.position = CGPointMake(150, 0);\n```\n\nCore Animation comes with a number of built-in easing functions beyond linear, such as:\n\n* Ease in (`kCAMediaTimingFunctionEaseIn`):  \n  ![](/images/issue-12/rect-easein@2x.gif)\n* Ease out (`kCAMediaTimingFunctionEaseOut`):  \n  ![](/images/issue-12/rect-easeout@2x.gif)\n* Ease in ease out (`kCAMediaTimingFunctionEaseInEaseOut`):  \n  ![](/images/issue-12/rect-easeineaseout@2x.gif)\n* Default (`kCAMediaTimingFunctionDefault`):  \n  ![](/images/issue-12/rect-default@2x.gif)\n\nIt's also possible, within limits, to create your own easing function using `+functionWithControlPoints::::`.[^3] By passing in the _x_ and _y_ components of two control points of a cubic Bézier curve, you can easily create custom easing functions, such as the one I chose for our little red rocket:\n\n[^3]: This method is infamous for having three nameless parameters, not something that we recommend you make use of in your APIs.\n\n![](/images/issue-12/rocket-custom@2x.gif)\n\n```objc\nCABasicAnimation *animation = [CABasicAnimation animation];\nanimation.keyPath = @\"position.x\";\nanimation.fromValue = @77;\nanimation.toValue = @455;\nanimation.duration = 1;\n\nanimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.5:0:0.9:0.7];\n\n[rocket.layer addAnimation:animation forKey:@\"basic\"];\n\nrocket.layer.position = CGPointMake(150, 0);\n```\n\nWithout going into too much detail on Bézier curves, they are a common technique to create smooth curves in computer graphics. You've probably seen them in vector-based drawing tools such as Sketch or Adobe Illustrator.\n\n![](/images/issue-12/bezier.png)\n\nThe values passed to `+functionWithControlPoints::::` effectively control the position of the handles. The resulting timing function will then adjust the speed of the animation based on the resulting path. The x-axis represents the fraction of the duration, while the y-axis is the input value of the interpolation function.\n\nWhile [the documentation](https://developer.apple.com/library/mac/documentation/cocoa/reference/CAMediaTimingFunction_class/Introduction/Introduction.html#//apple_ref/doc/uid/TP40004505-CH1-DontLinkElementID_1) states that the output range of `+functionWithControlPoints::::` is `[0, 1]`, some readers pointed out that you can actually use negative components to achieve anticipation -- where an animated object swings back before moving to its target -- or overshooting.\n\nSince `CAMediaTimingFunction` is limited to functions that can be expressed as cubic Bézier curves, I wrote a small library, called [RBBAnimation](https://github.com/robb/RBBAnimation), that contains a custom `CAKeyframeAnimation` subclass which allows you to use [more complex easing functions](https://github.com/robb/RBBAnimation#rbbtweenanimation), including bounces:\n\n![](/images/issue-12/bounce@2x.gif)\n\n```objc\nRBBTweenAnimation *animation = [RBBTweenAnimation animation];\nanimation.keyPath = @\"position.y\";\nanimation.fromValue = @50;\nanimation.toValue = @150;\nanimation.duration = 1;\n\nanimation.easing = RBBEasingFunctionEaseOutBounce;\n```\n\n## Animation Groups\n\nFor certain complex effects, it may be necessary to animate multiple properties at once. Imagine we were to implement a shuffle animation when advancing to a random track in a media player app, it could look like this:\n\n![](/images/issue-12/covers.gif)\n\nYou can see that we have to animate the position, rotation and z-position of the artworks at once. Using `CAAnimationGroup`, the code to animate one of the covers could look a little something like this:\n\n```objc\nCABasicAnimation *zPosition = [CABasicAnimation animation];\nzPosition.keyPath = @\"zPosition\";\nzPosition.fromValue = @-1;\nzPosition.toValue = @1;\nzPosition.duration = 1.2;\n\nCAKeyframeAnimation *rotation = [CAKeyframeAnimation animation];\nrotation.keyPath = @\"transform.rotation\";\nrotation.values = @[ @0, @0.14, @0 ];\nrotation.duration = 1.2;\nrotation.timingFunctions = @[\n    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],\n    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]\n];\n\nCAKeyframeAnimation *position = [CAKeyframeAnimation animation];\nposition.keyPath = @\"position\";\nposition.values = @[\n    [NSValue valueWithCGPoint:CGPointZero],\n    [NSValue valueWithCGPoint:CGPointMake(110, -20)],\n    [NSValue valueWithCGPoint:CGPointZero]\n];\nposition.timingFunctions = @[\n    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],\n    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]\n];\nposition.additive = YES;\nposition.duration = 1.2;\n\nCAAnimationGroup *group = [[CAAnimationGroup alloc] init];\ngroup.animations = @[ zPosition, rotation, position ];\ngroup.duration = 1.2;\ngroup.beginTime = 0.5;\n\n[card.layer addAnimation:group forKey:@\"shuffle\"];\n\ncard.layer.zPosition = 1;\n```\n\nOne benefit we get from using `CAAnimationGroup` is being able to expose all animations as a single object. This is useful if you have a factory object that creates animations to be reused at multiple points in your application.\n\nYou can also use the animation group to control the timing of all components at the same time.\n\n## Beyond Core Animation\n\nBy now, you've probably heard of UIKit Dynamics, a physics simulation framework introduced in iOS 7 that allows you to animate views by applying constraints and forces to them. Unlike Core Animation, the interaction with what you see onscreen is more indirect, but its dynamic nature allows you to create animations with outcomes you don't know beforehand.\n\nFacebook recently made [Pop](https://github.com/facebook/pop), the animation engine that powers Paper, open source. Conceptually, it sits somewhere between Core Animation and UIKit Dynamics. It makes prominent use of spring animations, and target values can be manipulated while the animation is running, without having to replace it.\nIt's also available on OS X and allows us to animate arbitrary properties on every `NSObject` subclass.\n\n## Further Reading\n\n- [Core Animation Programming Guide](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html)\n- [12 basic principles of animation](https://en.wikipedia.org/wiki/12_basic_principles_of_animation)\n- [Animating drawing of CGPath with CAShapeLayer](http://oleb.net/blog/2010/12/animating-drawing-of-cgpath-with-cashapelayer)\n- [Controlling animation timing](http://ronnqvi.st/controlling-animation-timing/)\n- [pop](https://github.com/facebook/pop)\n- [RBBAnimation](https://github.com/robb/RBBAnimation)\n"
  },
  {
    "path": "2014-05-08-collectionview-animations.md",
    "content": "---\ntitle:  \"Animating Collection Views\"\ncategory: \"12\"\ndate: \"2014-05-08 07:00:00\"\ntags: article\nauthor:\n  - name: Engin Kurutepe\n    url: https://twitter.com/ekurutepe\n---\n\n`UICollectionView` and the set of associated classes are extremely flexible and powerful. But with this flexibility comes a certain dose of complexity: a collection view is a good deal deeper and more capable than the good old `UITableView`.\n\nIt's so much deeper, in fact, that [Ole Begeman](http://oleb.net) and [Ash Furrow](https://twitter.com/ashfurrow) have written about [Custom Collection View Layouts](/issues/3-views/collection-view-layouts/) and [Collection Views with UIKit Dynamics](/issues/5-ios7/collection-views-and-uidynamics/) in objc.io previously, and I still have something to write about that they have not covered. In this post, I will assume that you're familiar with the basics of collection view layouts and have at least read Apple's excellent [programming guide](https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40012334) and Ole's [post](/issues/3-views/collection-view-layouts/).\n\nThe first section of this article will concentrate on how different classes and methods work together to animate a collection view layout with the help of a few common examples. In the second section, we will look at view controller transitions with collection views and see how to use `useLayoutToLayoutNavigationTransitions` for the cases when it works, and implement custom transitions for the cases when it does not.\n\nThe two example projects for this article are available on GitHub:\n\n- [Layout Animations](https://github.com/objcio/issue-12-CollectionViewAnimations)\n- [Custom Collection View Transitions](https://github.com/objcio/issue-12-CustomCollectionViewTransition)\n\n\n## Collection View Layout Animations\n\nThe standard `UICollectionViewFlowLayout` is very customizable except for its animations; Apple opted for the safe approach and implemented a simple fade animation as default for all layout animations. If you would like to have custom animations, the best way is to subclass the `UICollectionViewFlowLayout` and implement your animations at the appropriate locations. Let's go through a few examples to understand how various methods in your `UICollectionViewFlowLayout` subclasses should work together to deliver custom animations.\n\n### Inserting and Removing Items\n\nIn general, layout attributes are linearly interpolated from the initial state to the final state to compute the collection view animations. However, for the newly inserted or removed items, there are no initial and final attributes to interpolate from. To compute the animations for such cells, the collection view will ask its layout object to provide the initial and final attributes through the `initialLayoutAttributesForAppearingItemAtIndexPath:` and `finalLayoutAttributesForDisappearingItemAtIndexPath:` methods. The default Apple implementation returns the layout attributes corresponding to the normal position at the specific index path, but with an `alpha` value of 0.0, resulting in a fade-in or fade-out animation. If you would like to have something fancier, like having your new cells shoot up from the bottom of the screen and rotate while flying into place, you could implement something like this in your layout subclass:\n\n```objc\n- (UICollectionViewLayoutAttributes*)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath\n{\n    UICollectionViewLayoutAttributes *attr = [self layoutAttributesForItemAtIndexPath:itemIndexPath];\n\n    attr.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(0.2, 0.2), M_PI);\n    attr.center = CGPointMake(CGRectGetMidX(self.collectionView.bounds), CGRectGetMaxY(self.collectionView.bounds));\n\n    return attr;\n}\n```\n\nWhich results in this:\n\n![Insertion and Deletion](/images/issue-12/2014-05-01-collectionview-animations-1-insertion.gif)\n\nThe corresponding `finalLayoutAttributesForDisappearingItemAtIndexPath:` method for the shown animation is very similar, except that it assigns a different transform.\n\n### Responding to Device Rotations\n\nA device orientation change usually results in a bounds change for a collection view. The layout object is asked if the layout should be invalidated and recomputed with the method `shouldInvalidateLayoutForBoundsChange:`. The default implementation in `UICollectionViewFlowLayout` does the correct thing, but if you are subclassing `UICollectionViewLayout` instead, you should return `YES` on a bounds change:\n\n```objc\n- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds\n{\n    CGRect oldBounds = self.collectionView.bounds;\n    if (!CGSizeEqualToSize(oldBounds.size, newBounds.size)) {\n        return YES;\n    }\n    return NO;\n}\n```\n\nDuring the animation of the bounds change, the collection view acts as if the currently displayed items are removed and inserted again in the new bounds, resulting in a series of `finalLayoutAttributesForDisappearingItemAtIndexPath:` and `initialLayoutAttributesForAppearingItemAtIndexPath:` calls for each index path.\n\nIf you implemented some fancy animations for the insertion and deletion of items in the collection view, by now you should be seeing why Apple went with simple fade animations as a sensible default:\n\n![Wrong reaction to device rotation](/images/issue-12/2014-05-01-collectionview-animations-2-wrong-rotation.gif)\n\nOops…\n\nTo prevent such unwanted animations, the sequence of initial position -> removal animation -> insertion animation -> final position must be matched for each item in the collection view, so that they result in a smooth animation. In other words, `finalLayoutAttributesForDisappearingItemAtIndexPath:` and `initialLayoutAttributesForAppearingItemAtIndexPath:` should be able to return different attributes depending on if the item in question is really disappearing or appearing, or if the collection view is going through a bounds change animation.\n\nLuckily, the collection view tells the layout object which kind of animation is about to be performed. It does this by invoking the `prepareForAnimatedBoundsChange:` or `prepareForCollectionViewUpdates:` for bounds changes and item updates respectively. For the purposes of this example, we can use `prepareForCollectionViewUpdates:` to keep track of updated objects:\n\n```objc\n- (void)prepareForCollectionViewUpdates:(NSArray *)updateItems\n{\n    [super prepareForCollectionViewUpdates:updateItems];\n    NSMutableArray *indexPaths = [NSMutableArray array];\n    for (UICollectionViewUpdateItem *updateItem in updateItems) {\n        switch (updateItem.updateAction) {\n            case UICollectionUpdateActionInsert:\n                [indexPaths addObject:updateItem.indexPathAfterUpdate];\n                break;\n            case UICollectionUpdateActionDelete:\n                [indexPaths addObject:updateItem.indexPathBeforeUpdate];\n                break;\n            case UICollectionUpdateActionMove:\n                [indexPaths addObject:updateItem.indexPathBeforeUpdate];\n                [indexPaths addObject:updateItem.indexPathAfterUpdate];\n                break;\n            default:\n                NSLog(@\"unhandled case: %@\", updateItem);\n                break;\n        }\n    }  \n    self.indexPathsToAnimate = indexPaths;\n}\n```\n\nAnd modify our item insertion animation to only shoot the item if it is currently being inserted into the collection view:\n\n```objc\n- (UICollectionViewLayoutAttributes*)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath\n{\n    UICollectionViewLayoutAttributes *attr = [self layoutAttributesForItemAtIndexPath:itemIndexPath];\n\n    if ([_indexPathsToAnimate containsObject:itemIndexPath]) {\n        attr.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(0.2, 0.2), M_PI);\n        attr.center = CGPointMake(CGRectGetMidX(self.collectionView.bounds), CGRectGetMaxY(self.collectionView.bounds));\n        [_indexPathsToAnimate removeObject:itemIndexPath];\n    }\n\n    return attr;\n}\n```\n\nIf the item is not being inserted, the normal attributes as reported by `layoutAttributesForItemAtIndexPath` will be returned, canceling any special appearance animations. Combined with the corresponding logic inside `finalLayoutAttributesForDisappearingItemAtIndexPath:`, this will result in the items smoothly animating from their initial positions to their final positions in the case of a bounds change, creating a simple but cool animation:\n\n![Wrong reaction to device rotation](/images/issue-12/2014-05-01-collectionview-animations-3-correct-rotation.gif)\n\n### Interactive Layout Animations\n\nCollection views make it quite easy to allow the user to interact with the layout using gesture recognizers. As [suggested](https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/IncorporatingGestureSupport/IncorporatingGestureSupport.html#//apple_ref/doc/uid/TP40012334-CH4-SW1) by Apple, the general approach to add interactivity to a collection view layout follows these steps:\n\n1. Create the gesture recognizer\n2. Add the gesture recognizer to the collection view\n3. Handle the recognized gestures to drive the layout animations  \n\nLet's see how we can build something where the user can pinch an item to zoom, and the item returns to original size as soon as the user releases his or her pinch.\n\nOur handler method could look something like this:\n\n```objc\n- (void)handlePinch:(UIPinchGestureRecognizer *)sender {\n    if ([sender numberOfTouches] != 2)\n        return;\n\n\n    if (sender.state == UIGestureRecognizerStateBegan ||\n        sender.state == UIGestureRecognizerStateChanged) {\n        // Get the pinch points.\n        CGPoint p1 = [sender locationOfTouch:0 inView:[self collectionView]];\n        CGPoint p2 = [sender locationOfTouch:1 inView:[self collectionView]];\n\n        // Compute the new spread distance.\n        CGFloat xd = p1.x - p2.x;\n        CGFloat yd = p1.y - p2.y;\n        CGFloat distance = sqrt(xd*xd + yd*yd);\n\n        // Update the custom layout parameter and invalidate.\n        FJAnimatedFlowLayout* layout = (FJAnimatedFlowLayout*)[[self collectionView] collectionViewLayout];\n\n        NSIndexPath *pinchedItem = [self.collectionView indexPathForItemAtPoint:CGPointMake(0.5*(p1.x+p2.x), 0.5*(p1.y+p2.y))];\n        [layout resizeItemAtIndexPath:pinchedItem withPinchDistance:distance];\n        [layout invalidateLayout];\n\n    }\n    else if (sender.state == UIGestureRecognizerStateCancelled ||\n             sender.state == UIGestureRecognizerStateEnded){\n        FJAnimatedFlowLayout* layout = (FJAnimatedFlowLayout*)[[self collectionView] collectionViewLayout];\n        [self.collectionView\n         performBatchUpdates:^{\n            [layout resetPinchedItem];\n         }\n         completion:nil];\n    }\n}\n```\n\nThis pinch handler computes the pinch distance and figures out the pinched item, and tells the layout to update itself while the user is pinching. As soon as the pinch gesture is over, the layout is reset in a batch update to animate the return to the original size.\n\nOur layout, on the other hand, keeps track of the pinched item and the desired size and provides the correct attributes for them when needed:\n\n```objc\n- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect\n{\n    NSArray *attrs = [super layoutAttributesForElementsInRect:rect];\n\n    if (_pinchedItem) {\n        UICollectionViewLayoutAttributes *attr = [[attrs filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@\"indexPath == %@\", _pinchedItem]] firstObject];\n\n        attr.size = _pinchedItemSize;\n        attr.zIndex = 100;\n    }\n    return attrs;\n}\n```\n\n### Summary\n\nWe looked at how to build custom animations in collection view layout by using a few examples. Even though the `UICollectionViewFlowLayout` does not directly allow customization of its animations, it is clearly architected by Apple engineers to be subclassed to implement various custom behavior. Essentially, boundless custom layout and animations can be achieved by correctly reacting to signaling methods such as:\n\n- `prepareLayout`\n- `prepareForCollectionViewUpdates:`\n- `finalizeCollectionViewUpdates`\n- `prepareForAnimatedBoundsChange:`\n- `finalizeAnimatedBoundsChange`\n- `shouldInvalidateLayoutForBoundsChange:`\n\nin your `UICollectionViewLayout` subclass and returning the appropriate attributes from methods which return `UICollectionViewLayoutAttributes`. Even more engaging animations can be achieved by combining these techniques with UIKit Dynamics as introduced in objc.io [issue #5](/issues/5-ios7/collection-views-and-uidynamics/).\n\n## View Controller Transitions with Collection Views\n\nOne of the big improvements in iOS 7 was with the custom view controller transitions, as [Chris](https://twitter.com/chriseidhof) [wrote about](/issues/5-ios7/view-controller-transitions/) in objc.io [issue #5](/issues/5-ios7/). In parallel to the custom transitions, Apple also added the `useLayoutToLayoutNavigationTransitions` flag to `UICollectionViewController` to enable navigation transitions which reuse a single collection view. Apple's own Photos and Calendar apps on iOS represent a great example of what is possible using such transitions.\n\n### Transitions Between UICollectionViewController Instances\n\nLet's look at how we can achieve a similar effect using the same sample project from the previous section:\n\n![Layout to Layout Navigation Transitions](/images/issue-12/2014-05-01-collectionview-animations-4-layout2layout.gif)\n\nIn order for the layout-to-layout transitions to work, the root view controller in the navigation controller must be a collection view controller, where `useLayoutToLayoutNavigationTransitions` is set to `NO`. When another `UICollectionViewController` instance with `useLayoutToLayoutNavigationTransitions` set to `YES` is pushed on top of this root view controller, the navigation controller replaces the standard push animation with a layout transition animation. One important detail to note here is that the root view controller's collection view instance is recycled for the collection view controller instances pushed on the navigation stack, i.e. these collection view controllers don't have their own collection views, and if you try to set any collection view properties in methods like `viewDidLoad`, they will not have any effect and you will not receive any warnings.\n\nProbably the most common gotcha of this behavior is to expect the recycled collection view to update its data source and delegate to reflect the top collection view controller. It does not: the root collection view controller stays the data source and delegate unless we do something about it.\n\nThe workaround for this problem is to implement the navigation controller delegate methods and correctly set the data source and the delegate of the collection view as needed by the current view controller at the top of the navigation stack. In our simple example, this can be achieved by:\n\n```objc\n- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated\n{\n    if ([viewController isKindOfClass:[FJDetailViewController class]]) {\n        FJDetailViewController *dvc = (FJDetailViewController*)viewController;\n        dvc.collectionView.dataSource = dvc;\n        dvc.collectionView.delegate = dvc;\n        [dvc.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_selectedItem inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];\n    }\n    else if (viewController == self){\n        self.collectionView.dataSource = self;\n        self.collectionView.delegate = self;\n    }\n}\n```\n\nWhen the detail collection view is pushed onto the stack, we set the collection view's data source to the detail view controller, which makes sure that only the selected color of cells is shown in the detail collection view. If we were not to do this, the layout would correctly transition but the collection would still be showing all cells. In a real-world app, the detail data source would usually be responsible for showing more detail about the data in such a transition.\n\n### Collection View Layout Animations for General Transitions\n\nThe layout-to-layout navigation transitions using the `useLayoutToLayoutNavigationTransitions` flag are quite useful, but limited to transitions where both view controllers are `UICollectionViewController` instances  only and the transition takes place between their top-level collection views. We need a custom view controller transition in order to achieve a similar transition between arbitrary collection views in arbitrary view controllers.\n\n![Custom Collection View Transition](/images/issue-12/2014-05-01-collectionview-animations-5-custom-transitions.gif)\n\nAn animation controller for such a custom transition could be designed along the following steps:\n\n1. Make snapshots of all visible items in the initial collection view\n2. Add the snapshots to the transitioning context container view\n3. Compute the final positions using the layout of the target collection view\n4. Animate the snapshots to the correct positions\n5. Remove the snapshots while making the target collection view visible\n\nThe downside of such an animator design is two-fold: it can only animate the items visible in the initial collection view, since the [snapshot APIs](https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW198) only work for views already visible on the screen, and depending on the number of visible items, there could be a lot of views to correctly keep track of and to animate. On the other hand, the big advantage of this design would be that it would work for all kinds of `UICollectionViewLayout` combinations. The implementation of such a system is left as an exercise for the reader.\n\nAnother approach, which is implemented in the accompanying demo project, relies on a few quirks of the `UICollectionViewFlowLayout`.\n\nThe basic idea is that both the source and the destination collection views have valid flow layouts and the layout attributes of the source layout could act as the initial layout attributes for the items in the destination collection view to drive the transition animation. Once this is set up, the collection view machinery would take care of keeping track of all items and animate them for us, even if they're not initially visible on the screen. Here is the core of the `animateTransition:` method of our animation controller:\n\n```objc\nCGRect initialRect = [inView.window convertRect:_fromCollectionView.frame fromView:_fromCollectionView.superview];\nCGRect finalRect   = [transitionContext finalFrameForViewController:toVC];\n\nUICollectionViewFlowLayout *toLayout = (UICollectionViewFlowLayout*) _toCollectionView.collectionViewLayout;\n\nUICollectionViewFlowLayout *currentLayout = (UICollectionViewFlowLayout*) _fromCollectionView.collectionViewLayout;\n\n//make a copy of the original layout\nUICollectionViewFlowLayout *currentLayoutCopy = [[UICollectionViewFlowLayout alloc] init];\n\ncurrentLayoutCopy.itemSize = currentLayout.itemSize;\ncurrentLayoutCopy.sectionInset = currentLayout.sectionInset;\ncurrentLayoutCopy.minimumLineSpacing = currentLayout.minimumLineSpacing;\ncurrentLayoutCopy.minimumInteritemSpacing = currentLayout.minimumInteritemSpacing;\ncurrentLayoutCopy.scrollDirection = currentLayout.scrollDirection;\n\n//assign the copy to the source collection view\n[self.fromCollectionView setCollectionViewLayout:currentLayoutCopy animated:NO];\n\nUIEdgeInsets contentInset = _toCollectionView.contentInset;\n\nCGFloat oldBottomInset = contentInset.bottom;\n\n//force a very big bottom inset in the target collection view\ncontentInset.bottom = CGRectGetHeight(finalRect)-(toLayout.itemSize.height+toLayout.sectionInset.bottom+toLayout.sectionInset.top);\nself.toCollectionView.contentInset = contentInset;\n\n//set the source layout for the destination collection view\n[self.toCollectionView setCollectionViewLayout:currentLayout animated:NO];\n\ntoView.frame = initialRect;\n\n[inView insertSubview:toView aboveSubview:fromView];\n\n[UIView\n animateWithDuration:[self transitionDuration:transitionContext]\n delay:0\n options:UIViewAnimationOptionBeginFromCurrentState\n animations:^{\n   //animate to the final frame\n     toView.frame = finalRect;\n     //set the final layout inside performUpdates\n     [_toCollectionView\n      performBatchUpdates:^{\n          [_toCollectionView setCollectionViewLayout:toLayout animated:NO];\n      }\n      completion:^(BOOL finished) {\n          _toCollectionView.contentInset = UIEdgeInsetsMake(contentInset.top,\n                                                            contentInset.left,\n                                                            oldBottomInset,\n                                                            contentInset.right);\n      }];\n\n } completion:^(BOOL finished) {\n     [transitionContext completeTransition:YES];\n }];\n```\n\nFirst, the animation controller makes sure that the destination collection view starts with the exact same frame and layout as the original. Then, it assigns the layout of the source collection view to the destination collection view, making sure that it does not get invalidated. At the same time, the layout is 'copied' into a new layout object, which gets assigned to the original collection view to prevent strange layout bugs when navigating back to the original view controller. We also force a large bottom content inset on the destination collection view to make sure that the layout stays on a single line for the initial positions for the animation. If you look at the logs, you will see the collection view complaining about this temporary condition because the item size plus the insets are larger than the non-scrolling dimension of the collection view. In this state, the behavior of the collection view is not defined, and we are only using this unstable state as the initial state for our transition animation. Finally, the convoluted animation block does its magic by first setting the frame of the destination collection view to its final position, and then performing a non-animated layout change to the final layout inside the updates block of `performBatchUpdates:completion:`, which is followed by the resetting of the content insets to the original values in the completion block.\n\n### In Conclusion\n\nWe looked at two different approaches to achieve layout transitions between collection views. The first method, with the help of the built-in `useLayoutToLayoutNavigationTransitions`, looks quite impressive and is very easy to implement, but is limited in cases where it can be used. For the cases where `useLayoutToLayoutNavigationTransitions` is not applicable, a custom animator is required to drive the transition animation. In this post, we have seen an example of how such an animator could be implemented, however, since your app will almost certainly require a completely different animation between two different view hierarchies, as in this example, don't be reluctant about trying out a different approach and seeing if it works.\n"
  },
  {
    "path": "2014-05-08-custom-container-view-controller-transitions.md",
    "content": "---\ntitle:  \"Custom Container View Controller Transitions\"\ncategory: \"12\"\ndate: \"2014-05-08 09:00:00\"\ntags: article\nauthor:\n  - name: Joachim Bondo\n    url: https://twitter.com/osteslag\n---\n\nIn [issue #5](/issues/5-ios7/), [Chris Eidhof](http://twitter.com/chriseidhof) took us through the new custom [View Controller Transitions](/issues/5-ios7/view-controller-transitions/) in iOS 7. He [concluded](/issues/5-ios7/view-controller-transitions/#conclusion) (emphasis mine):\n\n> We only looked at animating between two view controllers in a navigation controller, but **you can do the same for** tab bar controllers or **your own custom container view controllers**…\n\nWhile it is technically true that you can customize the transition between two view controllers in custom containment, if you're using the iOS 7 API, it is not supported out of the box. Far from. \n\nNote that I am talking about custom container view controllers as direct subclasses of `UIViewController`, not `UITabBarController` or `UINavigationController` subclasses.\n\nThere is no ready-to-use API for your custom container `UIViewController` subclass that allows an arbitrary *animation controller* to automatically conduct the transition from one of your child view controllers to another, interactively or non-interactively. I am tempted to say it was not even Apple’s intention to support it. What is supported are the following transitions:\n\n- Navigation controller pushes and pops\n- Tab bar controller selection changes\n- Modal presentations and dismissals\n\nIn this chapter I will demonstrate how you *can* build a custom container view controller yourself while supporting third-party animation controllers.\n\nIf you need to brush up on view controller containment, introduced in iOS 5, make sure to read [Ricky Gregersen](https://twitter.com/rickigregersen)’s “[View Controller Containment](/issues/1-view-controllers/containment-view-controller/)” in the very [first issue](/issues/1-view-controllers/).\n\n## Before We Begin\n\nYou may ask yourself a question or two at this point, so let me answer them for you:\n\n*Why not just subclass `UINavigationController` or `UITabBarController` and get the support for free?*\n\nWell, sometimes that’s just not what you want. Maybe you want a very specific appearance or behavior, far from what these classes offer, and therefore would have to resort to tricky hacking, risking it to break with any new version of the framework. Or maybe you just want to be in total control of your containment and avoid having to support specialized functionality.\n\n*OK, but then why not just use `transitionFromViewController:toViewController:duration:options:animations:completion:` and be over with it?*\n\nAnother good question, and you may just want to do that. But perhaps you care about your code and want to encapsulate the transition. So why not use a now-established and well-proven design pattern? And, heck, as a bonus, have support for third-party transition animations thrown in for free.\n\n## Introducing the API\n\nNow, before we start coding – and we will in a minute, I promise – let's set the scene.\n\nThe components of the iOS 7 custom view controller transition API are mostly protocols which make them extremely flexible to work with, because you can very easily plug them into your existing class hierarchy. The five main components are:\n\n1. **Animation Controllers** conforming to the `UIViewControllerAnimatedTransitioning` protocol and in charge of performing the actual animations.\n\n2. **Interaction Controllers** controlling the interactive transitions by conforming to the `UIViewControllerInteractiveTransitioning` protocol.\n\n3. **Transitioning Delegates** conveniently vending animation and interaction controllers, depending on the kind of transition to be performed.\n\n4. **Transitioning Contexts** defining metadata about the transition, such as properties of the view controllers and views participating in the transition. These objects conform to the `UIViewControllerContextTransitioning` protocol, *and are created and provided by the system*.\n\n5. **Transition Coordinators** providing methods to run other animations in parallel with the transition animations. They conform to the `UIViewControllerTransitionCoordinator` protocol.\n\nAs you know, from otherwise reading this publication, there are interactive and non-interactive transitions. In this article, we will concentrate on non-interactive transitions. These are the simplest, so they're a great place to start. This means that we will be dealing with *animation controllers*, *transitioning delegates*, and *transitioning contexts* from the list above.\n\nEnough talk, let’s get our hands dirty…\n\n## The Project\n\nIn three stages, we will be creating a sample app featuring a custom container view controller, which implements support for custom child view controller transition animations.\n\nThe Xcode project, in its three stages, is put in a [repository on GitHub](https://github.com/objcio/issue-12-custom-container-transitions).\n\n### Stage 1: The Basics\n\nThe central class in our app is `ContainerViewController`, which hosts an array of `UIViewController` instances -- in our case, trivial `ChildViewController` objects. The container view controller sets up a private subview with tappable icons representing each child view controller:\n\n![Stage 1: no animation](/images/issue-12/2014-05-01-custom-container-view-controller-transitions-stage-1.gif)\n\nTo switch between child view controllers, tap the icons. At this stage, there is no transition animation when switching child view controllers.\n\nCheck out the [stage-1](https://github.com/objcio/issue-12-custom-container-transitions/tree/stage-1) tag to see the code for the basic app.\n\n### Stage 2: Animating the Transition\n\nWhen adding a transition animation, we want to support *animation controllers* conforming to `UIViewControllerAnimatedTransitioning`. The protocol defines these three methods, the first two of which are required:\n\n```objc\n- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext;\n- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext;\n- (void)animationEnded:(BOOL)transitionCompleted;\n```\n\nThis tells us everything we need to know. When our container view controller is about to perform the animation, we can query the animation controller for the duration and ask it to perform the actual animation. When it is done, we can call `animationEnded:` on the the animation controller, if it implements that optional method.\n\nHowever, there is one thing we need to figure out first. As you can see from the method signatures above, the two required ones take a *transitioning context* parameter, i.e., an object conforming to `UIViewControllerContextTransitioning`. Normally, when using the built-in classes, the framework creates and passes on this context to our animation controller for us. But in our case, since we are acting as the framework, *we* need to create that object.\n\nThis is where the convenience of the heavy use of protocols comes in. Instead of having to override a private class, which obviously is a no-go, we can make our own and just have it conform to the documented protocol.\n\nThere are a [lot of methods](https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewControllerContextTransitioning_protocol/Reference/Reference.html), though, and they are all required. But we can ignore some of them for now, because we are currently only supporting non-interactive transitions.\n\nJust like UIKit, we define a private `NSObject <UIViewControllerContextTransitioning>` class. In our specialized case, it is the `PrivateTransitionContext` class, and the initializer is implemented like this:\n\n```objc\n- (instancetype)initWithFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController goingRight:(BOOL)goingRight {\n    NSAssert ([fromViewController isViewLoaded] && fromViewController.view.superview, @\"The fromViewController view must reside in the container view upon initializing the transition context.\");\n    \n    if ((self = [super init])) {\n        self.presentationStyle = UIModalPresentationCustom;\n        self.containerView = fromViewController.view.superview;\n        self.viewControllers = @{\n            UITransitionContextFromViewControllerKey:fromViewController,\n            UITransitionContextToViewControllerKey:toViewController,\n        };\n        \n        CGFloat travelDistance = (goingRight ? -self.containerView.bounds.size.width : self.containerView.bounds.size.width);\n        self.disappearingFromRect = self.appearingToRect = self.containerView.bounds;\n        self.disappearingToRect = CGRectOffset (self.containerView.bounds, travelDistance, 0);\n        self.appearingFromRect = CGRectOffset (self.containerView.bounds, -travelDistance, 0);\n    }\n    \n    return self;\n}\n```\n\nWe basically capture state, including initial and final frames, for the appearing and disappearing views.\n\nNotice, our initializer requires information about whether we are going right or not. In our specialized `ContainerViewController` context, where buttons are arranged horizontally next to each other, the transition context is recording information about their positional relationship by setting the respective frames. The animation controller, or *animator*, can choose to use this when composing the animation. \n\nWe could gather this information in other ways, but it would require the animator to know about the `ContainerViewController` and its view controllers, and we don’t want that. The animator should only concern itself with the context, which is passed to it, because that would, ideally, make the animator reusable in other contexts.\n\nWe will keep this in mind when making our own animation controller next, now that we have the transition context available to us.\n\nYou probably remember that this was exactly what we did in [View Controller Transitions](/issues/5-ios7/view-controller-transitions/), [issue #5](/issues/5-ios7/). So why not just use that? In fact, because of the extensive use of protocols in this framework, we can take the animation controller, the `Animator` class, from that project and plug it right in to ours – without any modifications.\n\nUsing an `Animator` instance to animate our transition essentially looks like this:\n\n```objc\n[fromViewController willMoveToParentViewController:nil];\n[self addChildViewController:toViewController];\n\nAnimator *animator = [[Animator alloc] init];\n\nNSUInteger fromIndex = [self.viewControllers indexOfObject:fromViewController];\nNSUInteger toIndex = [self.viewControllers indexOfObject:toViewController];\nPrivateTransitionContext *transitionContext = [[PrivateTransitionContext alloc] initWithFromViewController:fromViewController toViewController:toViewController goingRight:toIndex > fromIndex];\n\ntransitionContext.animated = YES;\ntransitionContext.interactive = NO;\ntransitionContext.completionBlock = ^(BOOL didComplete) {\n    [fromViewController.view removeFromSuperview];\n    [fromViewController removeFromParentViewController];\n    [toViewController didMoveToParentViewController:self];\n};\n\n[animator animateTransition:transitionContext];\n```\n\nMost of this is the required container view controller song and dance, and finding out whether we going left or right. Doing the animation is basically three lines of code: 1) creating the animator, 2) creating the transition context, and 3) triggering the animation.\n\nWith that, the transition now looks like this:\n\n![Stage 2: third-party animation](/images/issue-12/2014-05-01-custom-container-view-controller-transitions-stage-2.gif)\n\nPretty cool. We haven’t even written any animation code ourselves!\n\nThis is reflected in the code with the [stage-2](https://github.com/objcio/issue-12-custom-container-transitions/tree/stage-2) tag. To see the full extent of the stage 2 changes, check the [diff against stage 1](https://github.com/objcio/issue-12-custom-container-transitions/compare/stage-1...stage-2).\n\n### Stage 3: Shrink-Wrapping\n\nOne last thing I think we should do is shrink-wrapping `ContainerViewController` so that it:\n\n1. comes with its own default transition animation, and\n2. supports a delegate for vending alternative animation controllers.\n\nThis entails conveniently removing the dependency to the `Animator` class, as well as creating a delegate protocol.\n\nWe define our protocol as:\n\n```objc\n@protocol ContainerViewControllerDelegate <NSObject>\n@optional\n- (void)containerViewController:(ContainerViewController *)containerViewController didSelectViewController:(UIViewController *)viewController;\n- (id <UIViewControllerAnimatedTransitioning>)containerViewController:(ContainerViewController *)containerViewController animationControllerForTransitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController;\n@end\n```\n\nThe `containerViewController:didSelectViewController:` method just makes it easier to integrate `ContainerViewController` into more feature-complete apps. \n\nThe interesting method is `containerViewController:animationControllerForTransitionFromViewController:toViewController:`, of course, which can be compared to the following container view controller delegate protocol methods in UIKit:\n\n- `tabBarController:animationControllerForTransitionFromViewController:toViewController:` (`UITabBarControllerDelegate`)\n- `navigationController:animationControllerForOperation:fromViewController:toViewController:` (`UINavigationControllerDelegate`)\n\nAll these methods return an `id<UIViewControllerAnimatedTransitioning>` object.\n\nInstead of always using an `Animator` object, we can now ask our delegate for an animation controller:\n\n```objc\nid<UIViewControllerAnimatedTransitioning>animator = nil;\nif ([self.delegate respondsToSelector:@selector (containerViewController:animationControllerForTransitionFromViewController:toViewController:)]) {\n    animator = [self.delegate containerViewController:self animationControllerForTransitionFromViewController:fromViewController toViewController:toViewController];\n}\nanimator = (animator ?: [[PrivateAnimatedTransition alloc] init]);\n```\n\nIf we have a delegate, and it returns an animator, we will use that. Otherwise, we will create our own private default animator of class `PrivateAnimatedTransition`. We will implement this next.\n\nAlthough the default animation is somewhat different than that of `Animator`, the code looks surprisingly similar. Here is the full implementation:\n\n```objc\n@implementation PrivateAnimatedTransition\n\nstatic CGFloat const kChildViewPadding = 16;\nstatic CGFloat const kDamping = 0.75f;\nstatic CGFloat const kInitialSpringVelocity = 0.5f;\n\n- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {\n    return 1;\n}\n\n- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {\n    \n    UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];\n    UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];\n    \n    // When sliding the views horizontally, in and out, figure out whether we are going left or right.\n    BOOL goingRight = ([transitionContext initialFrameForViewController:toViewController].origin.x < [transitionContext finalFrameForViewController:toViewController].origin.x);\n    \n    CGFloat travelDistance = [transitionContext containerView].bounds.size.width + kChildViewPadding;\n    CGAffineTransform travel = CGAffineTransformMakeTranslation (goingRight ? travelDistance : -travelDistance, 0);\n    \n    [[transitionContext containerView] addSubview:toViewController.view];\n    toViewController.view.alpha = 0;\n    toViewController.view.transform = CGAffineTransformInvert (travel);\n    \n    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:kDamping initialSpringVelocity:kInitialSpringVelocity options:0x00 animations:^{\n        fromViewController.view.transform = travel;\n        fromViewController.view.alpha = 0;\n        toViewController.view.transform = CGAffineTransformIdentity;\n        toViewController.view.alpha = 1;\n    } completion:^(BOOL finished) {\n        fromViewController.view.transform = CGAffineTransformIdentity;\n        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];\n    }];\n}\n\n@end\n```\n\nNote that even if the view frames haven’t been set to reflect the positional relationships, the code would still work, though it would always transition in the same direction. This class can therefore still be used in other codebases.\n\nThe transition animation now looks like this:\n\n![Stage 3: third-party animation](/images/issue-12/2014-05-01-custom-container-view-controller-transitions-stage-3.gif)\n\nIn the code with the [stage-3](https://github.com/objcio/issue-12-custom-container-transitions/tree/stage-3) tag, setting the delegate in the app delegate has been [commented out](https://github.com/objcio/issue-12-custom-container-transitions/blob/stage-3/Container%20Transitions/AppDelegate.m#L41) in order to see the default animation in action. Set it back in to use `Animator` again. You may want to check out the [full diff against stage-2](https://github.com/objcio/issue-12-custom-container-transitions/compare/stage-2...stage-3).\n\nWe now have a self-contained `ContainerViewController` with a nicely animated default transition that developers can override with their own, iOS 7 custom animation controller (`UIViewControllerAnimatedTransitioning`) objects – even without needing access to our source code.\n\n## Conclusion\n\nIn this article we looked at making our custom container view controller a first-class UIKit citizen by integrating it with the Custom View Controller Transitions, new in iOS 7.\n\nThis means you can apply your own non-interactive transition animation to our custom container view controller. We saw that because we could take an existing transition class, from seven issues ago, and plug it right in – without modification.\n\nThis is perfect if you are distributing your custom container view controller as part of a library or framework, or just want your code to be reusable.\n\nNote that we only support non-interactive transitions so far. The next step is supporting interactive transitions as well.\n\nI will leave that as an exercise for you. It is somewhat more complex because we are basically mimicking the framework behavior, which is all guesswork, really.\n\n**Update:** [Alek Åström](https://twitter.com/MisterAlek) was quick to take on the challenge and has posted a very interesting article, “[Interactive Custom Container View Controller Transitions](http://www.iosnomad.com/blog/2014/5/12/interactive-custom-container-view-controller-transitions)”. As an added bonus, it includes a new exercise…\n\n## Further Indulgence\n\n- iOS 7 Tech Talks Videos, 2014: “[Architecting Modern Apps, Part 1](https://developer.apple.com/tech-talks/videos/index.php?id=3#3)” (07:23-31:27)\n- Full code on [GitHub](https://github.com/objcio/issue-12-custom-container-transitions).\n- Follow-up article by [Alek Åström](https://twitter.com/MisterAlek): “[Interactive Custom Container View Controller Transitions](http://www.iosnomad.com/blog/2014/5/12/interactive-custom-container-view-controller-transitions)”.\n"
  },
  {
    "path": "2014-05-08-editorial.md",
    "content": "---\ntitle:  \"Editorial\"\ncategory: \"12\"\ndate: \"2014-05-08 12:00:00\"\ntags: editorial\n---\n\nHey everyone,\n\nThis issue is all about animations. It's our twelfth issue; next month is our one-year anniversary! After last month's fun escape to the world of Android, we're back to Objective-C. We tried to cover all angles of animations -- from UIView animations, to Core Animation, all the way down to creating your own animations from scratch. We talk about animation view controllers, collection views, and making everything interactive.\n\nAgain, for this issue we're very thankful for all the guest writers who contributed their posts.\nFirst of all, [Robert Böhnke](https://twitter.com/ceterum_censeo) explains to us [how animations work](/issues/12-animations/animations-explained/), and what to look out for when animating things.\n[Nick Lockwood](https://twitter.com/nicklockwood) demonstrates for us how to [animate custom layer properties](/issues/12-animations/animating-custom-layer-properties/), and also how to animate non-visual things.\nNext, [Joachim Bondo](https://twitter.com/osteslag) explains how we can [make custom container view controllers orchestrate tailor-made view controller transition animations in iOS 7](/issues/12-animations/custom-container-view-controller-transitions/).\n[David Rönnqvist](https://twitter.com/davidronnqvist) shows us [how UIView animations work](/issues/12-animations/view-layer-synergy/), and how you might implement them yourself.\n[Engin Kurutepe](https://twitter.com/ekurutepe) shares with us how to go about [creating custom collection view animations](/issues/12-animations/collectionview-animations/).\nFinally, Florian and Chris write about [interactive animations](/issues/12-animations/interactive-animations/), and show how to implement them in a couple of different ways.\n\nCheers from Berlin,\n\nChris, Daniel, and Florian.\n"
  },
  {
    "path": "2014-05-08-interactive-animations.md",
    "content": "---\r\ntitle:  \"Interactive Animations\"\r\ncategory: \"12\"\r\ndate: \"2014-05-08 06:00:00\"\r\ntags: article\r\nauthor:\r\n  - name: Chris Eidhof\r\n    url: https://twitter.com/chriseidhof\r\n  - name: Florian Kugler\r\n    url: https://twitter.com/floriankugler\r\n---\r\n\r\n\r\nWhen Steve Jobs introduced the first iPhone in 2007, the touch screen interaction had a certain kind of magic to it. A prime example of this was his [first demonstration of scrolling a table view](http://www.youtube.com/watch?v=t4OEsI0Sc_s&t=16m9s). You can hear in the reaction of the audience how impressive what seems the most normal thing to us today was back then. A little bit later in the presentation, he underlined this point by quoting somebody he had given a demo to before: [\"You got me at scrolling.\"](https://www.youtube.com/watch?v=t4OEsI0Sc_s&t=22m10s) \r\n\r\nWhat was it about scrolling that created this 'wow' effect?\r\n\r\nScrolling was a perfect example of direct manipulation through capacitive touch displays. The scroll view obeyed the movements of your finger so closely, and it continued the motion seamlessly after you let go. From there, it decelerated in a natural way, and even exhibited a nice bounce when it hit its boundaries. Scrolling was responsive at any time and behaved just like an object from the real world.\r\n\r\n\r\n## State of Animations\r\n\r\nMost animations in iOS still don't live up to the standard that scrolling set on the original iPhone. \r\nThey are fire-and-forget animations, which cannot be interacted with once they're running (for example the unlock animation, the animations opening and closing groups on the home screen, and the navigation controller animations, to name just a few).\r\n\r\nHowever, there are some apps out there that bring that aspect of always in control, direct manipulation to all animations they use. It's a big difference in how these apps feel compared to the rest. Prominent examples of such apps are the original Twitter iPad app and the current Facebook Paper app. But for the time being, apps that fully embrace direct manipulation and always interruptible animations are still rare. This creates an opportunity for apps that do this well, as they have a very different, high-quality feel to them. \r\n\r\n\r\n## Challenges of Truly Interactive Animations\r\n\r\nUsing `UIView` or `CAAnimation` animations has two big problems when it comes to interactive animations: those animations separate what you see on the screen from what the actual spatial properties are on the layer, and they directly manipulate the spatial properties.\r\n\r\n\r\n### Separation of Model and Presentation\r\n\r\nCore Animation is designed in a way that it decouples the layer's model properties from what you see on the screen (the presentation layer). This makes it more difficult to create animations you can interact with at any time, because those two representations do not match. It's up to you to do the manual work to get them in sync before you change the animation:\r\n\r\n```objc\r\nview.layer.center = view.layer.presentationLayer.center;\r\n[view.layer removeAnimationForKey:@\"animation\"];\r\n// add new animation...\r\n```\r\n\r\n### Direct vs. Indirect Control\r\n\r\nThe bigger problem with `CAAnimation` animations is that they directly operate on the spatial properties of a layer. This means, for example, that you specify that a layer should animate from position `(100, 100)` to position `(300, 300)`. If you want to stop this animation halfway and to animate the layer back to where it came from, things get very complicated. If you simply remove the current animation and add a new animation, then the layer's velocity would be discontinuous.\r\n\r\n![](/images/issue-12/abrupt.png)\r\n\r\nWhat we want to have, though, is a nice, smooth deceleration and acceleration.\r\n\r\n![](/images/issue-12/smooth.png)\r\n\r\nThis only becomes feasible once you start controlling animations *indirectly*, i.e. through simulated forces acting on the view. The new animation needs to take the layer's current velocity *vector* as input in order to produce a smooth result.\r\n\r\nLooking at the `UIView` animation API for spring animations (`animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:`), you'll notice that the velocity is a `CGFloat`. So while you can give the animation an initial velocity in the direction the animation moves the view, you cannot tell the animation that the view is, for example, currently moving at a certain velocity perpendicular to the new animation direction. In order to enable this, the velocity needs to be expressed as a vector.\r\n\r\n\r\n## Solutions\r\n\r\nSo let's take a look at how we can correctly implement interactive and interruptible animations. To do this, we're going to build something like the Control Center panel:\r\n\r\n<video controls=\"1\">\r\n  <source src=\"/images/issue-12/interactive-animation.mov\"></source>\r\n</video>\r\n\r\nThe panel has two states: opened and closed. You can toggle the states by tapping it, or dragging it up and down. The challenge is to make everything interactive, even while animating. For example, if you tap the panel while it's animating to the opened state, it should animate back to the closed state from its current position. In a lot of apps that use default animation APIs, you'll have to wait until the animation is finished before you can do anything. Or, if you don't have to wait, the animation exhibits a discontinuous velocity curve. We want to work around this.\r\n\r\n\r\n### UIKit Dynamics\r\n\r\nWith iOS 7, Apple introduced the animation framework UIKit Dynamics (see WWDC 2013 sessions [206](https://developer.apple.com/videos/wwdc/2013/index.php?id=206) and [221](https://developer.apple.com/videos/wwdc/2013/index.php?id=221)). UIKit Dynamics is based on a pseudo-physics engine that can animate everything that implements the [`UIDynamicItem`](https://developer.apple.com/library/ios/documentation/uikit/reference/UIDynamicItem_Protocol/Reference/Reference.html) protocol by adding specific behaviors to an animator object. This framework is very powerful and enables complex behaviors of many items like attachments and collisions. Take a look at the sample [dynamics catalog](https://developer.apple.com/library/ios/samplecode/DynamicsCatalog/Introduction/Intro.html) to see what's available.\r\n\r\nSince animations with UIKit Dynamics are driven indirectly, as we discussed above, this enables us to implement truly interactive animations that can be interrupted and that exhibit continuous acceleration behavior at any time. At the same time, the abstraction of UIKit Dynamics at the physics level can also seem overwhelming for the kind of animations that we generally need in user interfaces. In most cases, we'll only use a very small subset of its capabilities.\r\n\r\n\r\n#### Defining Behaviors\r\n\r\nIn order to implement our sliding-panel behavior, we'll make use of two different behaviors that come with UIKit Dynamics: [`UIAttachmentBehavior`](https://developer.apple.com/library/ios/documentation/uikit/reference/UIAttachmentBehavior_Class/Reference/Reference.html) and [`UIDynamicItemBehavior`](https://developer.apple.com/library/ios/documentation/uikit/reference/UIDynamicItemBehavior_Class/Reference/Reference.html). The attachment behavior fulfills the role of a spring, pulling our view toward its target point. The dynamic item behavior, on the other hand, defines intrinsic properties of the view, such as its friction coefficient.\r\n\r\nTo package these two behaviors for our sliding panel, we'll create our own behavior subclass:\r\n\r\n```objc\r\n@interface PaneBehavior : UIDynamicBehavior\r\n\r\n@property (nonatomic) CGPoint targetPoint;\r\n@property (nonatomic) CGPoint velocity;\r\n\r\n- (instancetype)initWithItem:(id <UIDynamicItem>)item;\r\n\r\n@end\r\n```\r\n\r\nWe initialize this behavior with one dynamic item and then can set its target point and velocity to whatever we want. Internally, we create the attachment behavior and the dynamic item behavior and add both as child behavior to our custom behavior:\r\n\r\n```objc\r\n- (void)setup\r\n{\r\n    UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self.item attachedToAnchor:CGPointZero];\r\n    attachmentBehavior.frequency = 3.5;\r\n    attachmentBehavior.damping = .4;\r\n    attachmentBehavior.length = 0;\r\n    [self addChildBehavior:attachmentBehavior];\r\n    self.attachmentBehavior = attachmentBehavior;\r\n    \r\n    UIDynamicItemBehavior *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.item]];\r\n    itemBehavior.density = 100;\r\n    itemBehavior.resistance = 10;\r\n    [self addChildBehavior:itemBehavior];\r\n    self.itemBehavior = itemBehavior;\r\n}\r\n```\r\n\r\nIn order to make the `targetPoint` and `velocity` properties affect the item's behavior, we overwrite their setters and modify the corresponding properties on the attachment and item behaviors, respectively. For the target point, this is very simple:\r\n\r\n```objc\r\n- (void)setTargetPoint:(CGPoint)targetPoint\r\n{\r\n    _targetPoint = targetPoint;\r\n    self.attachmentBehavior.anchorPoint = targetPoint;\r\n}\r\n```\r\n\r\nFor the velocity property, we have to jump through one more hoop, since the dynamic item behavior only allows relative changes in velocity. That means that in order to set the velocity to an absolute value, we first have to get its current velocity and then add the difference to the target velocity:\r\n\r\n```objc\r\n- (void)setVelocity:(CGPoint)velocity\r\n{\r\n    _velocity = velocity;\r\n    CGPoint currentVelocity = [self.itemBehavior linearVelocityForItem:self.item];\r\n    CGPoint velocityDelta = CGPointMake(velocity.x - currentVelocity.x, velocity.y - currentVelocity.y);\r\n    [self.itemBehavior addLinearVelocity:velocityDelta forItem:self.item];\r\n}\r\n```\r\n\r\n#### Putting the Behavior to Use\r\n\r\nOur sliding panel has three different states: it is at rest in one of its end positions, being dragged by the user, or animating without the user's interaction toward one of its end points.\r\n\r\nAt the transition from the direct manipulation state (the user dragging the panel) to the animation state, we have to do some extra work to make sure that the panel exhibits a smooth animation behavior. When the user stops dragging the panel, it sends a message to its delegate. Within this method, we decide toward what position the panel should animate and add our custom `PaneBehavior` with this endpoint and -- very important -- the initial velocity, in order to ensure a smooth transition from dragging to animation:\r\n\r\n```objc\r\n- (void)draggableView:(DraggableView *)view draggingEndedWithVelocity:(CGPoint)velocity\r\n{\r\n    PaneState targetState = velocity.y >= 0 ? PaneStateClosed : PaneStateOpen;\r\n    [self animatePaneToState:targetState initialVelocity:velocity];\r\n}\r\n\r\n- (void)animatePaneToState:(PaneState)targetState initialVelocity:(CGPoint)velocity\r\n{\r\n    if (!self.paneBehavior) {\r\n        PaneBehavior *behavior = [[PaneBehavior alloc] initWithItem:self.pane];\r\n        self.paneBehavior = behavior;\r\n    }\r\n    self.paneBehavior.targetPoint = [self targetPointForState:targetState];\r\n    if (!CGPointEqualToPoint(velocity, CGPointZero)) {\r\n        self.paneBehavior.velocity = velocity;\r\n    }\r\n    [self.animator addBehavior:self.paneBehavior];\r\n    self.paneState = targetState;\r\n}\r\n```\r\n\r\nAs soon as the user puts his or her finger down on the panel again, we have to remove the dynamic behavior from the animator, in order to not interfere with the pan gesture:\r\n\r\n```objc\r\n- (void)draggableViewBeganDragging:(DraggableView *)view\r\n{\r\n    [self.animator removeAllBehaviors];\r\n}\r\n```\r\n    \r\nWe not only allow the panel to be dragged, but it can also be tapped to toggle from one position to the other. When a tap happens, we immediately adjust the panel's target position. Since we don't control the animation directly, but via spring and friction forces, the animation will proceed smoothly without abruptly reversing its movement:\r\n\r\n```objc\r\n- (void)didTap:(UITapGestureRecognizer *)tapRecognizer\r\n{\r\n    PaneState targetState = self.paneState == PaneStateOpen ? PaneStateClosed : PaneStateOpen;\r\n    [self animatePaneToState:targetState initialVelocity:CGPointZero];\r\n}\r\n```\r\n\r\nAnd that's pretty much all there is to it. You can check out the whole example project on [GitHub](https://github.com/objcio/issue-12-interactive-animations-uidynamics). \r\n\r\nTo reiterate the crucial point: UIKit Dynamics allows us to drive the animation indirectly by simulating forces on the view (in our case, spring and friction forces). This indirection enables us to interact with the view at any time while maintaining a continuous velocity curve.\r\n\r\nNow that we have implemented this interaction with UIKit Dynamics, we'll take a look behind the scenes. Animations like the one in our example only use a tiny fraction of UIKit Dynamic's capabilities, and it's surprisingly simple to implement them yourself. That's a good exercise to understand what's going on, but it can also be necessary if you either don't have UIKit Dynamics available (e.g. on the Mac) or it's not a good abstraction for your use case.\r\n\r\n\r\n\r\n### Driving Animations Yourself\r\n\r\nAs for the animations you'll use most of the time in your apps, e.g. simple spring animations, it's surprisingly not difficult to drive those yourself. It's a good exercise to lift the lid of the huge black box of UIKit Dynamics and to see what it takes to implement simple interactive animations 'manually.' The idea is rather easy: we make sure to change the view's frame 60 times per second. For each frame, we adjust the view's frame based on the current velocity and the forces acting on the view.\r\n\r\n\r\n#### The Physics\r\n\r\nLet's first take a look at some basic physics necessary to drive a spring animation like we created before using UIKit Dynamics. To simplify things, we'll look at a purely one-dimensional case (as it is the case in our example), although introducing the second dimension is straightforward.\r\n\r\nThe objective is to calculate the new position of the panel based on its current position and the time that has elapsed since the last animation tick. This can be expressed as:\r\n\r\n```\r\ny = y0 + Δy\r\n```\r\n\r\nThe position delta is a function of the velocity and the time:\r\n \r\n```\r\nΔy = v ⋅ Δt\r\n```\r\n\r\nThe velocity can be calculated as the previous velocity plus the velocity delta, caused by the force acting on the view:\r\n\r\n```\r\nv = v0 + Δv\r\n```\r\n\r\nThe change in velocity can be calculated by the impulse applied to the view:\r\n\r\n```\r\nΔv = (F ⋅ Δt) / m\r\n```\r\n    \r\nNow, let's take a look at the force acting on the view. In order to get the spring effect, we have to combine a spring force with friction force:\r\n\r\n```\r\nF = F_spring + F_friction\r\n```\r\n\r\nThe spring force comes straight from the textbook:\r\n\r\n```\r\nF_spring = k ⋅ x\r\n```\r\n\r\nwhere `k` is the spring constant and `x` is the distance of the view to its target end point (the length of the spring). Therefore, we can also write this as:\r\n\r\n```\r\nF_spring = k ⋅ abs(y_target - y0)\r\n```\r\n\r\nWe calculate friction as being proportional to the view's velocity:\r\n\r\n```\r\nF_friction = μ ⋅ v\r\n```\r\n\r\n`μ` is a simple friction constant. You could come up with other ways to calculate the friction force, but this works well to create the animation we want to have.\r\n\r\nPutting this together, the force on the view is calculated as:\r\n\r\n```\r\nF = k ⋅ abs(y_target - y0) + μ ⋅ v\r\n```\r\n\r\nTo simplify things a bit more, we'll set the view's mass to `1`, so that we can calculate the change in position as:\r\n\r\n    Δy = (v0 + (k ⋅ abs(y_target - y0) + μ ⋅ v) ⋅ Δt) ⋅ Δt\r\n\r\n\r\n#### Implementing the Animation\r\n\r\nTo implement this, we first create our own `Animator` class, which drives the animations. This class uses a `CADisplayLink`, which is a timer made specifically for drawing synchronously with the display's refresh rate. In other words, if your animation is smooth, the timer calls your methods 60 times per second. Next, we implement a protocol `Animation` that works together with our `Animator`. This protocol has only one method, `animationTick:finished:`. This method gets called every time the screen is updated, and gets two parameters: the first parameter is the duration of the previous frame, while the second parameter is a pointer to a `BOOL`. By setting the value of the pointer to `YES`, we can communicate back to the `Animator` that we're done animating:\r\n\r\n```objc\r\n@protocol Animation <NSObject>\r\n- (void)animationTick:(CFTimeInterval)dt finished:(BOOL *)finished;\r\n@end\r\n```\r\n\r\nThe method is implemented below. First, based on the time interval, we calculate a force, which is a combination of the spring force and the friction force. Then we update the velocity with this force, and adjust the view's center accordingly. Finally, if the speed gets low and the view is at its goal, we stop the animation:\r\n\r\n```objc\r\n- (void)animationTick:(CFTimeInterval)dt finished:(BOOL *)finished\r\n{\r\n    static const float frictionConstant = 20;\r\n    static const float springConstant = 300;\r\n    CGFloat time = (CGFloat) dt;\r\n\r\n    // friction force = velocity * friction constant\r\n    CGPoint frictionForce = CGPointMultiply(self.velocity, frictionConstant);\r\n    // spring force = (target point - current position) * spring constant\r\n    CGPoint springForce = CGPointMultiply(CGPointSubtract(self.targetPoint, self.view.center), springConstant);\r\n    // force = spring force - friction force\r\n    CGPoint force = CGPointSubtract(springForce, frictionForce);\r\n\r\n    // velocity = current velocity + force * time / mass\r\n    self.velocity = CGPointAdd(self.velocity, CGPointMultiply(force, time));\r\n    // position = current position + velocity * time\r\n    self.view.center = CGPointAdd(self.view.center, CGPointMultiply(self.velocity, time));\r\n\r\n    CGFloat speed = CGPointLength(self.velocity);\r\n    CGFloat distanceToGoal = CGPointLength(CGPointSubtract(self.targetPoint, self.view.center));\r\n    if (speed < 0.05 && distanceToGoal < 1) {\r\n        self.view.center = self.targetPoint;\r\n        *finished = YES;\r\n    }\r\n}\r\n```\r\n\r\nThat's all there is to it. We capsulated this method in a `SpringAnimation` object. The only other method in this object is the initializer, which takes the view to animate, the target point for the view's center (in our case, it's either the center point for the opened state, or the closed state), and the initial velocity.\r\n\r\n#### Adding the Animation to the View\r\n\r\nOur view class is exactly the same as in the UIDynamics example: it has a pan recognizer and updates its center based on the pan gestures. It sends out the same two delegate methods, which we will implement to initialize our animation. First of all, when the user starts dragging, we cancel all animations:\r\n\r\n```objc\r\n- (void)draggableViewBeganDragging:(DraggableView *)view\r\n{\r\n    [self cancelSpringAnimation];\r\n}\r\n```\r\n\r\nAfter the dragging ends, we start our animation with the last velocity value from the pan gesture. The target point is calculated from the `paneState`:\r\n\r\n```objc\r\n- (void)draggableView:(DraggableView *)view draggingEndedWithVelocity:(CGPoint)velocity\r\n{\r\n    PaneState targetState = velocity.y >= 0 ? PaneStateClosed : PaneStateOpen;\r\n    self.paneState = targetState;\r\n    [self startAnimatingView:view initialVelocity:velocity];\r\n}\r\n\r\n- (void)startAnimatingView:(DraggableView *)view initialVelocity:(CGPoint)velocity\r\n{\r\n    [self cancelSpringAnimation];\r\n    self.springAnimation = [UINTSpringAnimation animationWithView:view target:self.targetPoint velocity:velocity];\r\n    [view.animator addAnimation:self.springAnimation];\r\n}\r\n```\r\n\r\nThe only thing left to do is add the tap animation and that is relatively easy. We toggle the state and start animating. If there is a spring animation, we start with that velocity. If the spring animation is nil, the initial velocity will be CGPointZero. To understand why it still animates, look at the `animationTick:finished:` code. When the initial velocity is zero, the spring force will slowly keep increasing the velocity until the pane arrives at the target center point:\r\n\r\n```objc\r\n- (void)didTap:(UITapGestureRecognizer *)tapRecognizer\r\n{\r\n    PaneState targetState = self.paneState == PaneStateOpen ? PaneStateClosed : PaneStateOpen;\r\n    self.paneState = targetState;\r\n    [self startAnimatingView:self.pane initialVelocity:self.springAnimation.velocity];\r\n}\r\n```\r\n\r\n#### The Animation Driver\r\n\r\nFinally, the last part we need is the `Animator`, which is the driver of the animations. The animator is a wrapper around the display link. Because each display link is coupled to a specific `UIScreen`, we initialize our animator with a specific screen. We set up a display link, and add it to the run loop. Because there are no animations yet, we start in a paused state: \r\n\r\n```objc\r\n- (instancetype)initWithScreen:(UIScreen *)screen\r\n{\r\n    self = [super init];\r\n    if (self) {\r\n        self.displayLink = [screen displayLinkWithTarget:self selector:@selector(animationTick:)];\r\n        self.displayLink.paused = YES;\r\n        [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];\r\n        self.animations = [NSMutableSet new];\r\n    }\r\n    return self;\r\n}\r\n```\r\n\r\nOnce we add the animation, we make sure that the display link is not paused anymore:\r\n\r\n```objc\r\n- (void)addAnimation:(id<Animation>)animation\r\n{\r\n    [self.animations addObject:animation];\r\n    if (self.animations.count == 1) {\r\n        self.displayLink.paused = NO;\r\n    }\r\n}\r\n```\r\n\r\nWe set up the display link to call `animationTick:`, and on each tick we iterate over the animations, send them a message, and that's it. If there are no animations left, we pause the display link:\r\n\r\n```objc\r\n - (void)animationTick:(CADisplayLink *)displayLink\r\n {\r\n     CFTimeInterval dt = displayLink.duration;\r\n     for (id<Animation> a in [self.animations copy]) {\r\n         BOOL finished = NO;\r\n         [a animationTick:dt finished:&finished];\r\n         if (finished) {\r\n             [self.animations removeObject:a];\r\n         }\r\n     }\r\n     if (self.animations.count == 0) {\r\n         self.displayLink.paused = YES;\r\n     }\r\n }\r\n```\r\n\r\nThe entire project is available on [GitHub](https://github.com/objcio/issue-12-interactive-animations).\r\n\r\n\r\n#### Tradeoffs\r\n\r\nIt's important to keep in mind that driving animations via display links (as demonstrated above or by using UIKit Dynamics or something like Facebook's POP framework) comes with a tradeoff. As [Andy Matuschak pointed out](https://twitter.com/andy_matuschak/status/464790108072206337) `UIView` and `CAAnimation` animations are less likely to be affected by other tasks running on the system, because the render server runs at a higher priority than your app. \r\n\r\n\r\n### Back to the Mac\r\n\r\nThere's nothing like UIKit Dynamics available on Mac at this time. If you want\r\nto create truly interactive animations here, you have to take the route of\r\ndriving those animations yourself.  Now that we've already shown how to\r\nimplement this on iOS, it's very simple to make the same example work on OS X; check out the [full project](https://github.com/objcio/issue-12-interactive-animations-osx) on GitHub. These are the things that need to be changed:\r\n\r\n* The first thing to change is the `Animator`. On the Mac, there is no `CADisplayLink`, but instead, a `CVDisplayLink`, which has a C-based API. Setting it up is a bit more work, but just as straightforward.\r\n* Our spring animation on iOS adjusts the center of the view. An `NSView` doesn't have a center property, so instead we animate the frame's origin.\r\n* On the Mac, there are no gesture recognizers. Instead, we have to implement `mouseDown:`, `mouseUp:`, and `mouseDragged:` in our custom view subclass.\r\n\r\nThese are the only changes we need to make to port our animation code to the Mac. For a simple view like this, it works really well. For more complex things, you might not want to animate the frame, but use `transform` instead, which is the topic of a blogpost on [OS X Animations](http://jwilling.com/osx-animations) by Jonathan Willing.\r\n\r\n\r\n### Facebook's POP Framework\r\n\r\nThere has been quite a bit of buzz in the last weeks around Facebook's [POP framework](https://github.com/facebook/pop). This is the animation engine that powers its Paper app. It operates very similar to the example above of driving your own animations, but it comes in a neat package with a lot more flexibility. \r\n\r\nSo, let's try to make our own manually driven animation work with POP instead. Since we already had our own spring animation packaged into its own class, the change is pretty trivial. All we have to do is instantiate a POP animation instead of our own one, and add this to the view:\r\n\r\n```objc\r\n- (void)animatePaneWithInitialVelocity:(CGPoint)initialVelocity\r\n{\r\n    [self.pane pop_removeAllAnimations];\r\n    POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];\r\n    animation.velocity = [NSValue valueWithCGPoint:initialVelocity];\r\n    animation.toValue = [NSValue valueWithCGPoint:self.targetPoint];\r\n    animation.springSpeed = 15;\r\n    animation.springBounciness = 6;\r\n    [self.pane pop_addAnimation:animation forKey:@\"animation\"];\r\n    self.animation = animation;\r\n}\r\n```\r\n\r\nYou can find the full working example using POP on [GitHub](https://github.com/objcio/issue-12-interactive-animations-pop).\r\n\r\nIt's super easy to get it to work, and it's pretty straightforward to create more complex animations. But the real power of it lies in the fact that it enables you to create truly interactive and interruptible animations, as we have talked about before, because the animations it supports out of the box take the velocity as input. If you plan your interactions from the get-go to be interruptible at any time, a framework like POP helps you to implement this in a way that ensures animations always stay smooth.\r\n\r\nIf you need more than what `POPSpringAnimation` and `POPDecayAnimation` can do out of the box, POP also comes with a `POPCustomAnimation` class, which basically is a convenient wraparound display link to drive your own animation in a callback block that gets called on each animation tick.\r\n\r\n\r\n## The Road Ahead\r\n\r\nWith iOS 7's shift away from visual imitation of real-world objects toward a stronger focus on the UI's behavior, truly interactive animations are a great way to stand out. They're also a way to extend the magic of the original iPhone's scrolling behavior into every aspect of the interaction. To make this work, it's important to consider those interactions early on in the design instead of just bolting on animations late in the development process.\r\n\r\nA special thanks goes to [Loren Brichter](https://twitter.com/lorenb) for his advice on this article!\r\n"
  },
  {
    "path": "2014-05-08-view-layer-synergy.md",
    "content": "---\ntitle:  \"View-Layer Synergy\"\ncategory: \"12\"\ndate: \"2014-05-08 08:00:00\"\ntags: article\nauthor:\n  - name: David Rönnqvist\n    url: https://twitter.com/davidronnqvist\n---\n\nOn iOS, views always have an underlying layer. There is a very strong relationship between the view and its layer, and the view derives most of its data from the layer object directly. There are also standalone layers -- for example, `AVCaptureVideoPreviewLayer` and `CAShapeLayer` -- that present content on the screen without being attached to a view. In either case, there is a layer involved. Still, the layers that are attached to views and the standalone layers behave slightly differently. \n\nIf you change almost any property of a standalone layer, it will make a brief animation from the old value to the new value.[^animatable] However, if you change the same property of a view's layer, it just changes from one frame to the next. Despite it being layers involved in both cases, the default layer behavior of implicit animations doesn't apply when the layer is attached to a view.\n\nAn explanation as to _why_ this is happening can be found in the Core Animation Programming Guide in the section \"How to Animate Layer-Backed Views\":\n\n> The UIView class disables layer animations by default but reenables them inside animation blocks\n\nThat is the behavior that we are seeing; when a property is changed outside of an animation block, there is no animation, but when the property is changed inside of an animation, there is an animation. The answer to the question of _how_ this is happening is both simple and elegant and speaks well to how views and layers were designed to work together. \n\nWhenever an animatable layer property changes, the layer looks for the appropriate 'action' to run for that property change. An action in Core Animation terminology is a more general term for an animation.[^CAAction] The layer searches for an action in a very well-documented manner, consisting of five steps. The first step is the most interesting when looking at the interaction between the view and the layer:\n\n[^CAAction]: Technically, it is a protocol and could be pretty much anything, but in practice you are talking about an animation of some sort.\n\nThe layer asks its delegate to provide an action for the property that was changed by sending the `actionForLayer:forKey:` message to its delegate. The delegate can respond with one out of three things:\n\n1. It can respond with an action object, in which case the layer will use that action.\n2. It can respond with `nil` to tell the layer to keep looking elsewhere.\n3. It can respond with the `NSNull` object to tell the layer that no action should run and that the search should be terminated.\n\nWhat makes this so interesting is that, for a layer that is backing a view, the view is always the delegate:\n\n> In iOS, if the layer is associated with a UIView object, this property _must_ be set to the view that owns the layer.\n\nWhat may have seemed complicated a minute ago is all of a sudden very simple: the view returns `NSNull` whenever the layer asks for an action, except when the property change happened inside of an animation block. But don't just take my word for it. It's very easy to verify that this is the case. Simply ask the view to provide an action for a layer property that would normally animate, for example, 'position':\n\n```objc\nNSLog(@\"outside animation block: %@\",\n      [myView actionForLayer:myView.layer forKey:@\"position\"]);\n\n[UIView animateWithDuration:0.3 animations:^{\n    NSLog(@\"inside animation block: %@\",\n          [myView actionForLayer:myView.layer forKey:@\"position\"]);\n}];\n```\n\nRunning the above code shows that the view returns the NSNull object outside of the block and returns a CABasicAnimation inside of the block. Elegant, isn't it? Note that the description of NSNull prints with angle brackets, just like other objects, (\"`<null>`\") and that nil prints with parenthesis (\"`(null)`\"): \n\n```\noutside animation block: <null>\ninside animation block: <CABasicAnimation: 0x8c2ff10>\n```\n\nFor backing layers, the search for an action doesn't go further than the first step.[^neverSeen] For standalone layers, there are four more steps that you can read more about in [the documentation for `actionForKey:` on CALayer][actionForKeyDocs]. \n\n[^neverSeen]: At least I have never seen a case where the view returns `nil` so that the search for an action continues.\n\n## Learning from UIKit\n\nI'm sure that we can all agree that UIView animation is a really nice API with its concise, declarative style. And the fact that it's using Core Animation to perform these animations gives us an opportunity to dig deep and see how UIKit uses Core Animation. There may even be some good practices and neat tricks to pick up along the way :)\n\nWhen a property changes inside of an animation block, the view returns a basic animation to the layer and that animation gets added to the layer via the regular `addAnimation:forKey:` method, just like an explicit animation would. Once again, don't just take my word for it. Let's verify.\n\nThe interaction between views and layers is rather easy to inspect, all thanks to the `+layerClass` class method on UIView. It determines what class is used when creating the backing layer of the view. By subclassing UIView and returning a custom layer class, we can override `addAnimation:forKey:` in that layer subclass and log to see that it gets called. The only thing we need to remember is to always call super so that we don't alter the behavior that we are trying to inspect:\n\n```objc\n@interface DRInspectionLayer : CALayer\n@end\n\n@implementation DRInspectionLayer\n- (void)addAnimation:(CAAnimation *)anim forKey:(NSString *)key\n{\n    NSLog(@\"adding animation: %@\", [anim debugDescription]);\n    [super addAnimation:anim forKey:key];\n}\n@end\n\n\n@interface DRInspectionView : UIView\n@end\n\n@implementation DRInspectionView\n+ (Class)layerClass\n{\n    return [DRInspectionLayer class];\n}\n@end\n```\n\nBy logging the debug description of the animation, we don't only see that it gets called as expected, but we also see how the animation is constructed:\n\n```\n<CABasicAnimation:0x8c73680; \n    delegate = <UIViewAnimationState: 0x8e91fa0>;\n    fillMode = both; \n    timingFunction = easeInEaseOut; \n    duration = 0.3; \n    fromValue = NSPoint: {5, 5}; \n    keyPath = position\n>\n```\n\nAt the time when the animation is added to the layer, the new value of the property hasn't yet been changed. The animation is constructed to make good use of this by only specifying an explicit `fromValue` (the current value). A quick glance at [the CABasicAnimation documentation][basicAnimation] reminds us what this means for the interpolation of the animation:\n\n> `fromValue` is non-`nil`. Interpolates between `fromValue` and the current presentation value of the property.\n\nThis is how I like to work with explicit animations as well, by changing the property to the new value and then adding the animation object to the layer:\n\n```objc\nCABasicAnimation *fadeIn = [CABasicAnimation animationWithKeyPath:@\"opacity\"];\nfadeIn.duration  = 0.75;\nfadeIn.fromValue = @0;\n\nmyLayer.opacity = 1.0; // change the model value ...\n// ... and add the animation object\n[myLayer addAnimation:fadeIn forKey:@\"fade in slowly\"];\n```\n\nI find it to be very clean, and you don't have to do anything extra when the animation is removed. If the animation starts after a delay, you can use a backward fill mode (or the 'both' fill mode), just like the animation that UIKit created.\n\n\nYou may have seen the animation delegate and wondered what that class is for. Looking at a [class dump][animationState], we can see that it's mostly maintaining state about the animations (duration, delay, repeat count, etc.). We can also see that it pushes and pops to a stack to be able to get the correct state when nesting one animation block inside of another. All of that is mostly an implementation detail and not very interesting unless you are trying to write your own block-based animation API (which is actually quite a fun idea). \n\nHowever, it _is_ interesting to see that the delegate implements `animationDidStart:` and `animationDidStop:finished:` and passes that information on to its own delegate. We can log the delegate's delegate to see that it is of another private class: UIViewAnimationBlockDelegate. Looking at [its class dump][blockDelegate], we can see that it is a very small class with a single responsibility: responding to the animation delegate callbacks and executing the corresponding blocks. This is something that we can easily add to our own Core Animation code if we prefer blocks over delegate callbacks:\n\n```objc\n@interface DRAnimationBlockDelegate : NSObject\n\n@property (copy) void(^start)(void);\n@property (copy) void(^stop)(BOOL);\n\n+(instancetype)animationDelegateWithBeginning:(void(^)(void))beginning\n                                   completion:(void(^)(BOOL finished))completion;\n\n@end\n\n@implementation DRAnimationBlockDelegate\n\n+ (instancetype)animationDelegateWithBeginning:(void (^)(void))beginning\n                                    completion:(void (^)(BOOL))completion\n{\n    DRAnimationBlockDelegate *result = [DRAnimationBlockDelegate new];\n    result.start = beginning;\n    result.stop  = completion;\n    return result;\n}\n\n- (void)animationDidStart:(CAAnimation *)anim\n{\n    if (self.start) {\n        self.start();\n    }\n    self.start = nil;\n}\n\n- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag\n{\n    if (self.stop) {\n        self.stop(flag);\n    }\n    self.stop = nil;\n}\n\n@end\n```\n\nDepending on personal preference, a block-based callback style, like this, may fit you better than implementing the delegate callbacks in your code: \n\n```objc\nfadeIn.delegate = [DRAnimationBlockDelegate animationDelegateWithBeginning:^{\n    NSLog(@\"beginning to fade in\");\n} completion:^(BOOL finished) {\n    NSLog(@\"did fade %@\", finished ? @\"to the end\" : @\"but was cancelled\");\n}];\n```\n\n## Custom Block-Based Animation APIs\n\nOnce you know about the `actionForKey:` mechanism, UIView animations are a lot less magical than they might first seem. In fact, there isn't really anything stopping us from writing our own block-based animation APIs that are tailored to our needs. The one I'm designing will be used to draw attention to a view by animating the change inside of the block with a very aggressive timing curve, and then slowly animate back to the original value. You could say that it makes the view 'pop.'[^pop] Unlike a regular animation block with the `UIViewAnimationOptionAutoreverse` option, I'm also changing the model value back to what it was before, since that's what the animation conceptually does. Using the custom animation API will look like this:\n\n```objc\n[UIView DR_popAnimationWithDuration:0.7\n                             animations:^{\n                                 myView.transform = CGAffineTransformMakeRotation(M_PI_2);\n                                }];\n```\n\nWhen we are done, it is going to look like this (animating the position, size, color, and rotation of four different views):\n\n![The custom block animation API, used to animate the position, size, color, and rotation of four different views](/images/issue-12/2014-05-01-view-layer-synergy-custom-block-animations.gif)\n\nTo start with, we need to get the delegate callback when a layer property changes. Since we can't know what layers are going to change beforehand, I have chosen to swizzle `actionForLayer:forKey:` in a category on UIView:\n\n```objc\n@implementation UIView (DR_CustomBlockAnimations)\n\n+ (void)load\n{        \n    SEL originalSelector = @selector(actionForLayer:forKey:);\n    SEL extendedSelector = @selector(DR_actionForLayer:forKey:);\n    \n    Method originalMethod = class_getInstanceMethod(self, originalSelector);\n    Method extendedMethod = class_getInstanceMethod(self, extendedSelector);\n    \n    NSAssert(originalMethod, @\"original method should exist\");\n    NSAssert(extendedMethod, @\"exchanged method should exist\");\n    \n    if(class_addMethod(self, originalSelector, method_getImplementation(extendedMethod), method_getTypeEncoding(extendedMethod))) {\n        class_replaceMethod(self, extendedSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));\n    } else {\n        method_exchangeImplementations(originalMethod, extendedMethod);\n    }\n}\n```\n\nTo make sure that we don't break any other code that relies on the `actionForLayer:forKey:` callback, we use a static variable to determine if this is our custom animation context or not. It could have been just a `BOOL` for this single use, but a context is more flexible if we would like to write more code like this in the future:\n\n```objc\nstatic void *DR_currentAnimationContext = NULL;\nstatic void *DR_popAnimationContext     = &DR_popAnimationContext;\n\n- (id<CAAction>)DR_actionForLayer:(CALayer *)layer forKey:(NSString *)event\n{\n    if (DR_currentAnimationContext == DR_popAnimationContext) {\n        // our custom code here...\n    }\n    \n    // call the original implementation\n    return [self DR_actionForLayer:layer forKey:event]; // yes, they are swizzled\n}\n```\n\nIn our implementation, we will make sure to set the animation context before executing the animation block, and then restore the context afterward:\n\n```objc\n + (void)DR_popAnimationWithDuration:(NSTimeInterval)duration\n                          animations:(void (^)(void))animations\n {\n     DR_currentAnimationContext = DR_popAnimationContext;\n     // execute the animations (which will trigger callbacks to the swizzled delegate method)\n     animations();\n     /* more code to come */\n     DR_currentAnimationContext = NULL;\n }\n```\n\nIf all we wanted to do was to add a basic animation from the old value to the new, then we could do so directly from within the delegate callback. But since we want more control of the animation, we need to use a keyframe animation. A keyframe animation requires all of the values to be known, and in our case, the new value hasn't been set so we can't know it yet.\n\nInterestingly, iOS 7 added a block-based animation API that encounters the same obstacle. Using the same inspection technique as above, we can see how it overcomes that obstacle. For each keyframe, the view returns `nil` when the property is changed, but saves the necessary state so that the CAKeyframeAnimation object can be created after all the keyframe blocks have executed. \n\nInspired by that approach, we can create a small class that stores the information that we need to create the animation: what layer was modified, what key path was changed, and what the old value was:\n\n```objc\n @interface DRSavedPopAnimationState : NSObject\n \n @property (strong) CALayer  *layer;\n @property (copy)   NSString *keyPath;\n @property (strong) id        oldValue;\n \n + (instancetype)savedStateWithLayer:(CALayer *)layer\n                             keyPath:(NSString *)keyPath;\n \n @end\n \n @implementation DRSavedPopAnimationState\n \n + (instancetype)savedStateWithLayer:(CALayer *)layer\n                             keyPath:(NSString *)keyPath\n {\n     DRSavedPopAnimationState *savedState = [DRSavedPopAnimationState new];\n     savedState.layer    = layer;\n     savedState.keyPath  = keyPath;\n     savedState.oldValue = [layer valueForKeyPath:keyPath];\n     return savedState;\n }\n \n @end\n```\n\nThen, in our swizzled delegate callback, we simply store the state for the property that changed in a static mutable array:\n\n```objc\n if (DR_currentAnimationContext == DR_popAnimationContext) {\n       [[UIView DR_savedPopAnimationStates] addObject:[DRSavedPopAnimationState savedStateWithLayer:layer\n                                                                                 keyPath:event]];\n       \n       // no implicit animation (it will be added later)\n       return (id<CAAction>)[NSNull null];\n   }\n```\n\nAfter the animation block has executed, all the properties have been changed and their states have been saved. Now, we can enumerate over the saved state and create the keyframe animations:\n\n```objc\n + (void)DR_popAnimationWithDuration:(NSTimeInterval)duration\n                          animations:(void (^)(void))animations\n {\n     DR_currentAnimationContext = DR_popAnimationContext;\n     \n     // do the animation (which will trigger callbacks to the swizzled delegate method)\n     animations();\n     \n     [[self DR_savedPopAnimationStates] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {\n         DRSavedPopAnimationState *savedState   = (DRSavedPopAnimationState *)obj;\n         CALayer *layer    = savedState.layer;\n         NSString *keyPath = savedState.keyPath;\n         id oldValue       = savedState.oldValue;\n         id newValue       = [layer valueForKeyPath:keyPath];\n     \n         CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:keyPath];\n         \n         CGFloat easing = 0.2;\n         CAMediaTimingFunction *easeIn  = [CAMediaTimingFunction functionWithControlPoints:1.0 :0.0 :(1.0-easing) :1.0];\n         CAMediaTimingFunction *easeOut = [CAMediaTimingFunction functionWithControlPoints:easing :0.0 :0.0 :1.0];\n         \n         anim.duration = duration;\n         anim.keyTimes = @[@0, @(0.35), @1];\n         anim.values = @[oldValue, newValue, oldValue];\n         anim.timingFunctions = @[easeIn, easeOut];\n         \n         // back to old value without an animation\n         [CATransaction begin];\n         [CATransaction setDisableActions:YES];\n         [layer setValue:oldValue forKeyPath:keyPath];\n         [CATransaction commit];\n         \n         // animate the \"pop\"\n         [layer addAnimation:anim forKey:keyPath];\n         \n     }];\n     \n     // clean up (remove all the stored state)\n     [[self DR_savedPopAnimationStates] removeAllObjects];\n     \n     DR_currentAnimationContext = nil;\n }\n```\n\nNote that the old model value was set on the layer so that the model and the presentation match when the animation finishes and is removed. \n\nCreating your own API like this is not going to be a good fit for every case, but if you are doing the same animation in many places throughout your app, it can help clean up your code and reduce duplication. Even if you never end up using it, having walked through it once demystifies the UIView block animation APIs, especially if you are comfortable with Core Animation.\n\n## Other Animation Inspiration\n\nI'd like to leave you with a completely different approach to a higher-level animation API: the UIImageView animation. On the surface, it barely resembles a traditional animation API. All that you are doing is specifying an array of images and a duration, and telling the image view to start animating. Behind that abstraction, it results in a discrete keyframe animation of the contents property being added to the image view's layer:\n\n```\n<CAKeyframeAnimation:0x8e5b020; \n    removedOnCompletion = 0; \n    delegate = <_UIImageViewExtendedStorage: 0x8e49230>; \n    duration = 2.5; \n    repeatCount = 2.14748e+09; \n    calculationMode = discrete; \n    values = (\n        \"<CGImage 0x8d6ce80>\",\n        \"<CGImage 0x8d6d2d0>\",\n        \"<CGImage 0x8d5cd30>\"\n    ); \n    keyPath = contents\n>\n```\n\nAnimation APIs can come in many different forms, and the same applies to the animation APIs you write yourself.\n\n[blockDelegate]: https://github.com/EthanArbuckle/IOS-7-Headers/blob/master/Frameworks/UIKit.framework/UIViewAnimationBlockDelegate.h \"UIViewAnimationBlockDelegate class dump\"\n\n[animationState]: https://github.com/rpetrich/iphoneheaders/blob/master/UIKit/UIViewAnimationState.h \"UIViewAnimationState class dump\"\n\n[keyframeState]: https://github.com/limneos/classdump-dyld/blob/master/iphoneheaders/iOS7.0.3/System/Library/Frameworks/UIKit.framework/UIViewKeyframeAnimationState.h \"UIViewKeyframeAnimationState class dump\"\n\n[actionForKeyDocs]: https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instm/CALayer/actionForKey: \"actionForKey: documentation\"\n\n[^animatable]: Almost all layer properties are implicitly animatable. You will see that their brief descriptions in the documentation end with 'animatable.' This applies to pretty much any numeric property, such as the position, size, color, and opacity, and even for boolean properties like isHidden and doubleSided. Properties that are paths are animatable but do not support implicit animations.\n\n[basicAnimation]: https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CABasicAnimation_class/Introduction/Introduction.html \"CABasicAnimation documentation\"\n\n\n[^pop]: Not to be confused with Facebook's new framework.\n\n"
  },
  {
    "path": "2014-06-09-behaviors.md",
    "content": "---\ntitle:  \"Behaviors in iOS Apps\"\ncategory: \"13\"\ndate: \"2014-06-09 09:00:00\"\ntags: article\nauthor:\n  - name: Krzysztof Zabłocki\n    url: https://twitter.com/merowing_\n---\n\n*Update*: There is [sample code](https://github.com/objcio/BehavioursExample) and a [follow-up blogpost](http://merowing.info/2014/06/behaviours-and-xcode-6/).\n\nAs developers, we strive to write clean and well-structured code. There are many patterns we can use to make it happen, and one of the best ones is composition. Composition makes it easier to follow the Single Responsibility Principle and simplify our classes. \n\nInstead of having a Massive View Controller that serves multiple different roles (like data sources and delegates), you separate those roles into different classes. The view controller can then just be responsible for configuring them and coordinating the work. After all, the less code we write, the less code we need to debug and maintain. \n\n\n## So What Exactly is a Behavior?\n\nA behavior is an object responsible for implementing a specific role, e.g. you can have a behavior that implements a parallax animation.\n\nBehaviors in this article will be leveraging Interface Builder to limit the amount of code one has to write, as well as enable more efficient cooperation with non-coders. However, you could use behaviors even if you don't use Interface Builder, and still reap most of the benefits.\n\nMany behaviors won't need any extra code other than that required to set them up, which can be done fully in Interface Builder or in code (same setup method). In many cases, you won't even need to have a property referencing them.\n\n\n## Why Use Behaviors?\n\nLots of iOS projects end up with massive view controller classes, because people put 80% of the application logic in there. This is a serious problem, because since view controllers are the least reusable parts of our code, they are hard to test and maintain.\n\nBehaviors are here to help avoid that scenario, so what benefits can they bring?\n\n### Lighter View Controllers\n\nUsing behaviors means moving lots of the code from view controllers into separate classes. If you use behaviors, you usually end up with very lightweight view controllers. For example, mine are usually less than 100 lines of code.\n\n### Code Reuse\n\nBecause a behavior is responsible for just a single role, it's easy to avoid dependencies between behavior-specific and application-specific logic. This allows you to share the same behaviors across different applications.\n\n### Testability\n\nBehaviors are small classes that work like a black box. This means they are very easy to cover with unit tests. You could test them without even creating real views, and instead by supplying mock objects.\n\n### Ability to Modify Application Logic by Non-Coders\n\nIf we decide to leverage behaviors with Interface Builder, we can teach our designer how to modify application logic. The designer can add or remove behaviors and modify parameters, all without knowing anything about Objective-C.\n\nThis is a great benefit for the workflow, especially on small teams.\n\n\n## How Can One Build Flexible Behaviors?\n\nBehaviors are simple objects that don't require much special code, but there are a few concepts that can really help to make them easier to use as well as more powerful.\n\n### Runtime Attributes\n\nMany developers disregard Interface Builder without even learning it, and as such, they often miss how powerful it can really be. \n\nRuntime attributes are one of the key features of using Interface Builder. They offer you a way to set up custom classes and even set properties on iOS's built-in classes. For example, have you ever had to set a corner radius on your layer? You can do that straight from Interface Builder by simply specifying runtime attributes for it: \n\n![](/images/issue-13/cornerRadius.png)\n\nWhen creating behaviors in Interface Builder, you are going to rely heavily on runtime attributes to set up the behavior options. As a result, there will typically be more runtime attributes: \n\n![](/images/issue-13/runtimeAttributes.png)\n\n\n### Behavior Lifetime\n\nIf an object is created from Interface Builder, it will be created and then removed immediately, unless another object holds a strong reference to it. However, this is not ideal for behaviors that need to be alive as long as the view controllers they are working on is.\n\nOne could create a property on the view controller to keep a strong reference to the behavior, but this is not perfect either, for a few reasons:\n\n- You won't need to interact with many behaviors after creating and configuring them.\n- Creating a property just to keep an object alive is messy.\n- If you want to remove a specific behavior, you need to go and clean up that unused property.\n\n#### Using Objective-C Runtime to Reverse Lifetime Binding \n\nInstead of manually setting up a strong reference to the behavior from the view controller, we make the behavior assign itself as an associated object of the view controller as part of the configuration process, if needed.\n\nThis means that if at some point we need to remove a specific behavior, we just need to remove the code or Interface Builder object that configures that behavior, and no extra changes should be necessary.\n\nThis can be implemented as follows:\n\n```objc\n@interface KZBehavior : UIControl\n\n//! object that this controller life will be bound to\n@property(nonatomic, weak) IBOutlet id owner;\n\n@end\n\n\n@implementation KZBehavior\n\n- (void)setOwner:(id)owner\n{\n    if (_owner != owner) {\n        [self releaseLifetimeFromObject:_owner];\n        _owner = owner;\n        [self bindLifetimeToObject:_owner];\n    }\n}\n\n- (void)bindLifetimeToObject:(id)object\n{\n    objc_setAssociatedObject(object, (__bridge void *)self, self, OBJC_ASSOCIATION_RETAIN_NONATOMIC);\n}\n\n- (void)releaseLifetimeFromObject:(id)object\n{\n    objc_setAssociatedObject(object, (__bridge void *)self, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);\n}\n\n@end\n```\n\nHere we leverage associated objects to create a strong reference to a specific owner object.\n\n### Behavior Events \n\nIt's very useful to have behaviors be able to post events, e.g. when an animation finishes. One can enable that in Interface Builder by making behaviors inherit from `UIControl`. Then a specific behavior can just call:\n\n```objc\n[self sendActionsForControlEvents:UIControlEventValueChanged];\n```\n\nThis will allow you to connect events from behaviors to your view controller code.\n\n\n## Examples of Basic Behaviors\n\nSo what kind of things are easiest to implement as behaviors?\n\nHere's how easy it is to add a parallax animation to a `UIViewController` class (no custom class):\n\n<video controls=\"1\">\n  <source src=\"/images/issue-13/parallaxAnimationBehaviour.mov\"></source>\n</video>\n\nEver needed to pick an image from your user library or camera?\n\n<video controls=\"1\">\n  <source src=\"/images/issue-13/imagePickerBehaviour.mp4\"></source>\n</video>\n\n\n## More Advanced\n\nThe above behaviors were straightforward, but have you ever wondered what to do when we need to have more advanced features? Behaviors are as powerful as you make them, so let's look at some more complex examples.\n\nIf your behavior needs a delegate of some kind, like `UIScrollViewDelegate`, you will soon run into a situation where you can't have more than one behavior like that on a specific screen. But we can deal with that by implementing a simple multiplexer proxy object:\n\n```objc\n@interface MultiplexerProxyBehavior : KZBehavior\n\n//! targets to propagate messages to\n@property(nonatomic, strong) IBOutletCollection(id) NSArray *targets;\n\n@end\n\n\n@implementation MultiplexerProxyBehavior\n\n- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel\n{\n    NSMethodSignature *sig = [super methodSignatureForSelector:sel];\n    if (!sig) {\n        for (id obj in self.targets) {\n            if ((sig = [obj methodSignatureForSelector:sel])) {\n                break;\n            }\n        }\n    }\n    return sig;\n}\n\n- (BOOL)respondsToSelector:(SEL)aSelector\n{\n    BOOL base = [super respondsToSelector:aSelector];\n    if (base) {\n        return base;\n    }\n    \n    return [self.targets.firstObject respondsToSelector:aSelector];\n}\n\n\n- (void)forwardInvocation:(NSInvocation *)anInvocation\n{\n    for (id obj in self.targets) {\n        if ([obj respondsToSelector:anInvocation.selector]) {\n            [anInvocation invokeWithTarget:obj];\n        }\n    }\n}\n\n@end\n```\n\nBy creating an instance of that multiplexer behavior, you can assign it as a delegate of a scroll view (or any other object that has a delegate) so that the delegate calls are forwarded to all of them.\n\n\n## Conclusion\n\nBehaviors are an interesting concept that can simplify your code base and allow you to reuse lots of code across different apps. They will also allow you to work more effectively with non-coders on your team by allowing them to tweak and modify the application behavior. \n"
  },
  {
    "path": "2014-06-09-editorial.md",
    "content": "---\ntitle:  \"Editorial\"\ncategory: \"13\"\ndate: \"2014-06-09 12:00:00\"\ntags: editorial\n---\n\nHey all,\n\nThe very first issue of objc.io appeared one year ago, so we're celebrating our first anniversary! Thank you for all your support during this time and especially for all the excellent contributions we have received from the community.\n\nYou're probably still as overwhelmed as we are by the slew of developer-centric announcements Apple came out with at WWDC last week. We're very happy about the less strict NDA this year, because it means that we don't have to wait until fall to write about any of this. \n\nHowever, before we take a deeper look at all the new stuff, this month we've prepared a more timeless topic for you. We wanted to pick something that comes back to what we've written about in our very first issue: [lighter view controllers](/issues/1-view-controllers). But this time around we chose a broader scope, so that the articles in this issue cover a variety of different issues you might encounter when thinking about app architecture.\n\nLast month we got a chance to sit together with a fantastic group of developers at [UIKonf](http://www.uikonf.com) in Berlin to brainstorm about this topic:\n\n![](/images/issue-13/uikonf-meeting.jpg)\n\nThe result of this is five articles addressing very different architectural issues: the [MVVM concept](/issues/13-architecture/mvvm/) by [Ash Furrow](https://twitter.com/ashfurrow), [avoiding singleton abuse](/issues/13-architecture/singletons/) by [Stephen Poletto](https://twitter.com/stephenpoletto), [subclassing versus composition](/issues/13-architecture/subclassing/) by our own [Chris Eidhof](https://twitter.com/chriseidhof), composing [modular behaviors with Interface Builder](/issues/13-architecture/behaviors/) by [Krzystof Zablłocki](https://twitter.com/merowing_), and finally, an alternative perspective to the traditional MVC architecture, called [VIPER](/issues/13-architecture/viper/) by [Conrad Stoll](https://twitter.com/conradstoll) and [Jeff Gilbert](mailto:jeff.gilbert@mutualmobile.com). \n\nAll the best from a very summery Berlin,\n\nChris, Daniel, and Florian.\n"
  },
  {
    "path": "2014-06-09-mvvm.md",
    "content": "---\ntitle: \"Introduction to MVVM\"\ndate: \"2014-06-09 11:00:00\"\ntags: article\ncategory: \"13\"\nauthor:\n  - name: Ash Furrow\n    url: https://twitter.com/ashfurrow\n---\n\nI got my first iOS job at 500px in 2011. I had been doing iOS contracting for a few years in college, but this was my first, real iOS gig. I was hired as the sole iOS developer to make the beautifully designed iPad app. In only seven weeks, we shipped a 1.0 and continued to iterate, adding more features and, intrinsically, more complexity to the codebase.\n\nIt felt at times like I didn't know what I was doing. I knew my design patterns – like any good coder – but I was way too close to the product I was making to objectively measure the efficacy of my architectural decisions. It took bringing another developer on board the team for me to realize that we were in trouble. \n\nEver heard of MVC? Massive View Controller, some call it. That's certainly how it felt at the time. I won't go into the embarrassing details, but it suffices to say that if I had to do it all over again, I would make different decisions. \n\nOne of the key architectural changes I would make, and have made in apps I've developed since then, would be to use an alternative to Model-View-Controller called Model-View-ViewModel. \n\nSo what is MVVM, exactly? Instead of focusing on the historical context of where MVVM came from, let's take a look at what a typical iOS app looks like and derive MVVM from there:\n\n![Typical Model-View-Controller setup](/images/issue-13/mvvm1.png)\n\nHere we see a typical MVC setup. Models represent data, views represent user interfaces, and view controllers mediate the interactions between the two of them. Cool. \n\nConsider for a moment that, although views and view controllers are technically distinct components, they almost always go hand-in-hand together, paired. When is the last time that a view could be paired with different view controllers? Or vice versa? So why not formalize their connection?\n\n![Intermediate](/images/issue-13/intermediate.png)\n\nThis more accurately describes the MVC code that you're probably already writing. But it doesn't do much to address the massive view controllers that tend to grow in iOS apps. In typical MVC applications, a *lot* of logic gets placed in the view controller. Some of it belongs in the view controller, sure, but a lot of it is what's called 'presentation logic,' in MVVM terms -- things like transforming values from the model into something the view can present, like taking an `NSDate` and turning it into a formatted `NSString`.\n\nWe're missing something from our diagram. Something where we can place all of that presentation logic. We're going to call this the 'view model' – it will sit between the view/controller and the model: \n\n![Model-View-ViewModel](/images/issue-13/mvvm.png)\n\nLooking better! This diagram accurately describes what MVVM is: an augmented version of MVC where we formally connect the view and controller, and move presentation logic out of the controller and into a new object, the view model. MVVM sounds complicated, but it's essentially a dressed-up version of the MVC architecture that you're already familiar with. \n\nSo now that we know *what* MVVM is, *why* would one want to use it? The motivation behind MVVM on iOS, for me, anyway, is that it reduces the complexity of one's view controllers and makes one's presentation logic easier to test. We'll see how it accomplishes these goals with some examples. \n\nThere are three really important points I want you to take away from this article:\n\n- MVVM is compatible with your existing MVC architecture.\n- MVVM makes your apps more testable.\n- MVVM works best with a binding mechanism.\n\nAs we saw earlier, MVVM is basically just a spruced-up version of MVC, so it's easy to see how it can be incorporated into an existing app with a typical MVC architecture. Let's take a simple `Person` model and corresponding view controller:\n\n```objc\n@interface Person : NSObject\n\n- (instancetype)initwithSalutation:(NSString *)salutation firstName:(NSString *)firstName lastName:(NSString *)lastName birthdate:(NSDate *)birthdate;\n\n@property (nonatomic, readonly) NSString *salutation;\n@property (nonatomic, readonly) NSString *firstName;\n@property (nonatomic, readonly) NSString *lastName;\n@property (nonatomic, readonly) NSDate *birthdate;\n\n@end\n```\n\nCool. Now let's say that we have a `PersonViewController` that, in `viewDidLoad`, just sets some labels based on its `model` property: \n\n```objc\n- (void)viewDidLoad {\n    [super viewDidLoad];\n    \n    if (self.model.salutation.length > 0) {\n        self.nameLabel.text = [NSString stringWithFormat:@\"%@ %@ %@\", self.model.salutation, self.model.firstName, self.model.lastName];\n    } else {\n        self.nameLabel.text = [NSString stringWithFormat:@\"%@ %@\", self.model.firstName, self.model.lastName];\n    }\n    \n    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];\n    [dateFormatter setDateFormat:@\"EEEE MMMM d, yyyy\"];\n    self.birthdateLabel.text = [dateFormatter stringFromDate:model.birthdate];\n}\n```\n\nThat's all fairly straightforward, vanilla MVC. Now let's see how we can augment this with a view model: \n\n```objc\n@interface PersonViewModel : NSObject\n\n- (instancetype)initWithPerson:(Person *)person;\n\n@property (nonatomic, readonly) Person *person;\n\n@property (nonatomic, readonly) NSString *nameText;\n@property (nonatomic, readonly) NSString *birthdateText;\n\n@end\n```\n\nOur view model's implementation would look like the following:\n\n```objc\n@implementation PersonViewModel\n\n- (instancetype)initWithPerson:(Person *)person {\n    self = [super init];\n    if (!self) return nil;\n    \n    _person = person;\n    if (person.salutation.length > 0) {\n        _nameText = [NSString stringWithFormat:@\"%@ %@ %@\", self.person.salutation, self.person.firstName, self.person.lastName];\n    } else {\n        _nameText = [NSString stringWithFormat:@\"%@ %@\", self.person.firstName, self.person.lastName];\n    }\n    \n    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];\n    [dateFormatter setDateFormat:@\"EEEE MMMM d, yyyy\"];\n    _birthdateText = [dateFormatter stringFromDate:person.birthdate];\n    \n    return self;\n}\n\n@end\n```\n\nCool. We've moved the presentation logic in `viewDidLoad` into our view model. Our new `viewDidLoad` method is now very lightweight: \n\n```objc\n- (void)viewDidLoad {\n    [super viewDidLoad];\n    \n    self.nameLabel.text = self.viewModel.nameText;\n    self.birthdateLabel.text = self.viewModel.birthdateText;\n}\n```\n\nSo, as you can see, not a lot changed from our MVC architecture. It's the same code, just moved around. It's compatible with MVC, leads to [lighter view controllers](/issues/1-view-controllers/), and is more testable.\n\nTestable, eh? How's that? Well, view controllers are notoriously hard to test since they do so much. In MVVM, we try and move as much of that code as possible into view models. Testing view controllers becomes a lot easier, since they're not doing a whole lot, and view models are very easy to test. Let's take a look:\n\n```objc\nSpecBegin(Person)\n    NSString *salutation = @\"Dr.\";\n    NSString *firstName = @\"first\";\n    NSString *lastName = @\"last\";\n    NSDate *birthdate = [NSDate dateWithTimeIntervalSince1970:0];\n\n    it (@\"should use the salutation available. \", ^{\n        Person *person = [[Person alloc] initWithSalutation:salutation firstName:firstName lastName:lastName birthdate:birthdate];\n        PersonViewModel *viewModel = [[PersonViewModel alloc] initWithPerson:person];\n        expect(viewModel.nameText).to.equal(@\"Dr. first last\");\n    });\n\n    it (@\"should not use an unavailable salutation. \", ^{\n        Person *person = [[Person alloc] initWithSalutation:nil firstName:firstName lastName:lastName birthdate:birthdate];\n        PersonViewModel *viewModel = [[PersonViewModel alloc] initWithPerson:person];\n        expect(viewModel.nameText).to.equal(@\"first last\");\n    });\n\n    it (@\"should use the correct date format. \", ^{\n        Person *person = [[Person alloc] initWithSalutation:nil firstName:firstName lastName:lastName birthdate:birthdate];\n        PersonViewModel *viewModel = [[PersonViewModel alloc] initWithPerson:person];\n        expect(viewModel.birthdateText).to.equal(@\"Thursday January 1, 1970\");\n    });\nSpecEnd\n```\n\nIf we hadn't moved this logic into the view model, we'd have had to instantiate a complete view controller and accompanying view, comparing the values inside our view's labels. Not only would that have been an inconvenient level of indirection, but it also would have represented a seriously fragile test. Now we're free to modify our view hierarchy at will without fear of breaking our unit tests. The testing benefits of using MVVM are clear, even from this simple example, and they become more apparent with more complex presentation logic. \n\nNote that in this simple example, the model is immutable, so we can assign our view model's properties at initialization time. For mutable models, we'd need to use some kind of binding mechanism so that the view model can update its properties when the model backing those properties changes. Furthermore, once the models on the view model change, the views' properties need to be updated as well. A change from the model should cascade down through the view model into the view. \n\nOn OS X, one can use Cocoa bindings, but we don't have that luxury on iOS. Key-value observation comes to mind, and it does a great job. However, it's a lot of boilerplate for simple bindings, especially if there are lots of properties to bind to. Instead, I like to use ReactiveCocoa, but there's nothing forcing one to use ReactiveCocoa with MVVM. MVVM is a great paradigm that stands on its own and is only made better with a nice binding framework. \n\nWe've covered a lot: deriving MVVM from plain MVC, seeing how they're compatible paradigms, looking at MVVM from a testability perspective, and seeing that MVVM works best when paired with a binding mechanism. If you're interested in learning more about MVVM, you can check out [this blog post](http://www.teehanlax.com/blog/model-view-viewmodel-for-ios/) explaining the benefits of MVVM in greater detail, or [this article](http://www.teehanlax.com/blog/krush-ios-architecture/) about how we used MVVM on a recent project of mine to great success. I also have a fully tested, MVVM-based app called [C-41](https://github.com/AshFurrow/C-41) that's open sourced. Check it out and [let me know](http://twitter.com/ashfurrow) if you have any questions. \n"
  },
  {
    "path": "2014-06-09-singletons.md",
    "content": "---\ntitle:  \"Avoiding Singleton Abuse\"\ncategory: \"13\"\ndate: \"2014-06-09 10:00:00\"\ntags: article\nauthor:\n  - name: Stephen Poletto\n    url: https://twitter.com/stephenpoletto\n---\n\nSingletons are one of the core design patterns used throughout Cocoa. In fact, Apple's Developer Library considers the singleton one of the \"Cocoa Core Competencies.\" As iOS developers, we're familiar with interacting with singletons, from `UIApplication` to `NSFileManager`. We've seen countless examples of singleton usage in open-source projects, in Apple's code samples, and on StackOverflow. Xcode even has a default code snippet, the \"Dispatch Once\" snippet, which makes it incredibly easy to add a singleton to your code:\n\n```objc\n+ (instancetype)sharedInstance\n{\n    static dispatch_once_t once;\n    static id sharedInstance;\n    dispatch_once(&once, ^{\n        sharedInstance = [[self alloc] init];\n    });\n    return sharedInstance;\n}\n```\n\nFor these reasons, singletons are commonplace in iOS programming. The problem is that they're easy to abuse.\n\nWhile others have called singletons an 'anti-pattern,' 'evil,' and ['pathological liars'][pathologicalLiars], I won't completely rule out the merit of singletons. Instead, I want to demonstrate a few problems with singletons so that the next time you're about to auto-complete that `dispatch_once` snippet, you think twice about the implications.\n\n## Global State\n\nMost developers agree that global mutable state is a bad thing. Statefulness makes programs hard to understand and hard to debug. We object-oriented programmers have much to learn from functional programming, in terms of minimizing the statefulness of code.\n\n```objc\n@implementation SPMath {\n    NSUInteger _a;\n    NSUInteger _b;\n}\n\n- (NSUInteger)computeSum\n{\n    return _a + _b;\n}\n```\n\nIn the above implementation of a simple math library, the programmer is expected to set instance variables `_a` and `_b` to the proper values before invoking `computeSum`. There are a few problems here:\n\n1. `computeSum` does not make the fact that it depends upon states `_a` and `_b` explicit by taking the values as parameters. Instead of inspecting the interface and understanding which variables control the output of the function, another developer reading this code must inspect the implementation to understand the dependency. Hidden dependencies are bad.\n2. When modifying `_a` and `_b` in preparation for calling `computeSum`, the programmer needs to be sure the modification does not affect the correctness of any other code that depends upon these variables. This is particularly difficult in multi-threaded environments.\n\nContrast the above example with this: \n  \n```objc\n+ (NSUInteger)computeSumOf:(NSUInteger)a plus:(NSUInteger)b\n{\n    return a + b;\n}\n```\n\nHere, the dependency on `a` and `b` is made explicit. We don't need to mutate instance state in order to call this method. And we don't need to worry about leaving behind persistent side effects as a result of calling this method. As a note to the reader of this code, we can even make this method a class method to indicate that it does not modify instance state.\n\nSo how does this example relate to singletons? In the words of Miško Hevery, [\"Singletons are global state in sheep’s clothing.\"][sheepsClothing] A singleton can be used anywhere, without explicitly declaring the dependency. Just like `_a` and `_b` were used in `computeSum` without the dependency being made explicit, any module of the program can call `[SPMySingleton sharedInstance]` and get access to the singleton. This means any side effects of interacting with the singleton can affect arbitrary code elsewhere in the program.\n\n\n```objc\n@interface SPSingleton : NSObject\n\n+ (instancetype)sharedInstance;\n\n- (NSUInteger)badMutableState;\n- (void)setBadMutableState:(NSUInteger)badMutableState;\n\n@end\n\n@implementation SPConsumerA\n\n- (void)someMethod\n{\n    if ([[SPSingleton sharedInstance] badMutableState]) {\n        // ...\n    }\n}\n\n@end\n\n@implementation SPConsumerB\n\n- (void)someOtherMethod\n{\n    [[SPSingleton sharedInstance] setBadMutableState:0];\n}\n\n@end\n```\n\nIn the example above, `SPConsumerA` and `SPConsumerB` are two completely independent modules of the program. Yet `SPConsumerB` is able to affect the behavior of `SPConsumerA` through the shared state provided by the singleton. This should only be possible if consumer B is given an explicit reference to A, making clear the relationship between the two. The singleton here, due to its global and stateful nature, causes hidden and implicit coupling between seemingly unrelated modules.\n\nLet's take a look at a more concrete example, and expose one additional problem with global mutable state. Let's say we want to build a web viewer inside our app. To support this web viewer, we build a simple URL cache:\n\n```objc\n@interface SPURLCache\n\n+ (SPCache *)sharedURLCache;\n\n- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request;\n\n@end    \n```\n\nThe developer working on the web viewer starts writing some unit tests to make sure the code works as expected in a few different situations. First, he or she writes a test to make sure the web viewer shows an error when there's no device connectivity. Then he or she writes a test to make sure the web viewer handles server failures properly. Finally, he or she writes a test for the basic success case, to make sure the returned web content is shown properly. The developer runs all of the tests, and they work as expected. Nice!\n\nA few months later, these tests start failing, even though the web viewer code hasn't changed since it was first written! What happened?\n\nIt turns out someone changed the order of the tests. The success case test is running first, followed by the other two. The error cases are now succeeding unexpectedly, because the singleton URL cache is caching the response across the tests.\n\nPersistent state is the enemy of unit testing, since unit testing is made effective by each test being independent of all other tests. If state is left behind from one test to another, then the order of execution of tests suddenly matters. Buggy tests, especially when a test succeeds when it shouldn't, are a very bad thing.\n\n## Object Lifecycle\nThe other major problem with singletons is their lifecycle. When adding a singleton to your program, it's easy to think, \"There will only ever be one of these.\" But in much of the iOS code I've seen in the wild, that assumption can break down.\n\nFor example, suppose we're building an app where users can see a list of their friends. Each of their friends has a profile picture, and we want the app to be able to download and cache those images on the device. With the `dispatch_once` snippet handy, we might find ourselves writing an `SPThumbnailCache` singleton:\n\n```objc\n@interface SPThumbnailCache : NSObject\n\n+ (instancetype)sharedThumbnailCache;\n\n- (void)cacheProfileImage:(NSData *)imageData forUserId:(NSString *)userId;\n- (NSData *)cachedProfileImageForUserId:(NSString *)userId;\n\n@end\n```\n\nWe continue building out the app, and all seems well in the world, until one day, when we decide it's time to implement the 'log out' functionality, so users can switch accounts inside the app. Suddenly, we have a nasty problem on our hands: user-specific state is stored in a global singleton. When the user signs out of the app, we want to be able to clean up all persistent states on disk. Otherwise, we'll leave behind orphaned data on the user's device, wasting precious disk space. In case the user signs out and then signs into a new account, we also want to be able to have a new `SPThumbnailCache` for the new user.\n\nThe problem here is that singletons, by definition, are assumed to be \"create once, live forever\" instances. You could imagine a few solutions to the problem outlined above. Perhaps we could tear down the singleton instance when the user signs out:\n\n```objc\nstatic SPThumbnailCache *sharedThumbnailCache;\n\n+ (instancetype)sharedThumbnailCache\n{\n    if (!sharedThumbnailCache) {\n        sharedThumbnailCache = [[self alloc] init];\n    }\n    return sharedThumbnailCache;\n}\n\n+ (void)tearDown\n{\n    // The SPThumbnailCache will clean up persistent states when deallocated\n    sharedThumbnailCache = nil;\n}\n```\n\nThis is a flagrant abuse of the singleton pattern, but it will work, right?\n\nWe could certainly make this solution work, but the cost is far too great. For one, we've lost the simplicity of the `dispatch_once` solution, a solution which guarantees thread safety and that all code calling `[SPThumbnailCache sharedThumbnailCache]` only ever gets the same instance. We now need to be extremely careful about the order of code execution for code that utilizes the thumbnail cache. Suppose while the user is in the process of signing out, there's some background task that is in the process of saving an image into the cache: \n\n```objc\ndispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{\n    [[SPThumbnailCache sharedThumbnailCache] cacheProfileImage:newImage forUserId:userId];\n});\n```\n\nWe need to be certain `tearDown` doesn't execute until after that background task completes. This ensures the `newImage` data will get cleaned up properly. Or, we need to make sure the background task is canceled when the thumbnail cache is shut down. Otherwise, a new thumbnail cache will be lazily created, and stale user state (the `newImage`) will be stored inside of it. \n\nSince there's no distinct owner for the singleton instance (i.e. the singleton manages its own lifecycle), it becomes very difficult to ever 'shut down' a singleton.\n\nAt this point, I hope you're saying, \"The thumbnail cache shouldn't have ever been a singleton!\" The problem is that an object's lifecycle may not be fully understood at the start of a project. As a concrete example, the Dropbox iOS app only ever had support for a single user account to be signed into. The app existed in this state for years, until one day when we wanted to support [multiple user accounts][twoDropboxes] (both personal and business accounts) to be signed in simultaneously. All of a sudden, assumptions that \"there will only ever be a single user signed in at a time\" started to break down. By assuming an object's lifecycle will match the lifecycle of your application, you'll limit the extensibility of your code, and you may need to pay for that assumption later when product requirements change.\n\nThe lesson here is that singletons should be preserved only for state that is global, and not tied to any scope. If state is scoped to any session shorter than \"a complete lifecycle of my app,\" that state should not be managed by a singleton. A singleton that's managing user-specific state is a code smell, and you should critically reevaluate the design of your object graph.\n\n## Avoiding Singletons\n\nSo, if singletons are so bad for scoped state, how do we avoid using them?\n\nLet's revisit the example above. Since we have a thumbnail cache that caches state specific to an individual user, let's define a user object:\n\n```objc\n@interface SPUser : NSObject\n\n@property (nonatomic, readonly) SPThumbnailCache *thumbnailCache;\n\n@end\n\n@implementation SPUser\n\n- (instancetype)init\n{\n    if ((self = [super init])) {\n        _thumbnailCache = [[SPThumbnailCache alloc] init];\n\n        // Initialize other user-specific state...\n    }\n    return self;\n}\n\n@end\n```\n\nWe now have an object to model an authenticated user session, and we can store all user-specific state under this object. Now suppose we have a view controller that renders the list of friends:\n\n```objc\n@interface SPFriendListViewController : UIViewController\n\n- (instancetype)initWithUser:(SPUser *)user;\n\n@end\n```\n\nWe can explicitly pass the authenticated user object into the view controller. This technique of passing a dependency into a dependent object is more formally referred to as [dependency injection,][dependencyInjection] and it has ton of advantages:\n\n1. It makes clear to the reader of this interface that the `SPFriendListViewController` should only ever be shown when there's a signed-in user.\n2. The `SPFriendListViewController` can maintain a strong reference to the user object as long as it's being used. For instance, updating the earlier example, we can save an image into the thumbnail cache within a background task as follows:\n        \n```objc\ndispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{\n    [_user.thumbnailCache cacheProfileImage:newImage forUserId:userId];\n});\n```\n\nWith this background task still outstanding, code elsewhere in the application is able to create and utilize an entirely new `SPUser` object, without blocking further interaction while the first instance is being torn down.\n\nTo demonstrate the second point a little further, let's visualize the object graph before and after using dependency injection.\n\nSuppose our `SPFriendListViewController` is currently the root view controller in the window. With the singleton model, we have an object graph that looks like this:\n\n![](/images/issue-13/Screen%20Shot%202014-06-02%20at%205.21.20%20AM.png)\n\nThe view controller itself, along with a list of custom image views, interacts with the `sharedThumbnailCache`. When the user logs out, we want to clear the root view controller and take the user back to a sign-in screen:\n\n![](/images/issue-13/Screen%20Shot%202014-06-02%20at%205.53.45%20AM.png)\n\nThe problem here is that the friend list view controller might still be executing code (due to background operations), and therefore may still have outstanding calls pending to the `sharedThumbnailCache`.\n\nContrast this with the solution that utilizes dependency injection:\n\n![](/images/issue-13/Screen%20Shot%202014-06-02%20at%205.38.59%20AM.png)\n\nSuppose, for simplicity, that the `SPApplicationDelegate` manages the `SPUser` instance (in practice, you probably want to offload such user state management to another object to keep your application delegate [lighter][lighterViewControllers]). When the friend list view controller is installed in the window, a reference to the user is passed. This reference can be funneled down the object graph to the profile image views as well. Now, when the user logs out, our object graph looks like this:\n\n![](/images/issue-13/Screen%20Shot%202014-06-02%20at%205.54.07%20AM.png)\n\nThe object graph looks pretty similar to the case in which we used a singleton. So what's the big deal?\n\nThe problem is scope. In the singleton case, the `sharedThumbnailCache` is still accessible to arbitrary modules of the program. Suppose the user quickly signs in to a new account. The new user will want to see his or her friends, too, which means interacting with the thumbnail cache again:\n\n![](/images/issue-13/Screen%20Shot%202014-06-02%20at%205.59.25%20AM.png)\n\nWhen the user signs in to a new account, we should be able to construct and interact with a brand new `SPThumbnailCache`, with no attention paid to the destruction of the old thumbnail cache. The old view controllers and old thumbnail cache should be cleaned up lazily in the background on their own accord, based on the typical rules of object management. In short, we should isolate the state associated with user A from the state associated with user B:\n\n![](/images/issue-13/Screen%20Shot%202014-06-02%20at%206.43.56%20AM.png)\n\n## Conclusion\n\nHopefully nothing in this article reads as particularly novel. People have been complaining about the abuse of singletons for years and we all know global state is bad. But in the world of iOS development, singletons are so commonplace that we can sometimes forget the lessons learned from years of object-oriented programming elsewhere. \n\nThe key takeaway from all of this is that in object-oriented programming we want to minimize the scope of mutable state. Singletons stand in direct opposition to that, since they make mutable state accessible from anywhere in the program. The next time you think to use a singleton, I hope you consider dependency injection as an alternative.\n\n\n[pathologicalLiars]: http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/\n[sheepsClothing]: http://misko.hevery.com/2008/08/25/root-cause-of-singletons/\n[dependencyInjection]: http://en.wikipedia.org/wiki/Dependency_injection\n[lighterViewControllers]: /issues/1-view-controllers/lighter-view-controllers/\n[twoDropboxes]: https://www.dropbox.com/business/two-dropboxes\n"
  },
  {
    "path": "2014-06-09-subclassing.md",
    "content": "---\ntitle:  \"Subclassing\"\ncategory: \"13\"\ndate: \"2014-06-09 08:00:00\"\ntags: article\nauthor:\n  - name: Chris Eidhof\n    url: https://twitter.com/chriseidhof\n---\n\nThis article is a little bit different from the usual articles I write. It's more a collection of thoughts and patterns than a guide. Almost all the patterns I will describe are found out the hard way: by making mistakes. By no means do I consider myself an authority on subclassing, but I did want to share a few things I've learned. Don't read this as a definitive guide, but rather as a collection of examples.\n\nWhen asked about OOP (object-oriented programming), Alan Kay (the inventor) wrote that it's not about classes, but rather about messaging.[^1] Still, a lot of people focus on creating class hierarchies. In this article, we'll look at some cases where it's useful, but we'll mostly pay attention to alternatives to creating complicated class hierarchies. In our experience, this leads to code that's simpler and easier to maintain. A lot of things have been written on this topic, which you can find in books like [Clean Code](http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882) and [Code Complete](http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670), both of which are recommended reading.\n\n[^1]: http://c2.com/cgi/wiki?AlanKayOnMessaging\n\n\n## When to Subclass\n\nFirst, let's talk about some cases where it makes sense to create subclasses. If you're building a `UITableViewCell` with custom layout, create a subclass. The same holds for almost every view; once you start doing layout, it makes sense to move this into a subclass, so that you not only have your code nicely bundled up, but also have a reusable object that you can share across projects.\n\nSuppose you're targeting multiple platforms and versions from your code, and you need to somehow write custom bits for every platform and version. It might then make sense to create an `OBJDevice` class, which has subclasses like `OBJIPhoneDevice` and `OBJIPadDevice`, and maybe even deeper subclasses like `OBJIPhone5Device`, which override specific methods. For example, your `OBJDevice` could contain a method `applyRoundedCornersToView:withRadius:`. It has a default implementation, but can be overridden by specific subclasses.\n\nAnother case where subclassing might be very helpful is in model objects. Most of the time, my model objects inherit from a class that implements `isEqual:`, `hash`, `copyWithZone:`, and `description`. These methods are implemented once by iterating over the properties, making it a lot harder to make mistakes. (If you're looking for a base class like this, you can consider using [Mantle](https://github.com/mantle/mantle), which does exactly this, and more.)\n\n## When Not to Subclass\n\nIn a lot of projects that I've worked on, I've seen deep hierarchies of subclasses. I am guilty of doing this as well. Unless the hierarchies are very shallow, you very quickly tend to hit limits.[^2] \n\nLuckily, if you find yourself in a deep hierarchy like that, there are lot of alternatives. In the sections below, we'll go into each in more detail. If your subclasses merely share the same interface, protocols can be a very good alternative. If you know an object needs to be modified a lot, you might want to use delegates to dynamically change and configure it. When you want to extend an existing object with some simple functionality, categories might be an option. When you have a set of subclasses that each override the same methods, you might instead use configuration objects. And finally, when you want to reuse some functionality, it might be better to compose multiple objects instead of extending them.\n\n[^2]: http://c2.com/cgi/wiki?LimitsOfHierarchies\n\n## Alternatives\n\n### Alternative: Protocols\n\nOften, a reason to use subclassing is when you want to make sure that an object responds to certain messages. Consider an app where you have a player object, that can play videos. Now, if you want to add YouTube support, you want the same interface, but a different implementation. One way you can achieve this with subclassing is like this:\n\n```objc\n@interface Player : NSObject\n\n- (void)play;\n- (void)pause;\n\n@end\n\n\n@interface YouTubePlayer : Player\n\n@end\n```\n\nMost likely, the two classes don't share a lot of code, just the same interface. When that's the case, it might be a good solution to use protocols instead. Using protocols, you would write the code like this:\n\n```objc\n@protocol VideoPlayer <NSObject>\n\n- (void)play;\n- (void)pause;\n\n@end\n\n\n@interface Player : NSObject <VideoPlayer>\n\n@end\n\n\n@interface YouTubePlayer : NSObject <VideoPlayer>\n\n@end\n```\n\nThis way, the `YouTubePlayer` doesn't need to know about the `Player` internals.\n\n\n### Alternative: Delegation\n\nAgain, suppose you have a `Player` class like in the example above. Now, at one place, you might want to perform a custom action on play. Doing this is relatively easy: you can create a custom subclass, override the `play` method, call `[super play]`, and then do your custom work. This is one way to deal with it. Another way is to change your `Player` object and give it a delegate. For example:\n\n```objc\n@class Player;\n\n@protocol PlayerDelegate\n\n- (void)playerDidStartPlaying:(Player *)player;\n\n@end\n\n\n@interface Player : NSObject\n\n@property (nonatomic,weak) id<PlayerDelegate> delegate;\n\n- (void)play;\n- (void)pause;\n\n@end\n```\n\nNow, in the player's `play` method, the delegate gets sent the `playerDidStartPlaying:` message. Any consumers of this class can now just implement the delegate protocol instead of having to subclass, and the `Player` object can stay very generic. This is a very powerful technique, which Apple uses abundantly in its own frameworks. Think of classes like `UITextField`, but also `NSLayoutManager`. Sometimes you want to group different methods together in separate protocols, such as `UITableView` -- which has not only a delegate but also a data source -- does.\n\n\n### Alternative: Categories\n\nSometimes, you might want to extend an object with a little bit of extra functionality. Suppose you want to extend `NSArray` by adding a method `arrayByRemovingFirstObject`. Instead of subclassing, you can put this into a category. It works like this:\n\n```objc\n@interface NSArray (OBJExtras)\n\n- (void)obj_arrayByRemovingFirstObject;\n\n@end\n```\n\nWhen using categories and extending a class that's not your own, it's good practice to prefix your methods. If you don't, there is a chance that somebody else might implement the same method using the same technique. Then, if the behavior doesn't match, unexpected things could happen.\n\nOne of the dangers of using categories is that you might end up with a large amount of categories, and you can lose your overview. In that case, it's probably easier to create custom classes.\n\n\n### Alternative: Configuration Objects\n\nOne of the mistakes I keep on making (but can now recognize fairly quickly), is having a class with some abstract functionality, and then a lot of subclasses that override one specific method. For example, in a presentation app, you might have a class `Theme`, which has a couple of properties, such as `backgroundColor` and `font`, and some logic for laying things out on a slide.\n\nThen, for each theme, you create a subclass of `Theme`, override a method (e.g. `setup`), and configure the properties. Using the superclass directly wouldn't make sense. In this case, you can make your code a bit simpler by using configuration objects. You can keep the shared logic in the `Theme` class (e.g. slide layout), but move the configuration into simpler objects that only have properties.\n\nFor example, a `ThemeConfiguration` class would have the `backgroundColor` and `font` properties, and the `Theme` class gets a value of this class in the initializer.\n\n\n### Alternative: Composition\n\nThe most powerful alternative to subclassing is composition. If you want to reuse existing code but you're not sharing the same interface, composition can be your weapon of choice. For example, suppose you are designing a caching class:\n\n```objc\n@interface OBJCache : NSObject\n\n- (void)cacheValue:(id)value forKey:(NSString *)key;\n- (void)removeCachedValueForKey:(NSString *)key;\n\n@end\n```\n\nOne easy way to do this is by subclassing `NSDictionary` and implementing the two methods by calling to dictionary methods: \n\n```objc\n@interface OBJCache : NSDictionary\n```\n\nHowever, there are a couple of drawbacks. The fact that it is implemented with a dictionary should be an implementation detail. Now, everywhere where you expect an `NSDictionary` parameter, you could provide an `OBJCache` value. If you would ever want to switch to something completely different (e.g. your own library), you might need to refactor a lot of code.\n\nA better approach is to store this dictionary in a private property (or instance variable), and only expose the two `cache` methods. Now, you maintain the flexibility to change the implementation as you gain more insight, and consumers of your class don't need to be refactored.\n\n"
  },
  {
    "path": "2014-06-09-viper.md",
    "content": "---\ntitle: \"Architecting iOS Apps with VIPER\"\ncategory: \"13\"\ndate: \"2014-06-07 07:00:00\"\ntags: article\nauthor:\n  - name: Jeff Gilbert\n    url: mailto:jeff.gilbert@mutualmobile.com\n  - name: Conrad Stoll\n    url: https://twitter.com/conradstoll\n---\n\nIt's well known in the field of architecture that we shape our buildings, and afterward our buildings shape us. As all programmers eventually learn, this applies just as well to building software.\n\nIt's important to design our code so that each piece is easily identifiable, has a specific and obvious purpose, and fits together with other pieces in a logical fashion. This is what we call software architecture. Good architecture is not what makes a product successful, but it does make a product maintainable and helps preserve the sanity of the people maintaining it!\n\nIn this article, we will introduce an approach to iOS application architecture called [VIPER](https://mutualmobile.com/posts/meet-viper-fast-agile-non-lethal-ios-architecture-framework). VIPER has been used to build many large projects, but for the purposes of this article we will be showing you VIPER by building a to-do list app. You can follow along with the example project [here on GitHub](https://github.com/objcio/issue-13-viper):\n\n<video poster=\"/images/issue-13/2014-06-07-viper-screenshot.png\" controls=\"1\">\n  <source src=\"/images/issue-13/2014-06-07-viper-preview.mp4\"></source>\n</video>\n\n## What is VIPER?\nTesting was not always a major part of building iOS apps. As we embarked on a quest to improve our testing practices at [Mutual Mobile](https://github.com/mutualmobile/), we found that writing tests for iOS apps was difficult. We decided that if we were going to improve the way we test our software, we would first need to come up with a better way to architect our apps. We call that method VIPER.\n\nVIPER is an application of [Clean Architecture](http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html) to iOS apps. The word VIPER is a backronym for View, Interactor, Presenter, Entity, and Routing. Clean Architecture divides an app’s logical structure into distinct layers of responsibility. This makes it easier to isolate dependencies (e.g. your database) and to test the interactions at the boundaries between layers:\n\n![VIPER stands for View Interactor Presenter Entity Routing.](/images/issue-13/2014-06-07-viper-intro.jpg)\n\nMost iOS apps are architected using MVC (model–view–controller). Using MVC as an application architecture can guide you to thinking every class is either a model, a view, or a controller. Since much of the application logic does not belong in a model or view, it usually ends up in the controller. This leads to an issue known as a [Massive View Controller](https://twitter.com/Colin_Campbell/status/293167951132098560), where the view controllers end up doing too much. [Slimming down](/issues/1-view-controllers/lighter-view-controllers/) these massive view controllers is not the only challenge faced by iOS developers seeking to improve the quality of their code, but it is a great place to start.\n\nVIPER's distinct layers help deal with this challenge by providing clear locations for application logic and navigation-related code. With VIPER applied, you'll notice that the view controllers in our to-do list example are lean, mean, view controlling machines. You'll also find that the code in the view controllers and all of the other classes is easy to understand, easier to test, and as a result, also easier to maintain.\n\n## Application Design Based on Use Cases\nApps are often implemented as a set of use cases. Use cases are also known as acceptance criteria, or behaviors, and describe what an app is meant to do. Maybe a list needs to be sortable by date, type, or name. That's a use case. A use case is the layer of an application that is responsible for business logic. Use cases should be independent from the user interface implementation of them. They should also be small and well-defined. Deciding how to break down a complex app into smaller use cases is challenging and requires practice, but it's a helpful way to limit the scope of each problem you are solving and each class that you are writing.\n\nBuilding an app with VIPER involves implementing a set of components to fulfill each use case. Application logic is a major part of implementing a use case, but it's not the only part. The use case also affects the user interface. Additionally, it's important to consider how the use case fits together with other core components of an application, such as networking and data persistence. Components act like plugins to the use cases, and VIPER is a way of describing what the role of each of these components is and how they can interact with one another.\n\nOne of the use cases or requirements for our to-do list app was to group the to-dos in different ways based on a user's selection. By separating the logic that organizes that data into a use case, we are able to keep the user interface code clean and easily wrap the use case in tests to make sure it continues to work the way we expect it to.\n\n## Main Parts of VIPER\nThe main parts of VIPER are:\n\n- _View_: displays what it is told to by the Presenter and relays user input back to the Presenter.\n- _Interactor_: contains the business logic as specified by a use case.\n- _Presenter_: contains view logic for preparing content for display (as received from the Interactor) and for reacting to user inputs (by requesting new data from the Interactor).\n- _Entity_: contains basic model objects used by the Interactor.\n- _Routing_: contains navigation logic for describing which screens are shown in which order.\n\nThis separation also conforms to the [Single Responsibility Principle](http://www.objectmentor.com/resources/articles/srp.pdf). The Interactor is responsible to the business analyst, the Presenter represents the interaction designer, and the View is responsible to the visual designer.\n\nBelow is a diagram of the different components and how they are connected:\n\n![VIPER breaks down an app into different components based around use cases, including components that create the user interface and the logic that powers it.](/images/issue-13/2014-06-07-viper-wireframe.png)\n\nWhile the components of VIPER can be implemented in an application in any order, we've chosen to introduce the components in the order that we recommend implementing them. You'll notice that this order is roughly consistent with the process of building an entire application, which starts with discussing what the product needs to do, followed by how a user will interact with it.\n\n### Interactor\nAn Interactor represents a single use case in the app. It contains the business logic to manipulate model objects (Entities) to carry out a specific task. The work done in an Interactor should be independent of any UI. The same Interactor could be used in an iOS app or an OS X app.\n\nBecause the Interactor is a PONSO (Plain Old `NSObject`) that primarily contains logic, it is easy to develop using TDD.\n\nThe primary use case for the sample app is to show the user any upcoming to-do items (i.e. anything due by the end of next week). The business logic for this use case is to find any to-do items due between today and the end of next week and assign a relative due date: today, tomorrow, later this week, or next week.\n\nBelow is the corresponding method from `VTDListInteractor`:\n\n```objc\n- (void)findUpcomingItems\n{\n    __weak typeof(self) welf = self;\n    NSDate* today = [self.clock today];\n    NSDate* endOfNextWeek = [[NSCalendar currentCalendar] dateForEndOfFollowingWeekWithDate:today];\n    [self.dataManager todoItemsBetweenStartDate:today endDate:endOfNextWeek completionBlock:^(NSArray* todoItems) {\n        [welf.output foundUpcomingItems:[welf upcomingItemsFromToDoItems:todoItems]];\n    }];\n}\n```\n\n### Entity\nEntities are the model objects manipulated by an Interactor. Entities are only manipulated by the Interactor. The Interactor never passes entities to the presentation layer (i.e. Presenter).\n\nEntities also tend to be PONSOs. If you are using Core Data, you will want your managed objects to remain behind your data layer. Interactors should not work with `NSManagedObjects`.\n\nHere is the Entity for our to-do item:\n\n```objc\n@interface VTDTodoItem : NSObject\n\n@property (nonatomic, strong)   NSDate*     dueDate;\n@property (nonatomic, copy)     NSString*   name;\n\n+ (instancetype)todoItemWithDueDate:(NSDate*)dueDate name:(NSString*)name;\n\n@end\n```\n\nDon’t be surprised if your entities are just data structures. Any application-dependent logic will most likely be in an Interactor.\n\n### Presenter\nThe Presenter is a PONSO that mainly consists of logic to drive the UI. It knows when to present the user interface. It gathers input from user interactions so it can update the UI and send requests to an Interactor.\n\nWhen the user taps the + button to add a new to-do item, `addNewEntry` gets called. For this action, the Presenter asks the wireframe to present the UI for adding a new item:\n\n```objc\n- (void)addNewEntry\n{\n    [self.listWireframe presentAddInterface];\n}\n```\n\nThe Presenter also receives results from an Interactor and converts the results into a form that is efficient to display in a View.\n\nBelow is the method that receives upcoming items from the Interactor. It will process the data and determine what to show to the user:\n\n```objc\n- (void)foundUpcomingItems:(NSArray*)upcomingItems\n{\n    if ([upcomingItems count] == 0)\n    {\n        [self.userInterface showNoContentMessage];\n    }\n    else\n    {\n        [self updateUserInterfaceWithUpcomingItems:upcomingItems];\n    }\n}\n```\n\nEntities are never passed from the Interactor to the Presenter. Instead, simple data structures that have no behavior are passed from the Interactor to the Presenter. This prevents any 'real work' from being done in the Presenter. The Presenter can only prepare the data for display in the View. \n\n### View\nThe View is passive. It waits for the Presenter to give it content to display; it never asks the Presenter for data. Methods defined for a View (e.g. LoginView for a login screen) should allow a Presenter to communicate at a higher level of abstraction, expressed in terms of its content, and not how that content is to be displayed. The Presenter does not know about the existence of `UILabel`, `UIButton`, etc. The Presenter only knows about the content it maintains and when it should be displayed. It is up to the View to determine how the content is displayed.\n\nThe View is an abstract interface, defined in Objective-C with a protocol. A `UIViewController` or one of its subclasses will implement the View protocol. For example, the 'add' screen from our example has the following interface:\n\n```objc\n@protocol VTDAddViewInterface <NSObject>\n\n- (void)setEntryName:(NSString *)name;\n- (void)setEntryDueDate:(NSDate *)date;\n\n@end\n```\n\nViews and view controllers also handle user interaction and input. It's easy to understand why view controllers usually become so large, since they are the easiest place to handle this input to perform some action. To keep our view controllers lean, we need to give them a way to inform interested parties when a user takes certain actions. The view controller shouldn't be making decisions based on these actions, but it should pass these events along to something that can.\n\nIn our example, Add View Controller has an event handler property that conforms to the following interface:\n\n```objc\n@protocol VTDAddModuleInterface <NSObject>\n\n- (void)cancelAddAction;\n- (void)saveAddActionWithName:(NSString *)name dueDate:(NSDate *)dueDate\n\n@end\n```\n\nWhen the user taps on the cancel button, the view controller tells this event handler that the user has indicated that it should cancel the add action. That way, the event handler can take care of dismissing the add view controller and telling the list view to update.\n\nThe boundary between the View and the Presenter is also a great place for [ReactiveCocoa](https://github.com/ReactiveCocoa/ReactiveCocoa). In this example, the view controller could also provide methods to return signals that represent button actions. This would allow the Presenter to easily respond to those signals without breaking separation of responsibilities.\n\n### Routing\nRoutes from one screen to another are defined in the wireframes created by an interaction designer. In VIPER, the responsibility for Routing is shared between two objects: the Presenter, and the wireframe. A wireframe object owns the `UIWindow`, `UINavigationController`, `UIViewController`, etc. It is responsible for creating a View/ViewController and installing it in the window. \n\nSince the Presenter contains the logic to react to user inputs, it is the Presenter that knows when to navigate to another screen, and which screen to navigate to. Meanwhile, the wireframe knows how to navigate. So, the Presenter will use the wireframe to perform the navigation. Together, they describe a route from one screen to the next. \n\nThe wireframe is also an obvious place to handle navigation transition animations. Take a look at this example from the add wireframe:\n\n```objc\n@implementation VTDAddWireframe\n\n- (void)presentAddInterfaceFromViewController:(UIViewController *)viewController \n{\n    VTDAddViewController *addViewController = [self addViewController];\n    addViewController.eventHandler = self.addPresenter;\n    addViewController.modalPresentationStyle = UIModalPresentationCustom;\n    addViewController.transitioningDelegate = self;\n\n    [viewController presentViewController:addViewController animated:YES completion:nil];\n\n    self.presentedViewController = viewController;\n}\n\n#pragma mark - UIViewControllerTransitioningDelegate Methods\n\n- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed \n{\n    return [[VTDAddDismissalTransition alloc] init];\n}\n\n- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented\n                                                                  presentingController:(UIViewController *)presenting\n                                                                      sourceController:(UIViewController *)source \n{\n    return [[VTDAddPresentationTransition alloc] init];\n}\n\n@end\n```\n\nThe app is using a custom view controller transition to present the add view controller. Since the wireframe is responsible for performing the transition, it becomes the transitioning delegate for the add view controller and can return the appropriate transition animations. \n\n## Application Components Fitting in with VIPER\nAn iOS application architecture needs to be considerate of the fact that UIKit and Cocoa Touch are the main tools that apps are built on top of. Architecture needs to coexist peacefully with all the components of the application, but it also needs to provide guidelines for how some parts of the frameworks are used and where they live.\n\nThe workhorse of an iOS app is `UIViewController`. It would be easy to assume that a contender to replace MVC would shy away from making heavy use of view controllers. But view controllers are central to the platform: they handle orientation change, respond to input from the user, integrate well with system components like navigation controllers, and now with iOS 7, allow customizable transitions between screens. They are extremely useful.\n\nWith VIPER, a view controller does exactly what it was meant to do: it controls the view. Our to-do list app has two view controllers, one for the list screen, and one for the add screen. The add view controller implementation is extremely basic because all it has to do is control the view:\n\n```objc\n@implementation VTDAddViewController\n\n- (void)viewDidAppear:(BOOL)animated \n{\n    [super viewDidAppear:animated];\n\n    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self\n                                                                                        action:@selector(dismiss)];\n    [self.transitioningBackgroundView addGestureRecognizer:gestureRecognizer];\n    self.transitioningBackgroundView.userInteractionEnabled = YES;\n}\n\n- (void)dismiss \n{\n    [self.eventHandler cancelAddAction];\n}\n\n- (void)setEntryName:(NSString *)name \n{\n    self.nameTextField.text = name;\n}\n\n- (void)setEntryDueDate:(NSDate *)date \n{\n    [self.datePicker setDate:date];\n}\n\n- (IBAction)save:(id)sender \n{\n    [self.eventHandler saveAddActionWithName:self.nameTextField.text\n                                     dueDate:self.datePicker.date];\n}\n\n- (IBAction)cancel:(id)sender \n{\n    [self.eventHandler cancelAddAction];\n}\n\n\n#pragma mark - UITextFieldDelegate Methods\n\n- (BOOL)textFieldShouldReturn:(UITextField *)textField \n{\n    [textField resignFirstResponder];\n\n    return YES;\n}\n\n@end\n```\n\nApps are usually much more compelling when they are connected to the network. But where should this networking take place and what should be responsible for initiating it? It's typically up to the Interactor to initiate a network operation, but it won't handle the networking code directly. It will ask a dependency, like a network manager or API client. The Interactor may have to aggregate data from multiple sources to provide the information needed to fulfill a use case. Then it's up to the Presenter to take the data returned by the Interactor and format it for presentation.\n\nA data store is responsible for providing entities to an Interactor. As an Interactor applies its business logic, it will need to retrieve entities from the data store, manipulate the entities, and then put the updated entities back in the data store. The data store manages the persistence of the entities. Entities do not know about the data store, so entities do not know how to persist themselves. \n\nThe Interactor should not know how to persist the entities either. Sometimes the Interactor may want to use a type of object called a data manager to facilitate its interaction with the data store. The data manager handles more of the store-specific types of operations, like creating fetch requests, building queries, etc. This allows the Interactor to focus more on application logic and not have to know anything about how entities are gathered or persisted. One example of when it makes sense to use a data manager is when you are using Core Data, which is described below. \n\nHere's the interface for the example app's data manager:\n\n```objc\n@interface VTDListDataManager : NSObject\n\n@property (nonatomic, strong) VTDCoreDataStore *dataStore;\n\n- (void)todoItemsBetweenStartDate:(NSDate *)startDate endDate:(NSDate *)endDate completionBlock:(void (^)(NSArray *todoItems))completionBlock;\n\n@end\n```\n\nWhen using TDD to develop an Interactor, it is possible to switch out the production data store with a test double/mock. Not talking to a remote server (for a web service) or touching the disk (for a database) allows your tests to be faster and more repeatable.\n\nOne reason to keep the data store as a distinct layer with clear boundaries is that it allows you to delay choosing a specific persistence technology. If your data store is a single class, you can start your app with a basic persistence strategy, and then upgrade to SQLite or Core Data later if and when it makes sense to do so, all without changing anything else in your application's code base.\n\nUsing Core Data in an iOS project can often spark more debate than architecture itself. However, using Core Data with VIPER can be the best Core Data experience you've ever had. Core Data is a great tool for persisting data while maintaining fast access and a low-memory footprint. But it has a habit of snaking its `NSManagedObjectContext` tendrils all throughout an app's implementation files, particularly where they shouldn't be. VIPER keeps Core Data where it should be: at the data store layer. \n\nIn the to-do list example, the only two parts of the app that know that Core Data is being used are the data store itself, which sets up the Core Data stack, and the data manager. The data manager performs a fetch request, converts the `NSManagedObjects` returned by the data store into standard PONSO model objects, and passes those back to the business logic layer. That way, the core of the application is never dependent on Core Data, and as a bonus, you never have to worry about stale or poorly threaded `NSManagedObjects` gunking up the works.\n\nHere's what it looks like inside the data manager when a request gets made to access the Core Data store:\n\n```objc\n@implementation VTDListDataManager\n\n- (void)todoItemsBetweenStartDate:(NSDate *)startDate endDate:(NSDate*)endDate completionBlock:(void (^)(NSArray *todoItems))completionBlock\n{\n    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];\n\n    NSPredicate *predicate = [NSPredicate predicateWithFormat:@\"(date >= %@) AND (date <= %@)\", [calendar dateForBeginningOfDay:startDate], [calendar dateForEndOfDay:endDate]];\n    NSArray *sortDescriptors = @[];\n\n    __weak typeof(self) welf = self;\n    [self.dataStore\n     fetchEntriesWithPredicate:predicate\n     sortDescriptors:sortDescriptors\n     completionBlock:^(NSArray* entries) {\n         if (completionBlock)\n         {\n             completionBlock([welf todoItemsFromDataStoreEntries:entries]);\n         }\n     }];\n}\n\n- (NSArray*)todoItemsFromDataStoreEntries:(NSArray *)entries\n{\n    return [entries arrayFromObjectsCollectedWithBlock:^id(VTDManagedTodoItem *todo) {\n        return [VTDTodoItem todoItemWithDueDate:todo.date name:todo.name];\n    }];\n}\n\n@end\n```\n\nAlmost as controversial as Core Data are UI Storyboards. Storyboards have many useful features, and ignoring them entirely would be a mistake. However, it is difficult to accomplish all of the goals of VIPER while employing all the features that a storyboard has to offer.\n\nThe compromise we tend to make is to choose not to use segues. There may be some cases where using the segue makes sense, but the danger with segues is they make it very difficult to keep the separation between screens -- as well as between UI and application logic -- intact. As a rule of thumb, we try not to use segues if implementing the prepareForSegue method appears necessary.\n\nOtherwise, storyboards are a great way to implement the layout for your user interface, especially while using Auto Layout. We chose to implement both screens for the to-do list example using a storyboard, and use code such as this to perform our own navigation:\n\n```objc\nstatic NSString *ListViewControllerIdentifier = @\"VTDListViewController\";\n\n@implementation VTDListWireframe\n\n- (void)presentListInterfaceFromWindow:(UIWindow *)window \n{\n    VTDListViewController *listViewController = [self listViewControllerFromStoryboard];\n    listViewController.eventHandler = self.listPresenter;\n    self.listPresenter.userInterface = listViewController;\n    self.listViewController = listViewController;\n\n    [self.rootWireframe showRootViewController:listViewController\n                                      inWindow:window];\n}\n\n- (VTDListViewController *)listViewControllerFromStoryboard \n{\n    UIStoryboard *storyboard = [self mainStoryboard];\n    VTDListViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:ListViewControllerIdentifier];\n    return viewController;\n}\n\n- (UIStoryboard *)mainStoryboard \n{\n    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@\"Main\"\n                                                         bundle:[NSBundle mainBundle]];\n    return storyboard;\n}\n\n@end\n```\n\n## Using VIPER to Build Modules\nOften when working with VIPER, you will find that a screen or set of screens tends to come together as a module. A module can be described in a few ways, but usually it's best thought of as a feature. In a podcasting app, a module might be the audio player or the subscription browser. In our to-do list app, the list and add screens are each built as separate modules.\n\nThere are a few benefits to designing your app as a set of modules. One is that modules can have very clear and well-defined interfaces, as well as be independent of other modules. This makes it much easier to add/remove features, or to change the way your interface presents various modules to the user.\n\nWe wanted to make the separation between modules very clear in the to-do list example, so we defined two protocols for the add module. The first is the module interface, which defines what the module can do. The second is the module delegate, which describes what the module did. Example:\n\n```objc\n@protocol VTDAddModuleInterface <NSObject>\n\n- (void)cancelAddAction;\n- (void)saveAddActionWithName:(NSString *)name dueDate:(NSDate *)dueDate;\n\n@end\n\n\n@protocol VTDAddModuleDelegate <NSObject>\n\n- (void)addModuleDidCancelAddAction;\n- (void)addModuleDidSaveAddAction;\n\n@end\n```\n\nSince a module has to be presented to be of much value to the user, the module's Presenter usually implements the module interface. When another module wants to present this one, its Presenter will implement the module delegate protocol, so that it knows what the module did while it was presented.\n\nA module might include a common application logic layer of entities, interactors, and managers that can be used for multiple screens. This, of course, depends on the interaction between these screens and how similar they are. A module could just as easily represent only a single screen, as is shown in the to-do list example. In this case, the application logic layer can be very specific to the behavior of its particular module.\n\nModules are also just a good simple way to organize code. Keeping all of the code for a module tucked away in its own folder and group in Xcode makes it easy to find when you need to change something. It's a great feeling when you find a class exactly where you expected to look for it.\n\nAnother benefit to building modules with VIPER is they become easier to extend to multiple form factors. Having the application logic for all of your use cases isolated at the Interactor layer allows you to focus on building the new user interface for tablet, phone, or Mac, while reusing your application layer.\n\nTaking this a step further, the user interface for iPad apps may be able to reuse some of the views, view controllers, and presenters of the iPhone app. In this case, an iPad screen would be represented by 'super' presenters and wireframes, which would compose the screen using existing presenters and wireframes that were written for the iPhone. Building and maintaining an app across multiple platforms can be quite challenging, but good architecture that promotes reuse across the model and application layer helps make this much easier.\n\n## Testing with VIPER\nFollowing VIPER encourages a separation of concerns that makes it easier to adopt TDD. The Interactor contains pure logic that is independent of any UI, which makes it easy to drive with tests. The Presenter contains logic to prepare data for display and is independent of any UIKit widgets. Developing this logic is also easy to drive with tests.\n\nOur preferred method is to start with the Interactor. Everything in the UI is there to serve the needs of the use case. By using TDD to test drive the API for the Interactor, you will have a better understanding of the relationship between the UI and the use case.\n\nAs an example, we will look at the Interactor responsible for the list of upcoming to-do items. The policy for finding upcoming items is to find all to-do items due by the end of next week and classify each to-do item as being due today, tomorrow, later this week, or next week.\n\nThe first test we write is to ensure the Interactor finds all to-do items due by the end of next week:\n\n\n```objc\n- (void)testFindingUpcomingItemsRequestsAllToDoItemsFromTodayThroughEndOfNextWeek\n{\n    [[self.dataManager expect] todoItemsBetweenStartDate:self.today endDate:self.endOfNextWeek completionBlock:OCMOCK_ANY];\n    [self.interactor findUpcomingItems];\n}\n```\n\nOnce we know that the Interactor asks for the appropriate to-do items, we will write several tests to confirm that it allocates the to-do items to the correct relative date group (e.g. today, tomorrow, etc.):\n\n```objc\n- (void)testFindingUpcomingItemsWithOneItemDueTodayReturnsOneUpcomingItemsForToday\n{\n    NSArray *todoItems = @[[VTDTodoItem todoItemWithDueDate:self.today name:@\"Item 1\"]];\n    [self dataStoreWillReturnToDoItems:todoItems];\n\n    NSArray *upcomingItems = @[[VTDUpcomingItem upcomingItemWithDateRelation:VTDNearTermDateRelationToday dueDate:self.today title:@\"Item 1\"]];\n    [self expectUpcomingItems:upcomingItems];\n\n    [self.interactor findUpcomingItems];\n}\n```\n\nNow that we know what the API for the Interactor looks like, we can develop the Presenter. When the Presenter receives upcoming to-do items from the Interactor, we will want to test that we properly format the data and display it in the UI:\n\n```objc\n- (void)testFoundZeroUpcomingItemsDisplaysNoContentMessage\n{\n    [[self.ui expect] showNoContentMessage];\n\n    [self.presenter foundUpcomingItems:@[]];\n}\n\n- (void)testFoundUpcomingItemForTodayDisplaysUpcomingDataWithNoDay\n{\n    VTDUpcomingDisplayData *displayData = [self displayDataWithSectionName:@\"Today\"\n                                                          sectionImageName:@\"check\"\n                                                                 itemTitle:@\"Get a haircut\"\n                                                                itemDueDay:@\"\"];\n    [[self.ui expect] showUpcomingDisplayData:displayData];\n\n    NSCalendar *calendar = [NSCalendar gregorianCalendar];\n    NSDate *dueDate = [calendar dateWithYear:2014 month:5 day:29];\n    VTDUpcomingItem *haircut = [VTDUpcomingItem upcomingItemWithDateRelation:VTDNearTermDateRelationToday dueDate:dueDate title:@\"Get a haircut\"];\n\n    [self.presenter foundUpcomingItems:@[haircut]];\n}\n\n- (void)testFoundUpcomingItemForTomorrowDisplaysUpcomingDataWithDay\n{\n    VTDUpcomingDisplayData *displayData = [self displayDataWithSectionName:@\"Tomorrow\"\n                                                          sectionImageName:@\"alarm\"\n                                                                 itemTitle:@\"Buy groceries\"\n                                                                itemDueDay:@\"Thursday\"];\n    [[self.ui expect] showUpcomingDisplayData:displayData];\n\n    NSCalendar *calendar = [NSCalendar gregorianCalendar];\n    NSDate *dueDate = [calendar dateWithYear:2014 month:5 day:29];\n    VTDUpcomingItem *groceries = [VTDUpcomingItem upcomingItemWithDateRelation:VTDNearTermDateRelationTomorrow dueDate:dueDate title:@\"Buy groceries\"];\n\n    [self.presenter foundUpcomingItems:@[groceries]];\n}\n```\n\nWe also want to test that the app will start the appropriate action when the user wants to add a new to-do item:\n\n```objc\n- (void)testAddNewToDoItemActionPresentsAddToDoUI\n{\n    [[self.wireframe expect] presentAddInterface];\n\n    [self.presenter addNewEntry];\n}\n```\n\nWe can now develop the View. When there are no upcoming to-do items, we want to show a special message:\n\n```objc\n- (void)testShowingNoContentMessageShowsNoContentView\n{\n    [self.view showNoContentMessage];\n\n    XCTAssertEqualObjects(self.view.view, self.view.noContentView, @\"the no content view should be the view\");\n}\n```\n\nWhen there are upcoming to-do items to display, we want to make sure the table is showing:\n\n```objc\n- (void)testShowingUpcomingItemsShowsTableView\n{\n    [self.view showUpcomingDisplayData:nil];\n\n    XCTAssertEqualObjects(self.view.view, self.view.tableView, @\"the table view should be the view\");\n}\n```\n\nBuilding the Interactor first is a natural fit with TDD. If you develop the Interactor first, followed by the Presenter, you get to build out a suite of tests around those layers first and lay the foundation for implementing those use cases. You can iterate quickly on those classes, because you won't have to interact with the UI in order to test them. Then, when you go to develop the View, you'll have a working and tested logic and presentation layer to connect to it. By the time you finish developing the View, you might find that the first time you run the app everything just works, because all your passing tests tell you it will work.\n\n## Conclusion\nWe hope you have enjoyed this introduction to VIPER. Many of you may now be wondering where to go next. If you wanted to architect your next app using VIPER, where would you start?\n\nThis article and our example implementation of an app using VIPER are as specific and well-defined as we could make them. Our to-do list app is rather straightforward, but it should also accurately explain how to build an app using VIPER. In a real-world project, how closely you follow this example will depend on your own set of challenges and constraints. In our experience, each of our projects have varied the approach taken to using VIPER slightly, but all of them have benefited greatly from using it to guide their approaches.\n\nThere may be cases where you wish to deviate from the path laid out by VIPER for various reasons. Maybe you have run into a warren of ['bunny'](http://inessential.com/2014/03/16/smaller_please) objects, or your app would benefit from using segues in Storyboards. That's OK. In these cases, consider the spirit of what VIPER represents when making your decision. At its core, VIPER is an architecture based on the [Single Responsibility Principle](http://en.wikipedia.org/wiki/Single_responsibility_principle). If you are having trouble, think about this principle when deciding how to move forward.\n\nYou may also be wondering if it's possible to use VIPER in your existing app. In this scenario, consider building a new feature with VIPER. Many of our existing projects have taken this route. This allows you to build a module using VIPER, and also helps you spot any existing issues that might make it harder to adopt an architecture based on the Single Responsibility Principle.\n\nOne of the great things about developing software is that every app is different, and there are also different ways of architecting any app. To us, this means that every app is a new opportunity to learn and try new things. If you decide to try VIPER, we think you'll learn a few new things as well. Thanks for reading.\n\n## Swift Addendum\nLast week at WWDC Apple introduced the [Swift](https://developer.apple.com/swift/) programming language as the future of Cocoa and Cocoa Touch development. It's too early to have formed complex opinions about the Swift language, but we do know that languages have a major influence on how we design and build software. We decided to [rewrite our VIPER TODO example app using Swift](https://github.com/objcio/issue-13-viper-swift) to help us learn what this means for VIPER. So far, we like what we see. Here are a few features of Swift that we feel will improve the experience of building apps using VIPER.\n\n### Structs\nIn VIPER we use small, lightweight, model classes to pass data between layers, such as from the Presenter to the View. These PONSOs are usually intended to simply carry small amounts of data, and are usually not intended to be subclassed. Swift structs are a perfect fit for these situations. Here's an example of a struct used in the VIPER Swift example. Notice that this struct needs to be equatable, and so we have overloaded the == operator to compare two instances of its type:\n\n```objc\nstruct UpcomingDisplayItem : Equatable, Printable {\n    let title : String = \"\"\n    let dueDate : String = \"\"\n\n    var description : String { get {\n        return \"\\(title) -- \\(dueDate)\"\n    }}\n\n    init(title: String, dueDate: String) {\n        self.title = title\n        self.dueDate = dueDate\n    }\n}\n\nfunc == (leftSide: UpcomingDisplayItem, rightSide: UpcomingDisplayItem) -> Bool {\n    var hasEqualSections = false\n    hasEqualSections = rightSide.title == leftSide.title\n\n    if hasEqualSections == false {\n        return false\n    }\n\n    hasEqualSections = rightSide.dueDate == rightSide.dueDate\n\n    return hasEqualSections\n}\n```\n\n### Type Safety\nPerhaps the biggest difference between Objective-C and Swift is how the two deal with types. Objective-C is dynamically typed and Swift is very intentionally strict with how it implements type checking at compile time. For an architecture like VIPER, where an app is composed of multiple distinct layers, type safety can be a huge win for programmer efficiency and for architectural structure. The compiler is helping you make sure containers and objects are of the correct type when they are being passed between layer boundaries. This is a great place to use structs as shown above. If a struct is meant to live at the boundary between two layers, then you can guarantee that it will never be able to escape from between those layers thanks to type safety.\n\n## Further Reading\n- [VIPER TODO, article example app](https://github.com/objcio/issue-13-viper)\n- [VIPER SWIFT, article example app built using Swift](https://github.com/objcio/issue-13-viper-swift)\n- [Counter, another example app](https://github.com/mutualmobile/Counter)\n- [Mutual Mobile Introduction to VIPER](http://mutualmobile.github.io/blog/2013/12/04/viper-introduction/)\n- [Clean Architecture](http://blog.8thlight.com/uncle-bob/2011/11/22/Clean-Architecture.html)\n- [Lighter View Controllers](/issues/1-view-controllers/lighter-view-controllers/)\n- [Testing View Controllers](/issues/1-view-controllers/testing-view-controllers/)\n- [Bunnies](http://inessential.com/2014/03/16/smaller_please)\n\n\n"
  },
  {
    "path": "2014-07-11-appkit-for-uikit-developers.md",
    "content": "---\ntitle:  \"AppKit for UIKit Developers\"\ncategory: \"14\"\ndate: \"2014-07-11 07:00:00\"\ntags: article\nauthor:\n  - name: Florian Kugler\n    url: https://twitter.com/floriankugler\n---\n\nThe Mac is not only a great platform to develop on — it's also a great platform to develop *for*. Last year we started building our first [Mac app](http://decksetapp.com), and it was a great experience to finally build something for the platform we're working on all day. However, we also had some difficulties discovering the peculiarities of the Mac compared to developing for iOS. In this article, we'll summarize what we've learned from this transition to hopefully give you a head start on your first Mac app.\n\nIn this article, we will assume OS X Yosemite to be the default platform we're talking about, as Apple made some significant strides this year to harmonize the platforms from a developer's perspective. However, we'll also point out what only applies to Yosemite, and what the situation was prior to this release.\n\n\n## What's Similar\n\nAlthough iOS and OS X are separate operating systems, they share a lot of commonalities, starting with the development environment — same language, same IDE. So you'll feel right at home. \n\nMore importantly though, OS X also shares a lot of the frameworks that you're already familiar with from iOS, like Foundation, Core Data, and Core Animation. This year, Apple harmonized the platforms further and brought frameworks to the Mac that were previously only on iOS, one example being Multipeer Connectivity. Also, on a lower level, you'll immediately see the APIs you're familiar with: Core Graphics, Core Text, libdispatch, and many more.\n\nThe UI framework is where things really start to diverge — UIKit feels like a slimmed down and modernized version of the AppKit that has been around and evolving since the NeXT days. The reason why, is when Apple introduced the iPhone, there was the chance to start from a clean slate and take what had been learned from AppKit: bring over the concepts and pieces that had proven to work well, and improve those that were less fortunate designs.\n\nIf you're interested in how this transition came about, check out these excellent episodes of the Debug podcast with [Nitin Ganatra](https://twitter.com/nitinganatra), former iOS apps director at Apple: [System 7 to Carbon](http://www.imore.com/debug-39-nitin-ganatra-episode-i-system-7-carbon), [OS X to iOS](http://www.imore.com/debug-40-nitin-ganatra-epsiode-ii-os-x-ios), and [iPhone to iPad](http://www.imore.com/debug-41-nitin-ganatra-episode-iii-iphone-ipad).\n\nWith this in mind, it's no wonder that UIKit and AppKit still share a lot of concepts. The UI is constructed out of windows and views, with messages being sent over the responder chain just as on iOS. Furthermore, `UIView` is `NSView`, `UIControl` is `NSControl`, `UIImage` is `NSImage`, `UIViewController` is `NSViewController`, `UITextView` is `NSTextView`... The list goes on and on.\n\nIt's tempting to assume that you can use these classes in the same way, just by replacing `UI` with `NS`. But that's not going to work in many cases. The similarity is more on the conceptual level than in the implementation. You'll pretty much know about the building blocks to look for to construct your user interface, which is a great help. And a lot of the design patterns, such as delegation, will be similar. But the devil is in the details — you really need to read the documentation and learn how these classes should be used.\n\nIn the next section, we'll take a look at some of the pitfalls we fell into the most.\n\n\n## What's Different\n\n### Windows and Window Controllers\n\nWhile you almost never interact with windows on iOS (since they take up the whole screen anyway), windows are key components on the Mac. Historically, Mac applications had multiple windows, each with its own role, very similar to view controllers on iOS. As a result, AppKit has an `NSWindowController` class that traditionally took on many of the tasks that you would handle in a view controller on iOS. View controllers are a relatively new addition to AppKit, and up until now, they did not receive actions by default, and missed a lot of the lifecycle methods, view controller containment, and other features you're used to from UIKit.\n\nBut AppKit has changed, since Mac apps are relying more and more on a single window. As of OS X 10.10 Yosemite, the `NSViewController` is similar in many ways to `UIViewController`. It is also part of the responder chain by default. Just remember that if you target your Mac app to OS X 10.9 or earlier, window controllers on the Mac are much more akin to what you're used to as view controllers from iOS. As [Mike Ash writes](https://www.mikeash.com/pyblog/friday-qa-2013-04-05-windows-and-window-controllers.html), a good pattern to instantiate windows on the Mac is to have one nib file and one window controller per window type.\n\nFurthermore, `NSWindow` is not a view subclass as is the case for `UIWindow`. Instead, each window holds a reference to its top-level view in the `contentView` property. \n\n\n### Responder Chain\n\nIf you're developing for OS X 10.9 or lower, be aware that view controllers are not part of the responder chain by default. Instead, events will bubble up through the view tree and then go straight to the window and the window controller. In this instance, if you want a view controller to handle events, you will have to add it to the responder chain [manually](http://www.cocoawithlove.com/2008/07/better-integration-for-nsviewcontroller.html).\n\nIn addition to the difference in the responder chain, AppKit also has a stricter convention as to the method signature of actions. In AppKit, an action method always looks like this:\n\n```objc\n- (void)performAction:(id)sender;\n```\n\nThe variants that are permissible on iOS with no arguments at all, or with a sender and an event argument, don't work on OS X. Furthermore, in AppKit, controls usually hold a reference to one target and an action pair, whereas you can associate multiple target-action pairs with a control on iOS using the `addTarget:action:forControlEvents:` method.\n\n\n### Views \n\nThe view system works very differently on the Mac, for historic reasons. On iOS, views were backed by Core Animation layers by default from the beginning. But AppKit predates Core Animation by decades. When AppKit was designed, there was no such thing as a GPU as we know it today. Therefore, the view system heavily relied on the CPU doing the work. \n\nWhen you're getting started with development on the Mac, we strongly recommend you check out Apple's [Introduction to View Programming Guide for Cocoa](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaViewsGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40002978). Furthermore, there are two excellent WWDC sessions you should watch: [Layer-Backed Views: AppKit + Core Animation](https://developer.apple.com/videos/wwdc/2012/#217) and [Optimizing Drawing and Scrolling](https://developer.apple.com/videos/wwdc/2013/#215).\n\n\n#### Layer-Backed Views\n\nBy default, AppKit views are not backed by Core Animation layers; layer-backing support has been integrated into AppKit retroactively. But while you never have to worry about this with UIKit, with AppKit there are decisions to make. AppKit differentiates between layer-backed and layer-hosting views, and layer backing can be turned on and off on a per-view-tree basis. \n\nThe most straightforward approach to enable layer backing is to set the `wantsLayer` property to `YES` on the window's content view. This will cause all views in the window's view tree to have their own backing layers, so there's no need to repeatedly set this property on each individual view. This can be done in code or simply in Interface Builder's View Effects Inspector.\n\nIn contrast to iOS, on the Mac you should treat the backing layers as an implementation detail. This means you should not try to interact with the layers directly, as AppKit owns those layers. For example, on iOS you could simply say:\n\n```objc\nself.layer.backgroundColor = [UIColor redColor].CGColor;\n```\n\nBut in AppKit, you shouldn't touch the layer. If you want to interact with the layer in such ways, then you have to go one step further. Overriding `NSView`'s `wantsUpdateLayer` method to return `YES` enables you to change the layer's properties. If you do this though, AppKit will no longer call the view's `drawRect:` method. Instead, `updateLayer` will be called during the view update cycle, and this is where you can modify the layer. \n\nYou can use this, for example, to implement a very simple view with a uniform background color (yes, `NSView` has no `backgroundColor` property):\n\n```objc\n@interface ColoredView: NSView\n\n@property (nonatomic) NSColor *backgroundColor;\n\n@end\n\n\n@implementation ColoredView\n\n- (BOOL)wantsUpdateLayer\n{\n    return YES;\n}\n\n- (void)updateLayer\n{\n    self.layer.backgroundColor = self.backgroundColor.CGColor;\n}\n\n- (void)setBackgroundColor:(NSColor *)backgroundColor\n{\n    _backgroundColor = backgroundColor;\n    [self setNeedsDisplay:YES];\n}\n\n@end\n```\n\nThis example assumes that layer backing is already enabled for the view tree where you'll insert this view. The alternative to this would be to simply override the `drawRect:` method to draw the colored background. \n\n\n##### Coalescing Layers\n\nOpting into layer-backed views will increase the amount of memory needed (each layer has its own backing store, probably overlapping with other views' backing stores) and introduce a potentially costly compositing step of all the layers. Since OS X 10.9, you have been able to tell AppKit to coalesce the contents of a view tree into one common backing layer by using the `canDrawSubviewsIntoLayer` property. This can be a good option if you know that you will not need to animate subviews individually. \n\nAll subviews that are implicitly layer-backed (i.e. you didn't explicitly set `wantsLayer = YES` on these sub views) will now get drawn into the same layer. However, subviews that do have `wantsLayer` set to `YES` will still have their own backing layer and their `drawRect:` method will be called, no matter what `wantsUpdateLayer` returns.\n\n\n##### Layer Redraw Policy\n\nAnother gotcha that's important to know is the fact that layer-backed views have their redraw policy set to `NSViewLayerContentsRedrawDuringViewResize` by default. This resembles the behavior of non-layer-backed views, but it might be detrimental to animation performance if a drawing step is introduced for each frame of the animation.\n\nTo avoid this, you can set the `layerContentsRedrawPolicy` property to `NSViewLayerContentsRedrawOnSetNeedsDisplay`. This way, you have control over when the layer contents need to be redrawn. A frame change will not automatically trigger a redraw anymore; you are now responsible for triggering it by calling `-setNeedsDisplay:`.\n\nOnce you change the redraw policy in this way, you might also want to look into the [`layerContentsPlacement`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/occ/instm/NSView/layerContentsPlacement) property, which is the view's equivalent to the layer's `contentGravity` property. This allows you to specify how the existing layer content will be mapped into the layer as it is resized.\n\n\n#### Layer-Hosting Views\n\n`NSView`'s layer story doesn't end here, though. There is a whole different option to work with Core Animation layers — called layer-hosting views. In short, with a layer-hosting view, you can do with the layer and its sublayers whatever you want. The price you pay for this is that you cannot add any subviews to this view anymore. A layer-hosting view is a leaf node in the view tree. \n\nTo create a layer-hosting view, you first have to assign a layer object to the `layer` property, and then set the `wantsLayer` to `YES`. Note that the sequence of these steps is crucial:\n\n```objc\n- (instancetype)initWithFrame:(NSRect)frame\n{\n    self = [super initWithFrame:frame];\n    if (self) {\n        self.layer = [[CALayer alloc] init];\n        self.wantsLayer = YES;\n    }\n}\n```\n\nIt's important that you set `wantsLayer` *after* you've set your custom layer.\n\n\n#### Other View-Related Gotchas\n\nBy default, the view's coordinate system origin is located at the lower left on the Mac, not the upper left as on iOS. This can be confusing at first, but you can also decide to restore the behavior you're used to by overriding `isFlipped` to return `YES`.\n\nAs AppKit views don't have background color properties that you can set to `[NSColor clearColor]` in order to let the background shine through, many `NSView` subclasses like `NSTextView` or `NSScrollView` have a `drawsBackground` property that you have to set to `NO` if you want the view to be transparent. \n\nIn order to receive events for the mouse cursor entering or exiting the view or being moved within the view, you need to create a tracking area. There's a special override point in `NSView` called `updateTrackingAreas`, which you can use to do this. A common pattern looks like this:\n\n```objc\n- (void)updateTrackingAreas\n{\n    [self removeTrackingArea:self.trackingArea];\n    self.trackingArea = [[NSTrackingArea alloc] initWithRect:CGRectZero \n                                                     options:NSTrackingMouseEnteredAndExited|NSTrackingInVisibleRect|NSTrackingActiveInActiveApp\n                                                       owner:self \n                                                    userInfo:nil];\n    [self addTrackingArea:self.trackingArea];\n}\n```\n\nAppKit controls have been traditionally backed by `NSCell` subclasses. These cells should not be confused with table view cells or collection view cells in UIKit. AppKit originally made the distinction between views and cells in order to save resources — views would delegate all their drawing to a more lightweight cell object that could be reused for all views of the same type. \n\nApple is deprecating this approach step by step, but you'll still encounter it from time to time. For example, if you would want to create a custom button, you would subclass `NSButton` *and* `NSButtonCell`, implement your custom drawing in the cell subclass, and then assign your cell subclass to be used for the custom button by overriding the  `+[NSControl cellClass]` method. \n\nLastly, if you ever wonder how to get to the current Core Graphics context when implementing your own `drawRect:` method, it's the `graphicsPort` property on `NSGraphicsContext`. Check out the [Cocoa Drawing Guide](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaDrawingGuide/) for more details.\n\n\n### Animations\n\nAs a consequence of the differences in the view system discussed above, animations also work quite differently on the Mac. For a good overview, watch the WWDC session on [Best Practices for Cocoa Animation](https://developer.apple.com/videos/wwdc/2013/#213).\n\nIf your views are not layer-backed, then naturally, animations will be a CPU-intensive process, as every step of the animation has to be drawn accordingly in the window-backing store. Since nowadays you'd mostly want to animate layer-backed views to get really smooth animations, we'll focus on this case here.\n\nAs mentioned above, you should never touch the backing layers of layer-backed views in AppKit (see the section \"Rules for Modifying Layers on OS X\" at the bottom of [this page of the Core Animation Programming Guide](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreAnimation_guide/CreatingBasicAnimations/CreatingBasicAnimations.html#//apple_ref/doc/uid/TP40004514-CH3-SW18). The layers are managed by AppKit, and — contrary to iOS — the views' geometry properties are not just a reflection of the corresponding layer properties, but AppKit actually syncs the view geometry internally to the layer geometry. \n\nThere are a few different ways you can trigger an animation on a view. First, you can use the [animator proxy](https://developer.apple.com/library/mac/documentation/cocoa/reference/NSAnimatablePropertyContainer_protocol/Introduction/Introduction.html#//apple_ref/occ/intfm/NSAnimatablePropertyContainer/animator):\n\n```objc\nview.animator.alphaValue = .5;\n```\n\nBehind the scenes, this will enable implicit animations on the backing layer, set the alpha value, and disable the implicit animations again.\n\nYou can also wrap this into an [animation context](https://developer.apple.com/library/mac/documentation/cocoa/reference/NSAnimationContext_class/Introduction/Introduction.html) in order to get a completion handler callback:\n\n```objc\n[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){\n    view.animator.alphaValue = .5;\n} completionHandler:^{\n    // ...\n}]; \n```\n\nIn order to influence the duration and the timing function, we have to set these values on the animation context:\n\n```objc\n[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){\n    context.duration = 1;\n    context.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];\n    view.animator.alphaValue = .5;\n} completionHandler:^{\n    // ...\n}]; \n```\n\nIf you don't need the completion handler, you can also use the shorthand form:\n\n```objc\n[NSAnimationContext currentContext].duration = 1;\nview.animator.alphaValue = .5;    \n```\n\nLastly, you can also enable implicit animations, so that you don't have to explicitly use the animator proxy each time:\n\n```objc\n[NSAnimationContext currentContext].allowsImplicitAnimations = YES;\nview.alphaValue = .5;\n```\n\nFor more control over the animation, you can also use `CAAnimation` instances. Contrary to on iOS, you don't add them directly to the layer (as you're not supposed to touch the layer yourself), but you use the API defined in the [`NSAnimatablePropertyContainer`](https://developer.apple.com/library/mac/documentation/cocoa/reference/NSAnimatablePropertyContainer_protocol/Introduction/Introduction.html), which is implemented by `NSView` and `NSWindow`. For example:\n\n```objc\nCAKeyframeAnimation *animation = [CAKeyframeAnimation animation];\nanimation.values = @[@1, @.9, @.8, @.7, @.6];\nview.animations = @{@\"alphaValue\": animation};\nview.animator.alphaValue = .5;\n```\n\nFor `frame` animations, it's important to set the view's `layerContentsRedrawPolicy` to `NSViewLayerContentsRedrawOnSetNeedsDisplay`, because the view's content will otherwise be redrawn on every frame.\n\nUnfortunately, `NSView` doesn't expose all animatable properties of Core Animation layers, `transform` being the most important example. Check out [this article](http://jwilling.com/osx-animations) by [Jonathan Willings](https://twitter.com/willing) for a description of how you can work around this limitation. Just be aware that you're leaving officially sanctioned territory here.\n\nAll the things mentioned above apply to *layer-backed* views. If you have a *layer-hosting* view, you can use `CAAnimation`s directly on the view's layer or sublayers, since you own them.\n\n\n### Collection View\n\nAlthough AppKit comes with an `NSCollectionView` class, its capabilities lag far behind its UIKit counterpart. Since `UICollectionView` is such a versatile building block on iOS, depending on your UI concept, it's a tough pill to swallow that there is nothing like it in AppKit. So when you're planning your user interface, take into account that it might be a lot of work to create grid layouts that are otherwise very easy to achieve on iOS.\n\n\n### Images\n\nComing from iOS, you'll be familiar with `UIImage`, and conveniently, there is a corresponding `NSImage` class in AppKit. But you'll quickly notice that these classes are vastly different. `NSImage` is, in many ways, a more powerful class than `UIImage`, but this comes at the cost of increased complexity. Apple's [Cocoa Drawing Guide](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Images/Images.html#//apple_ref/doc/uid/TP40003290-CH208-BCIBBFGJ) has a good introduction of how to work with images in AppKit.\n\nThe most important conceptual difference is that `NSImage` is backed by one or more image representations. AppKit comes with some `NSImageRep` subclasses, like `NSBitmapImageRep`, `NSPDFImageRep`, and `NSEPSImageRep`. For example, one `NSImage` object could hold a thumbnail, a full-size, and a PDF representation for printing of the same content. When you draw the image, an image representation matching the current graphics context and drawing dimensions will be picked based on the color space, dimensions, resolution, and depth. \n\nFurthermore, images on the Mac have the notion of resolution in addition to size. An image representation has three properties that play into that: `size`, `pixelsWide`, and `pixelsHigh`. The size property determines the size of the image representation when being rendered, whereas the pixel width and height values specify the raw image size as derived from the image data itself. Together, those properties determine the resolution of the image representation. The pixel dimensions can be different from the representation's size, which in turn can be different from the size of the image the representation belongs to. \n\nAnother difference from `UIImage` is that `NSImage` will cache the result when it's drawn to the screen (this behavior is configurable via the [`cacheMode`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSImage_Class/Reference/Reference.html#//apple_ref/occ/instm/NSImage/cacheMode) property). When you change an underlying image representation, you have to call `recache` on the image for the change to take effect.\n\nBut working with images on the Mac isn't always more complex than on iOS. `NSImage` provides a very easy way to draw a new image, whereas on iOS you would have to create a bitmap context, then create a `CGImage` from that, and finally use it to initialize an `UIImage` instance. With `NSImage`, you can simply do:\n\n```objc\n[NSImage imageWithSize:(NSSize)size \n            flipped:(BOOL)drawingHandlerShouldBeCalledWithFlippedContext \n     drawingHandler:^BOOL (NSRect dstRect) \n{\n    // your drawing commands here...\n}];\n```\n\n### Colors\n\nThe Mac supports fully color-calibrated workflows, so anything having to do with colors is potentially more complicated. Color management is a complex topic, and we're not even going to pretend that we're experts in this. Instead, we're going to refer you to Apple's guides on the topic: [Introduction to Color Programming Topics for Cocoa](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DrawColor/DrawColor.html#//apple_ref/doc/uid/10000082-SW1) and [Introduction to Color Management](https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/csintro/csintro_intro/csintro_intro.html#//apple_ref/doc/uid/TP30001148).\n\nA common task is to use a color in your app that your designers have specified for you. In order to get the correct color, it's important to pick the color from the design template using the same color space as the one you use to programatically specify it. The standard system color picker has a drop-down menu, where you can choose the color space you want to use. We suggest to use the device-independent sRGB color space, and then later use the `+[NSColor colorWithSRGBRed:green:blue:alpha:]` class method to create the color in code.\n\n![](/images/issue-14/color-picker.png)\n\n\n### Text System\n\nWith [TextKit](/issues/5-ios7/getting-to-know-textkit/), iOS 7 finally got an equivalent to what has been around on the Mac for ages as the [Cocoa Text System](https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/TextSystemArchitecture/ArchitectureOverview.html). But Apple didn't just transfer the system from the Mac to iOS; instead, Apple made some significant changes to it. \n\nFor example, AppKit exposes the `NSTypesetter` and `NSGlyphGenerator`, both of which you can subclass to customize their behaviors. On iOS, those classes are not exposed, but some of the hooks for customization are available via the `NSLayoutManagerDelegate` protocol. \n\nOverall, it's still pretty similar, and you'll be able to do everything on the Mac that you can do on iOS (and more), but for some things, you will have to look for the appropriate hooks in different places. \n\n\n### Sandboxing\n\nIf you want to sell your Mac app through the Mac App Store, it has to be sandboxed. You might wonder why we're mentioning this here, since sandboxing has been the norm on iOS from day one (so you're very much familiar with it). However, we're so used to what apps were able to do on the Mac before sandboxing appeared on the radar, that it's sometimes easy to overlook the fact that a feature you want to implement might get you into conflict with the sandboxing restrictions.\n\nThe file system has always been exposed to the user on the Mac, so sandboxed apps are able to get access to files outside of their containers if the user signals clear intent to do so. It's the same model that has since come to iOS 8. However, whereas this approach enhances the prior possibilities on iOS, it restricts the prior possibilities on the Mac. That makes it easy to oversee or forget.\n\nWe're guilty of this ourselves, which is why we hope to be able to prevent you from falling into the same trap. When we started development of [Deckset](http://decksetapp.com) — an app that transforms simple Markdown into presentation slides — we never thought that we might run into sandboxing issues. After all, we only needed read access to the Markdown file. \n\nWhat we forgot about is that we also needed to display the images that are referenced in the Markdown. And although you type the path to the image in your Markdown file, that's not a user intent that counts within the sandboxing system. In the end, we 'solved' the problem by adding a notification UI in the app that prompts the user to allow us access to the files by explicitly opening the common ancestor folder of all images in the file once.\n\nTake a look at Apple's [sandboxing guides](https://developer.apple.com/app-sandboxing/) early in the development process so that you don't get tripped up later on.\n\n\n## What's Unique\n\nThere are many things you can only do on the Mac, mostly either due to its different interaction model or to its more liberal security policies. In this issue, we have articles covering some of these things in depth: [cross-process communication](/issues/14-mac/xpc/), [making an app scriptable](/issues/14-mac/scripting-data/), [scripting other apps in the sandbox](/issues/14-mac/sandbox-scripting/), and creating a [plugin infrastructure](/issues/14-mac/plugins/) for your apps.\n\nOf course, that's just a small subset of features unique to the Mac, but it gives you a good idea of the aspects that iOS 8 just starts to scratch the surface of in terms of extensibility and communication between apps. There's, of course, much more to explore: Drag and Drop, Printing, Bindings, OpenCL, to name just a few examples.\n\n"
  },
  {
    "path": "2014-07-11-editorial.md",
    "content": "---\ntitle:  \"Editorial\"\ncategory: \"14\"\ndate: \"2014-07-11 12:00:00\"\ntags: editorial\n---\n\nHi all,\n\nThis month's issue is all about the Mac. \n\nMany of the topics we've written about so far are applicable to both iOS and OS X, but some are for iOS only. So we felt it was about time to dedicate a whole issue to the platform we work on every day.\n\nThe articles in this issue are focused around topics that are unique to the OS X platform, like [making your app scriptable](/issues/14-mac/scripting-data/), [scripting other apps within the sandbox](/issues/14-mac/sandbox-scripting/), [making your app extensible with plugins](/issues/14-mac/plugins/), and [communication between processes with XPC](/issues/14-mac/xpc/). However, since iOS developers outnumber Mac developers by far, we've also included one article about [AppKit for UIKit developers](/issues/14-mac/appkit-for-uikit-developers/), which has many tips and tricks to get you started with Mac development if you haven't done it before.\n\nIn addition to the regular objc.io issues, we also have started a new project: we're writing a [book](/books) about functional programming in Swift. This book is not going to be an introduction to Swift, rather it will focus on how to leverage Swift's language features to write functional code. We plan to have the major parts finished by September, but we're also offering a pre-order option with beta access now. If you're interested, check out the [details](/books).\n\nHave a great summer!\n\nChris, Daniel, and Florian.\n"
  },
  {
    "path": "2014-07-11-plugins.md",
    "content": "---\ntitle: Plugins\ncategory: \"14\"\ndate: \"2014-07-11 09:00:00\"\nauthor:\n  - name: Gus Mueller\n    url: https://twitter.com/ccgus\ntags: article\n---\n\n\nPlugins are a great way to add functionality to your app after it has already shipped. Applications on the Macintosh have a long history of supporting plugins. One example is Adobe Photoshop, which first supported plugins way back in 1991 with version 2.0.\n\nBack in the good old days (I’m talking about pre OS X), it used to be a bit of a mess to add support for loading executable code into your application at runtime. But these days, with the help of `NSBundle` and a bit of forward thinking on your part, it has never been easier.\n\n\n## Bundles and Interfaces\n\nIf you open up Xcode 5 and start to create a new project, you'll see categories under the OS X section called \"Application Plug-in\" and \"System Plug-in.\" From Screen Savers to Image Units, there are a total of 12 different templates for writing app-specific plugins in Xcode. And if you click on the \"Framework & Library\" section, you'll see an entry there named \"Bundle.\"  It's a very simple project, and it's what I'll be exploring today, along with adding support for loading a bundle in a modified version of TextEdit.\n\n> Note: Apple denotes these as \"plug-ins,\" while the rest of the world believes this is silly and instead uses the term \"plugins.\"  When writing anything that will show up in the UI, I think it's best to stick to what the platform uses, for the sake of consistency.  You'll be seeing \"plug-ins\" for the app UI, but in code and throughout this article, I'll use the term \"plugin\" (and I'll also use \"bundle\" and \"plugin\" interchangeably at times).\n\nWhat is a bundle?  If you create a new project in Xcode from the bundle template, you'll see that it's not much.  When built, you'll get a folder that is built much like applications are — there's a Contents folder with the familiar Info.plist and Resource folder.  And if you add a new class to your project, you'll find a MacOS folder with an executable file in it. The one thing you don't have, however, is a main() function in your project.  That's going to be handled by the host application.\n\n\n## Adding Plugin Support to TextEdit\n\nI'm going to show you two techniques for adding plugin support to your application.  The first technique is going to show the minimum amount of work that is needed to add plugin support to an application, and hopefully also illustrate how dead simple it is to do.\n\nThe second technique is going to be a bit more complicated, but it'll show a reasonable way to add support to an app without locking yourself into a specific implementation in the future.\n\nThe project files for this article are on [GitHub](https://github.com/objcio/issue-14-plugins) so that you can follow along better.\n\n### Scanning for Bundles in TextEdit\n\nMake sure to open up TextEdit.xcodeproj from the \"01 TextEdit\" folder, and follow along with the code it contains.\n\nThere are three easy parts to this modified version of TextEdit: scanning for bundles, loading a bundle, and adding a UI to call the bundle.\n\nOpen up Controller.m and you'll find the `-(void)loadPlugins` method (which is called from `applicationDidFinishLaunching:`).\n\nThe `loadPlugins` method adds a new `NSMenuItem` right after the View menu, which is where your plugins are going to be called from (normally you'd do this in MainMenu.xib and hook up outlets, but you’re being lazy today).  Then make and grab a path to your plugins folder (which lives in ~/Library/Application Support/Text Edit/Plug-Ins/), and scan it:\n\n```objc\n NSString *pluginsFolder = [self pluginsFolder];\n NSFileManager *fm = [NSFileManager defaultManager];\n\n NSError *outErr;\n for (NSString *item in [fm contentsOfDirectoryAtPath:pluginsFolder error:&outErr]) {\n\n     if (![item hasSuffix:@\".bundle\"]) {\n         continue;\n     }\n\n     NSString *bundlePath = [pluginsFolder stringByAppendingPathComponent:item];\n\n     NSBundle *b = [NSBundle bundleWithPath:bundlePath];\n\n     if (!b) {\n         NSLog(@\"Could not make a bundle from %@\", bundlePath);\n         continue;\n     }\n\n     id <TextEditPlugin> plugin = [[b principalClass] new];\n\n     NSMenuItem *item = [pluginsMenu addItemWithTitle:[plugin menuItemTitle] action:@selector(pluginMenuItemCalledAction:) keyEquivalent:@\"\"];\n\n     [item setRepresentedObject:plugin];\n\n }\n```\n\nSo far, this looks pretty easy.  You scan the plugins folder, make sure things kind of look like a .bundle (you don't want to try and load .DS_Store files, for instance), use `NSBundle` to load up what you've found (which does the heavy lifting), and then instantiate a class from the bundle.\n\nYou'll notice a reference to a TextEditPlugin protocol.  A quick look in TextEditMisc.h finds this:\n\n```objc\n @protocol TextEditPlugin <NSObject>\n - (NSString*)menuItemTitle;\n - (void)actionCalledWithTextView:(NSTextView*)textView inDocument:(id)document;\n @end\n```\n\nWhat this says is that you're expecting your instantiated class to respond to these two methods.  You could even verify what the class does with some more code (and that's generally a smart idea) — but you’re keeping things as simple as possible right now.\n\nOK, what's this `principalClass` method that you're calling on the bundle? When you make a bundle, you can have one or more classes in it, and you're going to need some way to let TextEdit know which class to instantiate from that bundle.  To help the host app out, you can add a key of `NSPrincipalClass` to the bundle's Info.plist file, and the value is going to be the name of the class that implements the plugin methods. `[NSBundle principalClass]` is a shortcut for looking up and creating a class from the `NSPrincipalClass` value.\n\nMoving on: add a new menu item under the Plug-Ins menu, set its action to `pluginMenuItemCalledAction:`, and set its represented object to the class you instantiated.\n\nNotice that you didn't set a target to the menu item.  If a menu item has a nil target, then it'll look through the responder chain for the first object that implements the `pluginMenuItemCalledAction:` method, and if it can't find it, the menu item will be disabled.\n\nFor this example, the best place to implement the `pluginMenuItemCalledAction:` method is going to be in the `Document`'s window controller class.  Open up DocumentWindowController.m, and scroll down to `pluginMenuItemCalledAction:`\n\n```objc\n- (void)pluginMenuItemCalledAction:(id)sender {\n    id <TextEditPlugin>plugin = [sender representedObject];\n    [plugin actionCalledWithTextView:[self firstTextView] inDocument:[self document]];\n}\n```\n\nThis is pretty self-explanatory.  Grab the plugin instance that you stuffed into the menu's represented object, call `actionCalledWithTextView:inDocument:` (which was defined in the protocol), and go on your merry way.\n\n\n#### Take a Look at the Plugin\n\nOpen up the \"01 MarkYellow\" project and take a look around.  This is a standard project made in Xcode (via the OS X ▸ Framework & Library ▸ Bundle template), with a single class added to it:  \"TEMarkYellow.\"\n\nIf you open up MarkYellow-Info.plist, you'll see your `NSPrincipalClass` set to `TEMarkYellow`, as described earlier.\n\nNext, open up TEMarkYellow.m, and you'll see the methods that are defined in the protocol.  One returns the name of your plugin, which goes under the menu, and the more interesting one (`actionCalledWithTextView:inDocument:`) does a relatively simple task: it takes the currently selected text and changes its background to yellow:\n    \n```objc\n- (void)actionCalledWithTextView:(NSTextView*)textView inDocument:(id)document {\n    if ([textView selectedRange].length) {\n       \n        NSMutableAttributedString *ats = [[[textView textStorage] attributedSubstringFromRange:[textView selectedRange]] mutableCopy];\n\n        [ats addAttribute:NSBackgroundColorAttributeName value:[NSColor yellowColor] range:NSMakeRange(0, [ats length])];\n\n        // By asking the text view if you can change the text first, it will automatically do the right thing to enable undoing of attribute changes\n        if ([textView shouldChangeTextInRange:[textView selectedRange] replacementString:[ats string]]) {\n            [[textView textStorage] replaceCharactersInRange:[textView selectedRange] withAttributedString:ats];\n            [textView didChangeText];\n        }\n    }\n}\n```\n\nMake sure to run the TextEdit project once (which will create the Plug-Ins folder), then build the MarkYellow project, drop the \"MarkYellow.bundle\" file it produced into your ~/Library/Application Support/Text Edit/Plug-Ins/ folder, and relaunch your hacked version of TextEdit.\n\nThat wasn't so bad.  Scan, load, and insert in a menu.  Then, when your menu item is used, just pass on some values to the plugin.  Try it out, and be amazed when your selected text gets a yellow background when the Plug-Ins ▸ Mark Selected Text Yellow is used.\n\nThis is awesome.  This is also extremely fragile and not progressive at all.\n\nSo close the two projects, throw them away, and maybe try to forget them.\n\n\n## OK, How Can You Make This Better?\n\nWhat are the problems with the above approach?\n\n* Only a single 'action' can live in a bundle.  That's horribly inconvenient for plugin authors.  Wouldn't it be easier if you could have more functionality in a bundle, with the ability to have more menu items?\n* It's not a forward-looking way of doing things.  Hardwiring in a specific method name on a plugin class locks things down quite a bit.  Instead, try re-engineering things so that the plugin drives things a bit more.\n\nThis time, you’re going to begin by inspecting the bundle.  Open up \"02 MarkYellow\" and its associated xcodeproj file.  Navigate to TEMarkYellow.m, and you'll instantly see that you've got a bunch more code in here, but it also does a lot more.\n\nInstead of implementing a method that says the plugin name, implement a `pluginDidLoad:` method which passes along an interface as an argument, and which you can use to tell TextEdit what your action names are and what selectors to use when calling them, as well as a user object which will aid in storing any state associated with the particular text action.\n\nAnd instead of only one action, you now have three: one for turning your text yellow, one for turning your text blue, and one that runs selected text as AppleScript. And this class only implements two methods for performing the actions by taking advantage of the `userObject` when registering.\n\nThis approach is so much more flexible than the first method described. However, it adds more complexity on the application side.\n\n\n## Add More Complexity to TextEdit\n\nOpen up \"02 TextEdit\" and take a look at Controller.m.  It doesn't do much anymore, but it does set things up in `applicationDidFinishLaunching:` for a new class, called \"PluginManager.\"  Open up PluginManager.m next, and navigate down to `-loadPlugins`.\n\nThis is almost the same as `-loadPlugins` in the previous version, except now instead of adding a menu item in the for loop, just instantiate the `principalClass` from the bundle and call `pluginDidLoad:` — which then does the driving of TextEdit.\n\nIf you take a look at `-addPluginsMenuWithTitle:…`, you'll see where the menu items are created.  And instead of stuffing the instance into the menu items' `representedObject`, you instantiate a helper class (PluginTarget), which holds a reference to the text action and friends.  Stuff the PluginTarget instance in the menu item for use later on.\n\nHowever, the selector set on the menu item is still `pluginMenuItemCalledAction:` — open up that method in DocumentWindowController.m and see what is happening there:\n\n```objc\n- (void)pluginMenuItemCalledAction:(id)sender {\n    \n    PluginTarget *p = [sender representedObject];\n\n    NSMethodSignature *ms = [[p target] methodSignatureForSelector:[p action]];\n    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:ms];\n    \n    NSTextView *tv = [self firstTextView];\n    id document = [self document];\n    id userObject = [p userObject];\n    \n    [invocation setTarget:[p target]];\n    [invocation setSelector:[p action]];\n    [invocation setArgument:&tv atIndex:2];\n    [invocation setArgument:&document atIndex:3];\n    [invocation setArgument:&userObject atIndex:4];\n    [invocation invoke];\n}\n```\n\nThis version is a bit more complicated than the previous implementation, but only because you have to deal with more information.  Build up an `NSInvocation`, set the arguments, and call it on the instance from the plugin.\n\nThis is more work on the host side, but way more flexible for plugin authors.\n\n\n## Where to Go from Here\n\nOne neat thing about this new interface is that you could then write a plugin which would load up other custom-made plugins.  Imagine that you want to add the ability for your users to write plugins in JavaScript.  A plugin for that would scan a folder for .js files when `pluginDidLoad:` is called, and then add an entry with `addPluginsMenuWithTitle:…` for each .js file.  Then, when called, it could set things up with JavaScriptCore and execute a script.  The same thing could be done with Python, Ruby, or Lua (and I've actually done this in the past).\n\n\n## A Final Note on Security\n\n\"Plugins make security people twitch\" - Anonymous\n\nSo the elephant in the room here is security.  When you're loading executable bundles into your process, you're basically saying: \"Here are the keys to my house. Make sure to turn off the lights when you are done. Please don't drink all the milk. And whatever you do, keep the fire pits outside.\" You're trusting whomever wrote the plugin to do the right thing, and that might not always happen.\n\nWhat could go wrong?  Well, a badly written plugin could take up all the available memory, keep the CPUs pegged at 100% continuously, and crash a lot (ahm, Flash).  Maybe someone wrote a plugin that looks like it does nice things, but a month after a customer installs it, a bit of code triggers to send off your Contacts database to a third party.  I could go on, but you get the idea.\n\nWhat can be done about this?  Well, you could also design your plugin API around the idea of running everything in separate address space (which would solve the crash problems, and possibly the memory and CPU problems), and force the plugins to be sandboxed (and if you check for the right permissions, it will make sure your Contacts database isn't read).  I can think of a number of ways to do this off the top of my head, but the best solution is probably to use Apple's XPC.\n\nI'll leave that exploration up to the reader, but you should always keep security in the back of your mind when dealing with plugins.  Of course, placing plugins in a sandbox or another process removes most of the fun and adds a bit of work to your end as well, so maybe it isn't a big deal for your application.\n\n\n"
  },
  {
    "path": "2014-07-11-sandbox-scripting.md",
    "content": "---\ntitle: Scripting from a Sandbox\ncategory: \"14\"\ndate: \"2014-07-11 10:00:00\"\nauthor:\n  - name: Craig Hockenberry\n    url: https://twitter.com/chockenberry/\ntags: article\n---\n\n\nIntroduction\n------------\n\nScripting between Mac applications has long been a part of the desktop ecosystem. It was originally [introduced](http://en.wikipedia.org/wiki/AppleScript) in October 1993 as part of System 7 as a way to create complex workflows using publishing applications like QuarkXPress. Since then, many applications have supported AppleScript through the use of scripting dictionaries ([Brent's article](/issues/14-mac/scripting-data/) shows you how to do this.) In this article, I'm going to explain how to communicate with another app using the commands and objects in its scripting dictionary.\n\nBut before we do that, we need to take a look at some recent events on the Mac platform. After opening the Mac App Store in late 2010, Apple announced that all developer submissions would need to run in a sandbox by November 2011. This deadline was pushed back several times, until it eventually went into effect on June 1, 2012.\n\nThe moving deadline should be your first clue that getting Mac apps to run in a sandbox was not exactly straightforward. Unlike their counterparts on iOS that had *always* run in a sandbox, many long-time developers realized that a secure environment would mean a lot of changes to their apps. As I heard one Apple security engineer put it, \"We're putting the genie back into the bottle.\"\n\nOne of the major challenges with this effort was with apps that used AppleScript. Many functions that used to be easy were suddenly difficult. Other things became outright impossible to accomplish. The main cause of this frustration was that apps could no longer arbitrarily control another app via scripting. From a security point of view, there are many good reasons why this is a bad idea. From a developer and customer point of view, a lot of things broke.\n\nInitially, Apple helped ease the transition by granting 'temporary exceptions' in an application's entitlements. These exceptions allowed apps to retain functionality that would have otherwise been lost. And as the name indicates, many of these special cases are disappearing, as alternative ways of controlling other apps have been made available in more recent versions of OS X.\n\nThis tutorial will show you the current best practices for controlling another app using AppleScript. I'll also show you some tricks that will help you and your customers get AppleScripts set up with a minimum amount of effort.\n\n\nFirst Steps\n-----------\n\nThe first thing you need to learn is how to run an AppleScript from your own app. Typically, the hardest part of this is writing AppleScript code. Behold:\n\n```\non chockify(inputString)\n    set resultString to \"\"\n\n    repeat with inputStringCharacter in inputString\n        set asciiValue to (ASCII number inputStringCharacter)\n        if (asciiValue > 96 and asciiValue < 123) then\n            set resultString to resultString & (ASCII character (asciiValue - 32))\n        else\n            if ((asciiValue > 64 and asciiValue < 91) or (asciiValue = 32)) then\n                set resultString to resultString & inputStringCharacter\n            else\n                if (asciiValue > 47 and asciiValue < 58) then\n                    set numberStrings to {\"ZERO\", \"ONE\", \"TWO\", \"THREE\", \"FOR\", \"FIVE\", \"SIX\", \"SEVEN\", \"EIGHT\", \"NINE\"}\n                    set itemIndex to asciiValue - 47\n                    set numberString to item itemIndex of numberStrings\n                    set resultString to resultString & numberString & \" \"\n                else\n                    if (asciiValue = 33) then\n                        set resultString to resultString & \" DUH\"\n                    else\n                        if (asciiValue = 63) then\n                            set resultString to resultString & \" IF YOU KNOW WHAT I MEAN\"\n                        end if\n                    end if\n                end if\n            end if\n        end if\n    end repeat\n\n    resultString\nend chockify\n```\n\nIn my opinion, AppleScript's greatest strength is not its syntax. Nor is its ability to process strings, even when it's making them AWESOME DUH\n\nWhen developing scripts like this, I constantly refer to the [AppleScript Language Guide](https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptlangguide/introduction/ASLR_intro.html#//apple_ref/doc/uid/TP40000983-CH208-SW1). The good news is that scripts that communicate with other apps are typically short and sweet. AppleScript can be thought of as a transport mechanism rather than a processing environment. The script shown above is atypical.\n\nOnce you have your script written and tested, you can move back to the comfortable environs of Objective-C. And the first line of code you'll write is a trip back in time to the Carbon era:\n\n```objc\n#import <Carbon/Carbon.h> // for AppleScript definitions\n```\n\nDon't worry; you're not going to do anything crazy like add a framework to the project. You just need Carbon.h because it has a list of all the AppleEvent definitions. Remember, this code has been around for more than 20 years!\n\nOnce you have the definitions, you can create an event descriptor. This is a chunk of data that is passed both to and from your script. At this point, you can think of it as an encapsulation of a target that will execute the event, a function to call, and a list of parameters for that function. Here is one for the \"chockify\" function above, using an `NSString` as a parameter:\n\n```objc\n- (NSAppleEventDescriptor *)chockifyEventDescriptorWithString:(NSString *)inputString\n{\n    // parameter\n    NSAppleEventDescriptor *parameter = [NSAppleEventDescriptor descriptorWithString:inputString];\n    NSAppleEventDescriptor *parameters = [NSAppleEventDescriptor listDescriptor];\n    [parameters insertDescriptor:parameter atIndex:1]; // you have to love a language with indices that start at 1 instead of 0\n\n    // target\n    ProcessSerialNumber psn = {0, kCurrentProcess};\n    NSAppleEventDescriptor *target = [NSAppleEventDescriptor descriptorWithDescriptorType:typeProcessSerialNumber bytes:&psn length:sizeof(ProcessSerialNumber)];\n\n    // function\n    NSAppleEventDescriptor *function = [NSAppleEventDescriptor descriptorWithString:@\"chockify\"];\n\n    // event\n    NSAppleEventDescriptor *event = [NSAppleEventDescriptor appleEventWithEventClass:kASAppleScriptSuite eventID:kASSubroutineEvent targetDescriptor:target returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID];\n    [event setParamDescriptor:function forKeyword:keyASSubroutineName];\n    [event setParamDescriptor:parameters forKeyword:keyDirectObject];\n\n    return event;\n}\n```\n\n_Note:_ This code is available on [GitHub](https://github.com/objcio/issue-14-sandbox-scripting). The `Automation.scpt` file contains the chockify function and all the other scripts used in this tutorial. The Objective-C code is all in `AppDelegate.m`.\n\nNow that you have an event descriptor that tells AppleScript what you want to do, you need to give it somewhere to do it. That means loading an AppleScript from your application bundle: \n\n```objc\nNSURL *URL = [[NSBundle mainBundle] URLForResource:@\"Automation\" withExtension:@\"scpt\"];\nif (URL) {\n    NSAppleScript *appleScript = [[NSAppleScript alloc] initWithContentsOfURL:URL error:NULL];\n\n    NSAppleEventDescriptor *event = [self chockifyEventDescriptorWithString:[self.chockifyInputTextField stringValue]];\n    NSDictionary *error = nil;\n    NSAppleEventDescriptor *resultEventDescriptor = [appleScript executeAppleEvent:event error:&error];\n    if (! resultEventDescriptor) {\n        NSLog(@\"%s AppleScript run error = %@\", __PRETTY_FUNCTION__, error);\n    }\n    else {\n        NSString *string = [self stringForResultEventDescriptor:resultEventDescriptor];\n        [self updateChockifyTextFieldWithString:string];\n    }\n}\n```\n\nAn instance of `NSAppleScript` is created using a URL from the application bundle. That script, in turn, is used with the chockify event descriptor created above. If everything goes according to plan, you end up with another event descriptor. If not, you get a dictionary back that contains information describing what went wrong. Although the pattern is similar to many other Foundation classes, the error _is not_ an instance of `NSError`.\n\nAll that's left to do now is extract the information you want from the descriptor:\n\n```objc\n- (NSString *)stringForResultEventDescriptor:(NSAppleEventDescriptor *)resultEventDescriptor\n{\n    NSString *result = nil;\n\n    if (resultEventDescriptor) {\n        if ([resultEventDescriptor descriptorType] != kAENullEvent) {\n            if ([resultEventDescriptor descriptorType] == kTXNUnicodeTextData) {\n                result = [resultEventDescriptor stringValue];\n            }\n        }\n    }\n\n    return result;\n}\n```\n\nYour InputString just got a facelift, and you've seen everything you need to run AppleScripts from your app. Sort of.\n\n\nThe Way It Used To Be\n---------------------\n\nThere was a time when you could send AppleEvents to any application, not just to the currently running application, as we did with chockify above.\n\nSay you wanted to know what URL was loaded into the foremost window of Safari. All you needed to do was `tell application \"Safari\"` what to do:\n\n```\non safariURL()\n    tell application \"Safari\" to return URL of front document\nend safariURL\n```\n\nThese days, all doing that is likely to produce is the following in your Debug Console:\n\n```\nAppleScript run error = {\n    NSAppleScriptErrorAppName = Safari;\n    NSAppleScriptErrorBriefMessage = \"Application isn\\U2019t running.\";\n    NSAppleScriptErrorMessage = \"Safari got an error: Application isn\\U2019t running.\";\n    NSAppleScriptErrorNumber = \"-600\";\n    NSAppleScriptErrorRange = \"NSRange: {0, 0}\";\n}\n```\n\nEven though Safari is running. What. The.\n\n\nSandbox Restrictions\n--------------------\n\nYou're trying to run this script from an application sandbox. As far as that sandbox is concerned, Safari is, in fact, not running.\n\nThe problem is that no one gave your app permission to talk to Safari. This turns out to be a pretty big security hole: a script can easily get the contents of the current page or even run JavaScript against any tab of any window in your browser. Imagine how great that would be if one of those pages was for your bank account. Or if one of the pages contained a form field with your credit card number. Ouch.\n\nThat, in a nutshell, is why arbitrary script execution was banned from the Mac App Store.\n\nLuckily, things have gotten much better in recent releases of OS X. In 10.8 Mountain Lion, Apple introduced a new abstract class called `NSUserScriptTask`. There are three concrete subclasses that let you run Unix shell commands (`NSUserUnixTask`), Automator workflows (`NSUserAutomatorTask`), and our beloved AppleScript (`NSUserAppleScriptTask`). The remainder of this tutorial will focus on that last class, since it's the one most commonly used.\n\nApple's mantra for the application sandbox is to drive security policy through user intent. In practice, this means a user has to decide to want to run your script. It might have come from the Internet or it might be a part of your application; the only thing that matters is that your customer says \"Yes, I want to run this script.\" Once that permission is granted, the scripts are run in a way where the interaction with the rest of the system is limited. The `NSUserScriptTask` class makes all this possible.\n\n\nInstalling Scripts\n------------------\n\nSo how does a user grant access for an application that wants to run scripts?\n\nThe mechanism is surprisingly simple: your application can only run scripts from a specific folder in the user's account. The only way scripts can get into that folder is if the user copies them there. Essentially, OS X gives you a read-only view of what's in those scripts.\n\nThis presents a challenge: the folder is in User > Library > Application Scripts and is named using the application's bundle identifier. For [Scriptinator](https://github.com/objcio/issue-14-sandbox-scripting), that folder is named in a way only a programmer could love: `com.iconfactory.Scriptinator`. None of this is very user-friendly, especially since the Library folder is hidden by default on OS X.\n\nOne approach to this problem is to implement some code that opens this hidden folder for your customer. For example:\n\n```objc\nNSError *error;\nNSURL *directoryURL = [[NSFileManager defaultManager] URLForDirectory:NSApplicationScriptsDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error];\n[[NSWorkspace sharedWorkspace] openURL:directoryURL];\n```\n\nThat's a great solution for scripts written by a user. The user can then open that folder using a control in your app and edit scripts in any way seen fit.\n\nBut sometimes you'll want to help the end user install scripts that you've written. Chances are, you're a better programmer than your average customer, and you know how to write code that makes your app work better with your customer's other apps. The natural place to put your own scripts is in the application bundle, but how do you get scripts into the user's scripts folder?\n\nThe solution here is to get permission to write into that folder. In Xcode, you need to update your app's Capabilities to \"User Selected File to Read/Write,\" under App Sandbox > File Access. Again, user intent is the guiding factor here, since you're being given permission to add scripts to the folder:\n\n```objc\nNSError *error;\nNSURL *directoryURL = [[NSFileManager defaultManager] URLForDirectory:NSApplicationScriptsDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error];\nNSOpenPanel *openPanel = [NSOpenPanel openPanel];\n[openPanel setDirectoryURL:directoryURL];\n[openPanel setCanChooseDirectories:YES];\n[openPanel setCanChooseFiles:NO];\n[openPanel setPrompt:@\"Select Script Folder\"];\n[openPanel setMessage:@\"Please select the User > Library > Application Scripts > com.iconfactory.Scriptinator folder\"];\n[openPanel beginWithCompletionHandler:^(NSInteger result) {\n    if (result == NSFileHandlingPanelOKButton) {\n        NSURL *selectedURL = [openPanel URL];\n        if ([selectedURL isEqual:directoryURL]) {\n            NSURL *destinationURL = [selectedURL URLByAppendingPathComponent:@\"Automation.scpt\"];\n            NSFileManager *fileManager = [NSFileManager defaultManager];\n            NSURL *sourceURL = [[NSBundle mainBundle] URLForResource:@\"Automation\" withExtension:@\"scpt\"];\n            NSError *error;\n            BOOL success = [fileManager copyItemAtURL:sourceURL toURL:destinationURL error:&error];\n            if (success) {\n                NSAlert *alert = [NSAlert alertWithMessageText:@\"Script Installed\" defaultButton:@\"OK\" alternateButton:nil otherButton:nil informativeTextWithFormat:@\"The Automation script was installed succcessfully.\"];\n                [alert runModal];\n            }\n            else {\n                NSLog(@\"%s error = %@\", __PRETTY_FUNCTION__, error);\n                if ([error code] == NSFileWriteFileExistsError) {\n                    // this is where you could update the script, by removing the old one and copying in a new one\n                }\n                else {\n                    // the item couldn't be copied, try again\n                    [self performSelector:@selector(installAutomationScript:) withObject:self afterDelay:0.0];\n                }\n            }\n        }\n        else {\n            // try again because the user changed the folder path\n            [self performSelector:@selector(installAutomationScript:) withObject:self afterDelay:0.0];\n        }\n    }\n}];\n```\n\nThat `Automation.scpt` file that we used to run from inside the application bundle is now exposed in the regular file system.\n\nIt's important throughout this entire process to let your customer know exactly what's going on. You have to remember that the customer is the one in control of the script, not you. If the customer decides to clear out all his or her scripts from the folder, you need to cope with that. You may need to disable an app feature that requires the script, or explain why the script needs to be installed again.\n\n_Note:_ The [Scriptinator](https://github.com/objcio/issue-14-sandbox-scripting) sample code includes both of the approaches shown above. For a real world example, take a look at the [Overlay](http://xscopeapp.com/guide#overlay) tool in the free trial version of [xScope](http://xscopeapp.com/). It has a user-friendly setup procedure and sophisticated scripting that lets the app communicate with the customer's web browser. As a bonus, you may find that xScope is a great tool for doing your own development!\n\n\nScripting Tasks\n---------------\n\nNow that you have the automation scripts in the right place, you can start to use them.\n\nIn the code below, the event descriptors that we created above have not changed. The only thing that's different is how they're run: you'll be using an `NSUserAppleScriptTask` instead of `NSAppleScript`.\n\nPresumably, you'll be using these script tasks frequently. The documentation warns that `NSUserAppleScriptTask` \"should be invoked no more than once for a given instance of the class,\" so it's a good idea to write a factory method that creates these tasks as needed:\n\n```objc\n- (NSUserAppleScriptTask *)automationScriptTask\n{\n    NSUserAppleScriptTask *result = nil;\n\n    NSError *error;\n    NSURL *directoryURL = [[NSFileManager defaultManager] URLForDirectory:NSApplicationScriptsDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error];\n    if (directoryURL) {\n        NSURL *scriptURL = [directoryURL URLByAppendingPathComponent:@\"Automation.scpt\"];\n        result = [[NSUserAppleScriptTask alloc] initWithURL:scriptURL error:&error];\n        if (! result) {\n            NSLog(@\"%s no AppleScript task error = %@\", __PRETTY_FUNCTION__, error);\n        }\n    }\n    else {\n        // NOTE: if you're not running in a sandbox, the directory URL will always be nil\n        NSLog(@\"%s no Application Scripts folder error = %@\", __PRETTY_FUNCTION__, error);\n    }\n\n    return result;\n}\n```\n\nIf you're writing a Mac app that has both a sandboxed and non-sandboxed version, you'll need to be careful getting the `directoryURL`. The `NSApplicationScriptsDirectory` is only available when sandboxed.\n\nAfter creating the script task, you execute it with an AppleEvent and provide a completion handler:\n\n```objc\nNSUserAppleScriptTask *automationScriptTask = [self automationScriptTask];\nif (automationScriptTask) {\n    NSAppleEventDescriptor *event = [self safariURLEventDescriptor];\n    [automationScriptTask executeWithAppleEvent:event completionHandler:^(NSAppleEventDescriptor *resultEventDescriptor, NSError *error) {\n        if (! resultEventDescriptor) {\n            NSLog(@\"%s AppleScript task error = %@\", __PRETTY_FUNCTION__, error);\n        }\n        else {\n            NSURL *URL = [self URLForResultEventDescriptor:resultEventDescriptor];\n            // NOTE: The completion handler for the script is not run on the main thread. Before you update any UI, you'll need to get\n            // on that thread by using libdispatch or performing a selector.\n            [self performSelectorOnMainThread:@selector(updateURLTextFieldWithURL:) withObject:URL waitUntilDone:NO];\n        }\n    }];\n}\n```\n\nFor scripts that a user has written, the user may expect your app to simply 'run' the script (and not call a function specified in an event descriptor). In this case, you'll pass `nil` for the event and the script will behave as if the user double-clicked on it in the Finder.\n\nOne of the nice things about `NSUserAppleScriptTask` is the completion handler. Scripts are run asynchronously, so your user interface doesn't need to block while a (potentially lengthy) script is run. Be careful about what you do when that completion handler is invoked; it's not running on the main thread, so you can't make any updates to your user interface there.\n\n\nBehind the Scenes\n-----------------\n\nWhat's going on behind the scenes?\n\nAs you may have guessed by the fact that scripts can only run once asynchronously, the code is now executed out of process using XPC. Just as iOS 8 uses XPC to make sure extensions don't affect the calling application in any way, a running script has no access to the address space of your calling application.\n\nIf you look at the `keySenderPIDAttr` attribute in an incoming event descriptor, you'll see that the process ID belongs to `/usr/libexec/lsboxd`, not your own application. This mysterious process is presumably the Launch Services sandbox daemon. In any case, your requests to another process are most certainly being marshaled.\n\nTo understand more about the security goals of the application sandbox at a high level, I'd recommend Ivan Krstić's talk _\"The OS X App Sandbox\"_ at [WWDC 2012](https://developer.apple.com/videos/wwdc/2012/). It's a surprisingly entertaining talk, and at 36 minutes into the presentation, the automation changes shown above are introduced. At that same conference, a talk entitled _\"Secure Automation Techniques in OS X\"_ by Sal Soghoian and Chris Nebel goes into the details of the automation changes. Skip ahead to the 35-minute mark if you just want to learn about Application-Run User Scripts.\n\nAnother important security announcement discussed in these talks — but not covered in this tutorial — are access groups. If you're going to be scripting system applications like Mail or iTunes, you'll definitely want to pay attention to this topic in the videos above.\n\n\nSynchronicity\n-------------\n\nAs I mentioned above, there is a subtle difference between `NSAppleScript` and `NSUserAppleScriptTask`: the new mechanism runs asynchronously. For the most part, using a completion handler is a much better way to deal with things, because there's nothing to block your application while a script runs.\n\nHowever, there are cases where it can get tricky if you're executing tasks with dependencies. If one task needs to complete before another is started, you'll quickly be missing the synchronous nature of `NSAppleScript`.\n\nA simple way to get behavior that mimics the old way of doing things is to use a semaphore that makes sure only one task is running at a time. In your class or application initialization, create the semaphore using `libdispatch`:\n\n```objc\nself.appleScriptTaskSemaphore = dispatch_semaphore_create(1);\n```\n\nThen simply wait on that semaphore before initiating the script task. When the task completes, signal on that same semaphore:\n\n```objc\n// wait for any previous tasks to complete before starting a new one — remember that you're blocking the main thread here!\ndispatch_semaphore_wait(self.appleScriptTaskSemaphore, DISPATCH_TIME_FOREVER);\n\n// run the script task\nNSAppleEventDescriptor *event = [self openNetworkPreferencesEventDescriptor];\n[automationScriptTask executeWithAppleEvent:event completionHandler:^(NSAppleEventDescriptor *resultEventDescriptor, NSError *error) {\n    if (! resultEventDescriptor) {\n        NSLog(@\"%s AppleScript task error = %@\", __PRETTY_FUNCTION__, error);\n    }\n    else {\n        [self performSelectorOnMainThread:@selector(showNetworkAlert) withObject:nil waitUntilDone:NO];\n    }\n    \n    // the task has completed, so let any pending tasks proceed\n    dispatch_semaphore_signal(self.appleScriptTaskSemaphore);\n}];\n```\n\nAgain, don't do this unless you have a really good reason.\n\n\nWhat Can You Script?\n--------------------\n\nIn the last example, the Network pane of System Preferences was opened with the following AppleScript code:\n\n```\ntell application \"System Preferences\"\n    launch\n    activate\n    \n    reveal pane id \"com.apple.preference.network\"\nend tell\n```\n\nPretty cool, but how the heck do you know what the IDs of the various panes are? How would you open the Accessibility view of the Security & Privacy pane instead of the Network pane?\n\nAs you can see in Brent's article, every application that supports AppleScript has a scripting dictionary. That dictionary describes the objects and properties of the app's data model. So just learn to poke around in that data model to find what you want!\n\nBegin by opening the Script Editor application in your Applications > Utilities folder. Then, from the File menu, select \"Open Dictionary….\" At that point, every application that supports AppleScript will be listed — more than you probably imagined! Select the System Preferences app from the list and click \"Choose.\"\n\nAt this point, you'll see a Standard Suite and System Preferences listed in a tree browser. The standard suite lists commands like \"open,\" classes like \"window,\" and other things that are common to most scripting dictionaries. The interesting stuff is in the other scripting suite: System Preferences. When you select it, you'll see a command named \"reveal\" and three classes (object types) named \"application,\" \"pane,\" and \"anchor.\"\n\nWhen you look at \"application,\" you'll see two things: elements and properties. Elements are collections of objects that are managed by the selected object. The properties list data maintained by the selected object.\n\n![](/images/issue-14/Scripting_Dictionary.png)\n\n\nSo an application contains panes. That sounds promising. In a new Script Editor window, create a simple script to show all the pane objects:\n\n```\ntell application \"System Preferences\"\n    panes\nend tell\n```\n\nOur goal is to open the Accessibility view of the security pane, so look through the Result in the output until you find something useful like:\n\n```\npane id \"com.apple.preference.security\" of application \"System Preferences\"\n```\n\nLearn more about it by looking at its \"localized name\" property:\n\n```\ntell application \"System Preferences\"\n    localized name of pane id \"com.apple.preference.security\"\nend tell\n```\n\nSecurity & Privacy. Bingo! Now try writing another script that uses that \"pane id\" along with the \"reveal\" command we saw earlier:\n\n```\ntell application \"System Preferences\"\n    reveal pane id \"com.apple.preference.security\"\nend tell\n```\n\nSystem Preferences just showed you the pane. Now let's figure out how to get to the right view. Start by querying for the only elements contained in a pane, the anchor objects:\n\n```\ntell application \"System Preferences\"\n    anchors of pane \"com.apple.preference.security\"\nend tell\n```\n\nLo and behold, we see:\n\n```\nanchor \"Privacy_Accessibility\" of pane id \"com.apple.preference.security\" of application \"System Preferences\"\n```\n\nThat's what we want. It also shows the hierarchy of the objects in System Preferences: an application has panes, which in turn have anchors. So let's tweak our script:\n\n```\ntell application \"System Preferences\"\n    reveal anchor \"Privacy_Accessibility\" of pane id \"com.apple.preference.security\"\nend tell\n```\n\nDone! Now imagine how helpful that could be to a user who needs to add accessibility permissions for your app. Rather than tell the user how to navigate to that preference panel, you just open it for him or her. Nice.\n\n\nWrapping Up\n-----------\n\nThere you have it: everything you need to know about controlling another app from your own app. Whether you're giving users the ability to automate their workflows or simply enabling internal functionality in your app, AppleScript is a powerful component of every Mac application, even if it's running in a sandbox. Hopefully this tutorial has given you new tools and insight on how to take advantage of these capabilities in your own projects!\n\n"
  },
  {
    "path": "2014-07-11-scripting-data.md",
    "content": "---\ntitle: Making Your Mac App’s Data Scriptable\ncategory: \"14\"\ndate: \"2014-07-11 11:00:00\"\nauthor:\n  - name: Brent Simmons\n    url: http://inessential.com/\ntags: article\n---\n\n\nWhen adding AppleScript support — which is also JavaScript support, as of OS X 10.10 — it’s best to start with your app’s data. Scripting isn’t a matter of automating button clicks; it’s about exposing the model layer to people who could use your app in their workflows.\n\nWhile that’s usually a small minority of users, they’re power users — the kind of people who recommend apps to friends and family. They blog and tweet about apps, and people listen to them. They can be your app’s biggest evangelists.\n\nOverall, the best reason to add scripting support is that it’s a matter of professionalism. But it doesn’t hurt that the effort is worth the reward.\n\n## Noteland\n\nNoteland is an app without any UI except for a blank window — but it has a model layer, and it’s scriptable. You can [find it on GitHub](https://github.com/objcio/issue-14-scriptable-apps) and follow along.\n\nIt supports AppleScript (and JavaScript on 10.10). It’s written in Objective-C in Xcode 5.1.1. We initially tried to use Swift and Xcode 6 Beta 2, but ran into snags, though it’s entirely likely they were our own fault, since we’re still learning Swift.\n\n### Noteland’s Object Model\n\nThere are two classes: notes and tags. There may be multiple notes, and a note may have multiple tags.\n\nNLNote.h declares several properties: `uniqueID`, `text`, `creationDate`, `archived`, `tags`, and a read-only `title` property.\n\nTags are simpler. NLTag.h declares two scriptable properties: `uniqueID` and `name`.\n\nWe want users to be able to create, edit, and delete notes and tags, and to be able to access and change all of their properties, with the exception of any that are read-only.\n\n### Scripting Definition File (.sdef)\n\nThe first step is to define the scripting interface — it’s conceptually like creating a .h file for scripters, but in a format that AppleScript understands.\n\nIn the past, we’d create and edit an aete resource (“aete” stands for Apple Event Terminology.) These days it’s much easier: we create and edit an sdef (scripting definition) XML file.\n\nYou might think you’d prefer JSON or a plist, but XML is a decent match for this — beats an aete resource hands-down, at least. In fact, there was a plist version for a while, but it required *two* different plists that you had to keep in sync. It was a pain.\n\nThe original name of the resource points to a matter worth noting. An Apple event is the low-level message that AppleScript generates, sends, and receives. It’s an interesting technology on its own, and has uses outside of scripting support. Additionally, it’s been around since System 7 in the early ’90s, and has survived the transition to OS X.\n\n(Speculation: Apple events survived because so many print publishers relied on AppleScript, and publishers were among Apple’s most loyal customers during the 'dark days,' in the middle and late ’90s.)\n\nAn sdef file always starts with the same header:\n\n```xml\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE dictionary SYSTEM \"file://localhost/System/Library/DTDs/sdef.dtd\">\n```\n\nThe top-level item is a dictionary — “dictionary” is AppleScript’s word for a scripting interface. Inside the dictionary you'll find one or more suites.\n\n(Tip: open AppleScript Editor and choose File > Open Dictionary… You’ll see a list of apps with scripting dictionaries. If you choose one — iTunes, for instance — you’ll see the classes, properties, and commands that iTunes understands.)\n\n```xml\n<dictionary title=\"Noteland Terminology\">\n```\n\n#### Standard Suite\n\nThe standard suite defines classes and commands all applications should support. It includes quitting, closing windows, making and deleting objects, querying objects, and so on.\n\nTo add it to your sdef file, copy and paste from the canonical copy of the standard suite at `/System/Library/ScriptingDefinitions/CocoaStandard.sdef`.\n\nCopy everything from `<suite name=\"Standard Suite\"`, through and including the closing `</suite>`.\n\nPaste it right below the `dictionary` element in your sdef.\n\nThen, in your sdef file, go through and delete everything that doesn’t apply. Noteland isn’t document-based and doesn’t print, so we removed the open and save commands, the document class, and everything to do with printing.\n\n(Tip: Xcode does a good job indenting XML. To re-indent, select all the text and choose the Editor > Structure > Re-Indent command.)\n\nOnce you’ve finished editing, use the command-line xmllint program — `xmllint path/to/noteland.sdef` — to make sure your XML is okay. If it just displays the XML, without errors or warnings, then it’s fine. (Remember that you can drag the document proxy icon from a window title in Xcode into Terminal and it will paste in the path to the file.)\n\n#### Noteland Suite\n\nA single app-defined suite is usually best, though not mandated: you could have more than one when it makes sense. Noteland defines just one, the Noteland Suite:\n\n```xml\n<suite name=\"Noteland Suite\" code=\"Note\" description=\"Noteland-specific classes.\">\n```\n\nA scripting dictionary expects things to be contained by other things. The top-level container is the application object itself.\n\nIn Noteland, its class name is `NLApplication`. You should always use the code `capp` for the application class: it’s a standard Apple event code. (Note that it’s also present in the standard suite.)\n\n```xml\n<class name=\"application\" code=\"capp\" description=\"Noteland’s top level scripting object.\" plural=\"applications\" inherits=\"application\">\n    <cocoa class=\"NLApplication\"/>\n```\n\nThe application contains an array of notes. It’s important to differentiate elements (items there can be more than one of) and properties. In other words, an array in code should be an element in your dictionary:\n\n```xml\n<element type=\"note\" access=\"rw\">\n    <cocoa key=\"notes\"/>\n</element>\n```\n\nCocoa scripting uses Key-Value Coding (KVC), and the dictionary specifies the key names.\n\n#### Note Class\n\n```xml\n<class name=\"note\" code=\"NOTE\" description=\"A note\" inherits=\"item\" plural=\"notes\">\n    <cocoa class=\"NLNote\"/>\n```\n\nThe code is `NOTE`. It could be almost anything, but note that Apple reserves all lowercase codes for its own use, so `note` wouldn’t be allowed. It could be `NOT*`, or `NoTe`, or `XYzy`, or whatever you want. (Ideally the code wouldn’t collide with codes used by other apps. But there’s no way that we know of to ensure that, so we just, well, *guess*. That said, `NOTE` may not be all that great of a guess.)\n\nYour classes should inherit from `item`. (In theory you could have a class the inherits from another of your classes, but we’ve never tried this.)\n\nThe note class has several properties:\n\n```xml\n<property name=\"id\" code=\"ID  \" type=\"text\" access=\"r\" description=\"The unique identifier of the note.\">\n    <cocoa key=\"uniqueID\"/>\n</property>\n<property name=\"name\" code=\"pnam\" type=\"text\" description=\"The name of the note — the first line of the text.\" access=\"r\">\n    <cocoa key=\"title\"/>\n</property>\n<property name=\"body\" code=\"body\" description=\"The plain text content of the note, including first line and subsequent lines.\" type=\"text\" access=\"rw\">\n    <cocoa key=\"text\"/>\n</property>\n<property name=\"creationDate\" code=\"CRdt\" description=\"The date the note was created.\" type=\"date\" access=\"r\"/>\n<property name=\"archived\" code=\"ARcv\" description=\"Whether or not the note has been archived.\" type=\"boolean\" access=\"rw\"/>\n```\n\nWhenever possible, it’s best to provide unique IDs for your objects. Otherwise, scripters have to rely on names and positions, which may change. Use the code 'ID  ' for unique IDs. (Note the two spaces; codes are four-character codes.) The name of the unique ID should always be `id`.\n\nIt’s also standard to provide a `name` property, whenever it makes sense, and the code should be `pnam`. Noteland makes this a read-only property, since the name is just the first line of the text of a note, and the text of the note is edited via the read-write `body` property.\n\nFor `creationDate` and `archived`, we don’t need to provide a Cocoa key element, since the key is the same as the property name.\n\nNote the types: text, date, and boolean. AppleScript supports these and several more, as [listed in the documentation](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_about_apps/SAppsAboutApps.html#//apple_ref/doc/uid/TP40001976-SW12).\n\nNotes can also have tags, and so there’s a tags element:\n\n```xml\n<element type=\"tag\" access=\"rw\">\n    <cocoa key=\"tags\"/>\n</element>\n</class>\n```\n\n#### Tag Class\n\nTags are `NLTag` objects:\n\n```xml\n<class name=\"tag\" code=\"TAG*\" description=\"A tag\" inherits=\"item\" plural=\"tags\">\n    <cocoa class=\"NLTag\"/>\n```\n\nTags have just two properties, `id` and `name`:\n\n```xml\n<property name=\"id\" code=\"ID  \" type=\"text\" access=\"r\" description=\"The unique identifier of the tag.\">\n    <cocoa key=\"uniqueID\"/>\n</property>\n<property name=\"name\" code=\"pnam\" type=\"text\" access=\"rw\">\n    <cocoa key=\"name\"/>\n</property>\n</class>\n```\n\nThat ends the Noteland suite and the entire dictionary:\n\n```xml\n    </suite>\n</dictionary>\n```\n\n### App Configuration\n\nApps aren’t scriptable out of the box. In Xcode, edit the app’s Info.plist.\n\nSince the app uses a custom `NSApplication` subclass — in order to provide the top-level container — we edit Principal Class (`NSPrincipalClass`) to say `NLApplication` (the name of Noteland’s `NSApplication` subclass).\n\nWe also add a Scriptable (`NSAppleScriptEnabled`) key and set it to YES. And finally, we add a Scripting definition file name (`OSAScriptingDefinition`) key and give it the name of the sdef file: noteland.sdef.\n\n### Code\n\n#### NSApplication subclass\n\nYou may be surprised by how little code there is to write.\n\nSee NLApplication.m in the Noteland project. It lazily creates a notes array and provides some dummy data. Lazily just because. It has no connection to scripting support.\n\n(Note that there’s no object persistence, since I want Noteland to be as free as possible from things other than scripting support. You’d use Core Data or an archiver or something to persist data.)\n\nIt could have just skipped the dummy data and provided an array.\n\nIn this case, the array is an `NSMutableArray`. It wouldn’t have to be — if it’s an `NSArray`, then Cocoa scripting will just replace the notes array when changes are made. But if we make it an `NSMutableArray` *and* we provide the following two methods, then the array won’t be replaced. Instead, objects will be added and removed from the mutable array:\n\n```objc\n- (void)insertObject:(NLNote *)object inNotesAtIndex:(NSUInteger)index {\n    [self.notes insertObject:object atIndex:index];\n}\n\n- (void)removeObjectFromNotesAtIndex:(NSUInteger)index {\n    [self.notes removeObjectAtIndex:index];\n}\n```\n\nAlso note that the notes array property is declared in the .m file in the class extension. There’s no need to put it in the .h file. Since Cocoa scripting uses KVC and doesn’t care about your headers, it will find it.\n\n#### NLNote Class\n\nNLNote.h declares the various properties of a note: `uniqueID`, `text`, `creationDate`, `archived`, `title`, and `tags`.\n\nThe `init` method sets the `uniqueID` and `creationDate` and sets the tags array to an empty `NSArray`. We're using an `NSArray` this time, rather than an `NSMutableArray`, just to show it can be done.)\n\nThe `title` method returns a calculated value: the first line of the text of the note. (Recall that this becomes the `name` to the scripting dictionary.)\n\nThe method to note is the `objectSpecifier` method. This is critical to your classes; scripting support needs this so it understands your objects.\n\nLuckily this method is easy to write. Though there are different types of object specifiers, it’s usually best to use `NSUniqueIDSpecifier`, since it’s stable. (Other options include `NSNameSpecifier`, `NSPositionalSpecifier`, and so on.)\n\nThe object specifier needs to know about the container, and the container is the top-level Application object.\n\nThe code looks like this:\n\n```objc\nNSScriptClassDescription *appDescription = (NSScriptClassDescription *)[NSApp classDescription];\nreturn [[NSUniqueIDSpecifier alloc] initWithContainerClassDescription:appDescription containerSpecifier:nil key:@\"notes\" uniqueID:self.uniqueID];\n```\n\n`NSApp` is the global application object; we get its `classDescription`. The key is `@\"notes\"`, a nil `containerSpecifier` refers to the top-level (app) container, and the `uniqueID` is the note’s `uniqueID`.\n\n#### Note as Container\n\nWe have to think ahead a little bit. Tags will need an `objectSpecifier` also, and tags are contained by notes — so a tag needs a reference to its containing note.\n\nCocoa scripting handles creating tags, but there’s a method we can override that lets us customize the behavior.\n\nNSObjectScripting.h defines `-newScriptingObjectOfClass:forValueForKey: withContentsValue:properties:`. That’s what we need. In NLNote.m, it looks like this:\n\n```objc\nNLTag *tag = (NLTag *)[super newScriptingObjectOfClass:objectClass forValueForKey:key withContentsValue:contentsValue properties:properties];\ntag.note = self;\nreturn tag;\n```\n\nWe create the tag using super’s implementation, then set the tag’s `note` property to the note. To avoid a possible retain cycle, NLTag.h makes the note a weak property.\n\n(You might think this is a bit inelegant, and we’d agree. We wish instead that containers were asked for the `objectSpecifiers` for their children. Something like `objectSpecifierForScriptingObject:` would be better. We filed a bug: [rdar://17473124](rdar://17473124).)\n\n#### NLTag Class\n\n`NLTag` has `uniqueID`, `name`, and `note` properties.\n\n`NLTag`’s `objectSpecifier` is conceptually the same as the code in `NLNote`, except that the container is a note rather than the top-level application class.\n\nIt looks like this:\n\n```objc\nNSScriptClassDescription *noteClassDescription = (NSScriptClassDescription *)[self.note classDescription];\nNSUniqueIDSpecifier *noteSpecifier = (NSUniqueIDSpecifier *)[self.note objectSpecifier];\nreturn [[NSUniqueIDSpecifier alloc] initWithContainerClassDescription:noteClassDescription containerSpecifier:noteSpecifier key:@\"tags\" uniqueID:self.uniqueID];\n```\n\nThat’s it. Done. That’s not much code — most of the work is in designing the interface and editing the sdef file.\n\nIn the old days, you’d still be writing Apple event handlers and working with Apple event descriptors and all kinds of crazy jazz. In other words, you’d be a long way from done. Thankfully, these aren’t the old days.\n\nThe fun part is next.\n\n### AppleScript Editor\n\nLaunch Noteland. Launch /Applications/Utilities/AppleScript Editor.app.\n\nRun the following script:\n\n```\ntell application \"Noteland\"\n    every note\nend tell\n```\n\nIn the Result pane at the bottom, you’ll see something like this:\n\n```\n{note id \"0B0A6DAD-A4C8-42A0-9CB9-FC95F9CB2D53\" of application \"Noteland\", note id \"F138AE98-14B0-4469-8A8E-D328B23C67A9\" of application \"Noteland\"}\n```\n\nThe IDs will be different, of course, but this is an indication that it’s working.\n\nTry this script:\n\n```\ntell application \"Noteland\"\n    name of every note\nend tell\n```\n\nYou’ll see `{\"Note 0\", \"Note 1\"}` in the Result pane.\n\nTry this script:\n\n```\ntell application \"Noteland\"\n    name of every tag of note 2\nend tell\n```\n\nResult: `{\"Tiger Swallowtails\", \"Steak-frites\"}`.\n\n(Note that AppleScript arrays are 1-based, so note 2 refers to the second note. Which doesn’t sound so crazy when we put it that way.)\n\nYou can also create notes:\n\n```\ntell application \"Noteland\"\n    set newNote to make new note with properties {body:\"New Note\" & linefeed & \"Some text.\", archived:true}\n    properties of newNote\nend tell\n```\n\nThe result will be something like this (with appropriate details changed):\n\n```\n{creationDate:date \"Thursday, June 26, 2014 at 1:42:08 PM\", archived:true, name:\"New Note\", class:note, id:\"49D5EE93-655A-446C-BB52-88774925FC62\", body:\"New Note\\nSome text.\"}`\n```\n\nAnd you can create new tags:\n\n```\ntell application \"Noteland\"\n    set newNote to make new note with properties {body:\"New Note\" & linefeed & \"Some text.\", archived:true}\n    set newTag to make new tag with properties {name:\"New Tag\"} at end of tags of newNote\n    name of every tag of newNote\nend tell\n```\n\nThe result will be: `{\"New Tag\"}`.\n\nIt works!\n\n### More to Learn\n\nScripting the object model is just part of adding scripting support; you can add support for commands, too. For instance, Noteland could have an export command that writes notes to files on disk. An RSS reader might have a refresh command, a Mail app might have a download mail command, and so on.\n\nMatt Neuburg’s [AppleScript: The Definitive Guide](http://www.amazon.com/AppleScript-Definitive-Guide-Matt-Neuburg/dp/0596102119/ref=la_B001H6OITU_1_1?s=books&ie=UTF8&qid=1403816403&sr=1-1) is worth checking out even though it was published in 2006, as things haven’t changed much since then. Matt also has a [tutorial on adding scripting support to Cocoa apps](http://www.apeth.net/matt/scriptability/scriptabilityTutorial.html). It's definitely worth reading, and it goes into more detail than this article.\n\nThere’s a session in the [WWDC 2014 videos](https://developer.apple.com/videos/wwdc/2014/) on JavaScript for Automation, which talks about the new JavaScript OSA language. (Years ago, Apple suggested that one day there would be a programmer’s dialect of AppleScript, since the natural language thing is a bit weird for people who write in C and C-like languages. JavaScript could be considered the programmer’s dialect.)\n\nAnd of course, Apple has documentation on the various technologies:\n\n- [Cocoa Scripting Guide](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_intro/SAppsIntro.html#//apple_ref/doc/uid/TP40002164)\n- [AppleScript Overview](https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptx/AppleScriptX.html#//apple_ref/doc/uid/10000156-BCICHGIE)\n\nAlso, see Apple’s Sketch app for an example of an app that implements scripting.\n"
  },
  {
    "path": "2014-07-11-xpc.markdown",
    "content": "---\ntitle:  \"XPC\"\ncategory: \"14\"\ndate: \"2014-07-11 08:00:00\"\ntags: article\nauthor:\n  - name: Daniel Eggert\n    url: http://twitter.com/danielboedewadt\n---\n\n## About XPC\n\n\nXPC is, first and foremost, about doing things in a different way than you'd otherwise be able to do them. It is less about enabling things that you couldn't do without XPC.\n\nXPC is all about security and stability of our apps. XPC allows us to relatively easily split our application into multiple processes. It does so by making the communication between processes easy. Additionally, it manages the life cycle of processes so that subprocesses are running when we need to to communicate with them.\n\nWe will be using the Foundation framework's API defined in the `NSXPCConnection.h` header. This API builds on top of the raw XPC API, which in turn lives inside `xpc/xpc.h`. The XPC API itself is a pure C API, which integrates nicely with libdispatch (aka GCD). We'll use the Foundation classes because they let us access almost all of the full power of XPC — they are very true in nature to how the underlying C API works. And the Foundation API is a lot easier to use than its C counterpart.\n\n### Where is XPC Used?\n\nApple uses XPC extensively for various parts of the operating system — and a lot of the system frameworks use XPC to implement their functionality. A quick run of\n\n```\n% find /System/Library/Frameworks -name \\*.xpc\n```\n\nshows 55 XPC services inside Frameworks, ranging from AddressBook to WebKit.\n\nIf we do the same search on `/Applications`, we find apps ranging from the [iWork suite](https://www.apple.com/creativity-apps/mac/) to Xcode — and even some third-party apps.\n\nAn excellent example of XPC is within Xcode itself: When you're editing Swift code in Xcode, Xcode communicates with SourceKit over XPC. SourceKit is responsible for things such as source code parsing, syntax highlighting, typesetting, and autocomplete. Read more about that [on JP Simard's blog](http://www.jpsim.com/uncovering-sourcekit/).\n\nIt's actually also used extensively on iOS — but only by Apple. It's not available to third-party developers, yet.\n\n## A Sample App\n\nLet us take a look at a trivial sample app: an app that shows multiple images in a table view. These images are downloaded as JPEG data from a web server.\n\nThe app will look something like this:\n\n![Screen shot of the sample app](/images/issue-14/xpc-SuperfamousImages-window@2x.jpg)\n\nThe `NSTableViewDataSource` will retrieve the images from a class called `ImageSet`, like this:\n\n```objc\nfunc tableView(tableView: NSTableView!, viewForTableColumn tableColumn: NSTableColumn!, row: Int) -> NSView! {\n    let cellView = tableView.makeViewWithIdentifier(\"Image\", owner: self) as NSTableCellView\n    var image: NSImage? = nil\n    if let c = self.imageSet?.images.count {\n        if row < c {\n            image = self.imageSet?.images[row]\n        }\n    }\n    cellView.imageView.image = image\n    return cellView\n}\n```\n\nThe `ImageSet` class has a simple\n\n```objc\nvar images: NSImage![]\n```\n\nproperty, which is filled asynchronously using the `ImageLoader` class.\n\n### Without XPC\n\nWithout XPC, we would implement the `ImageLoader` class to download and decompress an image, like so:\n\n\n```objc\nclass ImageLoader: NSObject {\n    let session: NSURLSession\n\n    init()  {\n        let config = NSURLSessionConfiguration.defaultSessionConfiguration()\n        session = NSURLSession(configuration: config)\n    }\n\n    func retrieveImage(atURL url: NSURL, completionHandler: (NSImage?)->Void) {\n        let task = session.dataTaskWithURL(url) {\n            maybeData, response, error in\n            if let data: NSData = maybeData {\n                dispatch_async(dispatch_get_global_queue(0, 0)) {\n                    let source = CGImageSourceCreateWithData(data, nil).takeRetainedValue()\n                    let cgImage = CGImageSourceCreateImageAtIndex(source, 0, nil).takeRetainedValue()\n                    var size = CGSize(\n                        width: CGFloat(CGImageGetWidth(cgImage)),\n                        height: CGFloat(CGImageGetHeight(cgImage)))\n                    let image = NSImage(CGImage: cgImage, size: size)\n                    completionHandler(image)\n                }\n            }\n        }\n        task.resume()\n    }\n}\n```\n\nThis is pretty straightforward and works well.\n\n### Fault Isolation and Split Privileges\n\nOur app does three distinct things: it downloads data from the internet, it decodes that data as JPEG, and it then displays the data.\n\nIf we split the app into three separate processes, we can give each process its own set of privileges; the UI process doesn't need to have access to the network. The service that downloads the images from the network does need network access, but doesn't need access to any files (it will just forward the data, not save it). The second service will decode the JPEG image data to RGB data. This process needs neither network access nor file access.\n\nThis way, we have made it dramatically more difficult for someone to find a security exploit in our app. And as a side effect, the app is way more stable; if a bug in the download service causes it to crash, the main app will keep running, and the download service will just get relaunched.\n\nThis might look something like this:\n\n![An App with two Services](/images/issue-14/xpc-app-2-services@2x.png)\n\nWe could also have designed it so that the app would talk to both services directly and be responsible for moving the data from one service to the other. The amount of flexibility is great. We'll look into how an app [locates services](#service-lookup) later.\n\nBy far, the largest set of security-related bugs is in the parsing of untrusted data, i.e. data that we have received, for example, over the Internet, and do not control. This is true for both handling the actual HTTP protocol and decoding JPEG data. With this design, we have moved parsing of untrusted data into a subprocess, an XPC service.\n\n### XPC Services at Our Service\n\nThere are two parts to the service: the service itself, and the code that communicates with it. The good news is that both are straightforward and similar to one another.\n\nXcode has a template to add a new target for a service. A service has a bundle identifier. It is good practice to set it to a *subdomain* of the app's bundle identifier. In our case, the sample app's bundle identifier is `io.objc.Superfamous-Images`, and we will set the bundle identifier of the download service to be `io.objc.Superfamous-Images.ImageDownloader`. The service target will create output in its own bundle, which will get copied into a folder called `XPCServices`, next to the `Resources` folder.\n\nWhen the app sends data to the `io.objc.Superfamous-Images.ImageDownloader` service, XPC will automatically launch the XPC service.\n\nCommunication over XPC is always asynchronous. It is defined by a protocol that both the app and the services use. In our case:\n\n```objc\n@objc(ImageDownloaderProtocol) protocol ImageDownloaderProtocol {\n    func downloadImage(atURL: NSURL!, withReply: (NSData?)->Void)\n}\n```\n\nNote the `withReply:` part. This is how the reply will get sent back to the caller asynchronously. Messages that return data need to use methods where the last part is a `withReply:` that takes a closure.\n\nIn our case, we only have one method call in the service, but we could have multiple by defining multiple in the protocol.\n\nThe connection from the app to the services is then created through an `NSXPCConnection` object, like so:\n\n```objc\nlet connection = NSXPCConnection(serviceName: \"io.objc.Superfamous-Images.ImageDownloader\")\nconnection.remoteObjectInterface = NSXPCInterface(`protocol`: ImageDownloaderProtocol.self)\nconnection.resume()\n```\n\nIf we store this connection object in `self.imageDownloadConnection`, we can communicate with the service, like so:\n\n```objc\nlet downloader = self.imageDownloadConnection.remoteObject as ImageDownloaderProtocol\ndownloader.downloadImageAtURL(url) {\n    (data) in\n    println(\"Got \\(data.length) bytes.\")\n}\n```\n\nWe should use an error handler, though. That would look like this:\n\n```objc\nlet downloader = self.imageDownloadConnection.remoteObjectProxyWithErrorHandler {\n        (error) in NSLog(\"remote proxy error: %@\", error)\n} as ImageDownloaderProtocol\ndownloader.downloadImageAtURL(url) {\n    (data) in\n    println(\"Got \\(data.length) bytes.\")\n}\n```\n\nThat's all there is to it on the app's side.\n\n### Listening to Service Requests\n\nThe service has a listener, `NSXPCListener` that listens to incoming requests (from the app). This listener will then create a connection on the service side that corresponds to each connection that the app makes to it.\n\nIn `main.swift`, we can put:\n\n```objc\nclass ServiceDelegate : NSObject, NSXPCListenerDelegate {\n    func listener(listener: NSXPCListener!, shouldAcceptNewConnection newConnection: NSXPCConnection!) -> Bool {\n        newConnection.exportedInterface = NSXPCInterface(`protocol`: ImageDownloaderProtocol.self)\n        var exportedObject = ImageDownloader()\n        newConnection.exportedObject = exportedObject\n        newConnection.resume()\n        return true\n    }\n}\n\n// Create the listener and run it by resuming:\nlet delegate = ServiceDelegate()\nlet listener = NSXPCListener.serviceListener()\nlistener.delegate = delegate;\nlistener.resume()\n```\n\nWe create an `NSXPCListener` at the global scope (this corresponds to the `main` function in C / Objective-C). We'll pass it a service delegate that can configure incoming connections. We need to set the same protocol on the connection that we use inside the app. Then we set an instance of `ImageDownloader`, which actually implements that protocol:\n\n```objc\nclass ImageDownloader : NSObject, ImageDownloaderProtocol {\n    let session: NSURLSession\n\n    init()  {\n        let config = NSURLSessionConfiguration.defaultSessionConfiguration()\n        session = NSURLSession(configuration: config)\n    }\n\n    func downloadImageAtURL(url: NSURL!, withReply: ((NSData!)->Void)!) {\n        let task = session.dataTaskWithURL(url) {\n            data, response, error in\n            if let httpResponse = response as? NSHTTPURLResponse {\n                switch (data, httpResponse) {\n                case let (d, r) where (200 <= r.statusCode) && (r.statusCode <= 399):\n                    withReply(d)\n                default:\n                    withReply(nil)\n                }\n            }\n        }\n        task.resume()\n    }\n}\n```\n\nOne important thing to note is that both `NSXPCListener` and `NSXPCConnection` start out being suspended. We need to resume them once they have been configured.\n\nYou can find a simple sample project [on GitHub](https://github.com/objcio/issue-14-xpc).\n\n## Listener, Connection, and Exported Object\n\nOn the app's side, there's a connection object. Each time we want to send data to the service, we use the `remoteObjectProxyWithErrorHandler` method, which creates a remote object proxy.\n\nOn the service's side, there's another layer. First there's a single listener, which listens for incoming connections from the app. The app can create multiple connections, and for each one, the listener will create a corresponding connection object on its side. The connection has a single exported object, which is what the app will send messages to as the remote object proxy.\n\nWhen an app creates a connection to an XPC service, XPC manages the lifecycle of that service. The service is launched and shut down transparently by the XPC runtime. And if the service crashes for some reason, it is transparently relaunched, too.\n\nWhen the app first creates the XPC connection, the XPC service is in fact not launched until the app actually sends the first message to the remote proxy object.\n\nAnd when there are no outstanding replies, the system may chose to stop the service because of memory pressure or because the XPC service has been idle for a while. The app's connection will stay valid, and when the app uses the connection again, the XPC system will relaunch the XPC service.\n\nIf the XPC service crashes, it will also be relaunched transparently. The connection to it will remain valid. But if the XPC service crashes while a message was being sent to it, and before the app received the response, the app needs to resend that message. This is what the error handler in the `remoteObjectProxyWithErrorHandler` method is for.\n\nThis method takes a closure that will be executed if an error occurred. The API guarantees that exactly one of either the error handler or the message reply closure will get executed; if the message reply closure gets executed, the error handler closure will not, and vice-versa. This makes resource cleanup easy.\n\n\n<a name=\"sudden-termination\"> </a>\n### Sudden Termination\n\nXPC manages the lifecycle of the service by keeping track of requests that are still being processed. The service will not get shut down while a request is running. A request is considered *running* if its reply has not been sent, yet. For requests that don't have a reply handler, the request is considered running as long as its method body runs.\n\nIn some situations, we might want to tell XPC that we have to do more work. To do this, we can use the `NSProcessInfo` API:\n\n```objc\nfunc disableAutomaticTermination(reason: String!)\nfunc enableAutomaticTermination(reason: String!)\n```\n\nThis is relevant if we need to perform some asynchronous background operation inside the XPC service as a result of an incoming request. We may also need to adjust our [quality of service](#quality-of-service) in that situation.\n\n\n### Interruption and Invalidation\n\nThe most common situation is for an app to send messages to its XPC service(s). But XPC allows very flexible setups. As we'll go through further below, connections are bidirectional, and connections can be made to anonymous listeners. Such connections will most likely not be valid if the other end goes away (due to a crash or even a normal process termination). In these situations, the connection becomes invalid.\n\nWe can set a connection invalidation handler that will get run when the XPC runtime cannot recreate the connection.\n\nWe can also set a connection interruption handler that will get called when the connection is interrupted, even if the connection will remain valid. The two properties on `NSXPCConnection` are:\n\n```objc\nvar interruptionHandler: (() -> Void)!\nvar invalidationHandler: (() -> Void)!\n```\n\n## Bidirectional Connections\n\nOne interesting fact that's often forgotten is that connections are bidirectional.\n\nThe app needs to make the initial connection to the service. The service cannot do this (see below on [service lookup](#service-lookup)). But once the connection has been made, both ends can initiate requests.\n\nJust as the the service sets an `exportedObject` on the connection, the app can do so at its end. This allows for the service to use the `remoteObjectProxy` on its end to talk to the app's exported object.\n\nThe thing to be aware of, though, is that the system managed the lifecycle of the service — it may be terminated when there are no outstanding requests (c.f. [Sudden Termination](#sudden-termination)).\n\n\n\n<a name=\"service-lookup\"> </a>\n## Service Lookup\n\nWhen we connect to an XPC service, the we need to *find* that other end that we're about to connect to. For an app using a private XPC service, the XPC system looks up the service by its name within the app’s scope. There are other ways to connect over XPC, though. Let us take a look at all the possibilities.\n\n### XPC Service\n\nWhen an app uses\n\n```objc\nNSXPCConnection(serviceName: \"io.objc.myapp.myservice\")\n```\n\nXPC will look up the `io.objc.myapp.myservice` service inside the app's own name space. This service is local to the app — other apps can't connect to this service. The XPC service bundle will either be located inside the app’s bundle or inside the bundle of a framework that the app is using.\n\n### Mach Service\n\nAnother option is to use:\n\n```objc\nNSXPCConnection(machServiceName: \"io.objc.mymachservice\", options: NSXPCConnectionOptions(0))\n```\n\nThis will look for the service `io.objc.mymachservice` in the user's login session. We can install so-called launch agents in either `/Library/LaunchAgents` or `~/Library/LaunchAgents` and these can provide XPC services in much the same way as an app's XPC service. But since these are per-login sessions, multiple apps running in a login session can talk to the same launch agent.\n\nThis approach can be very useful, e.g. for a menu in the [status bar] [statusbar] to talk to a UI app. Both the normal app and the menu extra can talk to a common launch agent and exchange information in that way. Particularly, when more than two processes need to communicate with each other, XPC can be a very elegant solution.\n\nIf we were to write a weather app, we could put the fetching and parsing of weather data into an XPC launch agent. We would then be able to create a menu extra, an app, and a Notification Center Widget that all display the weather. And they'd all be able to communicate with the same launch agent over an `NSXPCConnection`.\n\nIn the same way as with an XPC service, the launch agent lifecycle can be entirely managed by XPC: it will be launched on demand and terminated when it is no longer needed and the system is low on memory.\n\n### Anonymous Listeners and Endpoints\n\nXPC has the ability to pass so-called *listener endpoints* over a connection. This is, at first, very confusing, but allows for a great deal of flexibility.\n\nLet's say we have two apps, and we would like them to be able to communicate with each other over XPC. Each app doesn't know what the other app is. But they both know about a (shared) launch agent.\n\nBoth apps can connect to the launch agent. App A creates a so-called *anonymous listener* that it listens on, and sends a so-called *endpoint* to that anonymous listener over XPC to the launch agent. App B then retrieves that endpoint over XPC from the launch agent. At this point, App B can connect directly to the anonymous listener, i.e. App A.\n\nApp A would create the listener with\n\n```objc\nlet listener = NSXPCListener.anonymousListener()\n```\n\nin a similar way to how an XPC service would normally create its listener. It then creates an endpoint from this listener with:\n\n```objc\nlet endpoint = listener.endpoint\n```\n\nThis endpoint can then be sent over XPC (it implements `NSSecureCoding`). Once App B retrieves that endpoint, it can create a connection to the original listener inside App A with:\n\n```objc\nlet connection = NSXPCConnection(listenerEndpoint: endpoint)\n```\n\n### Privileged Mach Service\n\nThe final option is to use:\n\n```objc\nNSXPCConnection(machServiceName: \"io.objc.mymachservice\", options: .Privileged)\n```\n\nThis is very similar to launch agents, but will create a connection to a daemon instead. Launch agent processes are per user. They run as the user and inside the user's login session. Daemons are per machine. An XPC daemon will only have one instance running, even when multiple users are logged in.\n\nThere are quite a few security considerations about how to run daemon. It's possible for a daemon to run as the root user, which we should probably not do. We most likely want it to run as some unique user. Check the [\"Designing Secure Helpers and Daemons\"] [TN2083] document. For most things, we won’t need root privileges.\n\n\n## File Access\n\nLet's say we want to create a service that downloads a file over HTTP. We need to allow this service to make outgoing network connections in order to connect to the server. What's less obvious, though, is that we can have the service write to a file without the service having any file access.\n\nThe way this works, is that we first create the file that we want to download to inside the app. We then create a file handle for that file:\n\n```objc\nlet fileURL = NSURL.fileURLWithPath(\"/some/path/we/want/to/download/to\")\nif NSData().writeToURL(fileURL, options:0, error:&error) {\n    let maybeHandle = NSFileHandle.fileHandleForWritingToURL(url:fileURL, error:&error)\n    if let handle = maybeHandle {\n        self.startDownload(fromURL:someURL, toFileHandle: handle) {\n            self.downloadComplete(url: someURL)\n        }\n    }\n}\n```\n\nand\n\n```objc\nfunc startDownload(fromURL: NSURL, toFileHandle: NSFileHandlehandle, completionHandler: (NSURL)->Void) -> Void\n```\n\nwould then send the file handle over the XPC connection by passing it to the remote object proxy.\n\nSimilarly, we can open an `NSFileHandler` for reading in one process and pass it to another process, which can then read from that file without having file access itself.\n\n## Moving Data\n\nWhile XPC is very efficient, it's obviously not free to move messages between processes. When moving large amounts of binary data over XPC, there are some tricks available.\n\nNormal `NSData` needs to get copied to get sent to the other side. For large binary data, it is more efficient to use so-called *memory-mapped* data. [WWDC 2013 session 702][wwdc2013videos] covers sending of *Big Data*, beginning with slide 57.\n\nXPC has a fast path which makes sure data is never copied on its way from one process to another. The trick is that `dispatch_data_t` is toll-free bridged with `NSData`. If we create a `dispatch_data_t` instance that's backed by a memory-mapped region, we can send this over XPC more efficiently. This would look something like this:\n\n```objc\nlet size: UInt = 8000\nlet buffer = mmap(nil, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0)\nlet ddata = dispatch_data_create(buffer, size, DISPATCH_TARGET_QUEUE_DEFAULT, _dispatch_data_destructor_munmap)\nlet data = ddata as NSData\n```\n\n## Debugging\n\nXcode has your back covered when you need to debug multiple processes communicating with each other over XPC. If you set a breakpoint in an XPC service that's embedded into our application, the debugger will stop there as expected.\n\nBe sure to check out *Activity Tracing*. Its API is defined in `os/activity.h` and provides a way to convey across execution context and process boundaries what triggered a certain action to be taken. [WWDC 2014 talk 714, “Fix Bugs Faster Using Activity Tracing,”][wwdc2014videos] gives a good introduction.\n\n### Common Mistakes\n\nA very common mistake is to forget to resume either the connection or the listener. Both are created in a suspended state.\n\nIf the connection is invalid, that's most likely due to a configuration error. Check that the bundle identifier matches the service name, and that the service name is correctly specified in the code.\n\n### Debugging Daemons\n\nWhen debugging daemons, things are slightly more complicated, but it still works quite well. Our daemon will get launched by `launchd`. The trick is twofold: During development, we will change the [`launchd.plist`][launchdplist5man] of our daemon and set the `WaitForDebugger` key to true. Then in Xcode, we can edit the scheme of the daemon. In the **scheme editor** under **Run**, the **Info** tab allows us to switch **Launch** from “Automatically” to “Wait for executable to be launched.”\n\nNow when we 'run' the daemon in Xcode, the daemon will not get launched, but the debugger will wait for it to be launched. Once `launchd` starts the daemon, the debugger will connect, and we're good to go.\n\n## Security Attributes of the Connection\n\nEach `NSXPCConnection` has these properties\n\n```objc\nvar auditSessionIdentifier: au_asid_t { get }\nvar processIdentifier: pid_t { get }\nvar effectiveUserIdentifier: uid_t { get }\nvar effectiveGroupIdentifier: gid_t { get }\n```\n\nthat describe the connection. On the listener side, i.e. inside the agent or daemon, we can use these to see who's trying to connect and deny or allow the connection based on that. For XPC Services inside an app bundle, this doesn’t matter all that much, since only the app itself can look up the service.\n\nThe [`xpc_connection_create(3)` man page](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/xpc_connection_create.3.html) has a section called “Credentials” which talks about some of the caveats of this API. Use it with care.\n\n\n<a name=\"quality-of-service\"> </a>\n## Quality of Service and Boosts\n\nWith OS X 10.10, Apple introduced a concept called *Quality of Service* (QoS). It is used to help give the UI a higher priority and lower the priority of background activity. QoS is interesting when it comes to XPC services — what kind of work is an XPC service doing?\n\nSince the quality of service propagates across processes, in most cases we won't have to worry about it. When some UI code makes an XPC call, the code inside the service will get a boosted QoS. If background code inside the app makes an XPC call, that background QoS will get propagated across to the service, too. Its execution will be at a lower QoS.\n\n[WWDC 2014 talk 716, “Power, Performance and Diagnostics,”][wwdc2014videos] has a lot of information about Quality of Service. It mentions how to use `DISPATCH_BLOCK_DETACHED` to detach the current QoS from code, i.e. how to prevent propagation.\n\nWhen an XPC service starts unrelated work as a side effect of a request to it, it must make sure to *detach* from the QoS.\n\n## Lower-Level API\n\nAll of the `NSXPCConnection` API is built on top of a C API. This API is documented in the [`xpc(3) man page`][xpc3man] and its subpages.\n\nWe can create an XPC service for our app and use the C API, as long as we use the C API on both ends.\n\nIt is very similar to the Foundation framework API in concept, though one thing that is slightly confusing is that a connection can both be a **listener** that in turn accepts incoming connections, and a connection to another process.\n\n### Event Streams\n\nOne feature that, at this point, is only available to the C API, is the launch-on-demand for IOKit events, BSD notifications, or Core Foundation distributed notifications. These are available to launch daemons and agents.\n\nEvent streams are outlined in the [`xpc_events(3)` man page] [xpcevents3man]. With this API, it is relatively straightforward to write a launch agent (a background process) that will be launched on demand when a specific piece of hardware is connected (attached).\n\n\n\n\n\n[TN2083]: https://developer.apple.com/library/mac/technotes/tn2083/_index.html \"TN2083 Daemons and Agents\"\n\n[xpcserviceplist5man]: https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man5/xpcservice.plist.5.html \"xpcservice.plist(5) man page\"\n\n[launchdplist5man]: https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man5/launchd.plist.5.html \"launchd.plist(5) man page\"\n\n[wwdc2013videos]: https://developer.apple.com/videos/wwdc/2013/ \"WWDC 2013 Session Videos\"\n[wwdc2014videos]: https://developer.apple.com/videos/wwdc/2014/ \"WWDC 2014 Session Videos\"\n\n[xpc3man]: https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man3/xpc.3.html \"xpc(3) man page\"\n\n[xpcevents3man]: https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man3/xpc_events.3.html#//apple_ref/doc/man/3/xpc_events \"xpc_events(3) man page\"\n\n[statusbar]: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/StatusBar/StatusBar.html#//apple_ref/doc/uid/10000073i \"Status Bar Programming Topics\"\n"
  },
  {
    "path": "2014-08-11-bad-testing-practices.md",
    "content": "---\ntitle: Bad Testing Practices\ncategory: \"15\"\ndate: \"2014-08-11 08:00:00\"\nauthor:\n  - name: Luis Solano\n    url: http://twitter.com/luisobo\ntags: article\n---\n\nI've been writing automated tests for a few years now, and I have to\nconfess that it's a technique that still fascinates me when it comes down\nto making a code base more maintainable. In this article, I'd like to share some of my experiences, as well as the lessons I've learned either from others or via trial and error.\n\nAfter all these years, I've heard many good (and bad) reasons to write\nautomated tests. On the positive side, writing automated tests can:\n\n- Make refactoring easier\n- Avoid regressions\n- Provide executable specification and documentation\n- Reduce the time of creating software\n- Reduce the costs of creating software\n\n\nSure, you could say those are true, but I want to give another\nperspective to all those reasons — a unified perspective if you will:\n\n> The only reason automated tests are valuable is to allow us to modify\n  our own code later on.\n\nIn other words:\n\n> The only time a test will give value back is when we want to _change_ our code.\n\nLet's see how the classic arguments in favor of writing tests are connected\nto this premise:\n\n- Makes refactoring easier — You can change implementation details with confidence, leaving the public API untouched.\n- Avoids regressions — When do regressions occur? When you change your code.\n- Provides executable specification and documentation — When do you want to know more about how software actually works? When you want to modify it.\n- Reduces the time of creating software — How? By allowing you to modify your code faster, with the confidence that your tests will tell you when something went wrong.\n- Reduces the cost of creating software — Well, time is money.\n\n\nYes, all the reasons above are true at some point, but the reason that this\npertains to us developers is that automated tests let us change stuff.\n\nNote that I'm not including the design feedback of _writing_ tests, as in TDD. That\ncould be a separate conversation. We will be talking about tests, once they are written.\n\nIt seems then that writing tests and _how_ to write them should be motivated by _change_.\n\nAn easy way to take this fact into account when writing a test is to always ask your tests these two questions:\n\n- \"Are you going to fail (or pass) if I change my production code?\"\n- \"Is that a good reason for you to fail (or pass)?\"\n\nIf you find a bad reason for your test to fail (or pass), fix it.\n\nThat way, when you change your code later on down the road, your tests will pass or fail only\nfor good reasons, thus giving a better return than flakey tests that fail for the wrong reason.\n\nStill, you may be wondering: \"What's the big deal about that?\"\n\nLet's answer this question with another question: Why do our tests break when we\nchange our code?\n\nWe agreed that the main purpose of having tests is so we can change our code with ease. If that's so, then how are all those red tests helping us? Those failing tests are nothing but noise — impediments to getting the job done. So, how do we do testing in a way that will help us?\n\nIt depends on the reason why we are changing the code.\n\n### Changing the Behavior of the Code\n\nThe starting point should always be green, i.e. all tests should pass.\n\nIf you want to change your code to change its behavior (i.e. changing what\nyour code _does_) you would:\n\n1. Find the test or tests that define the current behavior that you want to\n  change.\n1. Modify those tests to expect the new desired behavior.\n1. Run the tests and see if those modified tests fail.\n1. Update your code to make all tests pass again.\n\nAt the end of this process, we are back at square one — all tests pass,\nand we are ready to start over if needed.\n\nBecause you know exactly which tests failed and which modification of the\ncode made them pass, you are confident that you only changed what you\nwanted to change. This is how automated tests help us modify our code — in this case, to change the behavior.\n\nNote that it is OK to see a test failing, as long as it's only a test related to the\nbehavior that we are updating.\n\n\n### Refactoring: Changing the Implementation of the Code — Leaving the Behavior Intact\n\nAgain, the starting point should always be green.\n\nIf all you want is to change the implementation of a piece of code to\nmake it simpler, more performant, easy to extend, etc. (i.e. changing\n_how_ your code does something but not _what_ it does), this is the extensive\nlist of steps to follow:\n\n1. Modify your code without touching your tests at all.\n\nOnce your code is simpler, faster, or more flexible, your tests should remain\njust as they were — green. When refactoring, tests should only fail in the case that you made a\nmistake, such as changing the external behavior of your code. When that happens, you\nshould reverse that mistake and go back to a green state.\n\nBecause your tests stayed green the whole time, you know that you didn't break\nanything. That's how automated tests let us modify our code.\n\nIn this case, it is not OK to see a test failing. It could mean that:\n- We accidentally changed the external behavior of the code. Good, our tests are helping us.\n- We didn't change the external behavior of the code. Bad, we got false negatives. This is where most of the trouble is.\n\n\nWe want our tests to aid in the processes described above. So let's see some specific tips that will help make our tests more cooperative.\n\n## Good Practices 101\n\nBefore jumping into how _not_ to write your tests, I'd like to go over a few good practices really quick. There are five basic rules that every test should obey to be considered a good — or even a _valid_ — test. There is a mnemonic for these five rules: F.I.R.S.T. \n\nTests should be:\n\n- **F**ast — tests should be able to be executed often.\n- **I**solated — tests on their own cannot depend on external factors or on the result of another test.\n- **R**epeatable — tests should have the same result every time we run them.\n- **S**elf-verifying — tests should include assertions; no human intervention needed.\n- **T**imely — tests should be written along with the production code.\n\nTo find out more about this set of rules, you can read [this article](http://pragprog.com/magazines/2012-01/unit-tests-are-first) by Tim Ottinger and Jeff Langr.\n\n\n## Bad Practices\n\nHow do we maximize the outcome of our tests? In one sentence:\n\n> Don't couple your tests to implementation details.\n\n### Don't Test Private Methods\n\n[Enough said](http://shoulditestprivatemethods.com).\n\nPrivate means private. Period. If you feel the need to test a private\nmethod, there is something conceptually wrong with that method. Usually\nit is doing too much to be a private method, which in turn violates the [Single Responsibility Principle](http://www.objectmentor.com/resources/articles/srp.pdf).\n\nToday:\nYour class has a private method. It does plenty of stuff, so you decide to\ntest it.\nYou make that method public, just for testing purposes, even though it\nmakes no sense to use that method on its own, as it's only meant to be used\ninternally by other public methods of the same class.\nYou write tests for that private (now technically public) method.\n\nTomorrow:\nYou decide to modify what this method does, because of some change in the\nrequirements (totally fine).\nYou find out that some coworker is using that method from another class\nfor something totally different because \"it does what I need.\" After all,\nit was public, right?\nThis private method is not part of the public API.\nYou cannot modify this method without breaking your colleague's code.\n\nWhat To Do: \nExtract that private method to a separate class, give that\nclass a properly defined contract, and test it separately.\nWhen testing code that relies on this new class, you can provide a test double\nof that class if needed.\n\nSo, how do I test the private methods of a given class? Via the public API of its class.\nAlways test your code via its public API. The public API of your code\ndefines a contract, which is a well-defined set of expectations about how your\ncode is going to act based on different inputs. The private API (private\nmethods or even entire classes) does not define that contract and it is\nsubject to change without notice, so your tests (or your colleagues)\ncannot rely on them.\n\nBy testing your private methods in this way, you will remain free to change your (truly) private\ncode and the design of your code will improve by having smaller classes\nthat do one thing and are properly tested.\n\n### Don't Stub Private Methods\n\nStubbing private methods has all the same caveats as exposing a private method for testing, but on top\nof that, doing this could be hard to debug. Usually, stubbing libraries rely on hacks to\nget the job done, making it hard to find out why a test is failing.\n\nAlso, when we stub a method, we should be doing it according to its contract.\nBut a private method has no specified contract — that's mostly why it's\nprivate, after all. Since the behavior of a private method is subject to\nchange without notice, your stub may diverge from reality, but your test\n_will still pass_. Horrendous. Let's see an example:\n\nToday:\nThe public method of a class relies on a private method of the same class.\nThe private method foo never returns nil.\nTests for the public method stub out the private method for convenience.\nWhen stubbing out method foo, you never consider making foo return nil,\nbecause it currently never happens.\n\nTomorrow:\nThe private method changes and now it returns nil. It's a private method, so that's fine.\nTests of the public method are never updated accordingly (\"I'm changing a private method, so\nwhy should I update any test at all?\").\nThe public method is now broken for the case in which the private method returns\nnil, but the tests still pass!\n\nHorrendous.\n\nWhat To Do: \nSince foo is doing too much, extract that method to a new\nclass and test it separately. Then, when testing bar, provide a test\ndouble for that new class.\n\n### Don't Stub External Libraries\n\nThird-party code should never be mentioned directly in your tests.\n\nToday:\nYour networking code relies on the famous HTTP library `LSNetworking`.\nTo avoid hitting the actual network (to make your tests fast and reliable),\nyou stub out the method `-[LSNetworking makeGETrequest:]` of that library,\nproperly replacing its behavior (it calls the success callback with a\ncanned response) but without hitting the network.\n\nTomorrow:\nYou need to swap `LSNetworking` with an alternative (it could be that\n`LSNetworking` is no longer maintained or that you need to switch\nto a more advanced library because it has that feature that you need, etc.).\nIt's a refactor, so you should not change your tests.\nYou replace the library.\nYour tests fail because the dependency of the network is no longer being\nstubbed (`-[LSNetworking makeGETrequest:]` is no longer being called by\nthe implementation).\n\nWhat To Do: Rely on umbrella stubbing to replace the entire\nfunctionality of that library during tests.\n\nUmbrella stubbing (a term that I just made up) consists of stubbing out\nall the possible ways that your code may use, now and in the future, to get\nsome task done, via a declarative API, agnostic to any implementation\ndetail.\n\nAs in the example above, your code can rely on \"HTTP library A\" today,\nbut there are other possible ways of making an HTTP request, right? Like\n\"HTTP library B.\"\n\nAs an example, one solution that provides umbrella stubbing for\nnetworking code is my open-source project, [Nocilla](https://github.com/luisobo/Nocilla).\nWith [Nocilla](https://github.com/luisobo/Nocilla), you can stub HTTP\nrequests in a declarative fashion, without mentioning any HTTP library.\n[Nocilla](https://github.com/luisobo/Nocilla) takes care of stubbing any HTTP library out there, so that you don't couple your tests to any implementation detail. This allows you to switch your\nnetworking stack without breaking your tests.\n\nAnother example could be stubbing out dates. There are many ways of\ngetting the current time in most programming languages, but libraries like\n[TUDelorean](https://github.com/tuenti/TUDelorean) take care of stubbing\nevery single time-related API so that you can simulate a different system time for testing purposes without coupling your tests to any of those multiple\ntime APIs. This lets you refactor your implementation to a different\ntime API without breaking your tests.\n\nIn realms other than HTTP or dates, where a variety of APIs may be\navailable, you can use similar solutions to do\numbrella stubbing, or you can create your own open-source solution and\nshare it with the community, so that the rest of us can properly write tests.\n\n\n### If You Stub Out a Dependency, Do It Properly\n\nThis goes hand in hand with the previous point, but this problem is more\ncommon. Our production code usually relies on dependencies to get something done. For instance, a dependency could help\nus query a database. Often, these dependencies offer many ways of achieving\nthe exact same thing or, at least, with the same external behavior; in our\ndatabase example, you could use the `find` method to retrieve a record by ID,\nor use a `where` clause to get the same record. The problem comes when we\nonly stub one of those possible mechanisms. If we only stub the `find`\nmethod — which is the one that our production code uses — but we don't stub\nthe other possibilities, like the `where` clause, when we decide to\nrefactor our implementation from using `find` to using `where`, our test will\nfail, even though the external behavior of our code hasn't changed.\n\nToday:\nThe `UsersController`class relies on the `UsersRepository`class to\nretrieve users from the database.\nYou are testing `UsersController` and you stub out the `find` method of the\n`UsersRepository` class to make your tests run faster and in a deterministic\nfashion, which is an awesome thing to do.\n\nTomorrow:\nYou decide to refactor `UsersController` to use the new query syntax of\n`UsersRepository`, which is more readable.\nBecause it's a refactoring, you should not touch your tests.\nYou update `UsersController` to use the more readable method `where`, in order to find the\nrecords of interest.\nNow your tests are broken because they stub the method `find` but not\n`where`.\n\nUmbrella stubbing can help here in some cases, but for our case\nwith the `UsersController` class... well, there is no alternative library to\nfetch _my_ users from _my_ database.\n\nWhat To Do: \nCreate an alternative implementation of the same\nclass for testing purposes and use it as a test double.\n\nTo continue with our example, we should provide an `InMemoryUsersRepository`.\nThis in-memory alternative should comply to every single aspect of the\ncontract established by the original `UsersRepository` class, except that\nit stores data in memory to make our tests fast. This means that when\nyou refactor `UsersRepository`, you do the same with its in-memory version.\nTo make it very clear: yes, you now have to maintain two different\nimplementations of the same class.\n\nYou can now provide this lightweight version of your dependency as a test\ndouble. The good thing is that it's a full implementation, so when you decide\nto move your implementation from one method to the other (from `find`\nto `where`, in our example) the test double being used will already\nsupport that new method and your tests won't fail when you refactor.\n\nThere is nothing wrong with maintaining another version of your class. In\nmy experience, it ends up requiring very little effort, and it definitely pays off.\n\nYou can also provide the lightweight version of your class as part of the\nproduction code, just like Core Data does with its in-memory version of the stack. Doing this could be of some use to someone.\n\n\n### Don't Test Constructors\n\nConstructors are implementation details by definition and, since we\nagreed that we should decouple our tests from implementation details,\nyou should not test constructors.\n\nFurthermore, constructors should have no behavior and, since we agree\nthat we should only test the behavior of our code, there is nothing to\ntest.\n\nToday:\nYour class, `Car`, has one constructor.\nYou test that, once a `Car` is constructed, its `Engine` is not nil (because you\nknow that the constructor creates a new `Engine` and assigns it to the\nvariable `_engine`).\n\nTomorrow:\nThe class `Engine` turns out to be costly to construct, so you decide to\nlazily initialize it the first time that the getter of `Engine` is called.\n(This is a totally fine thing to do.)\nYour test for the constructor of the `Car` class breaks because, upon\nconstruction, `Car` no longer has an `Engine`, even though the `Car` will\nwork perfectly.\nAnother option is that your test does not fail because testing that `Car` has an `Engine` triggers the lazy load initialization of the `Engine`.\nSo my question is: What are you testing again?\n\nWhat To Do:\nTest how the public API of your class behaves when\nconstructing it in different ways. A silly example: test how the method\n`count` of the class list behaves when the list is constructed with and\nwithout items. Just note that you are testing the behavior of `count`\nand not the behavior of the constructor.\n\nIn the case that your class has more than one constructor, consider it a\nsmell. Your class may be doing too much. Try to split it in smaller\nclasses, but if there is a legitimate reason for your class to have\nmultiple constructors, just follow the same piece of advice. Make sure\nyou test the public API of that class, constructing it in different ways. In this case, test it using every constructor (i.e. when this class is in this\ninitial state, it behaves like this, and when it's in this other initial\nstate, it behaves like that).\n\n## Conclusion\n\nWriting tests is an investment — we need to put in time in the form of writing\nand maintaining them. The only way we can justify such an investment is because\nwe expect to get that time back. Coupling tests to implementation details will\nreduce the amount of value that our tests will provide, making that investment\nless worthwhile, or even worthless in some cases.\n\nWhen writing tests, step back and ask yourself if those tests will maximize the\noutcome of your investment by checking if your tests could fail or pass for the\nwrong reason, either when refactoring, or when changing the behavior of the system.\n"
  },
  {
    "path": "2014-08-11-behavior-driven-development.md",
    "content": "---\ntitle:  \"Behavior-Driven Development\"\ncategory: \"15\"\ndate: \"2014-08-11 11:00:00\"\nauthor:\n  - name: Pawel Dudek\n    url: https://twitter.com/eldudi\ntags: article\n---\n\nStarting your adventure with testing is not an easy task, especially if you don't have someone to help you out. If you've ever tried it, then you probably remember that moment when you thought: \"This is it. I am starting testing now. I've heard so much about TDD and how beneficial it is, so I'm starting to do it right now.\"\n\nThen you sat down in front of your computer. You opened your IDE. You created a new test file for one of your components.\n\nAnd then void. You might have written a few tests that check some basic functionality, but you felt that something was wrong. You felt there was a question lurking somewhere in your head. A question that needed answering before you could really move forward:\n\n**What should I test?**\n\nThe answer to that question is not simple. In fact, it is a rather complicated issue. The good news is that you were not the first one to ask. And you will definitely not be the last one. \n\nBut you still wanted to pursue the idea of having tests. So you wrote tests that just called your methods (unit testing right?):\n\n```objc\n-(void)testDownloadData;\n```\n\nThere is one fundamental issue with tests like this: they don't really tell you what should happen. They don't tell you what is actually being expected. It is not *clear* what the requirements are. \n\nMoreover, when one of these tests fails, you have to dive into the code and *understand* why it failed. In an ideal world, you shouldn't have to do that in order to know what broke. \n\nThis is where behavior-driven development (BDD) comes it. It aims at solving these exact issues by helping developers determine *what* should be tested. Moreover, it provides a DSL that encourages developers to *clarify* their requirements, and it introduces an ubiquitous language that helps you to easily *understand* what the purpose of a test is. \n\n## What Should I Test?\n\nThe answer to this profound question is strikingly simple, however it does require a shift in how you perceive your test suite. As the first word in BDD suggests, you should no longer focus on *tests*, but you should instead focus on *behaviors*. This seemingly meaningless change provides an exact answer to the aforementioned question: you should test behaviors.\n\nBut what is a behavior? Well, to answer this question, we have to get a little bit more technical. \n\nLet's consider an object that is a part of an app you wrote. It has an interface that defines its methods and dependencies. These methods and these dependencies declare *contract* of your object. They define how it should interact with the rest of your application and what capabilities and functionalities it has. They define its *behavior*.\n\nAnd that is what you should be aiming at: testing how your object behaves. \n\n## BDD DSL\n\nBefore we talk about benefits of BDD DSL, let's first go through its basics and see how a simple test suite for class `Car` looks:\n\n```objc\nSpecBegin(Car)\n    describe(@\"Car\", ^{\n    \n        __block Car *car;\n    \n        // Will be run before each enclosed it\n        beforeEach(^{\n            car = [Car new];\n        });\n        \n        // Will be run after each enclosed it\n        afterEach(^{\n            car = nil;\n        });\n    \n        // An actual test\n        it(@\"should be red\", ^{\n            expect(car.color).to.equal([UIColor redColor]);\n        });\n        \n        describe(@\"when it is started\", ^{\n        \n            beforeEach(^{\n                [car start];\n            });\n        \n            it(@\"should have engine running\", ^{\n                expect(car.engine.running).to.beTruthy();\n            });\n        });\n        \n        describe(@\"move to\", ^{\n            \n            context(@\"when the engine is running\", ^{\n            \n                beforeEach(^{\n                    car.engine.running = YES;\n                    [car moveTo:CGPointMake(42,0)];\n                });\n                \n                it(@\"should move to given position\", ^{\n                    expect(car.position).to.equal(CGPointMake(42, 0));\n                });\n            });\n        \n            context(@\"when the engine is not running\", ^{\n            \n                beforeEach(^{\n                    car.engine.running = NO;\n                    [car moveTo:CGPointMake(42,0)];\n                });\n                \n                it(@\"should not move to given position\", ^{\n                    expect(car.position).to.equal(CGPointZero);\n                });\n            });\n        });\n    });\nSpecEnd\n```\n\n`SpecBegin` declares a test class named `CarSpec`. `SpecEnd` closes that class declaration. \n\nThe `describe` block declares a group of examples.\n\nThe `context` block behaves similarly to `describe` (syntax sugar).\n\n`it` is a single example (a single test). \n\n`beforeEach` is a block that gets called before every block that is nested on the same level as or below it. \n\nAs you probably noticed, nearly all components defined in this DSL consist of two parts: a string value that defines what is being tested, and a block that either has the test itself or more components. These strings have two very important functions.\n\nFirst of all, in `describe` blocks, these strings group behaviors that are tied to a certain part of tested functionality (for instance, moving a car). Since you can specify as many nested blocks as you wish, you can write different specifications based on contexts in which the object or its dependencies are. \n\nThat is exactly what happens in the `move to:` `describe` block: we created two `context` blocks to provide different expectations based on different states (engine either running or not) in which `Car` could be. This is an example of how BDD DSL encourages the defining of *clear* requirements of how the given object should behave in the given conditions. \n\nSecond of all, these strings are used to create sentences that inform you which test failed. For instance, let's assume that our test for moving with engine not started failed. We would then receive the `Car move to when engine is not running should not move to given position` error message. These sentences really help us with *understanding* what has failed and what was the expected behavior, without actually reading any code, and thus they minimize cognitive load. Moreover, they provide a standard language that is easily understandable by each member of your team, including those who are less technical. \n\nRemember that you can also write tests with clear requirements and understandable names without BDD-style syntax (XCtest for instance). However, BDD has been built from the ground up with these capabilities in mind and it provides syntax and functionality that will make such an approach easier.\n    \nIf you wish to learn more about BDD syntax, you should check out the [Specta guide for writing specs](https://github.com/specta/specta#writing-specs).\n\n### BDD Frameworks\n\nAs an iOS or Mac developer, you can choose from a variety of BDD frameworks:\n\n* [Cedar](https://github.com/pivotal/cedar)\n* [Kiwi](https://github.com/kiwi-bdd/Kiwi)\n* [Specta](https://github.com/specta/specta)\n \nWhen it comes to syntax, all these frameworks are nearly the same. The main difference between them lies in their configurability and bundled components. \n\n**Cedar** comes bundled with [matchers](https://github.com/pivotal/cedar/wiki/Writing-specs#matchers) and [doubles](https://github.com/pivotal/cedar/wiki/Writing-specs#doubles). Though it's not exactly true, for the sake of this article, let's consider doubles as mocks (you can learn the difference between mocks and doubles [here](/issues/15-testing/mocking-stubbing/)). \n\nApart from these helpers, Cedar has an additional configuration feature: focusing tests. Focusing tests means that Cedar will execute only a specific test or a test group. Focusing can be achieved by adding an `f` before the `it`, `describe`, or `context` block. \n\nThere's an opposite configuration capability: you can `x`' a test to turn it off. XCTest has similar configuration capabilities, however, they are achieved by operating on schemes (or by manually pressing \"Run this test\"). Cedar configuration capabilities are simpler and faster to configure.\n\nCedar uses a bit of hackery when it comes to integration with XCTest, and thus it's prone to breaking, should Apple decide to change some of its internal implementation. However, from a user perspective, Cedar will work just as if it was integrated with XCTest.\n\n**Kiwi** also comes bundled with [matchers](https://github.com/kiwi-bdd/Kiwi/wiki/Expectations), as well as [stubs and mocks](https://github.com/kiwi-bdd/Kiwi/wiki/Mocks-and-Stubs). Unlike Cedar, Kiwi is tightly integrated with XCTest, however, it lacks the configuration capabilities available in Cedar. \n\n**Specta** offers a different approach when it comes to testing tools, as it does not come bundled with any matchers, mocks, or stubs. It does, however, offer a separate library `Expecta` that provides a variety of matchers. \n\nSpecta is tightly integrated with XCTest and offers configuration capabilities similar to Cedar - focusing and x'ing tests. \n\nAs mentioned before, Cedar, Kiwi, and Specta offer similar syntax. I would not say that there is a framework that is better than all the others; they all have their small pros and cons. Choosing a BDD framework to work with comes down to personal preference. \n\nIt is also worth mentioning that there are already two BDD frameworks that are dedicated to Swift:\n\n* [Quick](https://github.com/Quick/Quick)\n* [Sleipnir](https://github.com/railsware/Sleipnir)\n\n## Examples\n\nThere's one last thing I'd like to point out before we move to examples. Remember that one of the key aspects of writing good behavioral tests is identifying dependencies (you can read more on this subject [here](/issues/15-testing/dependency-injection/)) and exposing them in your interface. \n\nMost of your tests will assert either whether a specific interaction happened, or whether a specific value was returned (or passed to another object), based on your tested object state. Extracting dependencies will allow you to easily mock values and states. Moreover, it will greatly simplify asserting whether a specific action happened or a specific value was calculated.\n\nKeep in mind that you shouldn't put *all* of your object dependencies and properties in the interface (which, especially when you start testing, is really tempting). This will decrease the readability and clarity of purpose of your object, whereas your interface should clearly state what it was designed for. \n\n#### Message Formatter\n\nLet's start with a simple example. We'll build a component that is responsible for formatting a text message for a given event object: \n\n```objc\n@interface EventDescriptionFormatter : NSObject\n@property(nonatomic, strong) NSDateFormatter *dateFormatter;\n\n- (NSString *)eventDescriptionFromEvent:(id <Event>)event;\n\n@end\n```\n\nThis is how our interface looks. The event protocol defines three basic properties of an event:\n\n```objc\n@protocol Event <NSObject>\n\n@property(nonatomic, readonly) NSString *name;\n\n@property(nonatomic, readonly) NSDate *startDate;\n@property(nonatomic, readonly) NSDate *endDate;\n\n@end\n```\n\nOur goal is to test whether `EventDescriptionFormatter` returns a formatted description that looks like `My Event starts at Aug 21, 2014, 12:00 AM and ends at Aug 21, 2014, 1:00 AM.` \n\nPlease note that this (and all other examples in this article) use mocking frameworks. If you've never used a mocking framework before, you should consult [this article](/issues/15-testing/mocking-stubbing/).\n\nWe'll start by mocking our only dependency in the component, which is the date formatter. We'll use the created mock to return fixture strings for the start and end dates. Then we'll check whether the string returned from the event formatter is constructed using the values that we have just mocked: \n\n```objc\n__block id mockDateFormatter;\n__block NSString *eventDescription;\n__block id mockEvent;\n\nbeforeEach(^{\n    // Prepare mock date formatter\n    mockDateFormatter = mock([NSDateFormatter class]);\n    descriptionFormatter.dateFormatter = mockDateFormatter;\n\n    NSDate *startDate = [NSDate mt_dateFromYear:2014 month:8 day:21];\n    NSDate *endDate = [startDate mt_dateHoursAfter:1];\n\n    // Pepare mock event\n    mockEvent = mockProtocol(@protocol(Event));\n    [given([mockEvent name]) willReturn:@\"Fixture Name\"];\n    [given([mockEvent startDate]) willReturn:startDate];\n    [given([mockEvent endDate]) willReturn:endDate];\n\n    [given([mockDateFormatter stringFromDate:startDate]) willReturn:@\"Fixture String 1\"];\n    [given([mockDateFormatter stringFromDate:endDate]) willReturn:@\"Fixture String 2\"];\n\n    eventDescription = [descriptionFormatter eventDescriptionFromEvent:mockEvent];\n});\n\nit(@\"should return formatted description\", ^{\n    expect(eventDescription).to.equal(@\"Fixture Name starts at Fixture String 1 and ends at Fixture String 2.\");\n});\n```\n\nNote that we have only tested whether our `EventDescriptionFormatter` uses its `NSDateFormatter` for formatting the dates. We haven't actually tested the format style. Thus, to have a fully tested component, we need to add two more tests that check format style:\n\n```objc\nit(@\"should have appropriate date style on date formatter\", ^{\n    expect(descriptionFormatter.dateFormatter.dateStyle).to.equal(NSDateFormatterMediumStyle);\n});\n\nit(@\"should have appropriate time style on date formatter\", ^{\n    expect(descriptionFormatter.dateFormatter.timeStyle).to.equal(NSDateFormatterMediumStyle);\n});\n```\n\nEven though we have a fully tested component, we wrote quite a few tests. And this is a really small component, isn't it? Let's try approaching this issue from a slightly different angle.\n\nThe example above doesn't exactly test *behavior* of `EventDescriptionFormatter`. It mostly tests its internal implementation by mocking the `NSDateFormatter`. In fact, we don't actually care whether there's a date formatter underneath at all. From an interface perspective, we could've been formatting the date manually by using date components. All we care about at this point is whether we got our string right. And that is the behavior that we want to test.\n\nWe can easily achieve this by not mocking the `NSDateFormatter`. As said before, we don't even care whether its there, so let's actually remove it from the interface: \n\n```objc\n@interface EventDescriptionFormatter : NSObject\n\n- (NSString *)eventDescriptionFromEvent:(id <Event>)event;\n\n@end\n```\n\nThe next step is, of course, refactoring our tests. Now that we no longer need to know the internals of the event formatter, we can focus on the actual behavior:\n\n```objc\ndescribe(@\"event description from event\", ^{\n\n    __block NSString *eventDescription;\n    __block id mockEvent;\n    \n    beforeEach(^{\n        NSDate *startDate = [NSDate mt_dateFromYear:2014 month:8 day:21];\n        NSDate *endDate = [startDate mt_dateHoursAfter:1];\n        \n        mockEvent = mockProtocol(@protocol(Event));\n        [given([mockEvent name]) willReturn:@\"Fixture Name\"];\n        [given([mockEvent startDate]) willReturn:startDate];\n        [given([mockEvent endDate]) willReturn:endDate];\n    \n        eventDescription = [descriptionFormatter eventDescriptionFromEvent:mockEvent];\n    });\n    \n    it(@\"should return formatted description\", ^{\n        expect(eventDescription).to.equal(@\"Fixture Name starts at Aug 21, 2014, 12:00 AM and ends at Aug 21, 2014, 1:00 AM.\");\n    });\n});\n```\n\nNote how simple our test has become. We only have a minimalistic setup block where we prepare a data model and call a tested method. By focusing more on the result of behavior, rather than the way it actually works, we have simplified our test suite while still retaining functional test coverage of our object. This is exactly what BDD is about — trying to think about results of behaviors, and not the actual implementation.\n\n#### Data Downloader\n\nIn this example, we will build a simple data downloader. We will specifically focus on one single behavior of our data downloader: making a request and canceling the download. Let's start with defining our interface:\n\n```objc\n@interface CalendarDataDownloader : NSObject\n\n@property(nonatomic, weak) id <CalendarDataDownloaderDelegate> delegate;\n\n@property(nonatomic, readonly) NetworkLayer *networkLayer;\n\n- (instancetype)initWithNetworkLayer:(NetworkLayer *)networkLayer;\n\n- (void)updateCalendarData;\n\n- (void)cancel;\n\n@end\n```\n\nAnd of course, the interface for our network layer: \n\n```objc\n@interface NetworkLayer : NSObject\n\n// Returns an identifier that can be used for canceling a request.\n- (id)makeRequest:(id <NetworkRequest>)request completion:(void (^)(id <NetworkRequest>, id, NSError *))completion;\n\n- (void)cancelRequestWithIdentifier:(id)identifier;\n\n@end\n```\n\nWe will first check whether the actual download took place. The mock network layer has been created and injected in a `describe` block above: \n\n```objc\ndescribe(@\"update calendar data\", ^{\n    beforeEach(^{\n        [calendarDataDownloader updateCalendarData];\n    });\n\n    it(@\"should make a download data request\", ^{\n        [verify(mockNetworkLayer) makeRequest:instanceOf([CalendarDataRequest class]) completion:anything()];\n    });\n});\n```\n\nThis part was pretty simple. The next step is to check whether that request was canceled when we called the cancel method. We need to make sure we don't call the cancel method with no identifier. Specifications for such behavior can look like this:\n\n```objc\ndescribe(@\"cancel \", ^{\n    context(@\"when there's an identifier\", ^{\n        beforeEach(^{\n            calendarDataDownloader.identifier = @\"Fixture Identifier\";\n            [calendarDataDownloader cancel];\n        });\n\n        it(@\"should tell the network layer to cancel request\", ^{\n            [verify(mockNetworkLayer) cancelRequestWithIdentifier:@\"Fixture Identifier\"];\n        });\n\n        it(@\"should remove the identifier\", ^{\n            expect(calendarDataDownloader.identifier).to.beNil();\n        });\n    });\n\n    context(@\"when there's no identifier\", ^{\n        beforeEach(^{\n            calendarDataDownloader.identifier = nil;\n            [calendarDataDownloader cancel];\n        });\n\n        it(@\"should not ask the network layer to cancel request\", ^{\n            [verifyCount(mockNetworkLayer, never()) cancelRequestWithIdentifier:anything()];\n        });\n    });\n});\n```\n\nThe request identifier is a private property of `CalendarDataDownloader`, so we will need to expose it in our tests:\n\n```objc\n@interface CalendarDataDownloader (Specs)\n@property(nonatomic, strong) id identifier;\n@end\n```\n\nYou can probably gauge that there's something wrong with these tests. Even though they are valid and they check for specific behavior, they expose the internal workings of our `CalendarDataDownloader`. There's no need for our tests to have knowledge of how the `CalendarDataDownloader` holds its request identifier. Let's see how we can write our tests without exposing internal implementation:\n\n```objc\ndescribe(@\"update calendar data\", ^{\n    beforeEach(^{\n        [given([mockNetworkLayer makeRequest:instanceOf([CalendarDataRequest class])\n                                  completion:anything()]) willReturn:@\"Fixture Identifier\"];\n        [calendarDataDownloader updateCalendarData];\n    });\n\n    it(@\"should make a download data request\", ^{\n        [verify(mockNetworkLayer) makeRequest:instanceOf([CalendarDataRequest class]) completion:anything()];\n    });\n\n    describe(@\"canceling request\", ^{\n        beforeEach(^{\n            [calendarDataDownloader cancel];\n        });\n\n        it(@\"should tell the network layer to cancel previous request\", ^{\n            [verify(mockNetworkLayer) cancelRequestWithIdentifier:@\"Fixture Identifier\"];\n        });\n\n        describe(@\"canceling it again\", ^{\n            beforeEach(^{\n                [calendarDataDownloader cancel];\n            });\n\n            it(@\"should tell the network layer to cancel previous request\", ^{\n                [verify(mockNetworkLayer) cancelRequestWithIdentifier:@\"Fixture Identifier\"];\n            });\n        });\n    });\n});\n```\n\nWe started by stubbing the `makeRequest:completion:` method. We returned a fixture identifier. In the same `describe` block, we defined a cancel `describe` block, which calls the `cancel` method on our `CalendarDataDownloader` object. We then check out whether the fixture string was passed to our mocked network layer `cancelRequestWithIdentifier:` method. \n\nNote that, at this point, we don't actually need a test that checks whether the network request was made — we would not get an identifier and the `cancelRequestWithIdentifier:` would never be called. However, we retained that test to make sure we know what happened should that functionality break.\n\nWe've managed to test the exact same behavior without exposing the internal implementation of `CalendarDataDownloader`. Moreover, we've done so with only three tests instead of four. And we've leveraged BDD DSL nesting capabilities to chain simulation of behaviors — we first simulated the download, and then, in the same `describe` block, we simulated the canceling of a request. \n\n### Testing View Controllers\n\nIt seems that the most common attitude to testing view controllers among iOS developers is that people don't see value in it — which I find odd, as controllers often represent the core aspect of an application. They are the place where all components are glued together. They are the place that connects the user interface with the application logic and model. As a result, damage caused by an involuntary change can be substantial. \n\nThis is why I strongly believe that view controllers should be tested as well. However, testing view controllers is not an easy task. The following upload photo and sign-in view controller examples should help with understanding how BDD can be leveraged to simplify building test suites for view controllers.\n\n#### Upload Photo View Controller\n\nIn this example, we will build a simple photo uploader view controller with a send button as `rightBarButtonItem`. After the button is pressed, the view controller will inform its photo uploader component that a photo should be uploaded. \n\nSimple, right? Let's start with the interface of `PhotoUploaderViewController`:\n\n```objc\n@interface PhotoUploadViewController : UIViewController\n@property(nonatomic, readonly) PhotoUploader *photoUploader;\n\n- (instancetype)initWithPhotoUploader:(PhotoUploader *)photoUploader;\n\n@end\n```\n\nThere's not much happening here, as we're only defining an external dependency on `PhotoUploader`. Our implementation is also pretty simple. For the sake of simplicity, we won't actually grab a photo from anywhere; we'll just create an empty `UIImage`: \n\n```objc\n@implementation PhotoUploadViewController\n\n- (instancetype)initWithPhotoUploader:(PhotoUploader *)photoUploader {\n    self = [super init];\n    if (self) {\n        _photoUploader = photoUploader;\n\n        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@\"Upload\", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(didTapUploadButton:)];\n    }\n\n    return self;\n}\n\n#pragma mark -\n\n- (void)didTapUploadButton:(UIBarButtonItem *)uploadButton {\n    void (^completion)(NSError *) = ^(NSError* error){};\n    [self.photoUploader uploadPhoto:[UIImage new] completion:completion];\n}\n\n@end\n```\n\nLet's see how we could test this component. First of all, we'll need to check whether our bar button item is properly set up by asserting that the title, target, and action have been properly initialized: \n\n```objc\ndescribe(@\"right bar button item\", ^{\n\n    __block UIBarButtonItem *barButtonItem;\n\n    beforeEach(^{\n        barButtonItem = [[photoUploadViewController navigationItem] rightBarButtonItem];\n    });\n\n    it(@\"should have a title\", ^{\n        expect(barButtonItem.title).to.equal(@\"Upload\");\n    });\n\n    it(@\"should have a target\", ^{\n        expect(barButtonItem.target).to.equal(photoUploadViewController);\n    });\n\n    it(@\"should have an action\", ^{\n        expect(barButtonItem.action).to.equal(@selector(didTapUploadButton:));\n    });\n});\n```\n\nBut this is only half of what actually needs to be tested — we are now sure that the appropriate method will be called when the button is pressed, but we're not sure whether the appropriate action will be taken (in fact, we don't even know whether that method actually exists). So let's test that as well:\n\n```objc\ndescribe(@\"tapping right bar button item\", ^{\n    beforeEach(^{\n        [photoUploadViewController didTapUploadButton:nil];\n    });\n\n    it(@\"should tell the mock photo uploader to upload the photo\", ^{\n        [verify(mockPhotoUploader) uploadPhoto:instanceOf([UIImage class])\n                                    completion:anything()];\n    });\n});\n```\n\nUnfortunately for us, the `didTapUploadButton:` is not visible in the interface. We can work around this issue by defining a category visible in our tests that exposes this method:\n\n```objc\n@interface PhotoUploadViewController (Specs)\n- (void)didTapUploadButton:(UIBarButtonItem *)uploadButton;\n@end\n```\n\nAt this point, we can say that `PhotoUploadViewController` is fully tested. \n\nBut what is wrong with the example above? The problem is that we are testing the internal implementation of `PhotoUploadViewController`. We shouldn't actually *care* what the target/action values on the bar button item are. We should only care about what happens when it is pressed. Everything else is an implementation detail.\n\nLet's go back to our `PhotoUploadViewController` and see how we could rewrite our tests to make sure we're testing our interface, and not implementation.\n\nFirst of all, we don't need to know that the `didTapUploadButton:` method exists at all. It is just an implementation detail. We care only for the behavior: when the user taps the upload button, the `UploadManager` should receive an `uploadPhoto:` message. This is great, as it means we don't really need our `Specs` category on `PhotoUploadViewController`. \n\nSecond of all, we don't need to know what target/action is defined on our `rightBarButtonItem`. Our *only* concern is what happens when it is tapped. Let's simulate that action in tests. We can use a helper category on `UIBarButtonItem` to do this:\n\n```objc\n@interface UIBarButtonItem (Specs)\n\n- (void)specsSimulateTap;\n\n@end\n```\n\nIts implementation is pretty simple, as we're performing `action` on the `target` of the `UIBarButtonItem`:\n\n```objc\n@implementation UIBarButtonItem (Specs)\n\n- (void)specsSimulateTap {\n    [self.target performSelector:self.action withObject:self];\n}\n\n@end\n```\n\nNow that we have a helper method that simulates a tap, we can simplify our tests to one top-level `describe` block:\n\n```objc\ndescribe(@\"right bar button item\", ^{\n\n    __block UIBarButtonItem *barButtonItem;\n\n    beforeEach(^{\n        barButtonItem = [[photoUploadViewController navigationItem] rightBarButtonItem];\n    });\n\n    it(@\"should have a title\", ^{\n        expect(barButtonItem.title).to.equal(@\"Upload\");\n    });\n\n    describe(@\"when it is tapped\", ^{\n        beforeEach(^{\n            [barButtonItem specsSimulateTap];\n        });\n\n        it(@\"should tell the mock photo uploader to upload the photo\", ^{\n            [verify(mockPhotoUploader) uploadPhoto:instanceOf([UIImage class])\n                                        completion:anything()];\n        });\n    });\n});\n```\n\nNote that we have managed to remove two tests and we still have a fully tested component. Moreover, our test suite is less prone to breaking, as we no longer rely on the existence of the `didTapUploadButton:` method. Last but not least, we have focused more on the behavioral aspect of our controller, rather than its internal implementation.\n\n#### Sign-In View Controller\n\nIn this example, we will build a simple app that requires users to enter their username and password in order to sign in to an abstract service.  \n\nWe will start out by building a `SignInViewController` with two text fields and a sign-in button. We want to keep our controller as small as possible, so we will abstract a class responsible for signing in to a separate component called `SignInManager`. \n    \nOur requirements are as follows: when the user presses our sign-in button, and when the username and password are present, our view controller will tell its sign-in manager to perform the sign in with the password and username. If there is no username or password (or both are gone), the app will show an error label above text fields. \n\nThe first thing that we will want to test is the view part:\n\n```objc\n@interface SignInViewController : UIViewController\n\n@property(nonatomic, readwrite) IBOutlet UIButton *signInButton;\n\n@property(nonatomic, readwrite) IBOutlet UITextField *usernameTextField;\n@property(nonatomic, readwrite) IBOutlet UITextField *passwordTextField;\n\n@property(nonatomic, readwrite) IBOutlet UILabel *fillInBothFieldsLabel;\n\n@property(nonatomic, readonly) SignInManager *signInManager;\n\n- (instancetype)initWithSignInManager:(SignInManager *)signInManager;\n\n- (IBAction)didTapSignInButton:(UIButton *)signInButton;\n\n@end\n```\n\nFirst, we will check some basic information about our text fields:\n\n```objc\n    beforeEach(^{\n        // Force view load from xib\n        [signInViewController view];\n    });\n\n    it(@\"should have a placeholder on user name text field\", ^{\n        expect(signInViewController.usernameTextField.placeholder).to.equal(@\"Username\");\n    });\n\n    it(@\"should have a placeholder on password text field\", ^{\n         expect(signInViewController.passwordTextField.placeholder).to.equal(@\"Password\");\n    });\n```\n\nNext, we will check whether the sign-in button is correctly configured and has it actions wired:\n\n```objc\n    describe(@\"sign in button\", ^{\n\n        __block UIButton *button;\n\n        beforeEach(^{\n            button = signInViewController.signInButton;\n        });\n\n        it(@\"should have a title\", ^{\n            expect(button.currentTitle).to.equal(@\"Sign In\");\n        });\n\n        it(@\"should have sign in view controller as only target\", ^{\n            expect(button.allTargets).to.equal([NSSet setWithObject:signInViewController]);\n        });\n\n        it(@\"should have the sign in action as action for login view controller target\", ^{\n            NSString *selectorString = NSStringFromSelector(@selector(didTapSignInButton:));\n            expect([button actionsForTarget:signInViewController forControlEvent:UIControlEventTouchUpInside]).to.equal(@[selectorString]);\n        });\n    });\n```\n\nAnd last but not least, we will check how our controller behaves when the button is tapped:\n\n```objc\ndescribe(@\"tapping the logging button\", ^{\n     context(@\"when login and password are present\", ^{\n\n         beforeEach(^{\n             signInViewController.usernameTextField.text = @\"Fixture Username\";\n             signInViewController.passwordTextField.text = @\"Fixture Password\";\n\n             // Make sure state is different than the one expected\n             signInViewController.fillInBothFieldsLabel.alpha = 1.0f;\n\n             [signInViewController didTapSignInButton:nil];\n         });\n\n         it(@\"should tell the sign in manager to sign in with given username and password\", ^{\n             [verify(mockSignInManager) signInWithUsername:@\"Fixture Username\" password:@\"Fixture Password\"];\n         });\n     });\n\n     context(@\"when login or password are not present\", ^{\n         beforeEach(^{\n             signInViewController.usernameTextField.text = @\"Fixture Username\";\n             signInViewController.passwordTextField.text = nil;\n\n             [signInViewController didTapSignInButton:nil];\n         });\n\n         it(@\"should not tell the sign in manager to sign in\", ^{\n             [verifyCount(mockSignInManager, never()) signInWithUsername:anything() password:anything()];\n         });\n     });\n\n     context(@\"when neither login or password are present\", ^{\n         beforeEach(^{\n             signInViewController.usernameTextField.text = nil;\n             signInViewController.passwordTextField.text = nil;\n\n             [signInViewController didTapSignInButton:nil];\n         });\n\n         it(@\"should not tell the sign in manager to sign in\", ^{\n             [verifyCount(mockSignInManager, never()) signInWithUsername:anything() password:anything()];\n         });\n     });\n });\n```\n\nThe code presented in the example above has quite a few issues. First of all, we've exposed a lot of internal implementation of `SignInViewController`, including buttons, text fields, and methods. The truth is that we didn't really need to do all of this. \n\nLet's see how we can refactor these tests to make sure we are not touching internal implementation. We will start by removing the need to actually know what target and method are hooked to the sign-in button:\n\n```objc\n@interface UIButton (Specs)\n\n- (void)specsSimulateTap;\n\n@end\n\n@implementation UIButton (Specs)\n\n- (void)specsSimulateTap {\n    [self sendActionsForControlEvents:UIControlEventTouchUpInside];\n}\n\n@end\n```\n\nNow we can just call this method on our button and assert whether the sign-in manager received the appropriate message. But we can still improve how this test is written. \n\nLet's assume that we do not want to know who has the sign-in button. Perhaps it is a direct subview of the view controller's view. Or perhaps we encapsulated it within a separate view that has its own delegate. We shouldn't actually care where it is; we should only care about whether it is somewhere within our view controller's view and what happens when it is tapped. We can use a helper method to grab the sign-in button, no matter where it is:\n\n```objc\n@interface UIView (Specs)\n\n- (UIButton *)specsFindButtonWithTitle:(NSString *)title;\n\n@end\n```\n\nOur method will traverse subviews of the view and return the first button that has a title that matches the title argument. We can write similar methods for text fields or labels:\n\n```objc\n@interface UIView (Specs)\n\n- (UITextField *)specsFindTextFieldWithPlaceholder:(NSString *)placeholder;\n- (UILabel *)specsFindLabelWithText:(NSString *)text;\n\n@end\n```\n\nLet's see how our tests look now:\n\n```objc\ndescribe(@\"view\", ^{\n\n    __block UIView *view;\n    \n    beforeEach(^{\n        view = [signInViewController view];\n    });\n\n    describe(@\"login button\", ^{\n\n        __block UITextField *usernameTextField;\n        __block UITextField *passwordTextField;\n        __block UIButton *signInButton;\n\n        beforeEach(^{\n            signInButton = [view specsFindButtonWithTitle:@\"Sign In\"];\n            usernameTextField = [view specsFindTextFieldWithPlaceholder:@\"Username\"];\n            passwordTextField = [view specsFindTextFieldWithPlaceholder:@\"Password\"];\n        });\n\n        context(@\"when login and password are present\", ^{\n            beforeEach(^{\n                usernameTextField.text = @\"Fixture Username\";\n                passwordTextField.text = @\"Fixture Password\";\n\n                [signInButton specsSimulateTap];\n            });\n\n            it(@\"should tell the sign in manager to sign in with given username and password\", ^{\n                [verify(mockSignInManager) signInWithUsername:@\"Fixture Username\" password:@\"Fixture Password\"];\n            });\n        });\n\n        context(@\"when login or password are not present\", ^{\n            beforeEach(^{\n                usernameTextField.text = @\"Fixture Username\";\n                passwordTextField.text = nil;\n\n                [signInButton specsSimulateTap];\n            });\n\n            it(@\"should not tell the sign in manager to sign in\", ^{\n                [verifyCount(mockSignInManager, never()) signInWithUsername:anything() password:anything()];\n            });\n        });\n\n        context(@\"when neither login or password are present\", ^{\n            beforeEach(^{\n                usernameTextField.text = nil;\n                passwordTextField.text = nil;\n\n                [signInButton specsSimulateTap];\n            });\n\n            it(@\"should not tell the sign in manager to sign in\", ^{\n                [verifyCount(mockSignInManager, never()) signInWithUsername:anything() password:anything()];\n            });\n        });\n    });\n});\n```\n\nLooks much simpler, doesn't it? Note that by looking for a button with \"Sign In\" as the title, we also tested whether such a button exists at all. Moreover, by simulating a tap, we tested whether the action is correctly hooked up. And in the end, by asserting that our `SignInManager` should be called, we tested whether or not that part is correctly implemented — all of this using three simple tests.\n\nWhat is also great is that we no longer need to expose any of those properties. As a matter of fact, our interface could be as simple as this:\n\n```objc\n@interface SignInViewController : UIViewController\n\n@property(nonatomic, readonly) SignInManager *signInManager;\n\n- (instancetype)initWithSignInManager:(SignInManager *)signInManager;\n\n@end\n```\n\nIn these tests, we have leveraged the capabilities of BDD DSL. Note how we used `context` blocks to define different requirements for how `SignInViewController` should behave, based on its text fields state. This is a great example of how you can use BDD to make your tests simpler and more readable while retaining their functionality.\n\n## Conclusion\n\nBehavior-driven development is not as hard as it might initially look. All you need to do is change your mindset a bit — think more of how an object should behave (and how its interface should look) and less of how it should be implemented. By doing so, you will end up with a more robust codebase, along with a great test suite. Moreover, your tests will become less prone to breaking during refactors, and they will focus on testing the contract of your object rather than its internal implementation.\n\nAnd with the great tools provided by the iOS community, you should be able to start BDDing your apps in no time. Now that you know *what* to test, there's really no excuse, is there?\n\n### Links\n\nIf you're interested in the roots of BDD and how it came to be, you should definitely read [this article](http://dannorth.net/introducing-bdd/).\nFor those of you who understand TDD, but don't exactly know how this differs from BDD, I recommend [this article](http://blog.mattwynne.net/2012/11/20/tdd-vs-bdd/).\nLast but not least, you can find an example project with the tests presented above [here](https://github.com/objcio/issue-15-bdd).\n\n"
  },
  {
    "path": "2014-08-11-dependency-injection.md",
    "content": "---\ntitle: Dependency Injection\ncategory: \"15\"\ndate: \"2014-08-11 09:00:00\"\nauthor:\n  - name: Jon Reid\n    url: http://qualitycoding.org/about/\ntags: article\n---\n\nLet's say you want to write a method that looks something like this:\n\n```objc\n- (NSNumber *)nextReminderId\n{\n    NSNumber *currentReminderId = [[NSUserDefaults standardUserDefaults] objectForKey:@\"currentReminderId\"];\n    if (currentReminderId) {\n        // Increment the last reminderId\n        currentReminderId = @([currentReminderId intValue] + 1);\n    } else {\n        // Set to 0 if it doesn't already exist\n        currentReminderId = @0;\n    }\n    // Update currentReminderId to model\n    [[NSUserDefaults standardUserDefaults] setObject:currentReminderId forKey:@\"currentReminderId\"];\n\n    return currentReminderId;\n}\n```\n\nHow do you write unit tests for this? The problem is that the method is interacting with another object that we don't control, namely `NSUserDefaults`.\n\nBear with me. As we work through this example, the issue isn't \"How do we test a method interacting with `NSUserDefaults`?\" Rather, I'm using `NSUserDefaults` as an example of a bigger question: \"How do we test a method that depends on another object when that object will keep the tests from being [fast and repeatable][firstTests]?\"\n\n[firstTests]: http://pragprog.com/magazines/2012-01/unit-tests-are-first\n\nOne of the biggest barriers to approaching unit testing for the first time is not knowing how to manage dependencies on things outside of the code you want to test. But there's an entire school full of approaches to this problem, which fall under the name dependency injection, or DI.\n\n## Forms of Dependency Injection\n\nNow as soon as I said DI, many of you thought of dependency injection frameworks or Inversion of Control (IoC) containers. Please set those thoughts aside; we'll return to them in the FAQ.\n\nThere are various techniques for taking a dependency and injecting something else in its place. In the Objective-C runtime, swizzling — that is, dynamically replacing one method with another — is certainly one such technique. Some even argue that [swizzling makes DI unnecessary][injectionIsNotAVirtue], and that the techniques below should be avoided. But I'd rather have code that makes dependencies explicit, so that I can see them (and be forced to deal with the code smelling when there are too many dependencies, or the wrong ones).\n\n[injectionIsNotAVirtue]: http://sharpfivesoftware.com/2013/03/20/dependency-injection-is-not-a-virtue-in-objective-c/\n\nSo with that, let's quickly run through a number of forms of DI. With one exception, these all come from [*Dependency Injection in .NET*][Seemann] by Mark Seemann.\n\n[Seemann]: http://www.amazon.com/Dependency-Injection-NET-Mark-Seemann/dp/1935182501\n\n### Constructor Injection\n\n*Note: Even though Objective-C doesn't have constructors per se, I use the term constructor injection instead of initializer injection. It's a standard DI term, and it's easier to look up across languages.*\n\nIn constructor injection, a dependency is passed into the constructor (in Objective-C, the designated initializer) and captured for later use:\n\n```objc\n@interface Example ()\n@property (nonatomic, strong, readonly) NSUserDefaults *userDefaults;\n@end\n\n@implementation Example\n- (instancetype)initWithUserDefaults:(NSUserDefaults *userDefaults)\n{\n    self = [super init];\n    if (self) {\n        _userDefaults = userDefaults;\n    }\n    return self;\n}\n@end\n```\n\nThe dependency can be captured in an instance variable or in a property. The example above uses a read-only property to make it a little harder to tamper with.\n\nIt may look odd to inject `NSUserDefaults`, and that's where this example may fall short. Remember, `NSUserDefaults` is standing in for a dependency that creates trouble. It would make more sense for the injected value to be an abstraction (that is, an `id` satisfying some protocol) instead of a concrete object. But I'm not going to discuss that in this article; let's keep going with `NSUserDefaults` for our examples.\n\nNow every place in this class that would refer to the singleton `[NSUserDefaults standardUserDefaults]` should instead refer to `self.userDefaults`:\n\n```objc\n- (NSNumber *)nextReminderId\n{\n    NSNumber *currentReminderId = [self.userDefaults objectForKey:@\"currentReminderId\"];\n    if (currentReminderId) {\n        currentReminderId = @([currentReminderId intValue] + 1);\n    } else {\n        currentReminderId = @0;\n    }\n    [self.userDefaults setObject:currentReminderId forKey:@\"currentReminderId\"];\n    return currentReminderId;\n}\n```\n\n### Property Injection\n\nIn property injection, the code for `nextReminderId` looks the same, referring to `self.userDefaults`. But instead of passing the dependency to the initializer, we make it a settable property:\n\n```objc\n@interface Example\n@property (nonatomic, strong) NSUserDefaults *userDefaults;\n- (NSNumber *)nextReminderId;\n@end\n```\n\nNow a test can construct the object, then set the `userDefaults` property with whatever it needs. But what should happen if the property isn't set? In that case, let's use lazy initialization to establish a reasonable default in the getter:\n\n```objc\n- (NSUserDefaults *)userDefaults\n{\n    if (!_userDefaults) {\n        _userDefaults = [NSUserDefaults standardUserDefaults];\n    }\n    return _userDefaults;\n}\n```\n\nNow, if any calling code sets the `userDefaults` property before it's used, `self.userDefaults` will use the given value. But if the property isn't set, then `self.userDefaults` will use `[NSUserDefaults standardUserDefaults]`.\n\n### Method Injection\n\nIf the dependency is only referenced in a single method, then we can just inject it directly as a method parameter:\n\n```objc\n- (NSNumber *)nextReminderIdWithUserDefaults:(NSUserDefaults *)userDefaults\n{\n    NSNumber *currentReminderId = [userDefaults objectForKey:@\"currentReminderId\"];\n    if (currentReminderId) {\n        currentReminderId = @([currentReminderId intValue] + 1);\n    } else {\n        currentReminderId = @0;\n    }\n    [userDefaults setObject:currentReminderId forKey:@\"currentReminderId\"];\n    return currentReminderId;\n}\n```\n\nAgain, this may look odd — and again, remember that `NSUserDefaults` may not quite fit every example. But an `NSDate` parameter would fit well with method injection. (More on this below when we discuss the benefits of each form.)\n\n### Ambient Context\n\nWhen the dependency is accessed through a class method (such as a singleton), then there are two ways to control that dependency from a test:\n\n* If you control the singleton, you may be able to **expose its properties** to control its state.\n* If fiddling with properties is insufficient, or the singleton isn't yours to control, then **it's time to swizzle**: replace the class method so that it returns the fake you need.\n\nI won't go into the details of a swizzling example; there are plenty of other resources on that. But see? Swizzling *can* be used for DI. Do read on, though. After this brief overview of different forms of DI, we'll discuss their pros and cons.\n\n### Extract and Override Call\n\nThis final technique falls outside the forms of DI from Seemann's book. Instead, the extract and override call comes from [Working Effectively with Legacy Code][Feathers] by Michael Feathers. Here's how to apply the technique to our `NSUserDefaults` problem in three steps:\n\n[Feathers]: http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052\n\nStep 1 — Select one of the calls `[NSUserDefaults standardUserDefaults]`. Use automated refactoring (in either Xcode or AppCode) to extract it to a new method.\n\nStep 2 — Change other places where the call is made, replacing them with calls to the new method. (Be careful not to change the new method itself.)\n  \nThe modified code looks like this:\n\n```objc\n- (NSNumber *)nextReminderId\n{\n    NSNumber *currentReminderId = [[self userDefaults] objectForKey:@\"currentReminderId\"];\n    if (currentReminderId) {\n        currentReminderId = @([currentReminderId intValue] + 1);\n    } else {\n        currentReminderId = @0;\n    }\n    [[self userDefaults] setObject:currentReminderId forKey:@\"currentReminderId\"];\n    return currentReminderId;\n}\n\n- (NSUserDefaults *)userDefaults\n{\n    return [NSUserDefaults standardUserDefaults];\n}\n```\n\nWith that in place, here's the final step:\n\nStep 3 — Create a special **testing subclass**, overriding the extracted method, like this:\n\n```objc\n@interface TestingExample : Example\n@end\n\n@implementation TestingExample\n\n- (NSUserDefaults *)userDefaults\n{\n    // Do whatever you want!\n}\n\n@end\n```\n\nTest code can now instantiate `TestingExample` instead of `Example`, and have complete control over what happens when the production code calls `[self userDefaults]`.\n\n## \"So Which Form Should I Use?\"\n\nWe have five different forms of DI. Each comes with its own set of pros and cons, so each has its place.\n\n### Constructor Injection\n\nConstructor injection should be your weapon of choice. When in doubt, start here. The advantage is that **it makes dependencies explicit**.\n\nThe disadvantage is that it can feel cumbersome at first. This is especially true when an initializer has a long list of dependencies. But this reveals a previously hidden code smell: does the class have *too many dependencies*? Perhaps it doesn't conform to the [Single Responsibility Principle][SRP].\n\n[SRP]: https://cleancoders.com/episode/clean-code-episode-9/show\n\n### Property Injection\n\nThe advantage of property injection is that it separates initialization from injection, which is necessary when you can't change the callers. The disadvantage is that, well, it separates initialization from injection! It makes it possible to have incomplete initialization. That's why it's best used when there's a specific default value for your dependency, or when you know the dependency will be filled in by a DI framework.\n\nProperty injection looks simple, but **making it robust is surprisingly tricky**:\n\n* You may want to guard against the property being reset arbitrarily. So instead of the default setter, you may want a custom setter that makes sure the backing instance variable is nil and the given argument is non-nil.\n* Does the getter need to be thread-safe? If so, you'll have an easier time using constructor injection instead of trying to make the getter both safe and fast.\n\nAlso, beware of automatically leaning toward property injection just because you have a specific instance in mind. **Make sure the default value doesn't refer to another library**. Otherwise, you will require users of your class to also include that other library, breaking the benefits of loose coupling. (In Seemann's terminology, this is the difference between a local default and a foreign default.)\n\n### Method Injection\n\nMethod injection is good when the dependency will vary with each call. This could be some app-specific context about the calling point. It could be a random number. It could be the current time.\n\nConsider a method that uses the current time. Instead of directly calling `[NSDate date]`, try adding an `NSDate` parameter to your method. With a small increase in calling complexity, it opens up options for the method to be used more flexibly.\n\n(While Objective-C makes it easy to substitute test doubles without requiring protocols, I recommend reading [\"Beyond Mock Objects\"][Rainsberger] by J.B. Rainsberger. It's an interesting example of how wrestling with an injected date opens up larger questions of design and reuse.)\n\n[Rainsberger]: http://blog.thecodewhisperer.com/2013/11/23/beyond-mock-objects/\n\n### Ambient Context\n\nIf you have a dependency that is used at various low-level points, you may have a cross-cutting concern. Passing this dependency around through higher levels can interfere with your code, especially when you can't predict in advance where it will be needed. Examples of this may include:\n\n* Logging\n* `[NSUserDefaults standardUserDefaults]`\n* `[NSDate date]`\n\nAmbient context may be just what you need. But because it affects global context, don't forget to reset it when you're done. For example, if you swizzle a method, use `tearDown` or `afterEach` (depending on your testing framework) to restore the original method.\n\nInstead of doing your own swizzling, see if someone has already written a library focusing on the ambient context you need. For example:\n\n* Networking — [Nocilla][Nocilla] or [OHHTTPStubs][OHHTTPStubs]\n* NSDate — [TUDelorean][TUDelorean]\n\n[Nocilla]: https://github.com/luisobo/Nocilla\n[OHHTTPStubs]: https://github.com/AliSoftware/OHHTTPStubs\n[TUDelorean]: https://github.com/tuenti/TUDelorean\n\n### Extract and Override Call\n\nBecause extract and override call is so simple and powerful, you may be tempted to use it everywhere. But because it requires test-specific subclasses, it's easy for tests to become fragile.\n\nThat said, it's effective with legacy code, especially when you don't want to change all the calling points.\n\n## FAQ\n\n### \"Which DI Framework Should I Use?\"\n\nMy advice for folks starting off with mock objects is to avoid using any mock object framework, at first, as you'll have a better sense of what's going on. My advice for folks starting off with DI is the same. But you can get even further in DI without a framework, relying solely on 'Poor Man's DI,' where you do it yourself.\n\nActually, chances are good that you've already used a DI framework! **It's called Interface Builder**. IB isn't just about laying out interfaces; arbitrary properties can be filled with the real objects by declaring those properties as IBOutlets. This works well for creating an object graph at the point when you create a view. In his 2009 article, [\"Dependency Inversion Principle and iPhone,\"][Smith] Eric Smith calls Interface Builder \"my favorite DI framework of all time,\" giving an example of how to use Interface Builder for dependency injection.\n\n[Smith]: http://blog.8thlight.com/eric-smith/2009/04/16/dependency-inversion-principle-and-iphone.html\n\nIf you decide you need a DI framework and Interface Builder isn't enough, how do you pick a good one? My advice is: **be cautious of any framework that requires you to change your code**. As soon as you have to subclass something, conform to a protocol, or add some kind of annotation, you're tying your code directly to a particular implementation. (This goes against the basic idea behind DI!) Instead, find a framework that lets you specify the wiring from *outside* your classes, whether that's specified via a DSL or in code.\n\n### \"I Don't Want to Expose All These Hooks.\"\n\nExposing injection points in initializers, properties, and method arguments can make it feel like you're breaking encapsulation. There's a desire to avoid showing the seams, because it's easy to tell yourself that the seams exist only to enable testing, and thus don't belong in the API. And this can be done by declaring them in a class category in a separate header file. For example, if we're dealing with Example.h, then create an additional header ExampleInternal.h. This will be imported only by Example.m and by test code.\n\nBut before you take that approach, I want to question the idea that DI leads to breaking encapsulation. What we're doing is making dependencies explicit. We are defining the edges of our components, and how they fit together. For example, if a class has an initializer with argument type `id <Foo>`, it's clear that in order to use the class, you need to give it an object that satisfies the Foo protocol. Think of it as defining a set of sockets on your class, along with the plugs that fit those sockets.\n\nWhen it feels cumbersome to expose dependencies, see if either of these scenarios fits:\n\n* Does it feel silly to expose dependencies on Apple's objects? Isn't anything Apple provides implicitly available, and thus fair game for any code? Not necessarily. Take our `NSUserDefaults` example: What if you've decided, for some reason, to avoid using `NSUserDefaults`? Having it explicitly identified as a dependency instead of hidden as an implementation detail will alert you to investigate this component. You can check to see if the use of `NSUserDefaults` violates your design constraints.\n* Does it feel like you have to expose a bunch of internals in order to test your class? First, see if you can write tests that only go through your existing public API (while still being fast and deterministic). If you can't, and if you need to manipulate dependencies that would otherwise be hidden, chances are there's another class trying to get out. Extract it, turn it into a dependency, and test it separately.\n\n## DI Is Bigger Than Testing\n\nMy initial motivation for exploring DI came from doing test-driven development, because in TDD you constantly wrestle with the question of \"How do I write a unit test for this?\" But I discovered that DI is actually concerned with a bigger idea: that **our code should be composed of modules that we snap together to build an application**.\n\nThere are many benefits to such an approach. Graham Lee's article, [\"Dependency Injection, iOS and You,\"][leeg] describes some of them: \"to adapt… to new requirements, make bug fixes, add new features, and test components in isolation.\"\n\n[leeg]: http://www.bignerdranch.com/blog/dependency-injection-ios/\n\nSo as you begin to apply DI to write unit tests, remember the bigger idea above. Keep *pluggable modules* in the back of your head. It will inform many design decisions and lead you to more DI patterns and principles.\n"
  },
  {
    "path": "2014-08-11-editorial.md",
    "content": "---\ntitle:  \"Editorial\"\ncategory: \"15\"\ndate: \"2014-08-11 12:00:00\"\ntags: editorial\n---\n\nHi all,\n\nThis month's issue is about a topic we've wanted to cover for a long time: Testing.\n\nAutomated testing is a disputed topic; some people love it, while other people hate it. But testing has many different aspects, and we hope that this issue — no matter what camp you're in — will offer something that helps you improve the quality of your projects.\n\nWe will take look at the umbrella term of 'testing' from many different perspectives to give you a broad overview of all the different techniques involved. We start out with a look at what [behavior-driven development](/issues/15-testing/behavior-driven-development/) is about and how to use it. Then we'll explore learnings from using Apple's [XCTest toolchain](/issues/15-testing/xctest/). Next, we will dive into [dependency injection](/issues/15-testing/dependency-injection/) and how it relates to testing. If you've ever wondered about [bad testing practices](/issues/15-testing/bad-testing-practices/), we have you covered, too. [Mocking](/issues/15-testing/mocking-stubbing/) is another common tool in the toolbox when writing tests, so we'll take a look at that. Finally, we finish off with two articles about testing related to user interface code: [user interface testing](/issues/15-testing/user-interface-testing/) and [snapshot testing](/issues/15-testing/snapshot-testing/). Thanks to all the authors for contributing their experience in this field!\n\n\nHappy testing,\n\nChris, Daniel, and Florian.\n"
  },
  {
    "path": "2014-08-11-mocking-stubbing.md",
    "content": "---\ntitle: \"Test Doubles: Mocks, Stubs, and More\"\ncategory: \"15\"\ndate: \"2014-08-11 07:00:00\"\nauthor:\n  - name: Mike Lazer-Walker\n    url: http://lazerwalker.com\ntags: article\n---\n\n\n## Intro\n\nIn an ideal world, all of your tests would be high-level tests that run against your actual code. UI tests would simulate actual user input (as Klaas discusses in [his article](/issues/15-testing/user-interface-testing/)), etc. In practice, this isn't always a good idea. Hitting the database or spinning up a UI for every test can make your test suite too slow, which either slows down productivity or encourages you to not run your tests as often. If the code you're testing depends on a network connection, this requires that your test environment has network access, and also makes it difficult to simulate edge cases, like when a phone is in airplane mode.\n\nBecause of all that, it can often be useful to write tests that replace some of your actual code with fake code.\n\n## When Would You Want to Use Some Sort of Mock Object?\n\nLet's start with some basic definitions of the different sorts of fake objects there are.\n\nA *double* is a general catch-all term for any sort of fake test object. In general, when you create any sort of test double, it's going to replicate an object of a specific class. \n\nA *stub* can be told to return a specified fake value when a given method is called. If your test subject requires a companion object to provide some sort of data, you can use a stub to \"stub out\" that data source and return consistent fake data in your test setup.\n\nA *spy* keeps track of what methods are called, and what arguments they are called with. You can use it to make test assertions, like whether a specific method was called or that it was called with the correct argument. This can be valuable for when you want to test the contract or relationship between two objects.\n\nA *mock* is similar to a spy, but the way you use it differs slightly. Rather than just capturing all method calls and letting you write assertions on them after the fact, a mock typically requires you to set up expectations beforehand. You tell it what you expect it to happen, execute the code you're testing, and then verify that the correct behavior happened.\n\nA *fake* is an object that has a full working implementation and behaves like a real object of its type, but differs from the class it is faking in a way that makes things easier to test. A classic example would be a data persistence object that uses an in-memory database instead of hitting a real production database.\n\nIn practice, these terms are often used differently than these definitions, or even interchangeably. The libraries we'll be looking at later in this article consider themselves to be \"mock object frameworks\" — even though they also provide stubbing capabilities, and the way you verify behavior more resembles what I've described as \"spies\" rather than \"mocks.\" But don't get too caught up on the specifics of the vocabulary; I give these definitions more because it's useful to think about different types of test object behaviors as distinct concepts at a high level.\n\nIf you're interested in a more in-depth discussion about the different types of fake test objects, Martin Fowler's article, [\"Mocks Aren't Stubs,\"](http://martinfowler.com/articles/mocksArentStubs.html) is considered the definitive article on the subject.\n\n### Mockists vs. Statists\nMany discussions of mock objects, mostly deriving from the Fowler article, talk about two different types of programmers who write tests: mockists and statists. \n\nThe mockist way of doing things is about testing the interaction between objects. By using mock objects, you can more easily verify that the subject under test follows the contract it has established with other classes, making the correct external calls at the correct time. For those who practice behavior-driven development, this is intricately tied in with the idea that your tests can help drive out better production code, as needing to explicitly mock out specific method calls can help you design a more elegant API contract between two objects. This sort of testing lends itself more to unit-level tests than full end-to-end tests.\n\nThe statist way of doing things doesn't use mock objects. The idea is that your tests should test state, rather than behavior, as that sort of test will be more robust. Mocking out a class requires you to update your mock if you update the actual class behavior; if you forget to do so, you can get into situations where your tests pass but your code doesn't work. By emphasizing only using real collaborators in your test environment, statist testing can help minimize tight coupling of your tests and your implementation, and reduce false negatives. This sort of testing, as you might guess, lends itself to more full end-to-end tests.\n\nNaturally, it's not like these are two rival schools of programmers; you'd be hard-pressed to see a mockist and a statist dueling it out on the street. This dichotomy is useful, though, in terms of recognizing that there are times when mocks are and are not the most appropriate tools in your tool belt. Different kinds of tests are useful for different tasks, and the most effective test suites will tend to have a blend of different testing styles. Thinking about what you are trying to accomplish with an individual test can help you figure out the best approach to take, and whether or not fake test objects might be the right tool for the job.\n\n## Diving into Code\n\nTalking about this theoretically is all well and good, but let's look at a real-word use case where you'd need to use mocks.\n\nLet's say we're trying to test an object with a method that opens another application by calling `UIApplication`'s `openURL:` method. (This is a real problem I faced while testing my [IntentKit](http://intentkit.github.io)  library.) Writing an end-to-end test for this is difficult (if not impossible), since 'success' involves closing your application. The natural choice is to mock out a `UIApplication` object, and assert that the code in question calls `openURL` on that object, with the correct URL.\n\nImagine the object in question has a single method:\n\n```objc\n@interface AppLinker : NSObject\n        - (instancetype)initWithApplication:(UIApplication *)application;\n        - (void)doSomething:(NSURL *)url;\n@end\n```\n\nThis is a pretty contrived example, but bear with me. In this case, you'll notice we're using constructor injection to inject a `UIApplication` object when we create our instance of `AppLinker`. In most cases, using mock objects is going to require some form of dependency injection. If this is a foreign concept to you, definitely check out [Jon's article](/issues/15-testing/dependency-injection/) in this issue.\n\n### OCMockito\n\n[OCMockito](https://github.com/jonreid/OCMockito) is a very lightweight mocking library: \n\n```objc\nUIApplication *app = mock([UIApplication class]);\nAppLinker *linker = [[AppLinker alloc] initWithApplication:app];\nNSURL *url = [NSURL urlWithString:@\"https://google.com\"];\n\n[linker doSomething:URL];\n\n[verify(app) openURL:url];\n```\n\n### OCMock\n[OCMock](http://ocmock.org) is another Objective-C mock object library. Like OCMockito, it provides full functionality for stubs, mocks, and just about everything else you might want. It has a lot more functionality than OCMockito, which, depending on your personal preference, could be a benefit or a drawback.\n\nAt the most basic level, we can rewrite the previous test using OCMock in a way that will look very familiar:\n\n```objc\nid app = OCMClassMock([UIApplication class]);\nAppLinker *linker = [[AppLinker alloc] initWithApplication:app];\nNSURL *url = [NSURL urlWithString:@\"https://google.com\"];\n\n[linker doSomething:url];\n\nOCMVerify([app openURL:url]);\n```\n\nThis style of mocking, where you verify that a method was called after your test, is known as a \"verify after running\" approach. OCMock just added support for this in its recent 3.0 release. It also supports an older style, known as expect-run-verify, that has you setting up your expectations before executing the code you are testing. At the end, you simply verify that the expectations were met:\n\n```objc\nid app = OCMClassMock([UIApplication class]);\n\nAppLinker *linker = [[AppLinker alloc] initWithApplication:app];\nNSURL *url = [NSURL urlWithString:@\"https://google.com\"];\n\nOCMExpect([app openURL:url]);\n\n[linker doSomething:url];\n\nOCMVerifyAll();\n```\n\nBecause OCMock lets you stub out class methods, you could also test this using OCMock, if your implementation of `doSomething` uses `[UIApplication sharedApplication]` rather than the `UIApplication` object injected in the initializer: \n\n```objc\nid app = OCMClassMock([UIApplication class]);\nOCMStub([app sharedInstance]).andReturn(app);\n\nAppLinker *linker = [[AppLinker alloc] init];\nNSURL *url = [NSURL urlWithString:@\"https://google.com\"];\n\n[linker doSomething:url];\n\nOCMVerify([app openURL:url]);\n```\n\nYou'll notice that stubbing out class methods looks exactly the same as stubbing out instance methods.\n\n## Roll Your Own\nFor a simple use case like this, you might not need the full weight of a mock object library. Often, you can just as easily create your own fake object to test the behavior you care about:\n\n```objc\n@interface FakeApplication : NSObject\n    @property (readwrite, nonatomic, strong) NSURL *lastOpenedURL;\n    \n    - (void)openURL:(NSURL *)url;\n@end\n\n@implementation FakeApplication\n    - (void)openURL:(NSURL *)url {\n        self.lastOpenedURL = url;\n    }\n@end\n```\n\nAnd then the test:\n\n```objc\nFakeApplication *app = [[FakeApplication alloc] init];\nAppLinker *linker = [[AppLinker alloc] initWithApplication:app];\nNSURL *url = [NSURL urlWithString:@\"https://google.com\"];\n\n[linker doSomething:url];\n\nXCAssertEqual(app.lastOpenedURL, url, @\"Did not open the expected URL\");\n```\n\nFor a contrived example such as this, it might appear that creating your own fake object just adds in a lot of unnecessary boilerplate, but if you find yourself needing to simulate more complex object interactions, having full control over the behavior of your mock object can be very valuable.\n\n### Which to Use?\nWhich approach you take depends completely on both the specifics of what you're testing and your own personal preference. OCMockito and OCMock are both installable via CocoaPods, so they're easy to integrate with your existing test setup, but there is also something to be said for avoiding adding dependencies and creating simple mock objects until you need something more. \n\n\n## Things to Watch Out for While Mocking\nOne of the biggest problems you run into with any form of testing is writing tests that are too tightly coupled to the implementation of your code. One of the biggest points of testing is to reduce the cost of future change; if changing the implementation details of some of your code breaks your tests, you've increased that cost. That said, there are a number of things you can do to minimize the possible negative effects of using test fakes.\n\n### Dependency Injection Is Your Friend\nIf you're not already using [dependency injection](/issues/15-testing/dependency-injection/), you probably want to. While there are sometimes sensible ways to mock out objects without DI (typically by mocking out class methods, as seen in the OCMock example above), it's often flat out not possible. Even when it is possible, the complexity of the test setup might outweigh the benefits. If you're using dependency injection consistently, you'll find writing tests using stubs and mocks will be much easier.\n\n### Don't Mock What You Don't Own\nMany experienced testers warn that you \"shouldn't mock what you don't own,\" meaning that you should only create mocks or stubs of objects that are part of your codebase itself, rather than third-party dependencies or libraries. There are two main reasons for this, one practical and one more philosophical.\n\nWith your codebase, you probably have a sense of how stable or volatile different interfaces are, so you can use your gut feeling about when using a double might lead to brittle tests. You generally have no such guarantee with third-party code. A common way to get around this is to create your own wrapper class to abstract out the third-party code's behavior. This can, in some situations, amount to little more than just shifting your complexity elsewhere without decreasing it meaningfully, but in cases where your third-party library is used very frequently, it can be a great way to clean up your tests. Your unit tests can mock out your own custom object, leaving your higher-level integration or functional tests to test the implementation of your wrapper itself. \n\nThe uniqueness of the iOS and OS X development world complicates things a bit, though. So much of what we do is dependent on first-party frameworks, which tend to be more overreaching than the standard library in many other languages. Although `NSUserDefaults` is an object you 'don't own,' for example, if you find yourself needing to mock it out, it's a fairly safe bet that Apple won't introduce breaking API changes in a future Xcode release.\n\nThe other reason to not mock out third-party dependencies is more philosophical. Part of the reason to write tests in a mockist style is to make it easier to find the cleanest possible interface between your two objects. But with a third-party dependency, you don't have control over that; the specifics of the API contract are already set in stone by a third party, so you can't effectively use tests as an experiment to see if things could be improved. This isn't a problem, per se, but in many cases, it reduces the effectiveness of mocking to the point that it's no longer worth it.\n\n## Don't Mock Me!\nThere is no silver bullet in testing; different strategies are needed for different situations, based both on your personal proclivities and the specifics of your code. While they might not be appropriate for every situation, test doubles are a very effective tool to have in your testing tool belt. Whether your inclination is to mock out everything in your unit tests using a framework, or to just create your own fake objects as needed, it's worth keeping mock objects in mind as you think about how to test your code.\n"
  },
  {
    "path": "2014-08-11-snapshot-testing.md",
    "content": "---\ntitle:  \"Snapshot Testing\"\ncategory: \"15\"\ndate: \"2014-08-11 05:00:00\"\nauthor:\n  - name: Orta Therox\n    url: https://orta.github.io\ntags: article\n---\n\nPeople have their own motivations to write tests for their applications. This article isn't to persuade you that you should do it, though in my opinion, you should.\n\nWriting tests for the visual aspects of an app is tricky. There's Apple built-in support for logical testing of objects, but no support for testing the end result of view-based code. This gap in functionality means a lot of people dismiss writing tests due to the difficulty of doing view-based tests.\n\nWhen Facebook released [`FBSnapshotTestCase`][fbsnapshot] to [CocoaPods][cocoapods], I initially dismissed it for this reason. I'm glad one of my coworkers didn't.\n\nView-based testing means verifying that what the user sees is what you want the user to see. Doing this means being able to ensure that different versions of your views, or different states of your views, continue to look the same. View-based testing can be used to provide a high-level test covering a lot of use cases surrounding an object.\n\n### How It Works\n\n`FBSnapShotTestCase` takes a `UIView` or `CALayer` subclass and renders it to a `UIImage`. This snapshot is used to create tests that compare a saved snapshot of the view/layer and the version generated by your test.  When it fails, it will create a reference image of the failed test, and another image to show the difference of the two.\n\nHere is an example of a failing test where we have less grid items than expected in a view controller:\n\n![Snapshots examples](/images/issue-15/snapshots-reference.png)\n\nIt makes the comparison by drawing both the view or layer and the existing snapshot into two `CGContextRefs` and doing a memory comparison of them with the C function `memcmp()`. This makes it extremely quick, with my tests ranging from 0.013 to 0.086 seconds per image for fullscreen iPad and iPhone images on a MacBook Air.\n\nWhen it's set up, it will default to storing the reference images inside your project's `[Project]Tests` folder, in a subfolder called `ReferenceImages`. Inside this is a library of folders based on the testcase class name. Inside the test case folders are the reference images per test. When a test fails, it will generate an output image of the resulting visuals from the test, and an image of the visual difference between itself and the reference. All three images are put inside the application's tmp folder. Snapshots will also `NSLog` a command to the console to load the two images into the visual diffing tool, [Kaleidoscope][kaleidoscope].\n\n### Installation\n\nLet's not beat around the bush here: you should be using [CocoaPods][cocoapods]. So installation is just adding `pod \"FBSnapshotTestCase\"` into the tests target of your Podfile. Running the command `pod install` will install the library.\n\n### XCTest with Snapshots\n\nThe default behavior of Snapshots is to subclass `FBSnapshotTestCase` instead of `XCTestCase`, and to then use the macro `FBSnapshotVerifyView(viewOrLayer, \"optional identifier\")` to verify against an already recorded image. There is a boolean property on the subclass `recordMode` that, when set, will make the macro record a new screenshot rather than check the result against a reference image.\n\n```objc\n@interface ORSnapshotTestCase : FBSnapshotTestCase\n@end\n\n@implementation ORSnapshotTestCase\n\n- (void)testHasARedSquare\n{\n    // Removing this will verify instead of recording\n    self.recordMode = YES;\n    \n    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];\n    view.backgroundColor = [UIColor redColor];\n    FBSnapshotVerifyView(view, nil);\n}\n\n@end\n```\n\n<a name=\"disadvantages\"> </a>\n\n### Disadvantages\n\nNothing's perfect. Let's start with the downsides.\n\n* Testing asynchronous code is hard. This is a similar pattern throughout testing in Cocoa. I tend to have two answers to this. Using testing frameworks like [Specta][specta] or [Kiwi][kiwi] provides ways of repeatedly running assertions in code until a timeout occurs or succeeds. This means you can give it 0.5 seconds to run, with the tests repeatedly being checked. Alternatively, you can build your application code so that asynchronous code is run synchronously if flagged.\n* Some components can be hard to test. There are two notable examples that come to mind: Some `UIView` classes cannot be initiated without a frame in a test, so get into the habit of always giving a frame to your views to avoid these messages: `<Error>: CGContextAddRect: invalid context 0x0. [..]`. If you write Auto Layout code a lot, then this is unintuitive. `CATiledLayer`-backed views require being on the main screen and being visible before they will render their tiles. They also render asynchronously. I tend to add a [two-second wait][arimagetiletest] for these tests.\n* Apple's OS patches can change the way their stock components are rendered. When Apple very subtly changed the font hinting in iOS 7.1, any snapshots with `UILabels` in them required re-recording.\n* Each snapshot is a PNG file stored in your repository, and together they average out at about 30-100kb per file for me. I record all my tests in \"@2x.\" The snapshots are as big as the view being rendered.\n\n### Advantages\n\n* I end up testing first. This is done by writing a screenshot test for every different view state as I add ways to make changes to the object. This gives me the ability to do a single test run and see the changes I make to the different states instantly. No tapping through my app to get to the right view, then changing state. I simply look at the images rendered by `FBSnapshotTestCase`. This saves a lot of time in building.\n* Snapshot tests are run at the same time as the rest of your tests. They don't have to run as another test scheme. They are written in the same language as the rest of your tests. They can [mostly](#disadvantages) be run without pushing the view to the screen.\n* Snapshots give code reviews a narrative. Tests show up first, offering a promise of what is coming up in the changeset. Next is the snapshots, proof that what the tests promise is true. Finally, the changes to the codebase show up. By the time you've hit the changeset, you're primed both by what has changed internally, and by what will be seen externally by users.\n* Providing visuals during the code review also opens up the review to designers; they can keep on top of changes by watching the project's repo.\n* I've found that writing snapshot tests provides overreaching test coverage. I don't believe it's optimal to aim for 100% coverage via unit tests. I try to be pragmatic in my approach to testing, wherein most of the changes introduced are tested. Snapshots test a large amount of code paths without specifically denoting the paths called. This is because snapshots test the output from a combination of systems easily. Compare the use of snapshots to painting with a wide brush, giving you the chance to make broad stokes at getting test coverage swiftly.\n* Snapshot tests are fast. Average tests on a modern MacBook Air using retina iPad-sized images range from 0.015 to 0.080 seconds per test. Having hundreds in an application's test suite is no problem. The [application I work on][folio] has hundreds of tests and they take less than five seconds.\n\n### Tooling\n\n##### FBSnapShots + Specta + Expecta\n\nI don't use vanilla XCTest. I uses [Specta and Expecta][specta], which provide a more concise and readable test environment to work in. This is the default testing setup when you create a [new CocoaPod][newcocoapod]. I'm a contributor to the pod [Expecta+Snapshots][expmatchers], which provides an Expecta-like API to `FBSnapshotTestCase`. It will handle naming screenshots for you, and can optionally run view controllers through their view event lifecycle. This means my Podfile looks like:\n\n```\ntarget 'MyApp Tests', :exclusive => true do\n    pod 'Specta','~> 1.0'\n    pod 'Expecta', '~> 1.0'\n    pod 'Expecta+Snapshots', '~> 1.0'\nend\n```\n\nIn turn, my tests look like:\n\n```objc\nSpecBegin(ORMusicViewController)\n\nit (@\"notations in black and white look correct\", ^{\n    UIView *notationView = [[ORMusicNotationView alloc] initWithFrame:CGRectMake(0, 0, 80, 320)];\n    notationView.style = ORMusicNotationViewStyleBlackWhite;\n\n    expect(notationView).to.haveValidSnapshot();\n});\n\nit (@\"Initial music view controller looks corrects\", ^{\n    id contoller = [[ORMusicViewController alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];\n    controller.view.frame = [UIScreen mainScreen].bounds;\n\n    expect(controller).to.haveValidSnapshot();\n});\n\nSpecEnd\n```\n\n### Snapshots Xcode Plugin\n\nParsing the console log to find the references to images is a lot of effort. Loading up all the different failing tests into a visual diff tool like [Kaleidoscope][kaleidoscope] requires running a lot of terminal commands.\n\nTo deal with nearly all of the common use cases, I built an Xcode plugin called [Snapshots][snapshots]. It can be installed via [Alcatraz][alcatraz] or built yourself. It makes it simple to compare the success and failure images for any inline failed test in Xcode.\n\n\n### Conclusion\n\n[`FBSnapshotTestCase`][fbsnapshot] gives you a way to test view-related code. It can be used to build and visualize view states without jumping through hoops in the simulator. You should use it with my plugin [Snapshots][snapshots] if you use Xcode. Sometimes it can be a bit frustrating, but it pays off. It welcomes designers into the code review stage. It can be a very easy first step into writing tests on an existing project. You should give it a try.\n\nExamples from Open Source:\n\n* [ARTiledImageView](https://github.com/dblock/ARTiledImageView)\n* [NAMapKit](https://github.com/neilang/NAMapKit/)\n* [ORStackView](https://github.com/orta/ORStackView/)\n* [ARCollectionViewMasonryLayout](https://github.com/AshFurrow/ARCollectionViewMasonryLayout)\n\n[cocoapods]: http://cocoapods.org \"CocoaPods homepage\"\n\n[fbsnapshot]: https://github.com/facebook/ios-snapshot-test-case \"FBSnapshotTestCase Github Repo\"\n\n[specta]: http://github.com/specta/specta/ \"Specta Github Repo\"\n\n[expmatchers]: https://github.com/dblock/ios-snapshot-test-case-expecta \"EXPMatchers+FBSnapshotTest Github Repo\"\n\n[kiwi]: https://github.com/kiwi-bdd/Kiwi \"Kiwi Github Repo\"\n\n[arimagetiletest]: https://github.com/dblock/ARTiledImageView/blob/master/IntegrationTests/ARTiledImageViewControllerTests.m#L31/ \"Test example from ARTiledImageView\"\n\n[kaleidoscope]: http://www.kaleidoscopeapp.com \"Kaleidoscope.app Web Site\"\n\n[snapshots]: http://github.com/orta/snapshots \"Snapshots Github Repo\"\n\n[alcatraz]: http://alcatraz.io \"Alcatraz the Xcode Plugin Manager\"\n\n[newcocoapod]: http://guides.cocoapods.org/making/using-pod-lib-create.html \"CocoaPods Guide\"\n\n[folio]: http://orta.github.io/#folio-header-unit \"Artsy Folio Website\"\n"
  },
  {
    "path": "2014-08-11-user-interface-testing.md",
    "content": "---\ntitle: User Interface Testing\ncategory: \"15\"\ndate: \"2014-08-11 06:00:00\"\nauthor:\n  - name: Klaas Pieter Annema\n    url: http://annema.me\ntags: article\n---\n\nOne question often asked about iOS (and I guess Mac, and every other UI-driven platform) is how to test UIs. A lot of us don't do it at all, often saying things like: “you should only test your business logic.” Others want to test the UI, but deem it too complex.\n\nWhenever someone tells me UI testing is hard, I think back to something [Landon Fuller](https://twitter.com/landonfuller) said about testing the UI of [Paper (by 53)](https://www.fiftythree.com/paper) during a [panel about testing](http://www.meetup.com/CocoaPods-NYC/events/164278492/) that we were both part of:\n\n> What you see on the screen is the culmination of a variety of data and transforms applied to that data over time ... Being able to decompose those things into testable units means you can break...down \\[things\\] that are relatively complex into more easily understood elements.\n\nPaper’s UI is relatively complex. Testability is usually not something taken into account when building such a UI. However, any action taken by the user is modeled in code somewhere; it’s always possible to fake the user’s action in a test. The problem is that most frameworks, including UIKit, often don’t publicly expose the necessary lower-level constructs.\n\nKnowing what to test is as important as knowing how to test. I've been referring to “UI testing” because that's the accepted term for the type of testing I'm going to discuss. In truth, I think you can split UI testing into two categories: 1) behavior and 2) aesthetics.\n\nThere is no way to deterministically say that aesthetics are correct, as they\ntend to change very often. You don't want to have to change your tests every\ntime you're tweaking the UI. That's not to say that you can't test the\naesthetics at all. I have no experience with it, but verifying aesthetics could\nbe done with snapshots. Read Orta’s article to learn more [about this\nmethod](/issues/15-testing/snapshot-testing/).\n\nThe remainder of this article will be about testing user behavior. I've provided a project on [GitHub](https://github.com/objcio/issue-15-ui-testing) that includes some practical examples. It’s written for iOS using Objective-C, but the underlying principles can be applied to the Mac and other UI frameworks.\n\nThe number one principle I apply to testing user experience is to make it appear to your code as if the user has triggered the action. This can be tricky because, as said before, frameworks don't always expose all of the necessary lower-level APIs.\n\nProjects like [KIF][], [Frank][], and [Calabash][] solve this problem, but at the cost of introducing an additional layer of complexity — and we should always use the simplest possible solution that gets the job done. You want your tests to be deterministic. They need to fail or pass consistently. The worst test suites are those that fail at random. I prefer not to use these solutions because, in my experience, they introduce too much complexity at the cost of reliability and stability.\n\n[KIF]: https://github.com/kif-framework/KIF\n[Frank]: http://www.testingwithfrank.com/\n[Calabash]: http://calaba.sh/\n\nNote that I've used [Specta][] and [Expecta][] in the example project. Technically, this isn't the simplest possible solution — XCTest is. But there are various reasons why [I prefer them](http://www.annema.me/why-i-prefer-testing-with-specta-expecta-and-ocmockito), and I know from experience that they don't affect the reliability and stability of my test. As a matter of fact, I'd wager that they make my tests better (a safe bet to make, since _better_ is ambiguous).\n\n[Specta]: https://github.com/specta/specta\n[Expecta]: https://github.com/specta/expecta\n\nRegardless of your method of testing, when testing user behavior, you want to stay as close to the user as possible. You want to make it appear to your code as if the user is interacting with it. Imagine the user is looking at a view controller, and then taps a button, which presents a new view controller. You'll want your test to present the initial view controller, tap the button, and verify that the new view controller was presented.\n\nBy focusing on exercising your code as if the user had interacted with your app, you verify multiple things at once. Most importantly, you verify the expected behavior. As a side effect, you're also simultaneously testing that controls are initialized and their actions set.\n\nFor example, consider a test in which an action method is called directly. This unnecessarily couples your test to what the button should do, and not what it will do. If the target or action method for the button is changed, your test will still pass. You want to verify that the button does what you expect. Which action the button uses, and on which target, should not concern your tests.\n\nUIKit provides the very useful `sendActionsForControlEvents:` method on `UIControl`, which we can use to fake user events. For example, use it to tap a button:\n\n```objc\n[_button sendActionsForControlEvent: UIControlEventTouchUpInside];\n```\n\nSimilarly, use it to change the selection of a `UISegmentedControl`:\n\n```objc\nsegments.selectedSegmentIndex = 1;\n[segments sendActionsForControlEvent: UIControlEventValueChanged];\n```\n\nNotice that it's not just sending `UIControlValueChanged`. When a user interacts with the control, it will first change its selected index, then send the `UIControlEventValueChanged`. This is a good example of doing some extra work to make it appear to your code as if the user is interacting with the control. \n\nNot all controls in UIKit have a method equivalent to `sendActionsForControlEvents:`, but with a bit of creativity, it's often possible to find a workaround. As said before, the most important thing is to make it appear to your code as if the user triggered the action.\n\nFor example, there is no method on `UITableView` to select a cell _and_ have it call its delegate or perform its associated segue. The sample project shows two ways of working around this. \n\nThe first method is specific to storyboards: it works by manually triggering the segue you want the table view cell to perform. Unfortunately, this does not verify that the table view cell is associated with that segue:\n\n```objc\n[_tableViewController performSegueWithIdentifier:@\"TableViewPushSegue\" sender:nil];\n```\n\nAnother option that does not require storyboards is to call the `tableView:didSelectRowAtIndexPath:` delegate method manually from your test code. If you're using storyboards, you can still use segues, but you have to trigger them from the delegate method manually:\n\n```objc\n[_viewController.tableView.delegate tableView:_viewController.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];\nexpect(_viewController.navigationController.topViewController).to.beKindOf([PresentedViewController class]);\n```\n\nI prefer the second option. It completely decouples the test from how the view controller is presented. It could be a custom segue, like a `presentViewController:animated:completion`, or some way that Apple hasn't invented yet. Yet all the test cares about is that at the end, the `topViewController` property is what it expects. The best option would be to ask the table view to select a row and perform the associated action, but that's not currently possible.\n\nAs a final example of testing controls, I want to present the special case of `UIBarButtonItem`s. They don't have a `sendActionsForControlEvent:` method because they're not descendents of `UIControl`. Let's figure out how we can send the button action and, to our code, make it look like the user tapped it.\n\nA `UIBarButtonItem`, unlike `UIControl`, can only have one target and one action associated with it. Performing the action can be as simple as:\n\n```objc\n[_viewController.barButton.target  performSelector:_viewController.barButton.action\n                                         withObject:_viewController.barButton];\n```\n\nIf you're using ARC, the compiler will complain because it can't infer the memory management semantics from an unknown selector. This solution is unacceptable to me because I treat warnings as errors.\n\nOne option is to use [#pragma directive](http://nshipster.com/pragma/#inhibiting-warnings) to hide the warning. Another alternative is to use the runtime directly:\n\n```objc\n#import <objc/message.h>\n\nobjc_msgSend(_viewController.barButton.target, _viewController.barButton.action, _viewController.barButton);\n```\n\nI prefer the runtime method because I dislike cluttering my test code with pragma directives, and also because it gives me an excuse to use the runtime.\n\nTo be honest, I'm not 100% certain these 'solutions' can't cause issues. This doesn't solve the underlying warning. Tests are usually short lived, so any memory issues that do occur are unlikely to cause problems. It's been working well for me for quite some time, but this is a case I don't fully understand, and it could turn into a bug that randomly fails at some point. I'm interested in [hearing about any potential issues](https://twitter.com/klaaspieter).\n\nI want to end with view controllers. View controllers are likely the most important component of any iPhone application. They're the abstraction used to mediate between the view and your business logic. In order to test the behavior as best as possible, we're going to have to present the view controllers. However, presenting view controllers in test cases quickly leads me to conclude they weren't built with testing in mind. \n\nPresenting and dismissing view controllers is the best way to make sure every test has a consistent start state. Unfortunately, doing so in rapid succession — like a test runner does — will quickly result in error messages like:\n\n- Warning: Attempt to dismiss from view controller \\<UINavigationController: 0x109518bd0\\> while a presentation or dismiss is in progress!\n- Warning: Attempt to present \\<PresentedViewController: 0x10940ba30\\> on \\<UINavigationController: 0x109518bd0\\> while a presentation is in progress!\n- Unbalanced calls to begin/end appearance transitions for \\<UINavigationController: 0x109518bd0\\>\n- nested push animation can result in corrupted navigation bar\n\nA test suite should be as fast as possible. Waiting for each presentation to finish is not an option. It turns out, the checks raising these warnings are on a per-window basis. Presenting each view controller in its own window gives you a consistent start state for your test, while also keeping it fast. By presenting each in its own window, you never have to wait for a presentation or dismissal to finish.\n\nThere are more issues with view controllers. For example, pushing to a navigation controller happens on the next run loop, while presenting a view controller modally doesn't. If you're interested in trying out this way of testing, I recommend you take a look at my [view controller test helper](https://github.com/klaaspieter/KPAViewControllerTestHelper), which solves these problems for you.\n\nWhen testing behavior, often you need to ensure that, through some interaction, a new view controller was presented. In other words, you need to verify the current state of the view controller hierarchy. UIKit does a great job providing the methods needed to verify this. For example, this is how you would make sure that a view controller was modally presented:\n\n```objc\nexpect(_viewController.presentedViewController).to.beKindOf([PresentedViewController class]);\n```\n\nOr pushed to a navigation controller:\n\n```objc\nexpect(_viewController.navigationController.topViewController).to.beKindOf([PresentedViewController class]);\n```\n\nTesting the UI isn't hard. Just be aware of what you're testing. You want to test user behavior, not application aesthetics. With creativity and persistence, most of the framework shortcomings can be worked around without sacrificing the stability and maintainability of your test suite. Just always remember to write tests to exercise the code as if the user is performing the action.\n"
  },
  {
    "path": "2014-08-11-xctest.markdown",
    "content": "---\ntitle:  \"Real-World Testing with XCTest\"\ncategory: \"15\"\ndate: \"2014-08-11 10:00:00\"\ntags: article\nauthor:\n  - name: Arne Schroppe\n    url: https://twitter.com/arneschroppe\n  - name: Daniel Eggert\n    url: http://twitter.com/danielboedewadt\n---\n\nAlmost four months ago, our team (Marco, Arne, and Daniel) set out to write the model layer of a new app. We wanted to use testing as part of our development process. After some discussion, we chose [XCTest](https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/testing_with_xcode/) as our testing framework.\n\nOur code base (excluding tests) has since grown to 544 kilobytes of source code spread across 190 files and 18,000 lines. Our tests click in at about 1,200 kilobytes, roughly twice that of the code they're testing. We're not quite done yet, but we're closing in. We wanted to share some of what we have learned, both about testing in general, and about testing with XCTest.\n\nNote that some of the model classes and methods in this article have been renamed, because the project is not in the App Store yet.\n\nWe chose XCTest for its simplicity and its tight integration into the Xcode IDE. With this article, we hope to shed some light on when XCTest is a good option, and when you might want to pick something else.\n\nWe tried to link to the other articles in [this issue](/issue-15/) where it makes sense.\n\n\n## Why We Are Testing\n\nAs the [article about bad practices in testing](/issue-15/bad-testing-practices.html) mentions, many people believe that \"the only time a test will give value back is when we want to change our code.\" There's obviously more to it, and you should read that article. But it is also important to remember that even when writing the first version of any code, most of the time will be spent changing code — as the project evolves, more and more features get added, and we realize that behavior needs to change slightly here and there. So even though you're not yet working on version 1.1 or 2.0, you'll still do a lot of changing, and tests do provide invaluable help at that point in time.\n\nWe are still finishing the initial version of our framework, and have just recently passed 1,000 test cases with more than 10 [man months](https://en.wikipedia.org/wiki/Man-hour) of effort spent on the project. Even though we had a clear vision of the project's architecture, we've still had to change and adapt the code along the way. The ever-growing set of test cases have helped us do this.\n\nThey have given us a level of comfort with the quality of our code, and at the same time given us the ability to refactor and change the code while knowing that we didn't break things. And we have been able to actually run our code from day one; we didn't have to wait for all the pieces to be in place.\n\n\n<a name=\"xctest-basics\"> </a>\n\n## How XCTest Works\n\nApple has some decent documentation on [using XCTest](https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/testing_with_xcode/). Tests are grouped into classes that subclass from `XCTestCase`. Each method that has a name starting with `test` is a test.\n\nBecause tests are simply classes and methods, we can add `@property` and helper methods to the class, as we see fit.\n\nIn order to be able to reuse code, we have a common superclass, `TestCase`, for all our test classes. This class subclasses from `XCTestCase`. All our test classes subclass from our `TestCase`.\n\nWe also put shared helper methods into the `TestCase` class. And we even have properties on it that get pre-populated for each test.\n\n\n\n<a name=\"naming\"> </a>\n\n## Naming\n\n\nSince a test is simply a method that begins with the word `test`, a typical test method would look like this:\n\n```objc\n- (void)testThatItDoesURLEncoding\n{\n  // test code\n}\n```\n\nAll our tests start with the words \"testThatIt.\" Another frequently used way of naming tests is to use the method or class being tested, as in `testHTTPRequest`. But which class and method being tested should be obvious by simply looking at the test.\n\nThe \"testThatIt\" style shifts the focus to the desired outcome, which, in most cases, is more difficult to understand at first glance.\n\nThere is a test case class for each production code class, and one is named after the other, e.g. `HTTPRequest` and `HTTPRequestTests`. If a class gets a bit bigger, we use categories to split up the test by topics.\n\nIf we ever need to disable a test, we simply prefix the name with `DISABLED`:\n\n\n```objc\n- (void)DISABLED_testThatItDoesURLEncoding\n```\n\nIt's easy to search for this string, and since the method name no longer starts with `test`, XCTest will skip this method.\n\n\n<a name=\"given-when-then\"> </a>\n\n## Given / When / Then\n\nWe structure our tests by using the *Given-When-Then* pattern — every test is split into three parts.\n\nThe **given** section sets up the environment for the test by creating model objects or bringing the system under test to a certain state. The **when** section contains the code we want to test. In most cases, this is only one method call. In the **then** section, we check the result of our action: Did we get the desired output? Was the object changed? This section consists mainly of assertions.\n\nIn a fairly simple test, it looks like this:\n\n```objc\n- (void)testThatItDoesURLEncoding\n{\n    // given\n    NSString *searchQuery = @\"$&?@\";\n    HTTPRequest *request = [HTTPRequest requestWithURL:@\"/search?q=%@\", searchQuery];\n\n    // when\n    NSString *encodedURL = request.URL;\n\n    // then\n    XCTAssertEqualObjects(encodedURL, @\"/search?q=%24%26%3F%40\");\n}\n```\n\nThis simple pattern makes it easier to write and understand tests, as they all follow the same basic pattern. For faster visual scanning, we even put the words \"given,\" \"when,\" and \"then\" as comments on top of their respective sections. This way, the method being tested immediately sticks out.\n\n\n<a name=\"reusable-code\"> </a>\n\n## Reusable Code\n\nOver time, we noticed that we repeated some code again and again in our tests, like waiting for an asynchronous operation to finish, or setting up an in-memory Core Data stack. To avoid code duplication, we began to gather all these useful snippets into a common base class for all our tests.\n\nIt turned out that this is not only useful as a collection of utility methods. The test base class can run its own `-setUp` and `-tearDown` methods to set up the environment. We use this mostly to initialize Core Data stacks for testing, to reseed our deterministic NSUUID (which is one of those small things that makes debugging a lot easier), and to set up background magic to simplify asynchronous testing.\n\nAnother useful pattern we started using recently is to implement delegate protocols directly in our `XCTestCase` classes. This way, we don't have to awkwardly mock the delegate. Instead, we can interact with the tested class in a fairly direct way.\n\n<a name=\"mocking\"> </a>\n\n## Mocking\n\nOur mocking framework is [OCMock](http://ocmock.org/). As described in the article about [mocking](/issue-15/mocking-stubbing.html) in this issue, a mock is an object that returns a prepared answer for method calls.\n\nWe use mocks for all dependencies of an object. This way, we can test the behavior of a class in isolation. The obvious disadvantage is that changes in a class don't automatically lead to failing unit tests for other classes that depend on it. But this is remedied by the fact that we also have integration tests, which test all the classes together.\n\nIt is important not to 'over-mock,' which is the habit of mocking every object except for the one being tested. When we started, we got into this habit at times, and we even mocked rather simple objects that were used as input to methods. Now we use many objects just the way they are, without mocking them.\n\nAs part of our common superclass for all test classes, we also added a\n\n```objc\n- (void)verifyMockLater:(id)mock;\n```\n\nmethod. It makes sure that the mocks get verified at the end of that method / test. This makes using mocks even more convenient. We can specify that a mock should be verified right at the point where we create that mock:\n\n```objc\n- (void)testThatItRollsBackWhenSaveFails;\n{\n    // given\n    id contextMock = [OCMockObject partialMockForObject:self.uiMOC];\n    [self verifyMockLater:contextMock];\n    NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSManagedObjectValidationError userInfo:nil];\n    [(NSManagedObjectContext *)[[contextMock stub] andReturnValue:@NO] save:[OCMArg setTo:error]];\n\n    // expect\n    [[contextMock expect] rollback];\n\n    // when\n    [ZMUser insertNewObjectInManagedObjectContext:self.uiMOC];\n    [self.uiMOC saveOrRollback];\n}\n```\n\n<a name=\"statelessness\"> </a>\n\n## State and Statelessness\n\nA lot has been said about stateless code during the last few years. But at the end of the day, our apps need state. Without state, most apps would be pretty pointless. But managing state is the source of a lot of bugs, because managing state is very complex.\n\nWe made our code a lot easier to work on by isolating the state. A few classes contain state, while most are stateless. In addition to the code, testing got a whole lot easier, too.\n\nFor example, we have a class, `EventSync`, that is responsible for sending local changes to our server. It needs to keep track of which local objects have changes that need to get pushed to the server, and which changes are currently being pushed. We can send multiple changes at once, but we don't want to send the same change twice.\n\nWe also had interdependencies between objects to keep track of. If *A* has a relationship to *B*, and *B* has local changes, we need to wait for those local changes to be pushed first, before we can send the changes of *A*.\n\nWe have a `UserSyncStrategy` that has a `-nextRequest` method that generates the next request. This request will send local changes to the server. The class itself is stateless, though. Or rather: All its state is encapsulated inside an `UpstreamObjectSync` class, which keeps track of all the user objects that have local changes, and for which we have running requests. There is no state outside this class.\n\nThis way, it was easy for us to have one set of tests for the `UpstreamObjectSync`. They check that this class manages the state correctly. For the `UserSyncStrategy`, we were mocking the `UpstreamObjectSync` and didn't have to worry about any state inside the `UserSyncStrategy` itself. That reduced the test complexity a lot, even more so because we were syncing many different kind of objects, and our different classes were all stateless and able to reuse the `UpstreamObjectSync` class.\n\n\n<a name=\"core-data\"> </a>\n\n## Core Data\n\nOur code relies heavily on [Core Data](https://developer.apple.com/technologies/mac/data-management.html). Since we need our tests to be isolated from one another, we have to bring up a *clean* Core Data stack for each test case, and then tear it down afterward. We need to be sure that we don't reuse the store from one test case for the next test.\n\nAll of our code is centered around two managed object contexts: one that the user-interface uses and that is tied to the main queue, and one that we use for synchronization and that has its own private queue.\n\nWe didn't want to repeat creating managed object contexts inside every test that needs them. Hence, inside our shared `TestCase` superclass' `-setUp` method, we create this set of two managed object contexts. This makes each individual test a lot easier to read.\n\nA test that needs a managed object context can simply call `self.managedObjectContext` or `self.syncManagedObjectContext`, like so:\n\n```objc\n- (void)testThatItDoesNotCrashWithInvalidFields\n{\n    // given\n    NSDictionary *payload =     // expected JSON response\n    @{\n      @\"status\": @\"foo\",\n      @\"from\": @\"eeeee\",\n      @\"to\": @44,\n      @\"last_update\": @[],\n    };\n\n    // when\n    ZMConnection *connection = [ZMConnection connectionFromTransportData:payload\n                                                    managedObjectContext:self.managedObjectContext];\n\n    // then\n    XCTAssertNil(connection);\n}\n```\n\nWe are using `NSMainQueueConcurrencyType` and `NSPrivateQueueConcurrencyType` to make the code consistent. But we implemented our own `-performGroupedBlock:` on top of `-performBlock:`, in order to solve the isolation problem. More about this under our [section about testing asynchronous code](#async-testing).\n\n\n<a name=\"merging-multiple-contexts\"> </a>\n\n### Merging Multiple Contexts\n\nWe have two contexts in our code. In production, we rely heavily on being able to merge from one context to another by means of `-mergeChangesFromContextDidSaveNotification:`. At the same time, we are using a separate persistent store coordinator for each context. Both contexts can then access the same SQLite store with minimal contention.\n\nBut for testing, we had to change this. We wanted to be able to use an in-memory store.\n\nUsing an on-disk SQLite store for testing does not work, because there are race conditions associated with deleting the store on disk. This would break isolation between tests. In-memory stores are a lot faster, which is good for testing.\n\nWe use factory methods to create all our `NSManagedObjectContext` instances. The base test class alters the behavior of these factory methods slightly, so that all contexts share the same `NSPersistentStoreCoordinator`. At the end of each test, we discard that shared persistent store coordinator to make sure that the next test uses a new one, and a new store.\n\n\n\n\n<a name=\"async-testing\"> </a>\n\n## Testing Asynchronous Code\n\nAsynchronous code can be tricky. Most testing frameworks offer some basic helpers for asynchronous code.\n\nLet's say we have an asynchronous message on `NSString`:\n\n```objc\n- (void)appendString:(NSString *)other resultHandler:(void(^)(NSString *result))handler;\n```\n\nWith XCTest, we could test it like this:\n\n```objc\n- (void)testThatItAppendsAString;\n{\n    NSString *s1 = @\"Foo\";\n    XCTestExpectation *expectation = [self expectationWithDescription:@\"Handler called\"];\n    [s1 appendString:@\"Bar\" resultHandler:^(NSString *result){\n        [expectation fulfill];\n        XCTAssertEqualObjects(result, @\"FooBar\");\n    }]\n    [self waitForExpectationsWithTimeout:0.1 handler:nil];\n}\n```\n\nMost testing frameworks have something like this.\n\nBut the main problem with asynchronous testing is isolation. Isolation is the “I“ in FIRST, as mentioned by [the article about testing practices](/issue-15/bad-testing-practices.html).\n\nWith asynchronous code, it can be very tricky to ensure that code from one test has completely stopped running on all threads and queues before the next test starts.\n\nWe found that the best solution to this problem is to consistently use groups, namely `dispatch_group_t`.\n\n<a name=\"dispatch-group\"> </a>\n\n### Don't Be Alone — Join a Group\n\nSome of our classes need to use `dispatch_queue_t` internally. Some of our classes enqueue blocks onto an `NSManagedObjectContext`'s private queue.\n\nInside our `-tearDown` method, we need to wait for all such asynchronous work to be completed. To achieve that, we had to do multiple things, as shown below.\n\nOur test classes have a property:\n\n```objc\n@property (nonatomic) dispatch_group_t;\n```\n\nWe define this once in our common superclass and set it there.\n\nNext, we can inject this group into any class that we're using that uses a `dispatch_queue` or similar, e.g. instead of calling `dispatch_async()`, we're consistently using `dispatch_group_async()`.\n\nSince we're relying heavily on Core Data, we added a method to `NSManagedObjectContext` called\n\n```objc\n- (void)performGroupedBlock:(dispatch_block_t)block ZM_NON_NULL(1);\n{\n    dispatch_group_enter(self.dispatchGroup);\n    [self performBlock:^{\n        block();\n        dispatch_group_leave(self.dispatchGroup);\n    }];\n}\n```\n\nand added a new `dispatchGroup` property to all managed object contexts. We then exclusively used `-performGroupedBlock:` in all our code.\n\nWith this, we could wait for all asynchronous work to be done inside our `tearDown` method:\n\n```objc\n- (void)tearDown\n{\n    [self waitForGroup];\n    [super tearDown];\n}\n\n- (void)waitForGroup;\n{\n    __block BOOL didComplete = NO;\n    dispatch_group_notify(self.requestGroup, dispatch_get_main_queue(), ^{\n        didComplete = YES;\n    });\n    NSDate *end = [NSDate dateWithTimeIntervalSinceNow:timeout];\n    while (! didComplete) {\n        NSTimeInterval const interval = 0.002;\n        if (! [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:interval]]) {\n            [NSThread sleepForTimeInterval:interval];\n        }\n    }\n}\n```\n\nThis works, because `-tearDown` gets called on the main loop. We spun the main loop to make sure that any code that might get enqueued onto the main queue got to run. The above code will hang indefinitely if the group never empties. In our case, we adapted it slightly, so that we had a timeout.\n\n### Waiting for All Work to Be Done\n\nWith this in place, a lot of our other tests became a lot easier, too. We created a `WaitForAllGroupsToBeEmpty()` helper, which we use like this:\n\n\n```objc\n- (void)testThatItDoesNotAskForNextRequestIfThereAreNoChangesWithinASave\n{\n    // expect\n    [[self.transportSession reject] attemptToEnqueueSyncRequestWithGenerator:OCMOCK_ANY];\n    [[self.syncStrategy reject] processSaveWithInsertedObjects:OCMOCK_ANY updateObjects:OCMOCK_ANY];\n    [self verifyMockLater:self.transportSession];\n\n    // when\n    NSError *error;\n    XCTAssertTrue([self.testMOC save:&error]);\n    WaitForAllGroupsToBeEmpty(0.1);\n}\n```\n\nThe last line waits for all asynchronous work to be done, i.e. the test makes sure that even asynchronous blocks enqueuing additional asynchronous work are all done, and that none of them trigger any of the rejected methods.\n\nWe implemented this with a simple macro\n\n```objc\n#define WaitForAllGroupsToBeEmpty(timeout) \\\n    do { \\\n        if (! [self waitForGroupToBeEmptyWithTimeout:timeout]) { \\\n            XCTFail(@\"Timed out waiting for groups to empty.\"); \\\n        } \\\n    } while (0)\n```\n\nwhich, in turn, uses a method on our shared test case superclass:\n\n```objc\n- (BOOL)waitForGroupToBeEmptyWithTimeout:(NSTimeInterval)timeout;\n{\n    NSDate * const end = [[NSDate date] dateByAddingTimeInterval:timeout];\n\n    __block BOOL didComplete = NO;\n    dispatch_group_notify(self.requestGroup, dispatch_get_main_queue(), ^{\n        didComplete = YES;\n    });\n    while ((! didComplete) && (0. < [end timeIntervalSinceNow])) {\n        if (! [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.002]]) {\n            [NSThread sleepForTimeInterval:0.002];\n        }\n    }\n    return didComplete;\n}\n```\n\n<a name=\"custom-expectations\"> </a>\n\n### Custom Expectations\n\nAt the [beginning of this section](#async-testing), we mentioned how\n\n```objc\n    XCTestExpectation *expectation = [self expectationWithDescription:@\"Handler called\"];\n```\n\nand\n\n```objc\n    [self waitForExpectationsWithTimeout:0.1 handler:nil];\n```\n\nare some basic building blocks for asynchronous tests.\n\nXCTest has some convenience stuff for `NSNotification` and key-value observing, both of which are built on top of these building blocks.\n\nSometimes, though, we found ourselves using the same patters in multiple places, e.g. if we are asynchronously expecting a managed object context to be saved, we may have code like this:\n\n```objc\n// expect\n[self expectationForNotification:NSManagedObjectContextDidSaveNotification\n                          object:self.syncManagedObjectContext\n                         handler:nil];\n```\n\nWe simplify this code by having a single, shared method\n\n```objc\n- (XCTestExpectation *)expectationForSaveOfContext:(NSManagedObjectContext *)moc;\n{\n    return [self expectationForNotification:NSManagedObjectContextDidSaveNotification\n                          object:moc\n                         handler:nil];\n}\n```\n\nand then use\n\n```objc\n// expect\n[self expectationForSaveOfContext:self.syncManagedObjectContext];\n```\n\ninside our tests. This is easier to read. Along this pattern, it is possible to add custom methods for other situations, too.\n\n\n\n<a name=\"fake-transport-session\"> </a>\n\n## The Ol’ Switcheroo — Faking the Transport Layer\n\nOne important question in testing an application is how to test the interaction with the server. The most ideal solution would be to quickly spin up a local copy of the real server, to provision it with fake data, and to run tests directly against it over http.\n\nWe are, in fact, working on this solution. It gives us a very realistic test setup. But the sad reality is that it is also a very slow setup. Clearing the database of the server between each test is slow. We have 1,000 tests. Even if only 30 of them depend on having a real server, if clearing the database and bringing up a 'clean' server instance takes five seconds, that would be two-and-a-half minutes of our test spent on waiting for that to happen. And we also needed to be able to test a server API before that API had been implemented. We needed something else.\n\nThis alternative solution is our 'fake server.' From the get-go, we structured our code so that all our communication with the server is channeled through a single class, the `TransportSession`, which is similar in style to `NSURLSession`, but also handles JSON conversion.\n\nWe have a set of tests that use the API we provide to the UI, and all the interaction with the server is then channeled through a *fake* implementation of the `TransportSession`. This transport session mimics both the behavior of the real `TransportSession` and the behavior of the server. The fake session implements the entire protocol of the `TransportSession` and adds a few methods that allow us to set up its state.\n\nHaving a custom class here has several advantages over mocking the server in each test using OCMock. For one, we can create more complex scenarios than what, realistically, would be possible with a mock. We can simulate edge cases that are hard to trigger with a real server.\n\nAlso, the fake server has tests of its own, so its answers are more precisely defined. If we ever need to change the server's reaction to a request, we only have to do so in one place. This makes all the tests that depend on the fake server much more stable, and we can more easily find parts in our code that do not play well with the new behavior.\n\nThe implementation of our `FakeTransportSession` is simple. An `HTTPRequest` object encapsulates the relative URL, method, and optional payload of a request. The `FakeTransportSession` maps all endpoints to internal methods, which then generate responses. It even has its own in-memory Core Data stack to keep track of the objects it knows about. This way, a GET can return a resource that a previous operation added with a PUT.\n\nAll of this may sound like a hard-to-justify time investment. But the fake server is actually quite simple: it is not a real server; we cut a lot of corners. The fake server can only serve a single client, and we do not have to worry about performance / scalability. We also did not implement everything in one huge effort, but wrote the parts we needed while developing and testing.\n\nOne thing worked in our favor, though: the server API was already quite stable and well defined when we started.\n\n## Custom Assert Macros\n\nWith the Xcode Test framework, one uses XCTAssert macros to do the actual checks:\n\n```objc\nXCTAssertNil(request1);\nXCTAssertNotNil(request2);\nXCTAssertEqualObjects(request2.path, @\"/assets\");\n```\n\nThere's a full list of \"Assertions Listed by Category\" in Apple’s [\"Writing Test Classes and Methods\"](https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/testing_with_xcode/testing_3_writing_test_classes/testing_3_writing_test_classes.html) article.\n\nBut we found ourselves often using very domain-specific checks, such as:\n\n```objc\nXCTAssertTrue([string isKindOfClass:[NSString class]] && ([[NSUUID alloc] initWithUUIDString:string] != nil),\n              @\"'%@' is not a valid UUID string\", string);\n```\n\nThat's very verbose and hard to read. And we didn't like the code duplication. We fixed that by writing our own simple assert macro:\n\n```objc\n#define AssertIsValidUUIDString(a1) \\\n    do { \\\n        NSUUID *_u = ([a1 isKindOfClass:[NSString class]] ? [[NSUUID alloc] initWithUUIDString:(a1)] : nil); \\\n        if (_u == nil) { \\\n            XCTFail(@\"'%@' is not a valid UUID string\", a1); \\\n        } \\\n    } while (0)\n```\n\nInside our tests, we then simply use:\n\n```objc\nAssertIsValidUUIDString(string);\n```\n\nThis approach can make a huge difference in making the tests readable.\n\n### One Step Further\n\nBut we all know it: [C preprocessor macros](https://en.wikipedia.org/wiki/C_preprocessor#Macro_definition_and_expansion) are a beast to dance with.\n\nFor some things, they're unavoidable, and it's all about limiting the pain. We need to use macros in this case in order for the test framework to know on which line and in which file the assertion failed. `XCTFail()` is itself a macro and relies on  `__FILE__` and `__LINE__` to be set.\n\nFor more complex asserts and checks, we implemented a simple helper class called `FailureRecorder`:\n\n```objc\n@interface FailureRecorder : NSObject\n\n- (instancetype)initWithTestCase:(XCTestCase *)testCase filePath:(char const *)filePath lineNumber:(NSUInteger)lineNumber;\n\n@property (nonatomic, readonly) XCTestCase *testCase;\n@property (nonatomic, readonly, copy) NSString *filePath;\n@property (nonatomic, readonly) NSUInteger lineNumber;\n\n- (void)recordFailure:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);\n\n@end\n\n\n#define NewFailureRecorder() \\\n    [[FailureRecorder alloc] initWithTestCase:self filePath:__FILE__ lineNumber:__LINE__]\n```\n\nIn our code, we had quite a few places where we wanted to check that two dictionaries are equal to one another. `XCTAssertEqualObjects()` can do that, but when it fails, the output is not very useful.\n\nWe wanted something like this\n\n```objc\nNSDictionary *payload = @{@\"a\": @2, @\"b\": @2};\nNSDictionary *expected = @{@\"a\": @2, @\"b\": @5};\nAssertEqualDictionaries(payload, expected);\n```\n\nto output\n\n```objc\nValue for 'b' in 'payload' does not match 'expected'. 2 == 5\n```\n\nSo we created\n\n```objc\n#define AssertEqualDictionaries(d1, d2) \\\n    do { \\\n        [self assertDictionary:d1 isEqualToDictionary:d2 name1:#d1 name2:#d2 failureRecorder:NewFailureRecorder()]; \\\n    } while (0)\n```\n\nwhich forwards into our method\n\n```objc\n- (void)assertDictionary:(NSDictionary *)d1 isEqualToDictionary:(NSDictionary *)d2 name1:(char const *)name1 name2:(char const *)name2 failureRecorder:(FailureRecorder *)failureRecorder;\n{\n    NSSet *keys1 = [NSSet setWithArray:d1.allKeys];\n    NSSet *keys2 = [NSSet setWithArray:d2.allKeys];\n    if (! [keys1 isEqualToSet:keys2]) {\n        XCTFail(@\"Keys don't match for %s and %s\", name1, name2);\n        NSMutableSet *missingKeys = [keys1 mutableCopy];\n        [missingKeys minusSet:keys2];\n        if (0 < missingKeys.count) {\n            [failureRecorder recordFailure:@\"%s is missing keys: '%@'\",\n             name1, [[missingKeys allObjects] componentsJoinedByString:@\"', '\"]];\n        }\n        NSMutableSet *additionalKeys = [keys2 mutableCopy];\n        [additionalKeys minusSet:keys1];\n        if (0 < additionalKeys.count) {\n            [failureRecorder recordFailure:@\"%s has additional keys: '%@'\",\n             name1, [[additionalKeys allObjects] componentsJoinedByString:@\"', '\"]];\n        }\n    }\n    for (id key in keys1) {\n        if (! [d1[key] isEqual:d2[key]]) {\n            [failureRecorder recordFailure:@\"Value for '%@' in '%s' does not match '%s'. %@ == %@\",\n             key, name1, name2, d1[key], d2[key]];\n        }\n    }\n}\n```\n\nThe trick is that the `FailureRecorder` captures `__FILE__`, `__LINE__`, and the test case. Inside its `-recordFailure:` method, it simply forwards the string to the test case:\n\n```objc\n- (void)recordFailure:(NSString *)format, ...;\n{\n    va_list ap;\n    va_start(ap, format);\n    NSString *d = [[NSString alloc] initWithFormat:format arguments:ap];\n    va_end(ap);\n    [self.testCase recordFailureWithDescription:d inFile:self.filePath atLine:self.lineNumber expected:YES];\n}\n```\n\n<a name=\"integration-with-xcode\"> </a>\n\n## Integration with Xcode and Xcode Server\n\nThe best part about XCTest is that it integrates extremely well with the [Xcode IDE](https://developer.apple.com/xcode/). With Xcode 6 and the Xcode 6 Server, this has been improved even more. This tight integration was very helpful and improved our productivity.\n\n### Focus\n\nWhile working on a single test or a set of tests inside a test class, the little diamond on the left-hand side gutter, next to the line numbers, lets us run that specific test or set of tests:\n\n![Diamond in Xcode Gutter](/images/issue-15/xctest-diamond-in-gutter@2x.png)\n\nIf the test fails, it turns red:\n\n![Failing Test](/images/issue-15/xctest-red-diamon-in-gutter@2x.png)\n\nIf it passes, it turns green:\n\n![Passing Test](/images/issue-15/xctest-green-diamon-in-gutter@2x.png)\n\nOne of our favorite keyboard shortcuts is ^⌥⌘G, which runs the last test or set of tests again. After clicking on the diamond in the gutter, we can change the test and then simply rerun it without having to take our hands off the keyboard. When debugging tests, that is extremely helpful.\n\n### Navigator\n\nThe navigators (on the left-hand side of the Xcode window) have a **Test Navigator**, which shows all tests grouped by their classes:\n\n![Test Navigator](/images/issue-15/xctest-test-navigator@2x.png)\n\nGroups of tests and individual tests can also be started from this UI. What's more useful, though, is we can filter this list to only show failing tests by enabling the third little icon at the bottom of the navigator:\n\n![Test Navigator with failing tests](/images/issue-15/xctest-test-navigator-failing@2x.png)\n\n\n### Continuous Integration\n\nOS X Server has a feature called [Xcode Server](https://www.apple.com/osx/server/features/#xcode-server), which is a [continuous integration](https://en.wikipedia.org/wiki/Continuous_integration) server based on Xcode. We have been using this.\n\nOur Xcode Server automatically checks out the project from GitHub whenever there's a new commit. We have it configured to run the static analyzer, run all our tests on an iPod touch and a few iOS simulators, and finally create an Xcode archive that can be downloaded.\n\nWith Xcode 6, the feature set of Xcode Server should be rather good, even for complex projects.\n\nWe have a custom *trigger* that runs as part of the build on Xcode Server on our release branch. This *trigger* script uploads the generated Xcode archive to a file server. This way, we have versioned archives. The UI team can then download a specific version of the precompiled framework from the file server.\n\n\n<a name=\"behaviour-driven-development\"> </a>\n\n## BDD and XCTest\n\nIf you are familiar with [behavior-driven development](/issue-15/behavior-driven-development.html), you might have noticed earlier that our style of naming is heavily inspired by this way of testing. Some of us have used [Kiwi](https://github.com/kiwi-bdd/Kiwi) as a testing library before, so it felt natural to concentrate on a method or class behavior. But does this mean that XCTest can replace a good BDD library? The answer is: not quite.\n\nIt's both an advantage and disadvantage that XCTest is very simple. There is not much to it: you create a class, prefix methods that hold tests with the word \"test,\" and you're good to go. The excellent integration into XCode speaks in XCTest's favor as well. You can press the diamond in the gutter to run just one test, you can easily display a list of failed tests, and you can quickly jump to a test by clicking on it in the list of tests.\n\nUnfortunately, that is also pretty much all you get. We did not hit any roadblocks while developing and testing our app with XCTest, but often, a bit more comfort would have been nice. XCTest classes look like plain classes, while the structure of a BDD test suite, with its nested contexts, is very visible. And this possibility to create nested contexts for tests is missing most. Nested contexts allow us to create more and more specific scenarios, while keeping the individual tests very simple. This is, of course, possible in XCTest too, for example by calling custom setup methods for some of the tests. It is just not as convenient.\n\nHow important the additional features of BDD frameworks are depends on the size of the project. Our conclusion is that XCTest can certainly be a good choice for small- to medium-sized projects. But for larger projects, it might pay off to take a closer look at BDD frameworks like [Kiwi](https://github.com/kiwi-bdd/Kiwi) or [Specta](https://github.com/specta/specta).\n\n\n<a name=\"the-project\"> </a>\n\n## Summary\n\nIs XCTest the right choice? You will have to judge based on the project at hand. We chose XCTest as part of [KISS](https://en.wikipedia.org/wiki/Keep_it_simple_stupid) — and have a wish list for things we'd like to be different. XCTest has served us well, even though we had to make tradeoffs. But with another testing framework, the tradeoff would have been something else.\n"
  },
  {
    "path": "2014-09-10-editorial.md",
    "content": "---\ntitle:  \"Editorial\"\ncategory: \"16\"\ndate: \"2014-09-10 12:00:00\"\ntags: editorial\n---\n\nWelcome to objc.io issue 16!\n\nOne day after Apple's iPhone and WATCH event, there's a lot of new stuff to digest. But in addition to new hardware, Apple's new programming language, Swift, went 1.0 yesterday. And that's what this issue is all about.\n\nThe purpose of this issue is neither to give an introduction to Swift nor to present best practices when writing Cocoa/CocoaTouch apps with Swift. For the former, there is Apple's documentation, and many other resources are coming up. Regarding the latter, the language is way too young, and none of us have had enough experience to confidently talk about best practices.\n\nInstead, we'll focus on some aspects of Swift that are new and potentially very foreign to Objective-C developers. \n\nChris starts off by showing us the [power of Swift](/issues/16-swift/power-of-swift/), highlighting some of his favorite parts of the new language. Andy takes a look at why you'd want to use [structs and value types](/issues/16-swift/swift-classes-vs-structs/), even though classes have been the traditional unit of structure for so long. Natasha then enlightens us with everything about [Swift functions](/issues/16-swift/swift-functions/), and Florian demonstrates how to leverage those first-class functions to build a [functional API](/issues/16-swift/functional-swift-apis/) around Core image. Last but not least, Brad explains how he uses [Swift playgrounds for rapid prototyping](/issues/16-swift/rapid-prototyping-in-swift-playgrounds/), providing an example of signal processing with the Accelerate framework. \n\nIf you'd like to dive deeper into the functional programming aspect of Swift, we're currently in the process of finishing up our book, [Functional Programming in Swift](/books). It's already available as an early-access version, and the final eBook will be published on October 1.\n \nAll the best from Berlin,\n\nChris, Daniel, and Florian.\n"
  },
  {
    "path": "2014-09-10-functional-swift-apis.md",
    "content": "---\ntitle:  \"Functional APIs with Swift\"\ncategory: \"16\"\ndate: \"2014-09-10 08:00:00\"\ntags: article\nauthor:\n  - name: Florian Kugler\n    url: https://twitter.com/floriankugler\n---\n\n\nWhen it comes to designing APIs, a lot of common patterns and best practices have evolved over the years. If nothing else, we always had lots of examples to draw from in the form of Apple's Foundation, Cocoa, Cocoa Touch, and many other frameworks. Undoubtedly, there are still ambiguities, and there's always room for discussion about how an API for a certain use case should ideally look. Nevertheless, the general patterns have become pretty much second nature to many Objective-C developers.\n       \nWith this year's emergence of Swift, designing an API poses many more questions than before. For the most part, we could just keep doing what we've been doing and translate existing approaches to Swift. But that's not doing justice to the added capabilities of Swift as compared to Objective-C. To quote Swift's creator, [Chris Lattner](https://twitter.com/clattner_llvm):     \n\n> Swift dramatically expands the design space through the introduction of generics and functional programming concepts.\n \nIn this article, we're going to explore how we can leverage these new tools at our disposal in the realm of API design. We're going to build a wrapper API around Core Image as an example. Core Image is a powerful image processing framework, but its API can be a bit clunky to use at times. The Core Image API is loosely typed — image filters are configured using key-value coding. It is all too easy to make mistakes in the type or name of arguments, which can result in runtime errors. The new API we develop will be safe and modular, exploiting *types* to guarantee the absence of such runtime errors.\n\n\n## The Goal\n \nThe goal is to build an API that allows us to safely and easily compose custom filters. For example, at the end of this article, we'll be able to write something like this:\n\n```swift\nlet myFilter = blur(blurRadius) >|> colorOverlay(overlayColor)\nlet result = myFilter(image)\n```\n\nThis constructs a custom filter that first blurs the image and then applies a color overlay to it. To achieve this, we will make heavy use of Swift's first-class functions. The code we're going to develop is available in a [sample project on GitHub](https://github.com/objcio/issue-16-functional-apis). \n\n\n## The Filter Type\n\nOne of the key classes in Core Image is the `CIFilter` class, which is used to create image filters. When you instantiate a `CIFilter` object, you (almost) always provide an input image via the `kCIInputImageKey` key, and then retrieve the filtered result via the `kCIOutputImageKey` key. Then you can use this result as input for the next filter. \n\nIn the API we will develop in this chapter, we'll encapsulate the exact details of these key-value pairs and present a safe, strongly typed API to our users. We define our own `Filter` type as a function that takes an image as its parameter and returns a new image:\n\n```swift\ntypealias Filter = CIImage -> CIImage\n```\n\nHere we use the [`typealias`](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_479) keyword to define our own name for the type `CIImage -> CIImage`, which is the type of a function that takes a `CIImage` as its argument and returns a `CIImage`. This is the base type that we are going to build upon.\n \nIf you're not used to functional programming, it may seem strange to use the name `Filter` for a function type. Usually, we'd use such a name for a class, and the temptation to somehow denote the function nature of this type is high. We could name it `FilterFunction` or something similar. However, we consciously chose the name `Filter`, since the key philosophy underlying functional programming is that functions are just values. They're no different from structs, integers, tuples, or classes. It took me some getting used to as well, but after a while, it started to make a lot of sense. \n\n\n## Building Filters\n\nNow that we have the `Filter` type defined, we can start defining functions that build specific filters. These are convenience functions that take the parameters needed for a specific filter and construct a value of type `Filter`. These functions will all have the following general shape:\n\n```swift\nfunc myFilter(/* parameters */) -> Filter\n```\n\nNote that the return value, `Filter`, is a function as well. Later on, this will help us compose multiple filters to achieve the image effects we want.\n\nTo make our lives a bit easier, we'll extend the `CIFilter` class with a [convenience initializer](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html) and a computed property to retrieve the output image:\n\n```swift\ntypealias Parameters = Dictionary<String, AnyObject>\n\nextension CIFilter {\n\n    convenience init(name: String, parameters: Parameters) {\n        self.init(name: name)\n        setDefaults()\n        for (key, value : AnyObject) in parameters {\n            setValue(value, forKey: key)\n        }\n    }\n\n    var outputImage: CIImage { return self.valueForKey(kCIOutputImageKey) as CIImage }\n\n}\n```\n\nThe convenience initializer takes the name of the filter and a dictionary as parameters. The key-value pairs in the dictionary will be set as parameters on the new filter object. Our convenience initializer follows the Swift pattern of calling the designated initializer first.\n\nThe [computed property](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html#//apple_ref/doc/uid/TP40014097-CH14-XID_329), `outputImage`, provides an easy way to retrieve the output image from the filter object. It looks up the value for the `kCIOutputImageKey` key and casts the result to a value of type `CIImage`. By providing this computed property of type `CIImage`, users of our API no longer need to cast the result of such a lookup operation themselves.\n\n\n### Blur\n\nWith these pieces in place, we can define our first simple filters. The Gaussian blur filter only has the blur radius as its parameter. As a result, we can write a blur `Filter` very easily:\n\n```swift\nfunc blur(radius: Double) -> Filter {\n    return { image in\n        let parameters : Parameters = [kCIInputRadiusKey: radius, kCIInputImageKey: image]\n        let filter = CIFilter(name:\"CIGaussianBlur\", parameters:parameters)\n        return filter.outputImage\n    }\n}\n```\n\nThat's all there is to it. The `blur` function returns a function that takes an argument `image` of type `CIImage` and returns a new image (`return filter.outputImage`). Because of this, the return value of the `blur` function conforms to the `Filter` type we previously defined as `CIImage -> CIImage`.\n\nThis example is just a thin wrapper around a filter that already exists in Core Image. We can use the same pattern over and over again to create our own filter functions.\n\n\n### Color Overlay\n\nLet's define a filter that overlays an image with a solid color of our choice. Core Image doesn't have such a filter by default, but we can, of course, compose it from existing filters.\n\nThe two building blocks we're going to use for this are the color generator filter (`CIConstantColorGenerator`) and the source-over compositing filter (`CISourceOverCompositing`). Let's first define a filter to generate a constant color plane:\n\n```swift\nfunc colorGenerator(color: UIColor) -> Filter {\n    return { _ in\n        let filter = CIFilter(name:\"CIConstantColorGenerator\", parameters: [kCIInputColorKey: color])\n        return filter.outputImage\n    }\n}\n```\n\nThis looks very similar to the `blur` filter we've defined above, with one notable difference: the constant color generator filter does not inspect its input image. Therefore, we don't need to name the image parameter in the function being returned. Instead, we use an unnamed parameter, `_`, to emphasize that the image argument to the filter we are defining is ignored.\n\nNext, we're going to define the composite filter:\n\n```swift\nfunc compositeSourceOver(overlay: CIImage) -> Filter {\n    return { image in\n        let parameters : Parameters = [ \n            kCIInputBackgroundImageKey: image, \n            kCIInputImageKey: overlay\n        ]\n        let filter = CIFilter(name:\"CISourceOverCompositing\", parameters: parameters)\n        return filter.outputImage.imageByCroppingToRect(image.extent())\n    }\n}\n```\n\nHere we crop the output image to the size of the input image. This is not strictly necessary, and it depends on how we want the filter to behave. However, this choice works well in the examples we will cover.\n\nFinally, we combine these two filters to create our color overlay filter:\n\n```swift\nfunc colorOverlay(color: UIColor) -> Filter {\n    return { image in\n        let overlay = colorGenerator(color)(image)\n        return compositeSourceOver(overlay)(image)\n    }\n}\n```\n\nOnce again, we return a function that takes an image parameter as its argument. The `colorOverlay` starts by calling the `colorGenerator` filter. The `colorGenerator` filter requires a `color` as its argument and returns a filter, hence the code snippet `colorGenerator(color)` has type `Filter`. The `Filter` type, however, is itself a function from `CIImage` to `CIImage`; we can pass an *additional* argument of type `CIImage` to `colorGenerator(color)` to compute a new overlay `CIImage`. This is exactly what happens in the definition of `overlay` — we create a filter using the `colorGenerator` function and pass the `image` argument to this filter to create a new image. Similarly, the value returned, `compositeSourceOver(overlay)(image)`, consists of a filter, `compositeSourceOver(overlay)`, being constructed and subsequently applied to the `image` argument.\n\n\n## Composing Filters\n\nNow that we have a blur and a color overlay filter defined, we can put them to use on an actual image in a combined way: first we blur the image, and then we put a red overlay on top. Let's load an image to work on:\n\n```swift\nlet url = NSURL(string: \"http://tinyurl.com/m74sldb\");\nlet image = CIImage(contentsOfURL: url)\n```\n\nNow we can apply both filters to these by chaining them together:\n\n```swift\nlet blurRadius = 5.0\nlet overlayColor = UIColor.redColor().colorWithAlphaComponent(0.2)\nlet blurredImage = blur(blurRadius)(image)\nlet overlaidImage = colorOverlay(overlayColor)(blurredImage)\n```\n\nOnce again, we assemble images by creating a filter, such as `blur(blurRadius)`, and applying the resulting filter to an image.\n\n\n### Function Composition\n\nHowever, we can do much better than the example above. The first alternative that comes to mind is to simply combine the two filter calls into a single expression:\n\n```swift\nlet result = colorOverlay(overlayColor)(blur(blurRadius)(image))\n```\n\nHowever, all the parentheses make this unreadable very quickly. A better approach is to compose filters by defining a custom function for this task:\n\n```swift\nfunc composeFilters(filter1: Filter, filter2: Filter) -> Filter {\n    return { img in filter2(filter1(img)) }\n}\n```\n\nThe `composeFilters` function takes two filters as arguments and defines a new filter. This composite filter expects an argument `img` of type `CIImage`, and passes it through both `filter1` and `filter2`, respectively. We can now use function composition to define our own composite filter, like this:\n\n```swift\nlet myFilter = composeFilters(blur(blurRadius), colorOverlay(overlayColor))\nlet result = myFilter(image)\n```\n\nBut we can go one step further to make this even more readable by introducing an operator for filter composition: \n\n```swift\ninfix operator >|> { associativity left }\n\nfunc >|> (filter1: Filter, filter2: Filter) -> Filter {\n    return { img in filter2(filter1(img)) }\n}\n```\n\nThe operator definition starts with the keyword `infix`, which specifies that the operator takes a left and a right argument, and `associativity left` specifies that an expression like `f1 >|> f2 >|> f3` will be evaluated as `(f1 >|> f2) >|> f3`. By making this a left-associative operator and applying the left-hand filter first, we can read the sequence of filters from left to right, just as Unix pipes. \n\nThe rest is a simple function identical to the `composeFilters` function we've defined before — the only difference being its name, `>|>`. \n\nApplying the filter composition operator turns the example we've used before into:\n\n```swift\nlet myFilter = blur(blurRadius) >|> colorOverlay(overlayColor)\nlet result = myFilter(image)\n```\n\nWorking with this operator makes it easier to read and understand the sequence the filters are applied in. It's also much more convenient if we want to reorder the filters. To use a simple analogy: `1 + 2 + 3 + 4` is much clearer and easier to change than `add(add(add(1, 2), 3), 4)`. \n\n\n## Custom Operators\n\nMany Objective-C developers are very skeptical about defining custom operators. It was a feature that didn't receive a too warm welcome when Swift was first introduced. Lots of people have been burned by custom operator overuse (or abuse) in C++, either in personal experience or with stories from others.\n\nYou might look equally skeptical at the `>|>` operator for filter composition that we've defined above. After all, if everybody starts to define one's own operators, isn't this going to make code really hard to understand? The good thing is that in functional programming there are a bunch of operations that come back all the time, and defining a custom operator for those operations is not an uncommon thing to do at all. \n\nThe filter composition operator we've defined above is just an example of [function composition](http://en.wikipedia.org/wiki/Function_composition_%28computer_science%29), a concept that's widely used in functional programming. In mathematics, the composition of the two functions `f` and `g`, sometimes written `f ∘ g`, defines a new function mapping an input to `x` to `f(g(x))`. This is precisely what our `>|>` operator does (apart from applying the functions in reverse order).\n\n\n### Generics\n\nWith this in mind, we don't actually have to define a special operator to compose filters, but we can use a generic function composition operator. So far, our `>|>` operator was defined as:\n \n```swift\nfunc >|> (filter1: Filter, filter2: Filter) -> Filter\n```\n\nWith this definition, we can only apply it to arguments of type `Filter`.\n \nHowever, we can leverage Swift's [generics](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Generics.html) feature to define a generic function composition operator:\n \n```swift\nfunc >|> <A, B, C>(lhs: A -> B, rhs: B -> C) -> A -> C {\n    return { x in rhs(lhs(x)) }\n}\n```\n\nThis is probably pretty hard to read at first — at least it was for me. But looking at all the pieces individually, it becomes clear what this does.\n\nFirst we take a look at what's between the angled brackets after the function's name. This specifies the generic types this function is going to work with. In this case, we have specified three generic types: `A`, `B`, and `C`. Since we haven't restricted those types in any way, they can represent anything.\n\nNext, let's inspect the function's arguments: the first argument, `lhs` (short for left-hand side), is a function of type `A -> B`, i.e. a function that takes an argument of type `A` and returns a value of type `B`. The second argument, `rhs` (right-hand side), is a function of type `B -> C`. The arguments are named `lhs` and `rhs` because they represent what is located to the left and right of the operator, respectively. \n\nRewriting our filter composition operator without using the `Filter` type alias, we quickly see that it was only a special case of the generic function composition operator:\n\n```swift\nfunc >|> (filter1: CIImage -> CIImage, filter2: CIImage -> CIImage) -> CIImage -> CIImage\n```\n\nTranslating the generic types `A`, `B`, and `C` in our minds to all represent `CIImage` makes it clear that the generic operator is indeed capable of replacing the specific filter composition operator.     \n\n\n## Conclusion\n\nHopefully the example of wrapping Core Image in a functional API was able to demonstrate that, when it comes to API design patterns, there is an entirely different world out there than what we're used to as Objective-C developers. With Swift, we now have the tools in our hands to explore those other patterns and make use of them where it makes sense. \n"
  },
  {
    "path": "2014-09-10-power-of-swift.md",
    "content": "---\ntitle:  \"The Power of Swift\"\ncategory: \"16\"\ndate: \"2014-09-10 11:00:00\"\ntags: article\nauthor:\n  - name: Chris Eidhof\n    url: https://twitter.com/chriseidhof\n---\n\nBefore writing anything else, I have to admit I'm very biased: I love Swift. I think it's the best thing that happened to the Cocoa ecosystem since I started. I want to let you know why I think this by sharing my experiences with Swift, Objective-C, and Haskell. This article is not really about any best practices (at the moment of writing, Swift is much too young to have any established best practices), but rather about showing some examples of where Swift really shines.\n\nTo give some personal background: before being a full-time programmer on the iOS and OS X platforms, I spent a few years doing lots of Haskell (among other functional programming languages). I still think Haskell is one of the most beautiful languages I've worked with. However, I switched to Objective-C because I believed (and still believe) that the iOS platform is the most exciting platform to work on. Admittedly, in the beginning it was a bit frustrating for me to be working in Objective-C, but I learned to love it.\n\nWhen Apple announced Swift during WWDC, I got really excited. I haven't been so excited about any new technology announcement in years. After looking at the documentation, I realized that Swift allows us to use all the existing knowledge from functional languages, yet still integrate seamlessly with Cocoa APIs. I think the combination of these two features is very unique: there is no other language that melds both things together so well. Looking at a language like Haskell, it's rather difficult to call Objective-C APIs, and looking at Objective-C, it's rather difficult to do functional programming.\n\nI learned functional programming during my time at Utrecht University. Because I learned it in the context of academia, I wasn't too overwhelmed by the complicated terminology used: monads, applicative functors, and lots of other things. I think the naming is one big stumbling block for people who want to get into functional programming.\n\nNot only is the naming different, but also the style. Being Objective-C programmers, we're used to object-oriented programming. And because most languages are either using object-oriented programming or a similar style, we can read most code in most languages. Not so much when reading functional programming — it might look like complete gibberish when you're not used to it.\n\nWhy would you use functional programming, then? It's weird, people are not used to it, and it takes quite a while to learn. Also, you can already solve any problem with object-oriented programming, so there's no need to learn anything new, right?\n\nFor me, functional programming is just another tool in the toolbox. It's a very powerful tool that changed the way I think about programming. It can be extremely useful when solving problems. For most problems, object-oriented programming is great. But for others, solving the problem functionally might save you massive amounts of time and energy.\n\nGetting started with functional programming might be a bit painful. For one, you have to let go of old patterns. Because a lot of us spent years thinking in an object-oriented way, this is very difficult. In functional programming, you think of immutable data structures and functions that convert them. In object-oriented programming, you think about objects that send messages to each other. If you don't immediately get functional programming, it's a good sign. Your brain is probably deeply wired to think of solving problems in the object-oriented way.\n\n## Examples\n\nOne of my favorite features of Swift is the use of optionals. Optionals allow us to deal with values that might or might not exist. In Objective-C, we have to be precise in our documentation about whether or not `nil` values are allowed. With optionals, we move this responsibility to the type system. If you have an optional value, it might be nil. If you have a value that's not of the optional type, you know it cannot be nil.\n\nFor example, consider the following snippet in Objective-C:\n\n```objc\n- (NSAttributedString *)attributedString:(NSString *)input \n{\n    return [[NSAttributedString alloc] initWithString:input];\n}\n```\n\nIt looks harmless, but if `input` is nil, this will crash. This is something you can only find out at runtime. Depending on how it is used, you might find it out very quickly, but you might also find out only after you ship the app, leading to crashes for your customers.\n\nContrast this with the same API in Swift:\n\n```swift\nextension NSAttributedString {\n    init(string str: String)\n}\n```\n\nIt might look like an exact translation from Objective-C, but Swift does not allow `nil` values to be passed in. If that would have been the case, the API would look like this:\n\n```swift\nextension NSAttributedString {\n    init(string str: String?)\n}\n```\n\nNote that there is an added question mark. This means that you can pass in either a value, or nil. The type is very *precise*: just by looking at it, we can see which values are allowed. After working with optionals for a while, you will find that you can read the type instead of the documentation. And if you make a mistake, you will get a compile-time warning instead of a runtime error.\n\n### Advice\n\nIf you can, avoid optionals. Optionals are an extra mental obstacle for consumers of your API. That said, there are definitely good uses for them. If you have a function that might not succeed for a clear reason, you can return an optional. For example, suppose you're parsing a string like `#00ff00` into a color. If the input doesn't conform to the format, you want to return `nil`:\n\n```swift\nfunc parseColorFromHexString(input: String) -> UIColor? {\n    // ...\n}\n```\n\nIn case you need to specify an error message, you could also use an `Either` or `Result` type, which is not in the standard library. This is very useful when the reason for failure is important. A good example is found in the post, \"[Error Handling in Swift](http://nomothetis.svbtle.com/error-handling-in-swift).\"\n\n## Enums\n\nEnums are a new thing with Swift, and they are quite different from anything we're used to in Objective-C. In Objective-C, we have something called enums, but they're not much more than glorified integers.\n\nLet's consider boolean types. A boolean can have exactly one of two possible values: true or false. It is important to realize that it's not possible to add another possible value — the boolean type is *closed*. The nice thing about booleans being closed is that in any function that uses the boolean type, we only have to take `true` and `false` into account. \n\nThe same holds true for optionals. There are only two cases: the `nil` case, and the case where there's a value. Both optionals and booleans can be defined as enums in Swift, with only one difference: in the optional enum, there is a case that has an associated value. Let's look at their respective definitions:\n\n```swift\nenum Boolean {\n    case False\n    case True\n}\n\nenum Optional<A> {\n    case Nil\n    case Some(A)\n}\n```\n\nThey are very similar. If you change the naming of the cases, the only thing that's different is the associated value. If you also add a value to the `Nil` case of the optional, you end up with the `Either` type:\n\n```swift\nenum Either<A,B> {\n    case Left(A)\n    case Right(B)\n}\n```\n\nThe `Either` type is used a lot in functional programming when you want to represent a choice between two things. For example, if you have function that returns either an integer or an error, you could use `Either<Int,NSError>`. If you would want to store either booleans or strings in a dictionary, you could use `Either<Bool,String>` as the key type.\n\n> Theoretical aside: sometimes enums are so-called *sum types*, because they represent a sum of different types. In the case of `Either`, they represent the sum of `A` and `B`. Structs or tuples are called *product types* because they represent the product of different types. See also: [algebraic data types](http://en.wikipedia.org/wiki/Algebraic_data_type).\n\nKnowing when to use enums and when to use other data types (such as [classes or structs](/issues/16-swift/swift-classes-vs-structs/)) can be a bit difficult. They are most useful when you have a closed set of possible values. For example, if we design a Swift wrapper around the GitHub API, we could represent the endpoints with an enum. There's a `/zen` endpoint, which doesn't take any parameters. To fetch a user profile, we have to provide the username, and finally, to display a user's repositories, we provide the username and a key that shows whether or not to sort the result ascendingly:\n\n```swift\nenum Github {\n    case Zen\n    case UserProfile(String)\n    case Repositories(username: String, sortAscending: Bool)\n}\n```\n\nDefining API endpoints is a good use case for enums. The list of API endpoints is finite, and we can just define a case for each endpoint. If we do a switch statement on values of this endpoint, we will get a warning if we forget to include case. So if at some point we will add a case, we need to update every function or method that pattern-matches on this enum.\n\nOther people who use our enum cannot just add extra cases to it, unless they have access to the source. This is a very useful limitation. Consider if you could add a case to `Bool` or `Optional` — then all the functions that use it would need to be rewritten.\n\nLet's say we are building a currency converter. We could define our currencies as an enum:\n\n```swift\nenum Currency {\n    case Eur\n    case Usd\n}\n```\n\nWe can now write a function that gets the symbol for any currency:\n\n```swift\nfunc symbol(input: Currency) -> String {\n    switch input {\n        case .Eur: return \"€\"\n        case .Usd: return \"$\"\n    }\n}\n```\n\nAnd finally, we can use our `symbol` function to produce a nicely formatted string, according to the system's locale:\n\n```swift\nfunc format(amount: Double, currency: Currency) -> String {\n    let formatter = NSNumberFormatter()\n    formatter.numberStyle = .CurrencyStyle\n    formatter.currencySymbol = symbol(currency)\n    return formatter.stringFromNumber(amount)\n}\n```\n\nThere is one big limitation. For currencies, we might want to allow users of our API to add more cases later on. In Objective-C, a common way to add more types to an interface is by subclassing. In Objective-C, you can, in theory, subclass any class, and extend it that way. In Swift, you can still use subclassing, but only on classes, not on enums. However, we can use another technique (which works in both Objective-C and Swift protocols).\n\nLet's suppose we define a protocol for currency symbols:\n\n```swift\nprotocol CurrencySymbol {\n    func symbol() -> String\n}\n```\n\nNow, we can make our `Currency` type an instance of this protocol. Note that we can now remove the `input` parameter, as it is implicitly passed as self:\n\n```swift\nextension Currency : CurrencySymbol {\n   func symbol() -> String {\n        switch self {\n            case .Eur: return \"€\"\n            case .Usd: return \"$\"\n        }\n    }\n}\n```\n\nAnd we can rewrite our `format` function to work on any type that conforms to the protocol:\n\n```swift\nfunc format(amount: Double, currency: CurrencySymbol) -> String {\n    let formatter = NSNumberFormatter()\n    formatter.numberStyle = .CurrencyStyle\n    formatter.currencySymbol = currency.symbol()\n    return formatter.stringFromNumber(amount)\n}\n```\n\nNow we have made our code very extensible — any type that conforms to `CurrencySymbol` can now be formatted. For example, if we create a new type that stores bitcoins, we can immediately make it work with our `format` function:\n\n```swift\nstruct Bitcoin : CurrencySymbol {\n    func symbol() -> String {\n        return \"B⃦\"\n    }\n}\n```\n\nThis is a great way of writing functions that are open for extension. By taking in values that should conform to a protocol — rather than concrete types — you leave it up to the user of your API to add more types. You can still use the flexibility of enums, but by combining them with protocols, you can be even more expressive. Depending on your use-case, you can now easily choose whether you want an open or a closed API.\n\n## Type Safety\n\nI think one really big win of Swift is type safety. As we have seen with the optionals, we can move certain checks from runtime to compile time by using types in a smart way. Another example is how arrays work in Swift: an array is generic, and it can only hold elements of the same type. It's not possible to append an integer value to an array of strings. This eliminates an entire class of possible bugs. (Note that if you want an array of either strings or integers, you can use the `Either` type above.)\n\nSuppose, again, that we are extending our currency converter to be a general unit converter. If we would use `Double` to represent the amounts, it could get a bit confusing. For example, 100.0 might mean 100 dollars, 100 kilograms, or anything else that's 100. What we can do is let the type system help us by creating different types for different physical quantities. For example, we can define a type that describes money:\n\n```swift\nstruct Money {\n    let amount : Double\n    let currency: Currency\n}\n```\n\nFor mass, we could define another struct:\n\n```swift\nstruct Mass {\n    let kilograms: Double\n}\n```\n\nNow there is no way we can accidentally add up `Money` and `Mass`. Depending on the domain of your application, it might be very useful to wrap simple types like this. In addition, reading code will become a lot simpler. Suppose we encounter the function `pounds`:\n\n```swift\nfunc pounds(input: Double) -> Double\n```\n\nIt is rather difficult to see from the type signature what it does. Does it convert euros into pounds? Or does it convert kilograms into pounds? We could name the function differently, or we could write documentation (both are very good ideas), but there is a third alternative. We can make the type more specific:\n\n```swift\nfunc pounds(input: Mass) -> Double\n```\n\nNot only have we made it easier for consumers of this function to immediately understand what it does, but we also made it impossible to accidentally pass in values that are in other units. If you try to call this function with a `Money` value, the compiler just won't accept it. One possible improvement would be to have a more precise return type as well; now it's just `Double`.\n\n## Immutability\n\nAnother really nice feature of Swift is the built-in support for immutability. In Cocoa, many of the APIs already show the value of immutability. For a good overview of why this is important, see also [Value Objects](/issues/7-foundation/value-objects/). For example, as Cocoa developers, we use a lot of pairs of classes (`NSString` vs. `NSMutableString`, `NSArray` vs. `NSMutableArray`). When you receive an `NSString` value, you can assume it won't be changed. To be completely sure, you still have to `copy` it, and then you know that you have a unique immutable copy that won't be changed.\n\nIn Swift, immutability is built directly into the language. For example, if you want to create a mutable string, you would write the following code:\n\n```swift\nvar myString = \"Hello\"\n```\n\nHowever, if you want to create an immutable string, you can do the following:\n\n```swift\nlet myString = \"Hello\"\n```\n\nHaving immutable data can greatly help when working with APIs that are called by consumers you might not know. For example, if you have a function that takes an array, it is very useful to know that the array will not be mutated while you iterate over it. In Swift, this is the case by default. Writing multithreaded code with immutable data is much easier, exactly because of this reason.\n\nThere is another really big advantage. If you write functions and methods that only operate on immutable data, your type signature is a huge source of documentation. In Objective-C, this is often not the case. For example, suppose that you want to use a `CIFilter` on OS X. After instantiating it, you need to call the `setDefaults` method. This is described in the documentation. There are many other classes like this, where you instantiate it, and then you have to call one or more methods before you can use them. The problem is, without reading the documentation, often it is not clear which methods to call, and you might end up with very strange behavior.\n\nWhen working with immutable data, it is immediately clear from the type signature what's happening. For example, consider the type signature for `map` on optionals. We know that there is an optional `T` value, and there is a function that converts `T`s into `U`s. The result is an optional `U` value. There is no way the original value has changed:\n\n```swift\nfunc map<T, U>(x: T?, f: T -> U) -> U?\n```\n\nIt's the same with `map` on an array. It is defined as an extension on array, so the input array is `self`. We can see that it takes a function that transforms `T` into `U`, and produces an array of `U` values. Because it's an immutable function, we know that the original array can't change, and we know that the result is immutable too. Having these constraints encoded in the type system and enforced by the compiler takes away the burden of having to look up the documentation and remembering exactly what changes:\n\n```swift\nextension Array {\n    func map<U>(transform: T -> U) -> [U]\n}\n```\n\n\n## Conclusion\n\nThere are a lot of interesting new possibilities with Swift. I especially like that the compiler can now check things that we used to do manually or by reading the documentation. We can choose to use these possibilities as we see fit. We can still write code using our existing, proven techniques, but we can opt in to some of the new possibilities for specific parts of our code. \n\nHere is my prediction: I think Swift will dramatically change the way we write code, in a good way. It will take a few years to make the move from Objective-C, but I think that most of us will make the change and not look back. Some people will transition fast, and for some it might take a long time. However, I am confident that in due time, almost everybody will see the benefits that Swift provides us with.\n"
  },
  {
    "path": "2014-09-10-rapid-prototyping-in-swift-playgrounds.md",
    "content": "---\ntitle:  \"Rapid Prototyping in Swift Playgrounds\"\ncategory: \"16\"\ndate: \"2014-09-10 07:00:00\"\nauthor:\n  - name: Brad Larson\n    url: https://twitter.com/bradlarson\ntags: article\n---\n\nMany developers enjoy building OS X or iOS applications because of how quickly one can create a viable application using the Cocoa frameworks. Even complex applications can be designed and built by small teams, in large part because of the capabilities provided by the tools and frameworks on these platforms. Swift playgrounds build on this tradition of rapid development, and they have the potential to change the way that we design and write OS X and iOS applications. \n\nFor those not familiar with the concept, Swift playgrounds are interactive documents where Swift code is compiled and run live as you type. Results of operations are presented in a step-by-step timeline as they execute, and variables can be logged and inspected at any point. Playgrounds can be created within an existing Xcode project or as standalone bundles that run by themselves.\n\nWhile a lot of attention has been focused on Swift playgrounds for their utility in learning this new language, you only need to look at similar projects like [IPython notebooks](http://ipython.org) to see the broader range of potential applications for interactive coding environments. IPython notebooks are being used today for tasks ranging from [scientific research](https://github.com/ipython/ipython/wiki/Research-at-UC-Berkeley-using-IPython) to [experimenting with machine vision](http://pyvideo.org/video/1796/simplecv-computer-vision-using-python). They're also being used to explore other language paradigms, such as [functional programming with Haskell](https://github.com/gibiansky/IHaskell).\n\nWe'll explore the use of Swift playgrounds for documentation, testing, and rapid prototyping. All Swift playgrounds used for this article can be [downloaded here](https://github.com/objcio/PersonalSwiftPlaygrounds).\n\n## Playgrounds for Documentation and Testing\n\nSwift is a brand new language, and many people are using playgrounds to understand its syntax and conventions. In addition to the language, we were provided with a new standard library. The functions in this standard library at present aren't documented particularly well, so resources like [the practicalswift.org list of standard library functions](http://webcache.googleusercontent.com/search?q=cache:vZ9kK9_J68oJ:practicalswift.com/2014/06/14/the-swift-standard-library-list-of-built-in-functions/+&cd=1&hl=de&ct=clnk&lr=lang_de%7Clang_en&client=safari) have sprung up.\n\nHowever, it's one thing to read about what a function should do and another to see it in action. In particular, many of these functions perform interesting actions on the new Swift collection classes, and it would be informative to examine how they act on these collections. \n\nPlaygrounds provide a great opportunity to document functions and library interfaces by showing syntax and live execution against real data sets. For the case of the collection functions, we've created the [CollectionOperations.playground](https://github.com/objcio/PersonalSwiftPlaygrounds), which contains a list of these functions, all run against sample data that can be changed live.\n\nAs a sample, we create our initial array using this:\n\n```objc\nlet testArray = [0, 1, 2, 3, 4]\n```\n\nWe then want to demonstrate the filter() function, so we write the following:\n\n```objc\nlet odds = testArray.filter{$0 % 2 == 1}\nodds\n```\n\nThe last line triggers the display of the array that results from this operation: `[1, 3]`. You get syntax, an example, and an illustration of how the function works, all in a live document.\n\nThis is effective for other Apple or third-party frameworks as well. For example, Scene Kit is an excellent framework that Apple provides for quickly building 3D scenes on Mac and iOS, and you might want to show someone how to get started with it. You could provide a sample application, but that requires a build and compile cycle to demonstrate. \n\nIn the [SceneKitMac.playground](https://github.com/objcio/PersonalSwiftPlaygrounds), we've built a fully functional 3D scene with an animating torus. You will need to show the Assistant Editor (View \\| Assistant Editor \\| Show Assistant Editor) to display the 3D view, which will automatically render and animate. This requires no compile cycle, and someone could play around with this to change colors, geometry, lighting, or anything else about the scene, and see it be reflected live. It documents and presents an interactive example for how to use this framework.\n\nIn addition to documenting functions and their operations, you'll also note that we can verify that a function still operates as it should by looking at the results it provides, or even whether it is still parsed properly when we load the playground. It's not hard to envision adding assertions and creating real unit tests within a playground. Taken one step further, tests could be created for desired conditions, leading to a style of test-driven development as you type.\n\nIn fact, in the [July 2014 issue of the PragPub magazine](http://www.swaine.com/pragpub/), Ron Jeffries has this to say in his article, \"Swift from a TDD Perspective\": \n\n> The Playground will almost certainly have an impact on how we TDD. I think we’ll go faster, by using Playground’s ability to show us quickly what we can do. But will we go *better*, with the same solid scaffolding of tests that we’re used to with TDD? Or will we take a hit in quality, either through defects or less refactoring?\n\nWhile the question about code quality is for others to answer, let's take a look at how playgrounds can speed up development through rapid prototyping.\n\n## Prototyping Accelerate — Optimized Signal Processing\n\nThe Accelerate framework contains powerful functions for parallel processing of large data sets. These functions take advantage of the vector-processing instructions present in modern CPUs, such as the SSE instruction set in Intel chips, or the NEON ones on ARM. However, for their power, their interfaces can seem opaque and documentation on their use is a little sparse. As a result, not as many developers take advantage of the libraries under Accelerate's umbrella.\n\nSwift presents opportunities to make it much easier to interact with Accelerate through function overloading and the creation of wrappers around the framework. Chris Liscio has been experimenting with this in [his SMUGMath library](https://github.com/liscio/SMUGMath-Swift), which acted as the inspiration for this prototype.\n\nLet's say you had a series of data samples that made up a sine wave, and you wanted to determine the frequency and amplitude of that sine wave. How would you do this? One way to find these values is by means of a Fourier transform, which can extract frequency and amplitude information from one or many overlapping sine waves. Accelerate provides a version of this, called a Fast Fourier Transform (FFT), for which a great explanation (with an IPython notebook) can be found [here](http://jakevdp.github.io/blog/2013/08/28/understanding-the-fft/).\n\nTo prototype this process, we'll be using the [AccelerateFunctions.playground](https://github.com/objcio/PersonalSwiftPlaygrounds), so you can follow along using that. Make sure you expose the Assistant Editor (View \\| Assistant Editor \\| Show Assistant Editor) to see the graphs generated at each stage.\n\nThe first thing to do is to generate some sample waveforms for us to experiment with. An easy way to do that is by the use of Swift's map() operator:\n\n```objc\nlet sineArraySize = 64\n\nlet frequency1 = 4.0\nlet phase1 = 0.0\nlet amplitude1 = 2.0\nlet sineWave = (0..<sineArraySize).map {\n    amplitude1 * sin(2.0 * M_PI / Double(sineArraySize) * Double($0) * frequency1 + phase1)\n}\n```\n\nFor later use in the FFT, our starting waveform array sizes need to be powers of two. Adjusting the `sineArraySize` to values like 32, 128, or 256 will vary the resolution of the graphs presented later, but it won't change the fundamental results of the calculations.\n\nTo plot our waveforms, we'll use the new XCPlayground framework (which needs to be imported first) and the following helper function:\n\n```objc\nfunc plotArrayInPlayground<T>(arrayToPlot:Array<T>, title:String) {\n    for currentValue in arrayToPlot {\n        XCPCaptureValue(title, currentValue)\n    }\n}\n```\n\nWhen we do this:\n\n```objc\nplotArrayInPlayground(sineWave, \"Sine wave 1\")\n```\n\nWe see a graph that looks like the following:\n\n![](/images/issue-16/Sine1.png)\n\nThat's a sine wave with a frequency of 4.0, amplitude of 2.0, and phase of 0.0. Let's make this more interesting by creating a second sine wave to add to the first, this time of frequency 1.0, amplitude 1.0, and a phase of pi / 2.0:\n\n```objc\nlet frequency2 = 1.0\nlet phase2 = M_PI / 2.0\nlet amplitude2 = 1.0\nlet sineWave2 = (0..<sineArraySize).map {\n    amplitude2 * sin(2.0 * M_PI / Double(sineArraySize) * Double($0) * frequency2 + phase2)\n}\n```\n\n![](/images/issue-16/Sine2.png)\n\nNow we want to combine them. This is where Accelerate starts to help us. Adding two arrays of independent floating-point values is well-suited to parallel processing. Accelerate's vDSP library has functions for just this sort of thing, so let's put them to use. For the fun of it, let's set up a Swift operator to use for this vector addition. Unfortunately, + is already used for array concatenation (perhaps confusingly so), and ++ is more appropriate as an increment operator, so we'll define a +++ operator for this vector addition:\n\n```objc\ninfix operator  +++ {}\nfunc +++ (a: [Double], b: [Double]) -> [Double] {\n    assert(a.count == b.count, \"Expected arrays of the same length, instead got arrays of two different lengths\")\n\n    var result = [Double](count:a.count, repeatedValue:0.0)\n    vDSP_vaddD(a, 1, b, 1, &result, 1, UInt(a.count))\n    return result\n}\n```\n\nThis sets up an operator that takes in two Swift arrays of `Double` values and outputs a single combined array from their element-by-element addition. Within the function, a blank result array is created at the size of our inputs (asserted to be the same for both inputs). Because Swift arrays of scalar values map directly to C arrays, we can just pass our input arrays of `Doubles` to the `vDSP_vaddD()` function and prefix our result array with `&`.\n\nTo verify that this is actually performing a correct addition, we can graph the results of our sine wave combination using a for loop and our Accelerate function:\n\n```objc\nvar combinedSineWave = [Double](count:sineArraySize, repeatedValue:0.0)\nfor currentIndex in 0..<sineArraySize {\n    combinedSineWave[currentIndex] = sineWave[currentIndex] + sineWave2[currentIndex]\n}\n\nlet combinedSineWave2 = sineWave +++ sineWave2\n\nplotArrayInPlayground(combinedSineWave, \"Combined wave (loop addition)\")\nplotArrayInPlayground(combinedSineWave2, \"Combined wave (Accelerate)\")\n```\n\n![](/images/issue-16/SineCombined.png)\n\nSure enough, they're the same.\n\nBefore moving on to the FFT itself, we will need another vector operation to work on the results from that calculation. All of the values provided from Accelerate's FFT implementation are squares, so we'll need to take their square root. We need to apply a function like `sqrt()` over each element in that array, so this sounds like another opportunity to use Accelerate.\n\nAccelerate's vecLib library has parallel equivalents of many mathematical functions, including square roots in the form of `vvsqrt()`. This is a great case for the use of function overloading, so let's create a version of `sqrt()` that works on arrays of `Double` values:\n\n```objc\nfunc sqrt(x: [Double]) -> [Double] {\n    var results = [Double](count:x.count, repeatedValue:0.0)\n    vvsqrt(&results, x, [Int32(x.count)])\n    return results\n}\n```\n\nLike with our addition operator, this takes in a `Double` array, creates a blank `Double` array for output values, and passes those directly into the `vvsqrt()` Accelerate function. We can verify that this works by typing the following into the playground:\n\n```objc\nsqrt(4.0)\nsqrt([4.0, 3.0, 16.0])\n```\n\nYou'll see that the standard `sqrt()` function returns 2.0, and our new overload gives back [2.0, 1.73205080756888, 4.0]. In fact, this is such an easy-to-use overload, you can imagine repeating this for all the vecLib functions to create parallel versions of the math functions (and Mattt Thompson [has done just that](https://github.com/mattt/Surge)). For a 100000000-element array on a 15\" mid-2012 i7 MacBook Pro, our Accelerate-based `sqrt()` runs nearly twice as fast as a simple array iteration using the normal scalar `sqrt()`.\n\nWith that done, let's implement the FFT. We're not going to go into extensive detail on the setup of this, but this is our FFT function:\n\n```objc\nlet fft_weights: FFTSetupD = vDSP_create_fftsetupD(vDSP_Length(log2(Float(sineArraySize))), FFTRadix(kFFTRadix2))\n\nfunc fft(var inputArray:[Double]) -> [Double] {\n    var fftMagnitudes = [Double](count:inputArray.count, repeatedValue:0.0)\n    var zeroArray = [Double](count:inputArray.count, repeatedValue:0.0)\n    var splitComplexInput = DSPDoubleSplitComplex(realp: &inputArray, imagp: &zeroArray)\n    \n    vDSP_fft_zipD(fft_weights, &splitComplexInput, 1, vDSP_Length(log2(CDouble(inputArray.count))), FFTDirection(FFT_FORWARD));\n    vDSP_zvmagsD(&splitComplexInput, 1, &fftMagnitudes, 1, vDSP_Length(inputArray.count));\n\n    let roots = sqrt(fftMagnitudes) // vDSP_zvmagsD returns squares of the FFT magnitudes, so take the root here\n    var normalizedValues = [Double](count:inputArray.count, repeatedValue:0.0)\n\n    vDSP_vsmulD(roots, vDSP_Stride(1), [2.0 / Double(inputArray.count)], &normalizedValues, vDSP_Stride(1), vDSP_Length(inputArray.count))\n    return normalizedValues\n}\n```\n\nAs a first step, we set up the FFT weights that need to be used for a calculation of the array size we're working with. These weights are used later on in the actual FFT calculation, but can be calculated via `vDSP_create_fftsetupD()` and reused for arrays of a given size. Since this array size remains constant in this document, we calculate the weights once as a global variable and reuse them in each FFT.\n\nWithin the FFT function, the `fftMagnitudes` array is initialized with zeroes at the size of our waveform in preparation for it holding the results of the operation. An FFT operation takes complex numbers as input, but we only care about the real part of that, so we initialize `splitComplexInput` with the input array as the real components, and zeroes for the imaginary components. Then `vDSP_fft_zipD()` and `vDSP_zvmagsD()` perform the FFT and load the `fftMagnitudes` array with squares of the magnitudes from the FFT.\n\nAt this point, we use the previously mentioned Accelerate-based array `sqrt()` operation to take the square root of the squared magnitudes, returning the actual magnitudes, and then normalize the values based on the size of the input array.\n\nThe results from this entire operation look like this for the individual sine waves:\n\n![](/images/issue-16/FFT12.png)\n\nAnd they look like this for the combined sine wave:\n\n![](/images/issue-16/FFTCombined.png)\n\nAs a very simplified explanation of these values: The results represent 'bins' of sine wave frequencies, starting at the left, with the values in those bins corresponding to the amplitude of the wave detected at that frequency. They are symmetric about the center, so you can ignore the values on the right half of that graph.\n\nWhat you can observe is that for the frequency 4.0, amplitude 2.0 wave, we see a value of 2.0 binned in bin number 4 in the FFT. Likewise, for the frequency 1.0, amplitude 1.0 wave, we see a 1.0 value in bin number 1 of the FFT. The FFT of the combined sine waves, despite the complex shape of that resultant wave, clearly pulls out the amplitude and frequency of both component waves in their separate bins, as if the FFTs themselves were added.\n\nAgain, this is a simplification of the FFT operation, and there are shortcuts taken in the above FFT code, but the point is that we can easily explore even a complex signal processing operation using step-by-step creation of functions in a playground and testing each operation with immediate graphical feedback.\n\n## The Case for Rapid Prototyping Using Swift Playgrounds\n\nHopefully, these examples have demonstrated the utility of Swift playgrounds for experimentation with new libraries and concepts. \n\nAt each step in the last case study, we could glance over to the timeline to see graphs of our intermediate arrays as they were processed. That would take a good amount of effort to set up in a sample application and display in an interface of some kind. All of these graphs also update live, so you can go back and tweak a frequency or amplitude value for one of our waveforms and see it ripple through these processing steps. That shortens the development cycle and helps to provide a gut feel for how calculations like this behave.\n\nThis kind of interactive development with immediate feedback makes an excellent case for prototyping even complex algorithms in a playground before deployment in a full application.\n"
  },
  {
    "path": "2014-09-10-swift-classes-vs-structs.md",
    "content": "---\ntitle:  \"A Warm Welcome to Structs and Value Types\"\ncategory: \"16\"\ndate: \"2014-09-10 10:00:00\"\nauthor:\n  - name: Andy Matuschak\n    url: https://twitter.com/andy_matuschak\ntags: article\n---\n\n\nIf you've stuck to Objective-C — and languages like Ruby, Python, or JavaScript — the prominence of structs in Swift might seem especially alien. Classes are the traditional unit of structure in object-oriented languages. Indeed, in contrast to structs, Swift classes support implementation inheritance, (limited) reflection, deinitializers, and multiple owners.\n\nIf classes are so much more powerful than structs, why use structs? Well, it's exactly their limited scope that makes them such flexible building blocks. In this article, you'll learn how structs and other value types can radically improve your code's clarity, flexibility, and reliability.\n\n## Value Types and Reference Types\n\nA small distinction in behavior drives the architectural possibilities at play here: structs are *value types* and classes are *reference types*.\n\nInstances of value types are copied whenever they're assigned or used as a function argument. Numbers, strings, arrays, dictionaries, enums, tuples, and structs are value types. For example:\n\n```objc\nvar a = \"Hello\"\nvar b = a\nb.extend(\", world\")\nprintln(\"a: \\(a); b: \\(b)\") // a: Hello; b: Hello, world\n```\n\nInstances of reference types (chiefly: classes, functions) can have multiple owners. When assigning a reference to a new variable or passing it to a function, those locations all point to the same instance. This is the behavior you're used to with objects. For instance:\n\n```objc\nvar a = UIView()\nvar b = a\nb.alpha = 0.5\nprintln(\"a: \\(a.alpha); b: \\(b.alpha)\") // a: 0.5; b: 0.5\n```\n\nThe distinction between these two categories seems small, but the choice between values and references can have huge ramifications for your system's architecture.\n\n### Building Our Intuition\n\nNow that we understand the differences between how value and reference types *behave*, let's talk about the differences between how we might use them. For this discussion, we'll focus on objects as the exemplar reference types.\n\nWe reference objects in code the same way we reference objects in the real world. Books often use a real-world metaphor to teach people object-oriented programming: you can make a `Dog` class, then instantiate it to define `fido`. If you pass `fido` around to different parts of the system, they're all still talking about the same `fido`. That makes sense, since if you actually had a dog named Fido, whenever you would talk about him in conversation, you'd be transmitting his *name* — not the dog itself, whatever that would mean. You'd be relying on everyone else having some idea of who Fido is. When you use objects, you're passing 'names' of instances around the system.\n\nValues are like data. If you send someone a table of expenses, you're not sending that person a label that represents that information — you're sending *the information itself*. Without talking to anyone else, the listener could calculate a total, or write the expenses down to consult later. If the listener prints out the expenses and modifies them, that doesn't modify the table you still have.\n\nA value can be a number, perhaps representing a price, or a string — like a description. It could be a selection among options — an enum: was this expense for a dinner, for travel, or for materials? It could contain several other values in named positions, like the `CLLocationCoordinate2D` struct, which specifies a latitude and longitude. Or it could be a list of other values... and so on.\n\nFido might run around and bark on his own accord. He might have special behavior that makes him different from every other dog. He might have relationships established with others. You can't just swap Fido out for another dog — your kids could tell the difference! But the table of expenses exists in isolation. Those strings and numbers don't *do* anything. They aren't going to change out from under you. No matter how many different ways you write the \"6\" in the first column, it's still just a \"6.\"\n\nAnd that's what's so great about value types.\n\n## The Advantages of Value Types\n\nObjective-C and C had value types, but Swift allows you to use them in previously impractical scenarios. For instance, the generics system permits abstractions that handle value and reference types interchangeably: `Array` works equally well for `Int`s as for `UIView`s. Enums are vastly more expressive in Swift, since they can now carry values and specify methods. Structs can conform to protocols and specify methods.\n\nSwift's enhanced support for value types affords a tremendous opportunity: value types are an incredibly flexible tool for making your code simpler. You can use them to extract isolated, predictable components from fat classes. Value types enforce — or at least encourage — many properties that work together to create clarity by default.\n\nIn this section, I'll describe some of the properties that value types encourage. It's worth noting that you *can* make objects that have these properties, but the language provides no pressure to do that. If you see an object in some code, you have no reasonable expectation of these properties, whereas if you see a value type, you do. It's true that not *all* value types have these properties — we'll cover that shortly — but these are reasonable generalizations.\n\n### Value Types Want to Be Inert\n\nA value type does not, in general, *behave*. It is typically *inert*. It stores data and exposes methods that perform computations using that data. Some of those methods might cause the value type to mutate itself, but control flow is strictly controlled by the single owner of the instance.\n\nAnd that's great! It's much easier to reason about code that will only execute when directly invoked by a single owner.\n\nBy contrast, an object might register itself as a target of a timer. It might receive events from the system. These kinds of interactions require reference types' multiple-owner semantics. Because value types can only have a single owner and they don't have deinitializers, it's awkward to write value types that perform side effects on their own.\n\n### Value Types Want to Be Isolated\n\nA typical value type has no implicit dependencies on the behavior of any external components. Its interactions with its one owner are vastly easier to understand at a glance than a reference type's interactions with an unknowable number of owners. It is *isolated*.\n\nIf you're accessing a reference to a mutable instance, you have an implicit dependency on all its other owners: they could change it out from under you at any time.\n\n### Value Types Want to Be Interchangeable\n\nBecause a value type is copied every time it's assigned to a new variable, all of those copies are completely interchangeable.\n\nYou can safely store a value that's passed to you, then later use that value as if it were 'new.' No one can compare that instance with another using anything but the data contained within it. Interchangeability also means that it doesn't matter *how* a given value was constructed — as long as it compares equal via `==`, it's equivalent for all purposes.\n\nSo if you use value types to communicate between components in your system, you can readily shift around your graph of components. Do you have a view that paints a sequence of touch samples? You can compensate for touch latency without touching the view's code by making a component that consumes a sequence of touch samples, appends an estimate of where the user's finger will move based on previous samples, and returns a new sequence. You can confidently give your new component's output to the view — it can't tell the difference.\n\nThere's no need for a fancy mocking framework to write unit tests that deal with value types. You can directly construct values indistinguishable from the 'live' instances flowing through your app. The touch-predicting component described above is easy to unit test: predictable value types in; predictable value types out; no side effects.\n\nThis is a huge advantage. In a traditional architecture of objects that behave, you have to test the interactions between the object you're testing and the rest of the system. That typically means awkward mocking or extensive setup code establishing those relationships. Value types want to be isolated, inert, and interchangeable, so you can directly construct a value, call a method, and examine the output. Simpler tests with greater coverage mean code that's easier to change.\n\n### Not All Value Types Have These Properties\n\nWhile the structure of value types encourages these properties, you can certainly make value types that violate them.\n\nValue types containing code that executes without being called by its owner are often unpredictable and should generally be avoided. For example: a struct initializer might call `dispatch_after` to schedule some work. But passing an instance of this struct to a function would duplicate the scheduled effect, inexplicitly, since a copy would be made. Value types should be inert.\n\nValue types containing references are not necessarily isolated and should generally be avoided: they carry a dependency on all other owners of that referent. These value types are also not readily interchangeable, since that external reference might be connected to the rest of your system in some complex way.\n\n## The Object of Objects\n\nI am emphatically not suggesting that we build everything out of inert values.\n\nObjects are useful precisely because they do *not* have the properties I described above. An object is an acting entity in the system. It has identity. It can *behave*, often independently.\n\nThat behavior is often complex and difficult to reason about, but some of the details can usually be represented by simple values and isolated functions involving those values. Those details don't need to be entangled with the complex behavior of the object. By separating them, the behavior of the object becomes clearer itself.\n\nThink of objects as a thin, imperative layer above the predictable, pure value layer.\n\nObjects maintain state, defined by values, but those values can be considered and manipulated independently of the object. The value layer doesn't really have state; it just represents and transmutes data. That data may or may not have higher-level meaning as state, depending on the context in which the value's used.\n\nObjects perform side effects like I/O and networking, but data, computations, and non-trivial decisions ultimately driving those side effects all exist at the value layer. The objects are like the membrane, channeling those pure, predictable results into the impure realm of side effects.\n\nObjects can communicate with other objects, but they generally send values, not references, unless they truly intend to create a persistent connection at the outer, imperative layer.\n\n## A Summarizing Pitch for Value Types\n\nValue types enable you to make typical architectures significantly clearer, simpler, and more testable.\n\nValue types typically have fewer or no dependencies on outside state, so there's less you have to consider when reasoning about them.\n\nValue types are inherently more composable and reusable because they're interchangeable.\n\nFinally, a value layer allows you to isolate the active, behaving elements from the inert business logic of your application. As you make more code inert, your system will become easier to test and change over time.\n\n## References\n\n* [Boundaries](https://www.destroyallsoftware.com/talks/boundaries), by Gary Bernhardt, proposes a similar two-level architecture and elaborates on its benefits for concurrency and testing.\n* [Are We There Yet?](http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey), by Rich Hickey, elaborates on the distinctions between value, state, and identity.\n* [The Structure and Interpretation of Computer Programs](http://mitpress.mit.edu/sicp/), by Hal Abelson and Gerald Sussman, illustrates just how much can be represented with simple values.\n"
  },
  {
    "path": "2014-09-10-swift-functions.md",
    "content": "---\ntitle:  \"The Many Faces of Swift Functions\"\ncategory: \"16\"\ndate: \"2014-09-10 09:00:00\"\nauthor:\n  - name: Natasha Murashev\n    url: https://twitter.com/NatashaTheRobot\ntags: article\n---\n\n\nAlthough Objective-C has some strange-looking syntax compared to other programming languages, the method syntax is pretty straightforward once you get the hang of it. Here is a quick throwback:  \n\n```objectivec\n\n+ (void)mySimpleMethod\n{\n    // class method\n    // no parameters \n    // no return values\n}\n\n- (NSString *)myMethodNameWithParameter1:(NSString *)param1 parameter2:(NSNumber *)param2\n{\n    // instance method\n    // one parameter of type NSString pointer, one parameter of type NSNumber pointer\n    // must return a value of type NSString pointer\n    return @\"hello, world!\";\n}\n```\n\nIn contrast, while Swift syntax looks a lot more like other programming languages, it can also get a lot more complicated and confusing than Objective-C. \n\nBefore I continue, I want to clarify the difference between a Swift *method* and *function*, as I'll be using both terms throughout this article. Here is the definition of methods, according to Apple's [Swift Programming Language Book](https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Methods.html):\n\n> Methods are functions that are associated with a particular type. Classes, structures, and enumerations can all define instance methods, which encapsulate specific tasks and functionality for working with an instance of a given type. Classes, structures, and enumerations can also define type methods, which are associated with the type itself. Type methods are similar to class methods in Objective-C.\n\nTL;DR: Functions are standalone, while methods are functions that are encapsulated in a class, struct, or enum. \n\n## Anatomy of Swift Functions\n\nLet's start with a simple \"Hello, World!\" Swift function: \n\n```swift\nfunc mySimpleFunction() {\n    println(\"hello, world!\")\n}\n```\n\nIf you've ever programmed in any other language aside from Objective-C, the above function should look very familiar. \n\n* The `func` keyword denotes that this is a function.\n* The name of this function is `mySimpleFunction`.\n* There are no parameters passed in — hence the empty `( )`.\n* There is no return value.\n* The function execution happens between the `{ }`.\n\nNow on to a slightly more complex function: \n\n```swift\nfunc myFunctionName(param1: String, param2: Int) -> String {\n    return \"hello, world!\"\n}\n```\n\nThis function takes in one parameter named `param1` of type `String` and one parameter named `param2` of type `Int` and returns a `String` value. \n\n## Calling All Functions\n\nOne of the big differences between Swift and Objective-C is how parameters work when a Swift function is called. If you love the verbosity of Objective-C, like I do, keep in mind that parameter names are not included externally by default when a Swift function is called: \n\n```swift\nfunc hello(name: String) {\n    println(\"hello \\(name)\")\n}\n\nhello(\"Mr. Roboto\")\n```\n\nThis might not seem so bad until you add a few more parameters to your function: \n\n```swift\nfunc hello(name: String, age: Int, location: String) {\n    println(\"Hello \\(name). I live in \\(location) too. When is your \\(age + 1)th birthday?\")\n}\n\nhello(\"Mr. Roboto\", 5, \"San Francisco\")\n```\n\nFrom reading only `hello(\"Mr. Roboto\", 5, \"San Francisco\")`, you would have a hard time knowing what each parameter actually is. \n\nIn Swift, there is a concept of an *external parameter name* to clarify this confusion: \n\n```swift\nfunc hello(fromName name: String) {\n    println(\"\\(name) says hello to you!\")\n}\n\nhello(fromName: \"Mr. Roboto\")\n```\n\nIn the above function, `fromName` is an external parameter, which gets included when the function is called, while `name` is the internal parameter used to reference the parameter inside the function execution. \n\nIf you want the external and internal parameter names to be the same, you don't have to write out the parameter name twice: \n\n```swift\nfunc hello(name name: String) {\n    println(\"hello \\(name)\")\n}\n\nhello(name: \"Robot\")\n```\n\nInstead, just add a `#` in front of the parameter name as a shortcut: \n\n```swift\nfunc hello(#name: String) {\n    println(\"hello \\(name)\")\n}\n\nhello(name: \"Robot\")\n```\n\nAnd of course, the rules for how parameters work are slightly different for methods...\n\n## Calling on Methods\n\nWhen encapsulated in a class (or struct or enum), the first parameter name of a method is *not* included externally, while all following parameter names are included externally when the method is called:\n\n```swift\nclass MyFunClass {\n    \n    func hello(name: String, age: Int, location: String) {\n        println(\"Hello \\(name). I live in \\(location) too. When is your \\(age + 1)th birthday?\")\n    }\n    \n}\n\nlet myFunClass = MyFunClass()\nmyFunClass.hello(\"Mr. Roboto\", age: 5, location: \"San Francisco\")\n```\n\nIt is therefore best practice to include your first parameter name in your method name, just like in Objective-C: \n\n```swift\nclass MyFunClass {\n    \n    func helloWithName(name: String, age: Int, location: String) {\n        println(\"Hello \\(name). I live in \\(location) too. When is your \\(age + 1)th birthday?\")\n    }\n    \n}\n\nlet myFunClass = MyFunClass()\nmyFunClass.helloWithName(\"Mr. Roboto\", age: 5, location: \"San Francisco\")\n```\n\nInstead of calling my function \"hello,\" I renamed it to `helloWithName` to make it very clear that the first parameter is a name. \n\nIf for some special reason you want to skip the external parameter names in your function (I'd recommend having a very good reason for doing so), use an `_` for the external parameter name: \n\n```swift\nclass MyFunClass {\n    \n    func helloWithName(name: String, _ age: Int, _ location: String) {\n        println(\"Hello \\(name). I live in \\(location) too. When is your \\(age + 1)th birthday?\")\n    }\n    \n}\n\nlet myFunClass = MyFunClass()\nmyFunClass.helloWithName(\"Mr. Roboto\", 5, \"San Francisco\")\n```\n\n### Instance Methods Are Curried Functions\n\nOne cool thing to note is that [instance methods are actually curried functions in Swift](http://oleb.net/blog/2014/07/swift-instance-methods-curried-functions/):\n\n> The basic idea behind currying is that a function can be partially applied, meaning that some of its parameter values can be specified (bound) before the function is called. Partial function application yields a new function.\n\nSo given that I have a class: \n\n```swift\nclass MyHelloWorldClass {\n    \n    func helloWithName(name: String) -> String {\n        return \"hello, \\(name)\"\n    }\n}\n```\n\nI can create a variable that points to the class's `helloWithName` function: \n\n```swift\nlet helloWithNameFunc = MyHelloWorldClass.helloWithName\n// MyHelloWorldClass -> (String) -> String\n``` \nMy new `helloWithNameFunc` is of type `MyHelloWorldClass -> (String) -> String`, a function that takes in an instance of my class and returns another function that takes in a string value and returns a string value. \n\nSo I can actually call my function like this: \n\n```swift\nlet myHelloWorldClassInstance = MyHelloWorldClass()\n\nhelloWithNameFunc(myHelloWorldClassInstance)(\"Mr. Roboto\") \n// hello, Mr. Roboto\n```\n\n## Init: A Special Note\nA special `init` method is called when a class, struct, or enum is initialized. In Swift, you can define initialization parameters, just like with any other method: \n\n```swift\nclass Person {\n    \n    init(name: String) {\n        // your init implementation\n    }\n    \n}\n\nPerson(name: \"Mr. Roboto\")\n```\n\nNotice that, unlike other methods, the first parameter name of an init method is required externally when the class is instantiated. \n\nIt is best practice in most cases to add a different external parameter name — `fromName` in this case — to make the initialization more readable: \n\n```swift\nclass Person {\n    \n    init(fromName name: String) {\n        // your init implementation\n    }\n    \n}\n\nPerson(fromName: \"Mr. Roboto\")\n```\n\nAnd of course, just like with other methods, you can add an `_` if you want your init method to skip the external parameter name. I love the readability and power of this initialization example from the [Swift Programming Language Book](https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-XID_306): \n\n```swift\nstruct Celsius {\n    var temperatureInCelsius: Double\n    init(fromFahrenheit fahrenheit: Double) {\n        temperatureInCelsius = (fahrenheit - 32.0) / 1.8\n    }\n    init(fromKelvin kelvin: Double) {\n        temperatureInCelsius = kelvin - 273.15\n    }\n    init(_ celsius: Double) {\n        temperatureInCelsius = celsius\n    }\n}\n\nlet boilingPointOfWater = Celsius(fromFahrenheit: 212.0)\n// boilingPointOfWater.temperatureInCelsius is 100.0\n\nlet freezingPointOfWater = Celsius(fromKelvin: 273.15)\n// freezingPointOfWater.temperatureInCelsius is 0.0\n\nlet bodyTemperature = Celsius(37.0)\n// bodyTemperature.temperatureInCelsius is 37.0\n```\n\nSkipping the external parameter can also be useful if you want to abstract how your class / enum / struct gets initialized. I love the use of this in [David Owen's](https://twitter.com/owensd) [json-swift library](https://github.com/owensd/json-swift/blob/master/src/JSValue.swift): \n\n```swift\npublic struct JSValue : Equatable {\n    \n    // ... truncated code\n\n    /// Initializes a new `JSValue` with a `JSArrayType` value.\n    public init(_ value: JSArrayType) {\n        self.value = JSBackingValue.JSArray(value)\n    }\n\n    /// Initializes a new `JSValue` with a `JSObjectType` value.\n    public init(_ value: JSObjectType) {\n        self.value = JSBackingValue.JSObject(value)\n    }\n\n    /// Initializes a new `JSValue` with a `JSStringType` value.\n    public init(_ value: JSStringType) {\n        self.value = JSBackingValue.JSString(value)\n    }\n\n    /// Initializes a new `JSValue` with a `JSNumberType` value.\n    public init(_ value: JSNumberType) {\n        self.value = JSBackingValue.JSNumber(value)\n    }\n\n    /// Initializes a new `JSValue` with a `JSBoolType` value.\n    public init(_ value: JSBoolType) {\n        self.value = JSBackingValue.JSBool(value)\n    }\n\n    /// Initializes a new `JSValue` with an `Error` value.\n    init(_ error: Error) {\n        self.value = JSBackingValue.Invalid(error)\n    }\n\n    /// Initializes a new `JSValue` with a `JSBackingValue` value.\n    init(_ value: JSBackingValue) {\n        self.value = value\n    }\n}\n```\n\n## Fancy Parameters\nCompared to Objective-C, Swift has a lot of extra options for what type of parameters can be passed in. Here are some examples. \n\n### Optional Parameter Types\n\nIn Swift, there is a new concept of [optional types](https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html): \n\n> Optionals say either “there is a value, and it equals x” or “there isn’t a value at all.” Optionals are similar to using nil with pointers in Objective-C, but they work for any type, not just classes. Optionals are safer and more expressive than nil pointers in Objective-C and are at the heart of many of Swift’s most powerful features.\n\nTo indicate that a parameter type is optional (can be nil), just add a question mark after the type specification: \n\n```swift\nfunc myFuncWithOptionalType(parameter: String?) {\n    // function execution\n}\n\nmyFuncWithOptionalType(\"someString\")\nmyFuncWithOptionalType(nil)\n```\n\nWhen working with optionals, don't forget to unwrap!\n\n```swift\nfunc myFuncWithOptionalType(optionalParameter: String?) {\n    if let unwrappedOptional = optionalParameter {\n        println(\"The optional has a value! It's \\(unwrappedOptional)\")\n    } else {\n        println(\"The optional is nil!\")\n    }\n}\n\nmyFuncWithOptionalType(\"someString\")\n// The optional has a value! It's someString\n\nmyFuncWithOptionalType(nil)\n// The optional is nil\n```\n\nComing from Objective-C, getting used to working with optionals definitely takes some time!\n\n### Parameters with Default Values\n```swift\nfunc hello(name: String = \"you\") {\n    println(\"hello, \\(name)\")\n}\n\nhello(name: \"Mr. Roboto\")\n// hello, Mr. Roboto\n\nhello()\n// hello, you\n```\n\nNote that a parameter with a default value automatically has an external parameter name. \n\nAnd since parameters with a default value can be skipped when the function is called, it is best practice to put all your parameters with default values at the end of a function's parameter list. Here is a note from the [Swift Programming Language Book](https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html) on the topic:  \n\n> Place parameters with default values at the end of a function’s parameter list. This ensures that all calls to the function use the same order for their non-default arguments, and makes it clear that the same function is being called in each case.\n\nI'm a huge fan of default parameters, mostly because it makes code easy to change and backward compatible. You might start out with two parameters for your specific use case at the time, such as a function to configure a custom `UITableViewCell,` and if another use case comes up that requires another parameter (such as a different text color for your cell's label), just add a new parameter with a default value — all the other places where this function has already been called will be fine, and the new part of your code that needs the parameter can just pass in the non-default value!\n\n### Variadic Parameters \n\nVariadic parameters are simply a more readable version of passing in an array of elements. In fact, if you were to look at the type of the internal parameter names in the below example, you'd see that it is of type `[String]` (array of strings): \n\n```swift\nfunc helloWithNames(names: String...) {\n    for name in names {\n        println(\"Hello, \\(name)\")\n    }\n}\n\n// 2 names\nhelloWithNames(\"Mr. Robot\", \"Mr. Potato\")\n// Hello, Mr. Robot\n// Hello, Mr. Potato\n\n// 4 names\nhelloWithNames(\"Batman\", \"Superman\", \"Wonder Woman\", \"Catwoman\")\n// Hello, Batman\n// Hello, Superman\n// Hello, Wonder Woman\n// Hello, Catwoman\n```\n\nThe catch here is to remember that it is possible to pass in 0 values, just like it is possible to pass in an empty array, so don't forget to check for the empty array if needed:\n\n```swift\nfunc helloWithNames(names: String...) {\n    if names.count > 0 {\n        for name in names {\n            println(\"Hello, \\(name)\")\n        }\n    } else {\n        println(\"Nobody here!\")\n    }\n}\n\nhelloWithNames()\n// Nobody here!\n```\n\nAnother note about variadic parameters: the variadic parameter must be the *last* parameter in your function's parameter list! \n\n### Inout Parameters\n\nWith inout parameters, you have the ability to manipulate external variables (aka pass by reference):\n\n```swift\nvar name1 = \"Mr. Potato\"\nvar name2 = \"Mr. Roboto\"\n\nfunc nameSwap(inout name1: String, inout name2: String) {\n    let oldName1 = name1\n    name1 = name2\n    name2 = oldName1\n}\n\nnameSwap(&name1, &name2)\n\nname1\n// Mr. Roboto\n\nname2\n// Mr. Potato\n``` \n\nThis is a very common pattern in Objective-C for handling error scenarios. `NSJSONSerialization` is just one example: \n\n```objectivec\n- (void)parseJSONData:(NSData *)jsonData\n{\n    NSError *error = nil;\n    id jsonResult = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];\n    \n    if (!jsonResult) {\n        NSLog(@\"ERROR: %@\", error.description);\n    }\n}\n```\n\nSince Swift is so new, there aren't clear conventions on handling errors just yet, but there are definitely a lot of options beyond inout parameters! Take a look at David Owen's recent blog post on [error handling in Swift](http://owensd.io/2014/08/22/error-handling-take-two.html). More on this topic should also be covered in [Functional Programming in Swift](/books/). \n\n### Generic Parameter Types\n\nI'm not going to get too much into generics in this post, but here is a very simple example for how you can make a function accept parameters of different types while making sure that both parameters are of the same type:\n\n```swift\nfunc valueSwap<T>(inout value1: T, inout value2: T) {\n    let oldValue1 = value1\n    value1 = value2\n    value2 = oldValue1\n}\n\nvar name1 = \"Mr. Potato\"\nvar name2 = \"Mr. Roboto\"\n\nvalueSwap(&name1, &name2)\n\nname1 // Mr. Roboto\nname2 // Mr. Potato\n\nvar number1 = 2\nvar number2 = 5\n\nvalueSwap(&number1, &number2)\n\nnumber1 // 5\nnumber2 // 2\n```\n\nFor a lot more information on generics, I recommend taking a look at the [generics section](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Generics.html) of the Swift Programming Language book. \n\n### Variable Parameters\n\nBy default, parameters that are passed into a function are constants, so they cannot be manipulated within the scope of the function. If you would like to change that behavior, just use the var keyword for your parameters:\n\n```swift\nvar name = \"Mr. Roboto\"\n\nfunc appendNumbersToName(var name: String, #maxNumber: Int) -> String {\n    for i in 0..<maxNumber {\n        name += String(i + 1)\n    }\n    return name\n}\n\nappendNumbersToName(name, maxNumber:5)\n// Mr. Robot12345\n\nname\n// Mr. Roboto\n```\n\nNote that this is different than an inout parameter — variable parameters do not change the external passed-in variable!\n\n### Functions as Parameters\n\nIn Swift, functions can be passed around just like variables. For example, a function can have another function passed in as a parameter:\n\n```swift\nfunc luckyNumberForName(name: String, #lotteryHandler: (String, Int) -> String) -> String {\n    let luckyNumber = Int(arc4random() % 100)\n    return lotteryHandler(name, luckyNumber)\n}\n\nfunc defaultLotteryHandler(name: String, luckyNumber: Int) -> String {\n    return \"\\(name), your lucky number is \\(luckyNumber)\"\n}\n\nluckyNumberForName(\"Mr. Roboto\", lotteryHandler: defaultLotteryHandler)\n// Mr. Roboto, your lucky number is 38\n```\n\nNote that only the function reference gets passed in — `defaultLotteryHandler` in this case. The function gets executed later as decided by the receiving function. \n\nInstance methods can also be passed in a similar way: \n\n```swift\nfunc luckyNumberForName(name: String, #lotteryHandler: (String, Int) -> String) -> String {\n    let luckyNumber = Int(arc4random() % 100)\n    return lotteryHandler(name, luckyNumber)\n}\n\nclass FunLottery {\n    \n    func defaultLotteryHandler(name: String, luckyNumber: Int) -> String {\n        return \"\\(name), your lucky number is \\(luckyNumber)\"\n    }\n    \n}\n\nlet funLottery = FunLottery()\nluckyNumberForName(\"Mr. Roboto\", lotteryHandler: funLottery.defaultLotteryHandler)\n// Mr. Roboto, your lucky number is 38\n```\n\nTo make your function definition a bit more readable, consider type-aliasing your function (similar to typedef in Objective-C): \n\n```swift\ntypealias lotteryOutputHandler = (String, Int) -> String\n\nfunc luckyNumberForName(name: String, #lotteryHandler: lotteryOutputHandler) -> String {\n    let luckyNumber = Int(arc4random() % 100)\n    return lotteryHandler(name, luckyNumber)\n}\n```\n\nYou can also have a function without a name as a parameter type (similar to blocks in Objective-C): \n\n```swift\nfunc luckyNumberForName(name: String, #lotteryHandler: (String, Int) -> String) -> String {\n    let luckyNumber = Int(arc4random() % 100)\n    return lotteryHandler(name, luckyNumber)\n}\n\nluckyNumberForName(\"Mr. Roboto\", lotteryHandler: {name, number in\n    return \"\\(name)'s' lucky number is \\(number)\"\n})\n// Mr. Roboto's lucky number is 74\n```\n\nIn Objective-C, using blocks as parameters is popular for completion and error handlers in methods that execute an asynchronous operation. This should continue to be a popular pattern in Swift as well. \n\n\n## Access Controls\n\nSwift has three levels of [access controls](https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html): \n\n- **Public access** enables entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use public access when specifying the public interface to a framework.\n- **Internal access** enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure.\n- **Private access** restricts the use of an entity to its own defining source file. Use private access to hide the implementation details of a specific piece of functionality.\n\nBy default, every function and variable is internal — if you want to change that, you have to use the `private` or `public` keyword in front of every single method and variable: \n\n```swift\npublic func myPublicFunc() {\n    \n}\n\nfunc myInternalFunc() {\n    \n}\n\nprivate func myPrivateFunc() {\n    \n}\n\nprivate func myOtherPrivateFunc() {\n    \n}\n```\n\nComing from Ruby, I prefer to put all my private functions at the bottom of my class, separated by a landmark: \n\n```swift \nclass MyFunClass {\n    \n    func myInternalFunc() {\n        \n    }\n    \n    // MARK: Private Helper Methods\n    \n    private func myPrivateFunc() {\n        \n    }\n    \n    private func myOtherPrivateFunc() {\n        \n    }\n}\n```\n\nHopefully future releases of Swift will include an option to use one private keyword to indicate that all methods below it are private, similar to how access controls work in other programming languages.\n\n## Fancy Return Types\n\nIn Swift, function return types and values can get a bit more complex than we're used to in Objective-C, especially with the introduction of optionals and multiple return types. \n\n### Optional Return Types\n\nIf there is a possibility that your function could return a nil value, you need to specify the return type as optional: \n\n```swift\nfunc myFuncWithOptonalReturnType() -> String? {\n    let someNumber = arc4random() % 100\n    if someNumber > 50 {\n        return \"someString\"\n    } else {\n        return nil\n    }\n}\n\nmyFuncWithOptonalReturnType()\n```\n\nAnd of course, when you're using the optional return value, don't forget to unwrap:\n\n```swift\nlet optionalString = myFuncWithOptonalReturnType()\n\nif let someString = optionalString {\n    println(\"The function returned a value: \\(someString)\")\n} else {\n    println(\"The function returned nil\")\n}\n```\n\nThe best explanation I've seen of optionals is from a [tweet by @Kronusdark](https://twitter.com/Kronusdark/status/496444128490967041): \n\n> I finally get @SwiftLang optionals, they are like Schrödinger's cat! You have to see if the cat is alive before you use it.\n\n### Multiple Return Values\n\nOne of the most exciting features of Swift is the ability for a function to have multiple return values:\n\n```swift \nfunc findRangeFromNumbers(numbers: Int...) -> (min: Int, max: Int) {\n\n    var min = numbers[0]\n    var max = numbers[0]\n    \n    for number in numbers {\n        if number > max {\n            max = number\n        }\n        \n        if number < min {\n            min = number\n        }\n    }\n    \n    return (min, max)\n}\n\nfindRangeFromNumbers(1, 234, 555, 345, 423)\n// (1, 555)\n```\n\nAs you can see, the multiple return values are returned in a tuple, a very simple data structure of grouped values. There are two ways to use the multiple return values from the tuple: \n\n```swift\nlet range = findRangeFromNumbers(1, 234, 555, 345, 423)\nprintln(\"From numbers: 1, 234, 555, 345, 423. The min is \\(range.min). The max is \\(range.max).\")\n// From numbers: 1, 234, 555, 345, 423. The min is 1. The max is 555.\n\nlet (min, max) = findRangeFromNumbers(236, 8, 38, 937, 328)\nprintln(\"From numbers: 236, 8, 38, 937, 328. The min is \\(min). The max is \\(max)\")\n// From numbers: 236, 8, 38, 937, 328. The min is 8. The max is 937\n```  \n\n### Multiple Return Values and Optionals\n\nThe tricky part about multiple return values is when the return values can be optional, but there are two ways to handle dealing with optional multiple return values. \n\nIn the above example function, my logic is flawed — it is possible that no values could be passed in, so my program would actually crash if that ever happened. If no values are passed in, I might want to make my whole return value optional: \n\n```swift\nfunc findRangeFromNumbers(numbers: Int...) -> (min: Int, max: Int)? {\n\n    if numbers.count > 0 {\n        \n        var min = numbers[0]\n        var max = numbers[0]\n        \n        for number in numbers {\n            if number > max {\n                max = number\n            }\n            \n            if number < min {\n                min = number\n            }\n        }\n        \n        return (min, max)\n    } else {\n        return nil\n    }\n}\n\nif let range = findRangeFromNumbers() {\n    println(\"Max: \\(range.max). Min: \\(range.min)\")\n} else {\n    println(\"No numbers!\")\n}\n// No numbers!\n```\n\nIn other cases, it might make sense to make each return value within a tuple optional, instead of making the whole tuple optional: \n\n```swift\nfunc componentsFromUrlString(urlString: String) -> (host: String?, path: String?) {\n    let url = NSURL(string: urlString)\n    return (url.host, url.path)\n}\n``` \n\nIf you decide that some of your tuple values could be optionals, things become a little bit more difficult to unwrap, since you have to consider every single combination of optional values: \n\n```swift\nlet urlComponents = componentsFromUrlString(\"http://name.com/12345;param?foo=1&baa=2#fragment\")\n\nswitch (urlComponents.host, urlComponents.path) {\ncase let (.Some(host), .Some(path)):\n    println(\"This url consists of host \\(host) and path \\(path)\")\ncase let (.Some(host), .None):\n    println(\"This url only has a host \\(host)\")\ncase let (.None, .Some(path)):\n    println(\"This url only has path \\(path). Make sure to add a host!\")\ncase let (.None, .None):\n    println(\"This is not a url!\")\n}\n// This url consists of host name.com and path /12345\n```\n\nAs you can see, this is not your average Objective-C way of doing things!\n\n### Return a Function\n\nAny function can also return a function in Swift: \n\n```swift\nfunc myFuncThatReturnsAFunc() -> (Int) -> String {\n    return { number in\n        return \"The lucky number is \\(number)\"\n    }\n}\n\nlet returnedFunction = myFuncThatReturnsAFunc()\n\nreturnedFunction(5) // The lucky number is 5\n```\n\nTo make this more readable, you can of course use type-aliasing for your return function: \n\n```swift\ntypealias returnedFunctionType = (Int) -> String\n\nfunc myFuncThatReturnsAFunc() -> returnedFunctionType {\n    return { number in\n        return \"The lucky number is \\(number)\"\n    }\n}\n\nlet returnedFunction = myFuncThatReturnsAFunc()\n\nreturnedFunction(5) // The lucky number is 5\n```\n\n## Nested Functions\nAnd in case you haven't had enough of functions from this post, it's always good to know that in Swift you can have a function inside a function:\n\n```swift\nfunc myFunctionWithNumber(someNumber: Int) {\n\n    func increment(var someNumber: Int) -> Int {\n        return someNumber + 10\n    }\n    \n    let incrementedNumber = increment(someNumber)\n    println(\"The incremented number is \\(incrementedNumber)\")\n}\n\nmyFunctionWithNumber(5)\n// The incremented number is 15\n``` \n\n## @end\nSwift functions have a lot of options and a lot of power. As you start writing in Swift, remember: with great power comes great responsibility. Optimize for READABILITY over cleverness! \n\nSwift best practices haven't been fully established yet, and the language is still constantly changing, so get your code reviewed by friends and coworkers. I've found that people who've never seen Swift before sometimes teach me the most about my Swift code.\n\nHappy Swifting!\n"
  },
  {
    "path": "2014-10-10-editorial.md",
    "content": "---\ntitle:  \"Editorial\"\ncategory: \"17\"\ndate: \"2014-10-10 12:00:00\"\ntags: editorial\n---\n\nHi all,\n\nThis month's issue is about security. It's a comparatively small issue — we really would have liked to include more articles on this important subject, but September was a busy month for us and our contributors, and several articles didn't make it into this issue because of time constraints. So if you still have to catch up with older issues, this month is your chance!\n   \nNevertheless, we do have three great articles in this issue. First, Graham talks about [why security still matters today](/issues/17-security/why-security/). Next, Thomas goes to great lengths to shed some light on every iOS and OS X developer's greatest nightmare: [code signing](/issues/17-security/inside-code-signing/). Last but not least, Laurent walks us through the process of how to [validate App Store receipts](/issues/17-security/receipt-validation/).\n      \nAdditionally, we have something else for you this month. On the heels of our recent book release ([Functional Programming in Swift](/books)), we decided to create a new newsletter: [Functional Snippets](/snippets). Every week, we'll send you a small functional Swift code snippet for learning and inspiration!\n         \nWe don't want to clutter up the main objc.io mailing list with this, so please sign up [here](/snippets) if you want to receive this weekly dose of Swift.\n\n\nAll the best from Berlin,\n\nChris, Daniel, and Florian.\n"
  },
  {
    "path": "2014-10-10-inside-code-signing.md",
    "content": "---\ntitle: Inside Code Signing\ncategory: \"17\"\ndate: \"2014-10-10 10:00:00\"\nauthor:\n  - name: Thomas 'toto' Kollbach\n    url: https://thomas.kollba.ch/\ntags: article\n---\n\n> \"Users appreciate code signing.\"  \n>  – Apple Developer Library: [Code Signing Guide](https://developer.apple.com/library/mac/documentation/Security/Conceptual/CodeSigningGuide/Introduction/Introduction.html)\n\nThere are many nice APIs you might encounter while building apps for iOS or OS X. You could do [beautiful animations](/issues/1-view-controllers/), [test your app](/issues/15-testing) really well, or store your data safely in [Core Data](/issues/4-core-data). But at some point, you will encounter code signing and provisioning. And more often than not, this is when you will start cursing.\n\nIf you have ever developed an application for iOS, you have probably fought with code signing or device provisioning at some point. Even as an OS X developer, you cannot avoid signing your code anymore, at least not if you want to be on the Mac App Store or are part of the Developer ID program. \n\nMost of the time, code signing seems like a magical machine that is hard to understand. I will try to shed some light on this machine.\n\nWhile the process and many of the internals are wrapped inside the iOS system and SDK, we can get a glance by looking at the infrastructure used to sign the app, in addition to looking at how OS X code signing works. Since under the hood, iOS is very similar to OS X, one can figure out a lot by looking both places.\n\nThe APIs and technology for signing executable code on OS X appeared on Mac OS X Leopard 10.5 around the time the first iPhone was released. This seems no coincidence, since on the iPhone OS itself, code signing is even more crucial. The iPhone was one of the first mass-market computing platforms after game consoles that relied on code signing from the ground up; iOS simply runs no unsigned code unless the device is jailbroken. Jailbreaking basically disables all the code signing and sandboxing security infrastructure, which is a very dangerous thing to do.  \n\n## Certificates and Keys\n\nAs an iOS developer, chances are you have a certificate, a public key, and a private key on your development machine. These are at the core of the code signing infrastructure. Code signing, like SSL, relies on [public-key cryptography](https://en.wikipedia.org/wiki/Public-key_cryptography) based on the X.509 standard.  \n\nThe main utility to manage the X.509 infrastructure on OS X is the Keychain Access utility. In the \"My Certificates\" section of your development machine, you will find the certificates you have the private key for. To use a certificate for signing, you need the private key, so that your code signing certificates will show up in this list. If you have a private key for a certificate, you can unfold the certificate to show the private key:\n\n![iOS Developer Certificate in the OS X keychain](/images/issue-17/iphone-developer-keychain.png)\n\nTo export a certificate from your keychain with the private key, Control-Click the certificate and choose 'Export \"Your Certificate Name\"' and choose the Personal Information Exchange format (.p12). You will be asked to enter a password to protect this file. Once you are done, anyone who has the p12 file and the password will be able to install the certificate and private key into their own keychain. \n\nAnother way to quickly get a glance at the identities on your system that can be used for signing code is with the very versatile `security` command line tool:\n\n```\n$ security find-identity -v -p codesigning                       \n  1) 01C8E9712E9632E6D84EC533827B4478938A3B15 \"iPhone Developer: Thomas Kollbach (7TPNXN7G6K)\"\n```\n\nA certificate is — very broadly speaking — a public key combined with a lot of additional information that was itself signed by some authority (also called a Certificate Authority, or CA) to state that the information in the certificate is correct. In this case, the authority is Apple's authority for developer stuff, the Apple Worldwide Developer Relations CA. This signature expires at some point, which means that anybody checking the certificate will also have to have a clock that is set correctly. This is one of the reasons why setting your system clock back in time can wreak havoc on a lot of things on iOS. \n\n![iOS Developer Certificate in detail](/images/issue-17/ios-dev-certificate.png)\n\nFor iOS development, you usually have two certificates: one prefixed with `iPhone Developer`, and one prefixed with `iPhone Distribution`. The first one is the one you use to build apps for your devices, and the other one is used to submit apps. This fact is baked into certificates. If you open the certificate in Keychain Utility, you will see a lot of extension entries. Look for the last one, labeled `Apple Developer Certificate (Submission) `, or `Apple Developer Certificate (Development)`, depending on the type of certificate — iOS uses this extension to determine if your app runs in development mode or distribution mode, and based on this, which rules that apply.\n\nIn addition to the certificate with the signed public key in it, we also need the private key. This private key is what you use to sign the binaries with. Without the private key, you cannot use the certificate and public key to sign anything.  \n\nThe signing itself is performed by the `codesign` command line tool. If you compile an application with Xcode, it will be signed by calling `codesign` after building the application — and `codesign` is also the tool that gives you so many nice and helpful error messages. You set the code signing identity in the project settings: \n\n![Set up of the code signing identity in Xcode project settings](/images/issue-17/xcode-code-signing-idenity.png)\n\nNote that Xcode only lets you pick code signing identities in this setting if you have a public and private key in your keychain. So if you expect one to be there, but it isn't, the first thing to check is if you have the private key in your keychain next to your certificate. Here, you also see the division between the development and distribution profiles. If you want to debug an app, you need to sign it with a key pair for development. If you want to distribute it either to testers or the App Store, you need to sign it with a key pair for distribution.\n\nFor a long time, this was the only setting regarding code signing, short of turning it off. \n\nWith Xcode 6, the option of setting a provisioning profile appeared in the project settings. If you set a provisioning profile, you can only choose the key pair that has a public key embedded in the certificate of your provisioning profile, or you can have Xcode pick the correct one automatically. But more on that later; let's look at code signing first.\n\n\n## Anatomy of a Signed App\n\nThe signature for any signed executable is embedded inside the Mach-O binary file format, or in the extended file system attributes if it's a non-Mach-O executable, such as a shell script. This way, any executable binary on OS X and iOS can be signed: dynamic libraries, command line tools, and .app bundles. But it also means that the process of signing your program actually modifies the executable file to place the signature data inside the binary file. \n\nIf you have a certificate and its private key, it's simple to sign a binary by using the `codesign` tool. Let's sign `Example.app` with the identity listed above:\n\n`$ codesign -s 'iPhone Developer: Thomas Kollbach (7TPNXN7G6K)' Example.app`\n\nThis can be useful, for example, if you have an app bundle that you want to re-sign. For that, you have to add the `-f` flag, and `codesign` will replace an existing signature with the one you choose:\n\n`$ codesign -f -s 'iPhone Developer: Thomas Kollbach (7TPNXN7G6K)' Example.app`\n\nThe `codesign` tool also gives you information about the code signing status of an executable, something that can be especially helpful if things go wrong. \nFor example, `$ codesign -vv -d Example.app` will tell you a few things about the code signing status of `Example.app`:\n\n```\nExecutable=/Users/toto/Library/Developer/Xcode/DerivedData/Example-cfsbhbvmswdivqhekxfykvkpngkg/Build/Products/Debug-iphoneos/Example.app/Example\nIdentifier=ch.kollba.example\nFormat=bundle with Mach-O thin (arm64)\nCodeDirectory v=20200 size=26663 flags=0x0(none) hashes=1324+5 location=embedded\nSignature size=4336\nAuthority=iPhone Developer: Thomas Kollbach (7TPNXN7G6K)\nAuthority=Apple Worldwide Developer Relations Certification Authority\nAuthority=Apple Root CA\nSigned Time=29.09.2014 22:29:07\nInfo.plist entries=33\nTeamIdentifier=DZM8538E3E\nSealed Resources version=2 rules=4 files=120\nInternal requirements count=1 size=184\n```\n\nThe first thing you can look at is the three lines starting with `Authority`. These tell you which certificate it was that actually signed this app. In this case, it was my certificate, the `iPhone Developer: Thomas Kollbach (7TPNXN7G6K)` certificate, which in turn was signed by `Apple Worldwide Developer Relations Certification Authority`, which is signed by, you guessed it, the `Apple Root CA`.\n\nIt also tells you something about the code in `Format`: it's not just a bare executable, but a bundle that contains an `arm64` binary. As you can see from the `Executable` path, this is a debug build, so it's a `thin` binary. \n\nIncluded among a bit of other diagnostics information are two more interesting entries. `Identifier` is the bundle identifier I set in Xcode. `TeamIdentifier` identifies my team (this is what is used by the system to see that apps are published by the same developer). Note that iOS distribution certificates have this very identifier in their names as well, which is useful if you want to distinguish many certificates under the same name. \n\nNow the binary is signed with a certificate. This seals the application, much like a seal of wax sealed an envelope in the Middle Ages. So let's check if the seal is unbroken:\n\n```\n$ codesign --verify Example.app\n$ \n```\n\nThis, like any good UNIX tool, tells you the signature is OK by printing nothing. So let's break the seal by modifying the binary: \n\n```\n$ echo 'lol' >> Example.app/Example\n$ codesign --verify Example.app\nExample.app: main executable failed strict validation\n```\n\nSo code signing works as expected. Mess with the signed app and the seal is broken.\n\n### Bundles and Resources\n\nFor command line tools or scripts, a single executable file is signed, but iOS and OS X applications and frameworks are bundled together with the resources they need. These resources can include images or translation files, but also more critical application components such as XIB/NIB files, archives, or even certificates. Therefore, when signing a bundled application, the resources are signed as well. \n\nFor this purpose, the signing process creates a `_CodeSignature/CodeResources` file inside the bundle. This file is used to store the signature of all files in the bundle that are signed. You can take a look at the list for yourself, as this is just a property list file. \n\nIn addition to the list of files and their signatures, this property list contains a set of rules about which resources should be considered in code signing. With the release of OS X 10.10 DP 5 and 10.9.5, Apple changed the code signing format, especially regarding these resource rules. If you use the `codesign` tool on 10.9.5 or later, you will find four sections in the `CodeResources` file: two named `rules` and `files` for older versions, and two named `files2` and `rules2` for the new version 2 code signing. The main change is that now you cannot exclude resources from being signed. You used to be able to use a file called `ResourceRules.plist` inside of the signed bundle to specify files which should not be considered when checking if the seal of a bundle was broken. As of the version 2 code signing, this does not work anymore. All code and resources must be signed — no exceptions. With version 2, the rules only specify that executable bundles inside of a bundle, such as extensions, are signed bundles themselves, and should be checked individually.  \n\n\n## Entitlements and Provisioning \n\nUp to this point, we have assumed that all certificates are created equally, and that — if we have a valid certificate — code signing is validated against this. But of course this is not the only rule that is applied. The system always evaluates certain rules to see if your code is allowed to run. \n\nThese rules are not always the same in all cases. For example, Gatekeeper on OS X can be configured to apply a different policy when starting an application, which is done by changing the setting in the security preferences. Setting this to \"Trusted Developers & Mac App Store\" requires the apps to be signed by a certificate issued to either a Mac App Store developer for app distribution or a Developer ID certificate. This is controlled by a system tool called `spctl`, which manages the system's security assessment policy.\n\nOn iOS, however, the rules are different. Neither user nor developer can change them: you need an Apple developer or distribution certificate to run an app on iOS. \n\nBut even if you can run an app, there are restrictions on what your app can do. These restrictions are managed by the sandbox. It is important to realize the distinction between the sandbox and the code signing infrastructure. Code signing is used to ensure that the application actually contains only what it says on the box — nothing more and nothing less. The sandbox restricts access to system resources. Both systems work hand in hand, both can keep your code from running, and both can cause strange errors in Xcode. But in everyday development, the sandbox is what gets in your way more often than code signing. When it does, it is mostly due to a mechanism called entitlements.\n\n### Entitlements\n\nEntitlements specify which resources of the system an app is allowed to use, and under what conditions. Basically, it is a configuration list for the sandbox on what to allow and what to deny your application. \n\nEntitlements are specified in — you might have guessed it at this point — a plist format. Xcode provides them to the `codesign` command using the `--entitlements` option. The format looks like this:\n\n```xml\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n    <key>application-identifier</key>\n    <string>7TPNXN7G6K.ch.kollba.example</string>\n    <key>aps-environment</key>\n    <string>development</string>\n    <key>com.apple.developer.team-identifier</key>\n    <string>7TPNXN7G6K</string>\n    <key>com.apple.developer.ubiquity-container-identifiers</key>\n    <array>\n            <string>7TPNXN7G6K.ch.kollba.example</string>\n    </array>\n    <key>com.apple.developer.ubiquity-kvstore-identifier</key>\n    <string>7TPNXN7G6K.ch.kollba.example</string>\n    <key>com.apple.security.application-groups</key>\n    <array>\n            <string>group.ch.kollba.example</string>\n    </array>\n    <key>get-task-allow</key>\n    <true/>\n</dict>\n</plist>\n```\n\nThis is the XML generated by Xcode after clicking around in the `Capabilities` tab and enabling a few things. Xcode automatically generates an `.entitlements` file and adds entries to it, as needed. This file is also provided to the codesign tool when building this app, and is the reference on the entitlements your app requests. These entitlements should all be enabled in the developer center's App ID, and embedded in the provisioning profile, but more on that later. The entitlements file used when building the app can be set up in the *Code Signing Entitlements* build setting.\n\nI configured this application to use iCloud key-value storage (`com.apple.developer.ubiquity-kvstore-identifier`) and iCloud document storage (`com.apple.developer.ubiquity-container-identifiers`), added it to an App Group (e.g. for sharing data with extensions, `com.apple.security.application-groups`), and enabled push notifications (`aps-environment`). This is also a development build, so I want to attach the debugger, which means setting `get-task-allow` to `true` is required. In addition to that, it includes the app identifier, which is the bundle identifier prefixed by the team identifier, also listed separately. \n\nOf course, you cannot just claim entitlements as you wish. There are certain rules in place that determine if you can use a certain entitlement or not. For example, an app with `get-task-allow` set to `true` is only allowed to run if the app is signed with a development certificate. The same is true for the `aps-environment` that you are allowed to use.\n\nThe list of entitlements that are available varies betweens OS versions, so an exhaustive list is hard to come by. At least all of the capabilities mentioned in the [Adding Capabilities](https://developer.apple.com/library/mac/documentation/IDEs/Conceptual/AppDistributionGuide/AddingCapabilities/AddingCapabilities.html) section of the Xcode documentation require entitlements. \n\nThe specific entitlements will be embedded in the signature of an application. If you are having trouble, it can help to look at what the signature actually says about the entitlements: `$ codesign -d --entitlements - Example.app` will show an XML property list similar to the one above. You could use this to add it to a build script to ensure that the built app always contains the push notification entitlement, and is therefore able to register for push notifications. The more of Apple's infrastructure you use, the more important this gets. Since Xcode 6, the entitlements list you specify is also embedded in the app bundle as `Example.app.xcent`. As far as I can tell, this is used for providing more helpful error messages when provisioning errors occur.\n\n\n### Provisioning Profiles\n\nThere is one component of the code signing and sandbox machinery that binds signing, entitlements, and the sandbox together: provisioning profiles.\n\nEvery iOS developer has probably spent a lot of hours fixing the provisioning profiles setup, and this is where a lot of problems start.\n\nA provisioning profile is a container for the information needed by the operating system to decide if it can let your app run. This means that if there is trouble with your provisioning profiles, it can become really annoying to fix.  \n\nA provisioning profile is a collection of all the components needed to determine if a particular app can run on a particular device. Provisioning profiles are used to enable app debugging on development devices, and also for ad-hoc and enterprise distribution. Xcode will embed the provisioning profile you select in the project settings within the app. As mentioned before, this selection has only been possible since Xcode 6. With Xcode 5 and before, the profile was picked by Xcode based on the certificate you chose when signing the app. As you can have multiple provisioning profiles with the same certificate, this can be a non-deterministic process, so it's always a good idea to select your provisioning profile, now that the option exists. \n\n![Project settings for selecting the provisioning profile](/images/issue-17/xcode-provisioning-profile.png)\n\nSo let's have a closer look at a provisioning profile. If you are looking for a file to play with, look inside `~/Library/MobileDevices/Provisioning Profiles`, which is where Xcode keeps all the profiles downloaded from Apple's developer portal.\n\nA provisioning profile is — you might be surprised at this point — not a property list. It is a file encoded in the Cryptographic Message Syntax (or CMS for short, but that is a really bad search keyword), which you might have encountered if you've ever dealt with S/MIME mail or certificates. It is specified in detail by the Internet Engineering Task force in [RFC 3852](http://tools.ietf.org/html/rfc3852). \n\nUsing the CMS format to encode a provisioning profile allows the profile to be signed, so that it cannot be changed once it has been issued by Apple. This signature is not the same as the code signature of the app itself. Instead, it is signed directly by Apple after being generated by the developer portal.\n\nYou can read this format with some versions of OpenSSL, but not the one that ships with OS X. Luckily for us, the `security` command line utility supports decoding the CMS format. So let's have a look at a `.mobileprovision` file:\n\n`$ security cms -D -i example.mobileprovision`\n\nThis will output the contents of the signed message to standard output. If you follow along, you will, once again, see XML of a property list. \n\nThis property list is the actual provisioning profile that iOS uses to determine if your app can run on a particular device. A provisioning profile is identified by its `UUID`. This is the reference that Xcode uses when you select a particular provisioning profile in the build settings in Xcode. \n\nThe first key is to look at `DeveloperCertificates`, which is a list of all certificates that an app using this provisioning profile can be signed with. If you sign the app with a certificate not in this list, it will not run, no matter if the certificate used for signing is valid or not. The certificates are Base64 encoded and in PEM format (Privacy Enhanced Mail, [RFC 1848](http://tools.ietf.org/html/rfc1848)). To take a closer look at one, copy and paste the encoded text into a file, like this:\n\n```\n-----BEGIN CERTIFICATE-----\nMIIFnjCCBIagAwIBAgIIE/IgVItTuH4wDQYJKoZIhvcNAQEFBQAwgZYxCzA…\n-----END CERTIFICATE-----`\n```\n\nThen let OpenSSL do the hard work: `openssl x509 -text -in file.pem`.\n\nGoing further along the provisioning profile, you might notice that the key `Entitlements` contains the entitlements for your app, with the same keys as documented in the `Entitlements` section. \n\nThese are the entitlements as configured on the developer portal in the App ID section when downloading your provisioning profile. Ideally, they should be in sync with the ones Xcode adds when signing the app, but this can break. And when it does, it is one of the most annoying things to fix.\n\nFor example, if you add an iCloud key-value store entitlement (`com.apple.developer.ubiquity-kvstore-identifier`) in Xcode, but do not update, re-download, and reconfigure the provisioning profile, the provisioning profile states that you do not have this entitlement. If you want to use it, iOS will refuse to let you run your application. This is the reason why a profile will be shown as invalid when you edit the capabilities of your App ID on the developer portal. \n\nIf you are looking at a development certificate, you will also find a `ProvisionedDevices` key, which contains a list of all the devices you set up for this provisioning profile. Because the profile needs to be signed by Apple, you need to download a new one each time you add a device to the developer portal. \n\n## Conclusion\n\nThe code signing and provisioning machinery might be one of the most complex things an iOS developer has to deal with, short of coding. It's certainly a very different experience then just compiling and running your code like you would on a Mac or PC. \n\nWhile it helps to understand the components at work, it still can get very cumbersome to keep all settings and tools under control — especially when working in teams, passing around certificates and profiles can be a very unwieldy task. While Apple tried to improve things in the last few releases of Xcode, I'm not sure every change is an improvement for the better. It certainly is a big dent in any developer's productivity to deal with code signing. \n\nAlthough all of this effort is very tedious for the developer, it has made iOS arguably one of the most secure end-user computing platforms out there. If you keep an eye on the security-related news, each time there is a new Trojan or malware, such as the infamous [FinFisher](https://en.wikipedia.org/wiki/FinFisher) that claims to work on iOS, look at the fine print. I have yet to encounter iOS-targeted malware where it did not say \"requires jailbreak\" in the fine print. \n\nSo going through all this hassle isn't for naught.\n\n"
  },
  {
    "path": "2014-10-10-receipt-validation.md",
    "content": "---\ntitle:  \"Receipt Validation\"\ncategory: \"17\"\ndate: \"2014-10-10 09:00:00\"\nauthor:\n  - name: Laurent Etiemble\n    url: https://twitter.com/letiemble\ntags: article\n---\n\n## About Receipts\n\nReceipts were introduced alongside the release of the Mac App Store, as part of the OS X 10.6.6 update. While iOS has always provided server-side receipts for in-app purchases, it was only with iOS 7 that iOS and OS X began to share the same receipt format.\n\nA receipt is meant to be a trusted record of a purchase, along with any in-app purchases that the user has made — much like a paper receipt that you get when shopping in a store.\n\nHere are some key points about receipts:\n\n- A receipt is created and signed by Apple through the App Store.\n- A receipt is issued for a specific version of an application and a specific device.\n- A receipt is stored **locally** on the device.\n- A receipt is issued **each time** an installation or an update occurs.\n\t- When an application is installed, a receipt that matches the application and the device is issued.\n\t- When an application is updated, a receipt that matches the new version of the application is issued.\n- A receipt is issued **each time** a transaction occurs:\n\t- When an in-app purchase occurs, a receipt is issued so that it can be accessed to verify that purchase.\n\t- When previous transactions are restored, a receipt is issued so that it can be accessed to verify those purchases.\n\n\n## About Validation\n\nVerifying receipts is a mechanism that helps you to protect your revenue and enforce your business model directly in your application.\n\nYou may wonder why Apple hasn't provided a simple API to validate the receipt. For the sake of demonstration, imagine that such a method exists (for example, `[[NSBundle mainBundle] validateReceipt]`). An attacker would simply look for this selector inside the binary and patch the code to skip the call. Since every developer would use the same validation method, hacking would be too easy.\n\nInstead, Apple made the choice to use standard cryptography and encoding techniques, and to provide some help — in the form of [documentation][apple-receipt-validation] and [WWDC sessions][apple-wwdc-2014-305] — for implementing your own receipt validation code. However, this is not an easy process, and it requires a good understanding of cryptography and of a variety of secure coding techniques.\n\nOf course, there are several off-the-shelf implementations available (for example, [on GitHub][github-?-receipt-validation]), but they are often just reference implementations and suffer from the same problem outlined above if everybody uses them: it becomes very easy for attackers to crack the validation code. So it's important to develop a solution that is unique and secure enough to resist common attacks.\n\nIn the interest of full disclosure, I'm the author of [Receigen](http://receigen.etiemble.com), a Mac app used to generate secure and changing receipt validation code. In this article, we'll take a look at the mechanics and best practices of receipt validation.\n        \n\n## Anatomy of a Receipt\n\nLet's take a technical look at the receipt file. Its structure looks like this:\n\n![Receipt Structure](/images/issue-17/ReceiptStructure@2x.png \"Receipt Structure\")\n\nA receipt file consist of a signed [PKCS #7][rfc-2315] container that embeds a [DER][wikipedia-x690-der]-encoded [ASN.1][itu-t-x690] payload, a certificate chain, and a digital signature.\n\n- **The payload** is a set of attributes that contains the receipt information; each attribute contains a type, a version, and a value. Among the attribute values, you find the bundle identifier and the bundle version for which the receipt was issued.\n- **The certificate chain** is the set of certificates required to properly verify the signature digest — the leaf certificate is the certificate that has been used to sign the payload.\n- **The signature** is the encrypted digest of the payload. By checking this digest, you can verify that the payload has not been tampered with.\n\n\n### The Container\n\nThe receipt container is a PKCS #7 envelope, which is signed by Apple with a dedicated certificate. The container's signature guarantees the authenticity and the integrity of the encapsulated payload.\n\nTo verify the signature, two checks are needed:\n\n- The certificate chain is validated against the Apple Certificate Authority Root certificate — this is the **authenticity** check.\n- A signature is computed using the certificate chain and compared to the one found in the container — this is the **integrity** check.\n\n\n### The Payload\n\nThe ASN.1 payload is defined by the following structure:\n\n```asn1\nReceiptModule DEFINITIONS ::=\nBEGIN\n\nReceiptAttribute ::= SEQUENCE {\n\ttype    INTEGER,\n\tversion INTEGER,\n\tvalue   OCTET STRING\n}\n\nPayload ::= SET OF ReceiptAttribute\n\nEND\n```\n\nA receipt attribute has three fields:\n\n- **The type field** identifies each attribute by its type. Apple has published a [list of public attributes][apple-receipt-validation-fields] that can be used to extract information from the receipt. You may also find unlisted attributes while parsing a receipt, but it's best to simply ignore them (mostly because they are reserved by Apple for future use).\n- **The version field** is not used for now.\n- **The value field** contains the data as an array of bytes (even if its name may suggest it, this is _**not**_ a string).\n\nThe payload is encoded using DER ([Distinguished Encoding Rules][wikipedia-x690-der]). This kind of encoding provides an unequivocal and compact result for ASN.1 structures. DER uses a pattern of [type-length-value][wikipedia-type-length-value] triplets and byte constants for each type tag.\n\nTo better illustrate the concept, here are some concrete examples of DER-encoded content applied to a receipt. The figure below shows how a receipt module is encoded:\n\n- The first byte identifies an ASN.1 set.\n- The three following bytes encode the length of the set's content.\n- The contents of the set are the receipt attributes.\n\n![ASN.1 DER - Receipt Module](/images/issue-17/ASN.1-DER-Receipt@2x.png \"ASN.1 DER - Receipt Module\")\n\nThe next figure shows how a receipt's attributes are encoded:\n\n- The first byte identifies an ASN.1 sequence.\n- The second byte encodes the length of the sequence's content.\n- The content of the sequence is:\n    - The attribute's type encoded as an ASN.1 INTEGER (the first byte identifies an ASN.1 INTEGER, the second byte encodes its length, and the third byte contains the value).\n    - The attribute's version encoded as an ASN.1 INTEGER (the first byte identifies an ASN.1 INTEGER, the second byte encodes its length, and the third byte contains the value).\n    - The attribute's value encoded as an ASN.1 OCTET-STRING (the first byte identifies an ASN.1 OCTET-STRING, the second byte encodes its length, and the remaining bytes contain the data).\n\n![ASN.1 DER - Receipt's attribute](/images/issue-17/ASN.1-DER-Attribute-OCTETSTRING@2x.png \"ASN.1 DER - Receipt's attribute\")\n\nBy using an ASN.1 OCTET-STRING for the attribute's value, it is very easy to embed various values like UTF-8 strings, ASCII strings, or numbers. The attribute's value can also contain a receipt module in the case of in-app purchases. Some examples are shown in the figures below:\n\n![ASN.1 DER - Receipt's attribute containing an integer](/images/issue-17/ASN.1-DER-Attribute-INTEGER@2x.png \"ASN.1 DER - Receipt's attribute containing an integer\")\n\n![ASN.1 DER - Receipt's attribute containing an IA5 string](/images/issue-17/ASN.1-DER-Attribute-IA5STRING@2x.png \"ASN.1 DER - Receipt's attribute containing an IA5 string\")\n\n![ASN.1 DER - Receipt's attribute containing an UTF-8 string](/images/issue-17/ASN.1-DER-Attribute-UTF8STRING@2x.png \"ASN.1 DER - Receipt's attribute containing an UTF-8 string\")\n\n![ASN.1 DER - Receipt's attribute containing an In-App purchase set string](/images/issue-17/ASN.1-DER-Attribute-SET@2x.png \"ASN.1 DER - Receipt's attribute containing an In-App purchase set\")\n\n\n## Validating the Receipt\n\nThe steps to validate a receipt are as follows:\n\n- Locate the receipt. If no receipt is found, then the validation fails.\n- Verify the receipt authenticity and integrity. The receipt must be properly signed by Apple and must not be tampered with.\n- Parse the receipt to extract attributes such as the bundle identifier, the bundle version, etc.\n- Verify that the bundle identifier found inside the receipt matches the bundle identifier of the application. Do the same for the bundle version.\n- Compute the hash of the GUID of the device. The computed hash is based on device-specific information.\n- Check the expiration date of the receipt if the Volume Purchase Program is used.\n\n**NOTE:** The following sections describe how to perform the various steps of the validation.\nThe code snippets are meant to illustrate each step; do not consider them the only solutions.\n\n\n### Locating the Receipt\n\nThe location of the receipt differs between OS X and iOS, as shown in the following figure:\n\n![Receipt Locations](/images/issue-17/ReceiptLocation@2x.png \"Receipt Locations\")\n\nOn OS X, the receipt file is located inside the application bundle, under the `Contents/_MASReceipt` folder. On iOS, the receipt file is located in the application's data sandbox, under the `StoreKit` folder.\n\nOnce located, you must ensure that the receipt is present: If the receipt exists at the correct place, it can be loaded.\nIf the receipt does not exist, this is considered a validation failure.\n\nOn OS X 10.7 and later or iOS 7 and later, the code is straightforward:\n\n```objc\n// OS X 10.7 and later / iOS 7 and later\nNSBundle *mainBundle = [NSBundle mainBundle];\nNSURL *receiptURL = [mainBundle appStoreReceiptURL];\nNSError *receiptError;\nBOOL isPresent = [receiptURL checkResourceIsReachableAndReturnError:&receiptError];\nif (!isPresent) {\n    // Validation fails\n}\n```\n\nBut if you target OS X 10.6, the `appStoreReceiptURL` selector is not available, and you have to manually build the URL to the receipt:\n\n```objc\n// OS X 10.6 and later\nNSBundle *mainBundle = [NSBundle mainBundle];\nNSURL *bundleURL = [mainBundle bundleURL];\nNSURL *receiptURL = [bundleURL URLByAppendingPathComponent:@\"Contents/_MASReceipt/receipt\"];\nNSError *receiptError;\nBOOL isPresent = [receiptURL checkResourceIsReachableAndReturnError:&receiptError];\nif (!isPresent) {\n\t// Validation fails\n}\n```\n\n\n### Loading the Receipt\n\nLoading the receipt is pretty straightforward. Here is the code to load and parse the PKCS #7 envelope with [OpenSSL][openssl]:\n\n```objc\n// Load the receipt file\nNSData *receiptData = [NSData dataWithContentsOfURL:receiptURL];\n\n// Create a memory buffer to extract the PKCS #7 container\nBIO *receiptBIO = BIO_new(BIO_s_mem());\nBIO_write(receiptBIO, [receiptData bytes], (int) [receiptData length]);\nPKCS7 *receiptPKCS7 = d2i_PKCS7_bio(receiptBIO, NULL);\nif (!receiptPKCS7) {\n    // Validation fails\n}\n\n// Check that the container has a signature\nif (!PKCS7_type_is_signed(receiptPKCS7)) {\n    // Validation fails\n}\n\n// Check that the signed container has actual data\nif (!PKCS7_type_is_data(receiptPKCS7->d.sign->contents)) {\n    // Validation fails\n}\n```\n\n### Verifying the Receipt Signature\n\nOnce the receipt is loaded, the first thing to do is make sure that it is authentic and unaltered. Here is the code to check the PKCS #7 signature with [OpenSSL][openssl]:\n\n```objc\n// Load the Apple Root CA (downloaded from https://www.apple.com/certificateauthority/)\nNSURL *appleRootURL = [[NSBundle mainBundle] URLForResource:@\"AppleIncRootCertificate\" withExtension:@\"cer\"];\nNSData *appleRootData = [NSData dataWithContentsOfURL:appleRootURL];\nBIO *appleRootBIO = BIO_new(BIO_s_mem());\nBIO_write(appleRootBIO, (const void *) [appleRootData bytes], (int) [appleRootData length]);\nX509 *appleRootX509 = d2i_X509_bio(appleRootBIO, NULL);\n\n// Create a certificate store\nX509_STORE *store = X509_STORE_new();\nX509_STORE_add_cert(store, appleRootX509);\n\n// Be sure to load the digests before the verification\nOpenSSL_add_all_digests();\n\n// Check the signature\nint result = PKCS7_verify(receiptPKCS7, NULL, store, NULL, NULL, 0);\nif (result != 1) {\n    // Validation fails\n}\n```\n\n### Parsing the Receipt\n\nOnce the receipt envelope has been verified, it is time to parse the receipt's payload. Here's an example of how to decode the DER-encoded ASN.1 payload with [OpenSSL][openssl]:\n\n```objc\n// Get a pointer to the ASN.1 payload\nASN1_OCTET_STRING *octets = receiptPKCS7->d.sign->contents->d.data;\nconst unsigned char *ptr = octets->data;\nconst unsigned char *end = ptr + octets->length;\nconst unsigned char *str_ptr;\n\nint type = 0, str_type = 0;\nint xclass = 0, str_xclass = 0;\nlong length = 0, str_length = 0;\n\n// Store for the receipt information\nNSString *bundleIdString = nil;\nNSString *bundleVersionString = nil;\nNSData *bundleIdData = nil;\nNSData *hashData = nil;\nNSData *opaqueData = nil;\nNSDate *expirationDate = nil;\n\n// Date formatter to handle RFC 3339 dates in GMT time zone\nNSDateFormatter *formatter = [[NSDateFormatter alloc] init];\n[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@\"en_US_POSIX\"]];\n[formatter setDateFormat:@\"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'\"];\n[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];\n\n// Decode payload (a SET is expected)\nASN1_get_object(&ptr, &length, &type, &xclass, end - ptr);\nif (type != V_ASN1_SET) {\n    // Validation fails\n}\n\nwhile (ptr < end) {\n    ASN1_INTEGER *integer;\n    \n    // Parse the attribute sequence (a SEQUENCE is expected)\n    ASN1_get_object(&ptr, &length, &type, &xclass, end - ptr);\n    if (type != V_ASN1_SEQUENCE) {\n        // Validation fails\n    }\n    \n    const unsigned char *seq_end = ptr + length;\n    long attr_type = 0;\n    long attr_version = 0;\n    \n    // Parse the attribute type (an INTEGER is expected)\n    ASN1_get_object(&ptr, &length, &type, &xclass, end - ptr);\n    if (type != V_ASN1_INTEGER) {\n        // Validation fails\n    }\n    integer = c2i_ASN1_INTEGER(NULL, &ptr, length);\n    attr_type = ASN1_INTEGER_get(integer);\n    ASN1_INTEGER_free(integer);\n    \n    // Parse the attribute version (an INTEGER is expected)\n    ASN1_get_object(&ptr, &length, &type, &xclass, end - ptr);\n    if (type != V_ASN1_INTEGER) {\n        // Validation fails\n    }\n    integer = c2i_ASN1_INTEGER(NULL, &ptr, length);\n    attr_version = ASN1_INTEGER_get(integer);\n    ASN1_INTEGER_free(integer);\n    \n    // Check the attribute value (an OCTET STRING is expected)\n    ASN1_get_object(&ptr, &length, &type, &xclass, end - ptr);\n    if (type != V_ASN1_OCTET_STRING) {\n        // Validation fails\n    }\n    \n    switch (attr_type) {\n        case 2:\n            // Bundle identifier\n            str_ptr = ptr;\n            ASN1_get_object(&str_ptr, &str_length, &str_type, &str_xclass, seq_end - str_ptr);\n            if (str_type == V_ASN1_UTF8STRING) {\n                // We store both the decoded string and the raw data for later\n                // The raw is data will be used when computing the GUID hash\n                bundleIdString = [[NSString alloc] initWithBytes:str_ptr length:str_length encoding:NSUTF8StringEncoding];\n                bundleIdData = [[NSData alloc] initWithBytes:(const void *)ptr length:length];\n            }\n            break;\n            \n        case 3:\n            // Bundle version\n            str_ptr = ptr;\n            ASN1_get_object(&str_ptr, &str_length, &str_type, &str_xclass, seq_end - str_ptr);\n            if (str_type == V_ASN1_UTF8STRING) {\n                // We store the decoded string for later\n                bundleVersionString = [[NSString alloc] initWithBytes:str_ptr length:str_length encoding:NSUTF8StringEncoding];\n            }\n            break;\n            \n        case 4:\n            // Opaque value\n            opaqueData = [[NSData alloc] initWithBytes:(const void *)ptr length:length];\n            break;\n            \n        case 5:\n            // Computed GUID (SHA-1 Hash)\n            hashData = [[NSData alloc] initWithBytes:(const void *)ptr length:length];\n            break;\n            \n        case 21:\n            // Expiration date\n            str_ptr = ptr;\n            ASN1_get_object(&str_ptr, &str_length, &str_type, &str_xclass, seq_end - str_ptr);\n            if (str_type == V_ASN1_IA5STRING) {\n                // The date is stored as a string that needs to be parsed\n                NSString *dateString = [[NSString alloc] initWithBytes:str_ptr length:str_length encoding:NSASCIIStringEncoding];\n                expirationDate = [formatter dateFromString:dateString];\n            }\n            break;\n            \n            // You can parse more attributes...\n            \n        default:\n            break;\n    }\n    \n    // Move past the value\n    ptr += length;\n}\n\n// Be sure that all information is present\nif (bundleIdString == nil ||\n    bundleVersionString == nil ||\n    opaqueData == nil ||\n    hashData == nil) {\n    // Validation fails\n}\n```\n\n### Verifying Receipt Information\n\nThe receipt contains the bundle identifier and the bundle version for which the receipt was issued. You need to make sure that both match the data of your application:\n\n```objc\n// Check the bundle identifier\nif (![bundleIdString isEqualTo:@\"io.objc.myapplication\"]) {\n    // Validation fails\n}\n\n// Check the bundle version\nif (![bundleVersionString isEqualTo:@\"1.0\"]) {\n    // Validation fails\n}\n```\n\n**IMPORTANT:** When the receipt is issued, the bundle version is taken from the `Info.plist` file:\n\n- On OS X, the version comes from the `CFBundleShortVersionString` value.\n- On iOS, the version comes from the `CFBundleVersion` value.\n\nYou should be careful when setting these values, as they will be picked up when a receipt is issued.\n\n### Computing the GUID Hash\n\nWhen the receipt is issued, three values are used to generate a SHA-1 hash: the device GUID (only available on the device), an opaque value (the type 4 attribute), and the bundle identifier (the type 2 attribute). A SHA-1 hash is computed on the concatenation of these three values and stored into the receipt (type 5 attribute).\n\nDuring the validation, the same computation must be done. If the resulting hash matches, then the receipt is valid.\nThe figure below describes the computation:\n\n![GUID Computation](/images/issue-17/GUIDComputation@2x.png \"GUID Computation\")\n\nIn order to compute this hash, you need to retrieve the device GUID.\n\n#### Device GUID (OS X)\n\nOn OS X, the device GUID is the [MAC][wikipedia-mac-address] address of the primary network card. A way to retrieve it is to use the [IOKit framework][apple-iokit]:\n\n```objc\n#import <IOKit/IOKitLib.h>\n\n// Open a MACH port\nmach_port_t master_port;\nkern_return_t kernResult = IOMasterPort(MACH_PORT_NULL, &master_port);\nif (kernResult != KERN_SUCCESS) {\n    // Validation fails\n}\n\n// Create a search for primary interface\nCFMutableDictionaryRef matching_dict = IOBSDNameMatching(master_port, 0, \"en0\");\nif (!matching_dict) {\n    // Validation fails\n}\n\n// Perform the search\nio_iterator_t iterator;\nkernResult = IOServiceGetMatchingServices(master_port, matching_dict, &iterator);\nif (kernResult != KERN_SUCCESS) {\n    // Validation fails\n}\n\n// Iterate over the result\nCFDataRef guid_cf_data = nil;\nio_object_t service, parent_service;\nwhile((service = IOIteratorNext(iterator)) != 0) {\n    kernResult = IORegistryEntryGetParentEntry(service, kIOServicePlane, &parent_service);\n    if (kernResult == KERN_SUCCESS) {\n        // Store the result\n        if (guid_cf_data) CFRelease(guid_cf_data);\n        guid_cf_data = (CFDataRef) IORegistryEntryCreateCFProperty(parent_service, CFSTR(\"IOMACAddress\"), NULL, 0);\n        IOObjectRelease(parent_service);\n    }\n    IOObjectRelease(service);\n    if (guid_cf_data) {\n        break;\n    }\n}\nIOObjectRelease(iterator);\n\nNSData *guidData = [NSData dataWithData:(__bridge NSData *) guid_cf_data];\n```\n\n#### Device GUID (iOS)\n\nOn iOS, the device GUID is an alphanumeric string that uniquely identifies the device, relative to the application's vendor:\n\n```objc\nUIDevice *device = [UIDevice currentDevice];\nNSUUID *idintifier = [device identifierForVendor];\nuuid_t uuid;\n[identifier getUUIDBytes:uuid];\nNSData *guidData = [NSData dataWithBytes:(const void *)uuid length:16];\n```\n\t\n#### Hash Computation\n\nNow that we have retrieved the device GUID, we can calculate the hash. The hash computation must be done using the ASN.1 attribute's raw values (i.e. the binary data of the OCTET-STRING), and not on the interpreted values. Here's an example to perform the SHA-1 hashing, and the comparison with [OpenSSL][openssl]:\n\n```objc\nunsigned char hash[20];\n\n// Create a hashing context for computation\nSHA_CTX ctx;\nSHA1_Init(&ctx);\nSHA1_Update(&ctx, [guidData bytes], (size_t) [guidData length]);\nSHA1_Update(&ctx, [opaqueData bytes], (size_t) [opaqueData length]);\nSHA1_Update(&ctx, [bundleIdData bytes], (size_t) [bundleIdData length]);\nSHA1_Final(hash, &ctx);\n\n// Do the comparison\nNSData *computedHashData = [NSData dataWithBytes:hash length:20];\nif (![computedHashData isEqualToData:hashData]) {\n    // Validation fails\n}\n```\n\n\n### Volume Purchase Program\n\nIf your app supports the Volume Purchase Program, another check is needed: the receipt’s expiration date. This date can be found in the type 21 attribute:\n\n```objc\n// If an expiration date is present, check it\nif (expirationDate) {\n    NSDate *currentDate = [NSDate date];\n    if ([expirationDate compare:currentDate] == NSOrderedAscending) {\n        // Validation fails\n    }\n}\n```\n\n\n## Handling the Validation Result\n\nSo far, if all the checks are OK, then the validation passes. If any check fails, the receipt must be considered invalid. There are several ways to handle an invalid receipt, depending on the platform and the time when the validation is done.\n\n### Handling on OS X\n\nOn OS X, a receipt validation **must** be performed at application startup, before the main method is called. If the receipt is invalid (missing, incorrect, or tampered with), the application **must** exit with code `173`. This particular code tells the system that the application needs to retrieve a receipt. Once the new receipt has been issued, the application is restarted.\n\nNote that when the application exits with code `173`, an App Store credential dialog will be displayed to sign in. This requires an active Internet connection, so the receipt can be issued and retrieved.\n\nYou can also perform receipt validation during the lifetime of the application. It is up to you to decide how the application will handle an invalid receipt: ignore it, disable features, or crash in a bad way.\n\n### Handling on iOS\n\nOn iOS, the receipt validation can be performed at any time. If the receipt is missing, you can trigger a receipt refresh request in order to tell the system that the application needs to retrieve a new receipt. Note that after triggering a receipt refresh, an App Store credential dialog will be displayed to sign in. This requires an active Internet connection, so the receipt can be issued and retrieved.\n\nIt is up to you to decide how the application will handle an invalid receipt: ignore it or disable features.\n\n\n## Testing\n\nWhen it comes to testing, the major hurdle is to retrieve a test receipt in the sandbox environment.\n\nApple is making a distinction between the production and the sandbox environment by looking at the certificate used to sign the application:\n\n- If the application is signed with a developer certificate, then the receipt request will be directed to the sandbox environment.\n- If the application is signed with an Apple certificate, then the receipt request will be directed to the production environment.\n\nIt is important to code sign your application with a valid developer certificate; otherwise, the `storeagent` daemon (the daemon responsible for communicating with the App Store) will not recognize your application as an App Store application.\n\n### Configuring Test Users\n\nIn order to simulate real users in the sandbox environment, you have to define test users. The test users behave the same way as real users, except that nobody gets charged when they make a purchase.\n\nTest users can be created and configured through [iTunes Connect][itunes-connect]. You can define as many test users you want. Each test user requires a valid email address that must not be a real iTunes account. If your email provider supports the `+` sign in email addresses, you can use email aliases for the test accounts: foo+us@objc.io, foo+uk@objc.io, and foo+fr@objc.io emails will be sent to foo@objc.io.\n\n### Testing on OS X\n\nTo test receipt validation on OS X, go through the following steps:\n\n- Launch the application from the Finder. _Do **not** launch it from Xcode_, otherwise the `launchd` daemon cannot trigger the receipt retrieval.\n- The missing receipt should make the application exit with code `173`. This will trigger the request for a valid receipt. An App Store login window should appear; use the test account credentials to sign in and retrieve the test receipt.\n- If the credentials are valid and the bundle information matches the one you entered, then a receipt is generated and installed in the application bundle. After the receipt is retrieved, the application is relaunched automatically.\n\nOnce a receipt has been retrieved, you can launch the application from Xcode to debug or fine-tune the receipt validation code.\n\n### Testing on iOS\n\nTo test receipt validation on iOS, go through the following steps:\n\n- Launch the application on a real device. _Do **not** launch it in the simulator_. The simulator lacks the API required to issue receipts.\n- The missing receipt should make the application trigger a receipt refresh request. An App Store login window should appear; use the test account credentials to sign in and retrieve the test receipt.\n- If the credentials are valid and the bundle information matches the one you entered, then a receipt is generated and installed in the application sandbox. After the receipt is retrieved, you can perform another validation to ensure that everything is OK.\n\nOnce a receipt has been retrieved, you can launch the application from Xcode to debug or fine-tune the receipt validation code.\n\n\n## Security\n\nThe receipt validation code must be considered highly sensitive code. If it is bypassed or hacked, you lose the ability to check if the user has the right to use your application, or if he or she has paid for it. This is why it is important to protect the validation code against attackers.\n\n**NOTE:** There are many ways of attacking an application, so don't try to be fully hacker-proof. The rule is simple: make the hack of your application as costly as possible.\n\n### Kinds of Attacks\n\nAll attacks begin with an analysis of the target:\n\n- **Static analysis** is performed on the binaries that compose your application. It uses tools like `strings`, `otool`, disassembler, etc.\n- **Dynamic analysis** is performed by monitoring the behavior of the application at runtime, for example, by attaching a debugger and setting breakpoints on known functions.\n\nOnce the analysis is done, some common attacks can be performed against your application to bypass or hack the receipt validation code:\n\n- **Receipt replacement** — if you fail to properly validate the receipt, an attacker can use a receipt from another application that appears to be legitimate.\n- **Strings replacement** — if you fail to hide/obfuscate the strings involved in the validation (i.e. `en0`, `_MASReceipt`, bundle identifier, or bundle version), you give the attacker the ability to replace *your* strings with *his or her* strings.\n- **Code bypass** — if your validation code uses well-known functions or patterns, an attacker can easily locate the place where the application validates the receipt and bypass it by modifying some assembly code.\n- **Shared library swap** — if you are using an external shared library for cryptography (like OpenSSL), an attacker can replace *your* copy of OpenSSL with *his or her* copy and thus bypass anything that relies on the cryptographic functions.\n- **Function override/injection** — this kind of attack consists of patching well-known functions (user or system ones) at runtime by prepending a shared library to the application's shared library path. The [mach_override][github-mach-override] project makes that dead simple.\n\n### Secure Practices\n\nWhile implementing receipt validation, there are some secure practices to follow. Here are a few things to keep in mind:\n\n#### Dos\n\n- **Validate several times** — validate the receipt at startup and periodically during the application lifetime. The more validation code you have, the more an attacker has to work.\n- **Obfuscate strings** — never leave the strings used in validation in clear form, as this can help an attacker locate or hack the validation code. String obfuscation can use xor-ing, value shifting, bit masking, or anything else that makes the string human unreadable.\n- **Obfuscate the result of receipt validation** — never leave the literal strings used in validation in clear form (i.e. `\"en0\"`, `\"AppleCertificateRoot\"`, etc.) as this can help an attacker locate or hack the validation code. In order to obfuscate strings, you can use apply algorithms like xor-ing, value shifting, bit masking, or anything else that makes the result appears as random bytes.\n- **Harden the code flow** — use an [opaque predicate][wikipedia-opaque-predicate] (i.e. a condition only known at runtime) to make your validation code flow hard to follow. Opaque predicates are typically made of function call results which are not known at compile time. You can also use loops, goto statements, static variables, or any control flow structure where you'd usually not need one.\n- **Use static libraries** — if you include third-party code, link it statically whenever it is possible; static code is harder to patch, and you do not depend on external code that can change.\n- **Tamper-proof the sensitive functions** — make sure that sensitive functions have not been replaced or patched. As a function can have several behaviors based on its input arguments, make calls with invalid arguments; if the function does not return an error or the correct return code, then it may be have been replaced, patched, or tampered with.\n\n#### Don'ts\n\n- **Use Objective-C** — Objective-C carries a lot of runtime information that makes it vulnerable to symbol analysis/injection/replacement. If you still want to use Objective-C, obfuscate the selectors and the calls.\n- **Use shared libraries for secure code** — a shared library can be swapped or patched.\n- **Use separate code** — bury the validation code into your business logic to make it hard to locate and patch.\n- **Factor receipt validation** — vary, duplicate, and multiply validation code implementations to avoid pattern detection.\n- **Underestimate the determination of attackers** — with enough time and resources, an attacker will ultimately succeed in cracking your application. What you can do is make the process more painful and costly.\n\n\n\n[rfc-2315]: https://www.ietf.org/rfc/rfc2315.txt\n[apple-iokit]: https://developer.apple.com/library/mac/documentation/IOKit/Reference/IOKitLib_header_reference/Reference/reference.html\n[apple-receipt-validation]: https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Introduction.html\n[apple-receipt-validation-fields]: https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html#//apple_ref/doc/uid/TP40010573-CH106-SW1\n[apple-wwdc-2014-305]: https://developer.apple.com/videos/wwdc/2014/#305\n[itu-t-x690]: http://www.itu.int/ITU-T/recommendations/rec.aspx?id=9608\n[wikipedia-asn1]: http://en.wikipedia.org/wiki/Abstract_Syntax_Notation_One\n[wikipedia-x690-der]: http://en.wikipedia.org/wiki/X.690#DER_encoding\n[wikipedia-type-length-value]: http://en.wikipedia.org/wiki/Type-length-value\n[wikipedia-mac-address]: http://en.wikipedia.org/wiki/MAC_address\n[wikipedia-script-kiddie]:http://en.wikipedia.org/wiki/Script_kiddie\n[wikipedia-opaque-predicate]: http://en.wikipedia.org/wiki/Opaque_predicate\n[github-mach-override]: https://github.com/rentzsch/mach_override\n[github-?-receipt-validation]: https://github.com/search?utf8=%E2%9C%93&q=receipt+validation\n[gnu-libtasn1]: http://www.gnu.org/software/libtasn1/\n[asn1-compiler]: http://lionet.info/asn1c/compiler.html\n[openssl]: https://www.openssl.org/\n[itunes-connect]: http://itunesconnect.apple.com/\n"
  },
  {
    "path": "2014-10-10-why-security.md",
    "content": "---\ntitle: Why Security Still Matters Today\ncategory: \"17\"\ndate: \"2014-10-10 11:00:00\"\nauthor:\n  - name: Graham Lee\n    url: http://twitter.com/secboffin\ntags: article\n---\n\n\nAs this article was being written, systems administrators were rushing to ensure their networks were robust against [CVE-2014-6271](http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-6271), also known as \"Shellshock.\" The vulnerability report on this bug describes the ability to get the `bash` shell, installed on most Linux and OS X systems (and, though most people are unlikely to use it there, on iOS too), to run functions under the attacker's control. Considering that security has been a design consideration in software systems at least since the [Compatible Time-Sharing System](http://publications.csail.mit.edu/lcs/pubs/pdf/MIT-LCS-TR-016.pdf) (CTTS) of the 1960s, why isn't it a solved problem? Why do software systems still have security problems, and why are we app developers told we have to do something about it?\n\n## Our Understanding Has Changed\n\nThe big challenge faced by CTSS — which informed architectural decisions in later systems, including the UNIX system from which OS X and iOS are derived — was to allow multiple people to access the same computer without getting in each other's way. The solution was that each user runs his or her own tasks in an environment that looks like a real computer, but in fact is a sandbox carved out of a shared computer.\n\nWhen one user needs substantial computing resources, the program shouldn't adversely affect the resources of the other users. This is the model that leads to multiple user accounts, with quotas and resource usages and accounting systems. It works pretty well: in 2006, I was using the same system to give more than 1,000 users access to a shared OS X computer.\n\nUnfortunately, that view of the problem is incomplete. Technical countermeasures to stop one _account_ from using the resources allocated to another account often do not, in fact, stop one _user_ from using resources allocated to another user. If one user can gain access to the account of another, or can convince the other user to run his or her programs, then the accounting and permissions system is circumvented. That is the root problem exposed in the Shellshock vulnerability: one user can get another to run his or her program with the permissions and resources normally available to the victim.\n\n## The Problem Has Changed\n\nIn the time since UNIX was designed, computers have become smaller, faster, and better connected. They've also found uses in more and more situations that weren't foreseen when the software that supports those situations was created. Electronic mail as an effectively public, plain-text system was OK when every user of the mail system worked for the same university, and every terminal was owned by that university. However, supporting communication between people in different organizations, in different locations, and on different networks is a different problem and requires different solutions.\n\nIn one way, iOS is still a multi-user system. Unlike the environment in which UNIX was designed, all of the users have access to the same account that's operated by the phone's owner. Those users are the owners themselves: you, me, all the other app developers whose products are installed on the phone, and Apple.\n\nThat's actually a bit of an oversimplification, because many apps aren't the sole work of the developers who submitted them to the store. SDKs, remote services like analytics, and open-source components mean that many apps actually contain code from multiple organizations, and must communicate over networks that are potentially being surveilled. The game is no longer to protect different people at the same computer from each other, but to protect one person's different _tasks_ from each other.\n\nThis all sounds pretty negative, perhaps like a mild form of paranoia. The reality is that security can be an _enabling_ force, because it reduces the risks of new scenarios and processes to make them accessible to people in a wider range of contexts. Imagine how much more risky mobile banking would be without the availability of cryptography, and how few people (or even banks) would participate.\n\n## …While Some Things Stayed the Same\n\nThe only reason that UNIX is still at all relevant to modern discussions of software security is that we haven't gotten rid of it, which is mainly because we've never tried. The history of computing is full of examples of systems that were shown to suffer from significant security problems, but which are still in use because the industry is collectively bad at cleaning up its own mess. Even on the latest version of iOS, using the latest tools and the latest programming language, we can use functions like those in the C string library that have been known to be broken for decades.\n\nAt almost every step in the evolution of software systems, patches over existing, broken technology have been accepted in favor of reinventions designed to address the problems that have been discovered. While we like to claim that we're inventing the future, in fact, we spend a lot of time and resources in clinging onto the past. Of course, maybe _replacing_ these systems would reintroduce a lot of the problems that we _have_ already fixed.\n\n## Apple Can't Solve Your Problems\n\nApple tells us that each version of iOS is more secure than the last, and publishes a white paper [detailing the security features of its systems](https://www.apple.com/privacy/docs/iOS_Security_Guide_Sept_2014.pdf). Apple explains how it's using ever-newer (and hopefully more advanced) cryptographic algorithms and protocols, stronger forms of identification, and more. Why isn't this enough?\n\nThe operating system security features can only make provisions that apply to _any_ app; they cannot do everything required to support _your_ app. While Apple can tell you that your app connected to a server that presented _some_ valid identity, it cannot tell you whether it is [an identity _you_ trust](http://www.securemacprogramming.com/SSL_handout.pdf).\n\nApple can provide file protection to encrypt your data, and unlock it when requested. It cannot tell you _when_ it's appropriate to make that request.\n\nApple can limit the ways in which apps can communicate, so that data is only exchanged over controlled channels like URL schemes. It can neither decide what _level_ of control is appropriate for your app, nor can it tell what _forms_ of data your app should accept, or what forms are inappropriate.\n\n## You Can't Either (Not Entirely, Anyway)\n\nSimilar to operating system features, popularity charts of [mobile app vulnerabilities](https://www.owasp.org/index.php/Projects/OWASP_Mobile_Security_Project_-_Top_Ten_Mobile_Risks) tell you what problems are encountered by _many_ apps, but not which are relevant to _your_ app, or _how_ they manifest. They certainly say nothing about vulnerabilities that are specific to the uses to which _your customers_ are putting _your app_ — vulnerabilities that emerge from the tasks and processes your customers are completing, and the context and environment in which they are doing so.\n\nSecurity is a part of your application architecture: a collection of constraints that your proposed solution must respect as it respects response time, scale of the customer base, and compatibility with external systems. This means that you have to design it into your application as you design the app to operate within the other constraints.\n\nA common design technique used in considering application security is [threat modeling](http://msdn.microsoft.com/en-us/magazine/cc163519.aspx): identify the reasons people would want to attack your system, the ways in which they would try to do that with the resources at their disposal, and the vulnerabilities in the system's design that could be exploited to make the attack a success.\n\nEven once you've identified the vulnerabilities, there are multiple ways to deal with them. As a real-world analogy, imagine that you're booking a holiday, but there's a possibility that your employer will need you to be on call that week to deal with emergencies, and ready to show up in the office. You could deal with that by:\n\n - accepting the risk — book the holiday anyway, but be ready to accept that you might not get to go.\n - preventing the risk — quit your job, so you definitely won't be on call.\n - reacting to the risk — try to deal with it once it arises, rather than avoiding it in advance.\n - transferring the risk — buy insurance for your holiday so you can reschedule or get a refund if you end up being called in.\n - withdraw from the activity — give up on the idea of going on holiday.\n\nAll of these possibilities are available in software security, too. You can select one, or combine multiple approaches. The goal is usually not to _obviate_ the risk, but to _mitigate_ it. How much mitigation is acceptable? That depends on how much residual risk you, your business, your customers, and your partners are willing to accept.\n\nYour mitigation technique also depends on your goals: What are you trying to achieve in introducing any security countermeasure? Are you trying to protect your customers' privacy, ensure the continued availability of your service, or comply with applicable legislation? If these goals come into conflict, you will need to choose which is most important. Your decision will probably depend on the specific situation, and may not be something you can design out in advance. Plenty of contingency plans are created so that people know what to do when something bad happens…_again_.\n\n## Conclusion\n\nDespite advances and innovations in software security technology and the security capabilities of systems like iOS, risk analysis and designing security countermeasures are still the responsibility of app developers. There are threats and risks to the use of our applications that cannot possibly be addressed by operating system vendors or framework developers. These threats are specific to the uses to which our apps are put, and to the environments and systems in which they are deployed.\n\nWith the security and cryptography features of the iOS SDK, Apple has led us to the water. It's up to us to drink.\n"
  },
  {
    "path": "2014-11-10-designing-elegant-mobile-games.md",
    "content": "---\ntitle: \"Designing Elegant Mobile Games\"\ncategory: \"18\"\ndate: \"2014-11-10 11:00:00\"\ntags: article\nauthor:\n  - name: Mike Lazer-Walker\n    url: https://twitter.com/lazerwalker\n---\n\nThe idea of designing mobile games is a funny one. In theory, there isn't anything about making games for smartphones or tablets that's fundamentally different from any other form of game design. The formal properties of games as systems are the same across any genre or platform, and so the design process tends to look relatively similar, whether you're trying to make the next Farmville, Call of Duty, or chess.\n\nIn practice, though, creating a successful mobile game can be a very different beast. So many different concerns — from market saturation and lack of discoverability, to usage patterns and form factor issues — can make it feel like making a good mobile game is like turning on \"hard mode\" as a designer.\n\nAll of these various factors at play mean that most successful mobile games tend toward elegant rulesets. That is, they strive to be capable of deep, meaningful play, but that meaning needs to arise from a minimal set of simple rules. There's certainly a place for more ornate and baroque games, but very few successful mobile games tend to hew to that style, no matter what your metric for success is.\n\nSo how do we achieve these sorts of elegant designs we want in our games? Let's look at two defining characteristics of mobile games — play session length and methods of interaction — and see a few ways we might approach thinking about designing systems that suit the platform.\n\n## Play Session Length\n\nPeople play mobile games differently than most other kinds of games. Players demand games that are playable in short bursts, such as while they're waiting in line or on the toilet, but they also want the ability to partake in more meaningful, longer-term play sessions. Most studies peg the average iOS game session length at somewhere between one and two minutes, but at the same time, most mobile games are in actuality played at home rather than on the go. Striking a balance to make your game fun and rewarding in both situations is a *really* challenging problem.\n\nTo help us think about designing for both of these contexts, it's useful to think about a game as a collection of feedback loops. At any given moment of a game, you have a mental model of the game's system. Based on that, you're going to perform some action that will result in the game giving you feedback, which will in turn inform your mental model.\n\nThe key thing about these feedback loops is that they're fractal in nature; at any given moment, there could be any number of nested feedback loops at play. As an example, let's think about what is happening when you play a game of [Angry Birds](https://www.angrybirds.com).\n\n![How to play Angry Birds](/images/issue-18/AngryBirds.PNG)\n\nLet's start at the level of each individual move you make. Flinging an individual bird across the map gives you the satisfaction of accomplishing something, but it also gives you feedback: did you destroy the blocks or pigs you thought you would? Did the arc your bird took (still visible onscreen after the bird has landed) mirror what you thought it would? This information informs your future shots.\n\nTaking a step back, the next most atomic unit of measurement is the level. Each level also acts as its own closed system of feedback and rewards: clearing it gives you anywhere from one to three stars, encouraging you to develop the skills necessary to truly 'beat' it.\n\nIn aggregate, all of the levels themselves form a feedback loop and narrative arc, giving you a clear sense of progression over time, in addition to providing you with a sense of your skill, relative to the overall system.\n\nWe could keep going with examples, but I think the concept is clear. Again, this isn't a game design concept that is unique to mobile games; if either the moment-to-moment experience of playing your game or the overarching sense of personal progression is lacking, your game likely has room for improvement, no matter the platform.\n\nThis becomes particularly relevant when thinking about the problem of play session length, though. It's possible to have a game with fun moment-to-moment gameplay, while still having the smallest systemic loops be sufficiently long that trying to pick it up for a minute or two while in line wouldn't be fun. That same minute or two in Angry Birds lets you experience multiple full iterations of some of the game's feedback loops, giving a sense of fun even in such a short playtime. The existence of higher-level feedback loops means that these atomic micro-moments of fun don't come at the expense of ruining the potential for longer-term meaningful play.\n\n## The Controller Conundrum\n\nMost platforms for digital games — game consoles, PCs, and even arcade cabinets — have a larger number of inputs than smartphones or tablets. Many great mobile games find unique ways to use multitouch or the iPhone's accelerometer, rather than throwing lots of virtual buttons on the screen, but that still leaves iOS devices with far less discrete inputs than most other forms of digital games. The result is a difficult design challenge: how can we make interesting, meaningful, and deep game systems when our input is constrained? This is a relatively frequent topic of discussion for game design students — creating a one-button game is a classic educational exercise for aspiring designers — but the restrictions of iOS frequently make it more than an academic concern. Ultimately, it's a similar problem to that of gameplay session length: how do you create something that's simple and immediately approachable without giving up the depth and meaningful play that other forms of games exhibit?\n\nOne useful way for framing interactions in a game is to reduce the formal elements of the game down to 'nouns' and 'verbs.' Let's take the original Super Mario Bros. as an example. Mario has two main verbs: he can *run* and he can *jump*. The challenge in Mario comes from the way the game introduces and arranges nouns to shape these verbs over the course of the game, giving you different obstacles that require you to apply and combine these two verbs in interesting and unique ways.\n\nOf course, Mario would be much more boring if you could only run *or* jump. But even the six buttons required to play Mario (a four-directional d-pad and discrete 'run' and 'jump' buttons) are in many ways too complex an input for an ideal touchscreen game; there's a reason there are few successful traditional 2D platformers on iOS.\n\nSo how can we add depth and complexity to a game while minimizing the types of input? Within this framework of nouns and verbs, there are essentially three ways we can add complexity to a game. We can add a new input, we can add a new verb that uses an existing input, or we can take an existing verb and add more nouns that color that verb with new meaning. The first option is generally going to add complexity in the way we don't want, but the other two can be very effective when done right. Let's look at some examples of mobile games that use each of these ways to layer in additional depth without muddying the core game interactions.\n\n### Hundreds\nThe game [Hundreds](http://playhundreds.com)[^1] is a great example of adding in new verbs without complicating the way you perform the game's verbs.\n\n![Hundreds in action](/images/issue-18/Hundreds.PNG)\n\nInitially, the only verb at your disposal is \"touch a bubble to grow it.\" As the game progresses, new types of objects are introduced: bubbles that slowly deflate over time, spiky gears that puncture anything they touch, ice balls that freeze bubbles in place. It would be easy for this to become overwhelming to the player, but crucially, nothing ever breaks the input model of \"tap an object to do something to it.\" Even though the number of possible verbs balloons to a pretty large number, they cohere in a way that keeps it simple. The interaction between these elements is rich — moments such as using the ice balls to freeze the dangerous gears, rendering them harmless, are particularly satisfying — but the fundamental way you interact with the system stays simple.\n\n### Threes\nThe puzzle game [Threes](http://asherv.com/threes/)[^2] exemplifies the other approach, managing to layer in complexity and strategy without making any changes to the things you can do in the game.\n\n![How to play Threes](/images/issue-18/THREES_trailer.gif)\n\nThroughout the game, its rules remain completely constant. From beginning to end, the only verb in your tool belt is \"swipe to shift blocks over,\" with no variation at all. Because of the way the rules of the system create new objects at a predictable rate, complexity emerges naturally as a result of progression. When the screen only has a few low-numbered blocks at the beginning of the game, decisions are easy. When you're balancing building up lower-level numbers with managing a cluster of higher numbers, that same one verb suddenly has a lot more meaning and nuance behind it.\n\nBoth of these are great examples of games that manage to offer simplicity on the surface but great depth underneath by carefully managing where and how they add complexity and meaning to their verbs. The approach between the two might be different, but both do a laudable job of shifting some of that complexity away from the lowest levels of the game to make them more accessible.\n\n## Elegance\n\nWe've now explored two different lenses we can use to think about designing games. Thinking about your systems in terms of nested feedback loops, and managing the relative lengths of one iteration of each loop, can help you design something that is fun for both 10 seconds and an hour at a time. Being cognizant of the way you add complexity to your game through the way you handle your game's verbs can help you increase your game's strategic depth without sacrificing accessibility to new players.\n\nUltimately, these two concepts explore similar ground: the idea that gameplay depth and systemic complexity, while related, are not necessarily equivalent. Being conscious about at what layer of your game the complexity lies can help make your games as accessible as possible to new players, and encourage short pick-up-and-play game sessions without sacrificing depth or long-term engagement.\n\nAgain, neither of these concepts is particularly new to the world of game design. In particular, design blogger Dan Cook talks a lot about nested feedback loops in his article, [\"The Chemistry of Game Design,\"](http://www.gamasutra.com/view/feature/129948/the_chemistry_of_game_design.php) and Anna Anthropy and Naomi Clark's book, [\"A Game Design Vocabulary,\"](http://www.amazon.com/Game-Design-Vocabulary-Foundational-Principles/dp/0321886925) has an insightful exploration of what it means to conceptualize your game in terms of verbs.\n\nBut these problems are exacerbated on mobile. The mobile context makes it vital to keep your lowest-level loops and arcs as short and self-contained as possible, without losing sight of the bigger picture. The practicalities of touchscreen controls make adding complexity and nuance at the input level difficult, making it that much more important that your more higher-level systems can still provide rewarding advanced play for experienced players. The unforgiving nature of mobile games means that elegance in design isn't merely ideal, but a necessity; recognizing that simple doesn't have to equal shallow is vital to designing good mobile games.\n\n\n[^1]: The basic concept of Hundreds is simple: each level has a bunch of bubbles bouncing around, each with a number inside it. The larger the number, the bigger the bubble. While you are touching a bubble with your finger, it turns red, grows larger, and its number increases. When you stop touching it, it stops growing and turns black again, but it maintains its new number and size. Once the sum of all on-screen bubbles is at least 100, you've beaten the level. However, if a circle touches another circle while it is red (i.e. being touched), you need to restart.\n\n[^2]: If you don't know Threes, you might instead be familiar with the more popular clone, [2048](http://gabrielecirulli.github.io/2048/). Threes presents you with a 4x4 game grid with a few numbered squares, where every square's number is either a 1, a 2, or a 3, doubled some number of times (3, 6, 12, 24, etc.). When you swipe in any direction, every square that is capable of doing so will shift over one space in that direction, with a new square being pushed onto the game board in an empty square from the appropriate side. If two squares of the same number are pushed into each other, they will become one square whose number is the sum of them together (1s and 2s are different; you must combine a 1 and a 2 to get a 3). When you can no longer move, your score is calculated based on the numbers visible on the board, with higher numbers being worth disproportionately more than lower ones.\n"
  },
  {
    "path": "2014-11-10-editorial.md",
    "content": "---\ntitle:  \"Editorial\"\ncategory: \"18\"\ndate: \"2014-11-10 12:00:00\"\ntags: editorial\n---\n\n \nWelcome to the first objc.io issue about game development!\n\nIn the previous 17 issues of objc.io, we never touched on any game-specific development topics. This was on our list for a while, but since we don't have experience with this ourselves, we've put it off several times. Until now.\n\nWe've found great contributors who chimed in with their knowledge about game-related topics. This issue contains articles covering many varied aspects of game development, providing even non-game developers insight into this related, but nonetheless very different, field.\n\nMike starts off with an article about what makes [mobile games great](/issues/18-games/designing-elegant-mobile-games/). Max and Warren give an introduction to [Apple's new low-level graphics framework, \"Metal.\"](/issues/18-games/metal/) JP and David write about topics that are relevant to not only game developers: [Multipeer Connectivity](/issues/18-games/multipeer-connectivity-for-games/) and [Scene Kit](/issues/18-games/scenekit/). Last but not least, Janie gives us a taste of the wonderful world of [sound design](/issues/18-games/sound-design/).\n\nAll the best from Berlin,\n\nChris, Daniel, and Florian.\n"
  },
  {
    "path": "2014-11-10-metal.md",
    "content": "---\ntitle:  \"Metal\"\ncategory: \"18\"\ndate: \"2014-11-10 10:00:00\"\ntags: article\nauthor:\n  - name: Max Christ\n    url: http://twitter.com/mczonk\n  - name: Warren Moore\n    url: http://twitter.com/warrenm\n---\n\n> The Metal framework supports GPU-accelerated advanced 3D graphics rendering and data-parallel computation workloads. Metal provides a modern and streamlined API for fine-grain, low-level control of the organization, processing, and submission of graphics and computation commands and the management of the associated data and resources for these commands. A primary goal of Metal is to minimize the CPU overhead necessary for executing these GPU workloads.\n\n– [Metal Programming Guide](https://developer.apple.com/library/ios/documentation/Miscellaneous/Conceptual/MetalProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40014221-CH1-SW1)\n\nMetal is a highly optimized framework for programming the GPUs found in iPhones and iPads. The name derives from the fact that Metal is the lowest-level graphics framework on the iOS platform (i.e. it is \"closest to the metal\").\n\nThe framework is designed to achieve two different goals: 3D graphics rendering and parallel computations. These two things have a lot in common. Both tasks run special code on a huge amount of data in parallel and can be executed on a [GPU](https://en.wikipedia.org/wiki/Graphics_processing_unit). \n\n\n## Who Should Use Metal?\n\nBefore talking about the API and shading language itself, we should discuss which developers will benefit from using Metal. As previously mentioned, Metal offers two functionalities: graphics rendering and parallel computing.\n\nFor those who are looking for a game engine, Metal is not the first choice. In this case, Apple's [Scene Kit](https://developer.apple.com/library/ios/documentation/SceneKit/Reference/SceneKit_Framework/) (3D) and [Sprite Kit](https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Introduction/Introduction.html) (2D) are better options. These APIs offer a high-level game engine, including physics simulation. Another alternative would be a full-featured 3D engine like Epic's [Unreal Engine](https://www.unrealengine.com/), or [Unity](http://unity3d.com), both of which are not limited to Apple's platform. In each of those cases, you profit (or will profit in the future) from the power of Metal without using the API directly.\n\nWhen writing a rendering engine based on a low-level graphics API, the alternatives to Metal are OpenGL and OpenGL ES. Not only is OpenGL available for nearly every platform, including OS X, Windows, Linux, and Android, but there are also a huge amount of tutorials, books, and best practice guides written about OpenGL. Right now, Metal resources are very limited and you are constrained to iPhones and iPads with a 64-bit processor. On the other hand, due to limitations in OpenGL, its performance is not always optimal compared to Metal, which was written specifically to overcome such issues.\n\nWhen looking for a high-performance parallel computing library on iOS, the question of which one to use is answered very simply. Metal is the only option. OpenCL is a private framework on iOS, and Core Image (which uses OpenCL) is neither powerful nor flexible enough for this task.\n\n## Benefits of Using Metal\n\nThe biggest advantage of Metal is a dramatically reduced overhead compared to OpenGL ES. Whenever you create a buffer or a texture in OpenGL, it is copied to ensure that the data cannot be accessed accidentally while the GPU is using it. Copying large resources such as textures and buffers is an expensive operation undertaken for the sake of safety. Metal, on the other hand, does not copy resources. The developer is responsible for synchronizing the access between the CPU and GPU. Luckily, Apple provides another great API, which makes resource synchronization much easier: [Grand Central Dispatch](https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html). Still, using Metal requires some awareness of this topic, but a modern engine that is loading and unloading resources while rendering profits a lot when this extra copy is avoided.\n\nAnother advantage of Metal is its use of pre-evaluated GPU state to avoid redundant validation and compilation. Traditionally in OpenGL, you set GPU states one after the other, and the new set of states needs to be validated when making a draw call. In the worst case, OpenGL needs to recompile shaders again to reflect the new states. Of course, this evaluation is necessary, but Metal chooses to take a different approach here. During the initialization of the rendering engine, a set of states is baked into a pre-evaluted rendering pass. This rendering pass object can be used with different resources, but the other states will be constant. A render pass in Metal can be used without further validation, which reduces the API overhead to a minimum, and allows a greatly increased number of draw calls per frame.\n\n## The Metal API\n\nAlthough many APIs on the platform expose concrete classes, Metal offers many of its types as protocols. The reason for this is that the concrete types of Metal objects are dependent on the device on which Metal is running. This also encourages programming to an interface rather than an implementation. This also means, however, that you won't be able to subclass Metal classes or add categories without making extensive and dangerous use of the Objective-C runtime.\n\nMetal necessarily compromises some safety for speed. In regard to errors, other Apple frameworks are getting more secure and robust, but Metal is totally different. In some instances, you receive a bare pointer to an internal buffer, the access to which you must carefully synchronize. When things go wrong in OpenGL, the result is usually a black screen; in Metal, the results can be totally random effects, including a flickering screen and occasionally a crash. These pitfalls occur because the Metal framework is a very light abstraction of a special-purpose processor next to your CPU.\n\nAn interesting side note is that Apple has not yet implemented a software render for Metal that can be used in the iOS Simulator. When linking the Metal framework, the application must be executed on a physical device.\n\n## A Basic Metal Program\n\nIn this section, we introduce the necessary ingredients for writing your first Metal program. This simple program draws a square rotating about its center. You can download the [sample code for this article from GitHub](https://github.com/objcio/metal-demo-objcio).\n\nAlthough we can't cover each topic in detail, we will try to at least mention all of the moving parts. For more insight, you can read the sample code and consult other online resources.\n\n### Creating a Device and Interfacing with UIKit\n\nIn Metal, a _device_ is an abstraction of the GPU. It is used to create many other kinds of objects, such as buffers, textures, and function libraries. To get the default device, use the `MTLCreateSystemDefaultDevice` function:\n\n```objc\nid<MTLDevice> device = MTLCreateSystemDefaultDevice();\n```\n\nNotice that the device is not of a particular concrete class, but instead conforms to the `MTLDevice` protocol as mentioned above.\n\nThe following snippet shows how to create a Metal layer and add it as a sublayer of a UIView's backing layer:\n\n```objc\nCAMetalLayer *metalLayer = [CAMetalLayer layer];\nmetalLayer.device = device;\nmetalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm;\nmetalLayer.frame = view.bounds;\n[view.layer addSublayer:self.metalLayer];\n```\n\n`CAMetalLayer` is a subclass of [`CALayer`](https://developer.apple.com/library/mac/Documentation/GraphicsImaging/Reference/CALayer_class/index.html) that knows how to display the contents of a Metal framebuffer. We must tell the layer which Metal device to use (the one we just created), and inform it of its expected pixel format. We choose an 8-bit-per-channel BGRA format, wherein each pixel has blue, green, red, and alpha (transparency) components, with values ranging from 0-255, inclusive.\n\n### Libraries and Functions\n\nMuch of the functionality of your Metal program will be written in the form of vertex and fragment functions, colloquially known as shaders. Metal shaders are written in the Metal shading language, which we will discuss in greater detail below. One of the advantages of Metal is that shader functions are compiled at the time your app builds into an intermediate language, saving valuable time when your app starts up.\n\nA Metal library is simply a collection of functions. All of the shader functions you write in your project are compiled into the default library, which can be retrieved from the device:\n\n```objc\nid<MTLLibrary> library = [device newDefaultLibrary]\n```\n\nWe will use the library when building the render pipeline state below.\n\n### The Command Queue\n\nCommands are submitted to a Metal device through its associated command queue. The command queue receives commands in a thread-safe fashion and serializes their execution on the device. Creating a command queue is straightforward:\n\n```objc\nid<MTLCommandQueue> commandQueue = [device newCommandQueue];\n```\n\n### Building the Pipeline\n\nWhen we speak of the pipeline in Metal programming, we mean the different transformations the vertex data undergoes as it is rendered. Vertex shaders and fragment shaders are two programmable junctures in the pipeline, but there are other things that must happen (clipping, scan-line rasterization, and the viewport transform) that are not under our direct control. This latter class of pipeline features constitute the fixed-function pipeline.\n\nTo create a pipeline in Metal, we need to specify which vertex and fragment function we want executed for each vertex and each pixel, respectively. We also need to tell the pipeline the pixel format of our framebuffer. In this case, it must match the format of the Metal layer, since we want to draw to the screen.\n\nTo get the functions, we ask for them by name from the library:\n\n```objc\nid<MTLFunction> vertexProgram = [library newFunctionWithName:@\"vertex_function\"];\nid<MTLFunction> fragmentProgram = [library newFunctionWithName:@\"fragment_function\"];\n```\n\nWe then create a pipeline descriptor configured with the functions and the pixel format:\n\n```objc\nMTLRenderPipelineDescriptor *pipelineStateDescriptor = [[MTLRenderPipelineDescriptor alloc] init];\n[pipelineStateDescriptor setVertexFunction:vertexProgram];\n[pipelineStateDescriptor setFragmentFunction:fragmentProgram];\npipelineStateDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;\n```\n\nFinally, we create the pipeline state itself from the descriptor. This compiles the shader functions from their intermediate representation into optimized code for the hardware on which the program is running:\n\n```objc\nid<MTLRenderPipelineState> pipelineState = [device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:nil];\n```\n\n### Loading Data into Buffers\n\nNow that we have a pipeline built, we need data to feed through it. In the sample project, we draw a simple bit of geometry: a spinning square. The square is comprised of two right triangles that share an edge:\n\n```objc\nstatic float quadVertexData[] =\n{\n     0.5, -0.5, 0.0, 1.0,     1.0, 0.0, 0.0, 1.0,\n    -0.5, -0.5, 0.0, 1.0,     0.0, 1.0, 0.0, 1.0,\n    -0.5,  0.5, 0.0, 1.0,     0.0, 0.0, 1.0, 1.0,\n    \n     0.5,  0.5, 0.0, 1.0,     1.0, 1.0, 0.0, 1.0,\n     0.5, -0.5, 0.0, 1.0,     1.0, 0.0, 0.0, 1.0,\n    -0.5,  0.5, 0.0, 1.0,     0.0, 0.0, 1.0, 1.0,\n};\n```\n\nThe first four numbers of each row represent the x, y, z, and w components of each vertex. The second four numbers represent the red, green, blue, and alpha color components of the vertex.\n\nYou may be surprised that there are four numbers required to express a position in 3D space. The fourth component of the vertex position, w, is a mathematical convenience that allows us to represent 3D transformations (rotation, translation, and scaling) in a unified fashion. This detail is not relevant to the sample code for this article.\n\nTo draw the vertex data with Metal, we need to place it into a buffer. Buffers are simply unstructured blobs of memory that are shared by the CPU and GPU:\n\n```objc\nvertexBuffer = [device newBufferWithBytes:quadVertexData\n                                   length:sizeof(quadVertexData)\n                                  options:MTLResourceOptionCPUCacheModeDefault];\n```\n\nWe will use another buffer for storing the rotation matrix we use to spin the square around. Rather than providing the data up front, we'll just make room for it by creating a buffer of a prescribed length:\n\n```objc\nuniformBuffer = [device newBufferWithLength:sizeof(Uniforms) \n                                    options:MTLResourceOptionCPUCacheModeDefault];\n```\n\n### Animation\n\nIn order to rotate the square on the screen, we need to transform the vertices as part of the vertex shader. This requires updating the uniform buffer each frame. To do this, we use trigonometry to generate a rotation matrix from the current rotation angle and copy it into the uniform buffer.\n\nThe `Uniforms` struct has a single member, which is a 4x4 matrix that holds the rotation matrix. The type of the matrix, `matrix_float4x4`, comes from Apple's SIMD library, a collection of types that take advantage of [data-parallel operations](http://en.wikipedia.org/wiki/SIMD) where they are available:\n\n```objc\ntypedef struct\n{\n    matrix_float4x4 rotation_matrix;\n} Uniforms;\n```\n\nTo copy the rotation matrix into the uniform buffer, we get a pointer to its contents and `memcpy` the matrix into it:\n\n```objc\nUniforms uniforms;\nuniforms.rotation_matrix = rotation_matrix_2d(rotationAngle);\nvoid *bufferPointer = [uniformBuffer contents];\nmemcpy(bufferPointer, &uniforms, sizeof(Uniforms));\n```\n\n### Getting Ready to Draw\n\nIn order to draw into the Metal layer, we first need to get a 'drawable' from the layer. The drawable object manages a set of textures that are appropriate for rendering into:\n\n```objc\nid<CAMetalDrawable> drawable = [metalLayer nextDrawable];\n```\n\nNext, we create a render pass descriptor, which describes the various actions Metal should take before and after rendering is done. Below, we describe a render pass that will first clear the framebuffer to a solid white color, then execute its draw calls, and finally store the results into the framebuffer for display:\n\n```objc\nMTLRenderPassDescriptor *renderPassDescriptor = [MTLRenderPassDescriptor renderPassDescriptor];\nrenderPassDescriptor.colorAttachments[0].texture = drawable.texture;\nrenderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;\nrenderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(1, 1, 1, 1);\nrenderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;\n```\n\n### Issuing Draw Calls\n\nTo place commands into a device's command queue, they must be encoded into a command buffer. A command buffer is a set of one or more commands that will be executed and encoded in a compact way that the GPU understands:\n\n```objc\nid<MTLCommandBuffer> commandBuffer = [self.commandQueue commandBuffer];\n```\n\nIn order to actually encode render commands, we need yet another object that knows how to convert from our draw calls into the language of the GPU. This object is called a command encoder. We create it by asking the command buffer for an encoder and passing the render pass descriptor we created above:\n\n```objc\nid<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];\n```\n\nImmediately before the draw call, we configure the render command encoder with our pre-compiled pipeline state and set up the buffers, which will become the arguments to our vertex shader:\n\n```objc\n[renderEncoder setRenderPipelineState:pipelineState];\n[renderEncoder setVertexBuffer:vertexBuffer offset:0 atIndex:0];\n[renderEncoder setVertexBuffer:uniformBuffer offset:0 atIndex:1];\n```\n\nTo actually draw the geometry, we tell Metal the type of shape we want to draw (triangles), and how many vertices it should consume from the buffer (six, in this case):\n\n```objc\n[renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:6];\n```\n\nFinally, to tell the encoder we are done issuing draw calls, we call `endEncoding`:\n\n```objc\n[renderEncoder endEncoding];\n```\n\n### Presenting the Framebuffer\n\nNow that our draw calls are encoded and ready for execution, we need to tell the command buffer that it should present the results to the screen. To do this, we call `presentDrawable` with the current drawable object retrieved from the Metal layer:\n\n```objc\n[commandBuffer presentDrawable:drawable];\n```\n\nTo tell the buffer that it is ready for scheduling and execution, we call `commit`: \n\n```objc\n[commandBuffer commit];\n```\n\nAnd that's it!\n\n## Metal Shading Language\n\nAlthough Metal was announced in the same [WWDC keynote](https://www.apple.com/apple-events/june-2014/) as the [Swift](https://developer.apple.com/swift/) programming language, the shading language is based on [C++11](https://en.wikipedia.org/wiki/C%2B%2B11), with some limited features and some added keywords.\n\n### The Metal Shading Language in Practice\n\nTo use the vertex data from our shaders, we define a struct type that corresponds to the layout of the vertex data in the Objective-C program:\n\n```objc\ntypedef struct\n{\n    float4 position;\n    float4 color;\n} VertexIn;\n```\n\nWe also need a very similar type to describe the vertex type that will be passed from our vertex shader to the fragment shader. However, in this case, we must identify (through the use of the `[[position]]` attribute) which member of the struct should be regarded as the vertex position:\n\n```objc\ntypedef struct {\n float4 position [[position]];\n float4 color;\n} VertexOut;\n```\n\nThe vertex function is executed once per vertex in the vertex data. It receives a pointer to the list of vertices, and a reference to the uniform data, which contains the rotation matrix. The third parameter is an index that tells the function which vertex it is currently operating on.\n\nNote that the vertex function arguments are followed by attributes that indicate their usage. In the case of the buffer arguments, the index in the parameter corresponds to the index we specified when setting the buffers on the render command encoder. This is how Metal figures out which parameter corresponds to each buffer.\n\nInside the vertex function, we multiply the rotation matrix by the vertex's position. Because of how we built the matrix, this has the effect of rotating the square about its center. We then assign this transformed position to the output vertex. The vertex's color is copied directly from the input to the output:\n\n```objc\nvertex VertexOut vertex_function(device VertexIn *vertices [[buffer(0)]],\n                                 constant Uniforms &uniforms [[buffer(1)]],\n                                 uint vid [[vertex_id]])\n{\n    VertexOut out;\n    out.position = uniforms.rotation_matrix * vertices[vid].position;\n    out.color = vertices[vid].color;\n    return out;\n}\n```\n\nThe fragment function is executed once per pixel. The argument is produced by Metal in the process of [rasterization](http://fgiesen.wordpress.com/2013/02/08/triangle-rasterization-in-practice/) by interpolating between the position and color parameters specified at each vertex. In this simple fragment function, we simply pass out the interpolated color already produced by Metal. This then becomes the color of the pixel on the screen:\n\n```objc\nfragment float4 fragment_function(VertexOut in [[stage_in]])\n{\n    return in.color;\n}\n```\n\n## Why Not Just Extend OpenGL?\n\nApple is on the OpenGL Architecture Review Board, and has also historically provided its own GL extensions in iOS. But changing OpenGL from the inside seems to be a difficult task because it has different design goals. In particular, it must run on a broad variety of devices with a huge range of hardware capabilities. Although OpenGL is continuing to improve, the process is slower and more subtle.\n\nMetal, on the other hand, was exclusively created with Apple's platforms in mind. Even if the protocol-based API looks unusual at first, it fits very well with the rest of the frameworks. Metal is written in Objective-C, is based on [Foundation](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/ObjC_classic/index.html), and makes use of Grand Central Dispatch to synchronize between the CPU and GPU. It is a much more modern abstraction of the GPU pipeline than OpenGL can be without a complete rewrite.\n\n## Metal on the Mac?\n\nIt will only be a matter of time before Metal will be available for OS X, too. The API itself is not limited to the ARM processors that power iPhones and iPads. Most of Metal's benefits are transferable to modern GPUs. Additionally, the iPhone and iPad share RAM between the CPU and GPU, which enables data exchange without actually copying any data. Current generations of Macs don't offer unified memory, but this too will only be a matter of time. Perhaps the API will be adjusted to support architectures with dedicated RAM, or Metal will only run on a future generation of Macs.\n\n## Summary\n\nIn this article, we have attempted to provide a helpful and unbiased introduction to the Metal framework.\n\nOf course, most game developers will not have direct contact with Metal. However, top game engines already take advantage of it, and developers will profit from the newly available power without touching the API itself. Additionally, for those who want to use the full power of the hardware, Metal may enable developers to create unique and spectacular effects in their games, or perform parallel computations much faster, giving them a competitive edge.\n\n## Resources\n\n* [Metal Programming Guide](https://developer.apple.com/Library/ios/documentation/Miscellaneous/Conceptual/MetalProgrammingGuide/Introduction/Introduction.html)\n* [Metal Shading Language Guide](https://developer.apple.com/library/ios/documentation/Metal/Reference/MetalShadingLanguageGuide/Introduction/Introduction.html)\n* [Metal by Example](http://metalbyexample.com)\n"
  },
  {
    "path": "2014-11-10-multipeer-connectivity-for-games.md",
    "content": "---\ntitle:  \"Multipeer Connectivity in Games\"\ncategory: \"18\"\ndate: \"2014-11-10 08:00:00\"\ntags: article\nauthor:\n  - name: JP Simard\n    url: https://twitter.com/simjp\n---\n\nSince its unveiling at WWDC 2013, [Multipeer Connectivity][mpc] (or MPC as we'll refer to it here) has garnered much hype, but relatively few products have successfully integrated it in meaningful ways. So let's see what MPC is and how to leverage it to build impressive experiences, especially in games.\n\n## What is Multipeer Connectivity?\n\nMultipeer Connectivity is an Apple framework that offers transport-agnostic mechanisms for network discoverability, creation, and communication. It's the spiritual successor to [Bonjour][bonjour], which was mostly useful for device discoverability on LAN and Wi-Fi networks.\n\nA key benefit of MPC is that ad-hoc peer-to-peer networks can be created regardless of whether or not existing Wi-Fi or Bluetooth personal area networks are available. Once connected, peers can securely share messages, streams, or file resources.\n\nMost MPC functionality is also available through the higher-level [GameKit framework][gamekit]. Using GameKit to power your game can allow developers to work with very useful game concepts to abstract the underlying networking protocols.\n\nEven though most games will benefit more from integrating GameKit and its game-related abstractions over direct use of MPC, this article should serve as a useful guide for more advanced MPC usage.\n\n## When Should It Be Used?\n\nWhen your game or app may run on multiple devices in close proximity to each other, MPC has the potential of drastically improving the user experience. Whether you're building a remote control or multiplayer game, MPC helps reduce user experience friction, server costs, and even latency.\n\nFor example, a remote control app that avoids any user configuration and automatically connects to the service being controlled immediately after installation can transform your app from good to great. This is true whether it is a remote control for a game, presentation software, a media player, or something else. An open-source example of this is [DeckRocket][deckrocket], an iOS remote for the [Deckset][deckset] presentation app.\n\nMultiplayer game scenarios can also benefit from MPC's zero-configuration and offline connectivity features. For example, a card game app containing game logic, rules, and scorekeeping could allow any two players to instantly start playing, regardless of Internet connectivity. In this article, we'll take some real-world examples from the CardsAgainst app, an open-source iOS version of the popular [Cards Against Humanity][cah] game. The full source to the CardsAgainst app can be found on [GitHub][cardsagainst].\n\nOther examples in this article will be taken from [PeerKit][peerkit], an open-source framework for building event-driven, zero-configuration MPC apps.\n\n## Discovery Setup\n\nThere are several ways to integrate the device discovery aspect of MPC into your app or game. We'll look at three different design patterns that cover a fairly wide variety of use cases.\n\n### The Default Way\n\nApple provides a built-in view controller to facilitate discovering peers and initiating a common session. Simply present an [`MCBrowserViewController`][MCBrowserViewController] with a `serviceType` and `session`, and MPC will do the rest. Note that `serviceType` is limited to 15 ASCII letters, numbers, and dashes. A common approach is to use a style similar to reverse-DNS notation (e.g. `io-objc-mpc`):\n\n```objc\nlet session = MCSession(peer: MCPeerID(displayName: \"Mary\"))\nlet serviceType = \"io-objc-mpc\" // Limited to 15 ASCII characters\nwindow!.rootViewController = MCBrowserViewController(serviceType: serviceType, session: session)\n```\n\n![](/images/issue-18/browser.png)\n\nSince `MCBrowserViewController` is not easily customizable, it's likely that you'll want to provide your own mechanism for selecting peers. That brings us to the next approach.\n\n### The Dedicated Advertiser/Browser Approach\n\nIf your game already requires a mechanism to elect a primary node to coordinate game logic, and secondary nodes to simply attach to the primary one, then you should leverage this information by only advertising from the primary node and browsing from secondary nodes:\n\n![](/images/issue-18/dedicated.gif)\n\n```objc\n// Advertise from the primary node\nadvertiser = MCNearbyServiceAdvertiser(peer: myPeerID, discoveryInfo: discoveryInfo, serviceType: serviceType)\nadvertiser.delegate = self\nadvertiser.startAdvertisingPeer()\n\n// Browse from secondary nodes\nmcBrowser = MCNearbyServiceBrowser(peer: myPeerID, serviceType: serviceType)\nmcBrowser.delegate = self\nmcBrowser.startBrowsingForPeers()\n```\n\nHowever, there are often cases in which it is preferable to establish a connection earlier in the app's lifecycle, without any user input. The next approach demonstrates how to accomplish this.\n\n### The Zero-Config Approach\n\nMPC makes it possible to create truly frictionless user experiences. When integrated properly into your app, your users may start communicating immediately after installing the app from the App Store, with no configuration necessary. This is a powerful way to delight them.\n\n![](/images/issue-18/zero-config.gif)\n\nTo do this, it's possible to both advertise and browse for sessions simultaneously. We'll call this behavior transceiving (transmitting and receiving).\n\nOne challenge that arises when all peers transceive is contention. There can be many peers attempting to mutually connect to each other's advertised sessions. This is a thoroughly researched type of problem called [leader election][leader-election], with several well-known solutions.\n\nA simple but effective way to elect an MPC leader is to include the running time of each node as metadata when inviting peers to join sessions, with advertisers always joining the oldest session:\n\n```objc\n// Browser Delegate Code\nfunc browser(browser: MCNearbyServiceBrowser!, foundPeer peerID: MCPeerID!, withDiscoveryInfo info: [NSObject : AnyObject]!) {\n    var runningTime = -timeStarted.timeIntervalSinceNow\n    let context = NSData(bytes: &runningTime, length: sizeof(NSTimeInterval))\n    browser.invitePeer(peerID, toSession: mcSession, withContext: context, timeout: 30)\n}\n\n// Advertiser Delegate Code\nfunc advertiser(advertiser: MCNearbyServiceAdvertiser!, didReceiveInvitationFromPeer peerID: MCPeerID!, withContext context: NSData!, invitationHandler: ((Bool, MCSession!) -> Void)!) {\n    var runningTime = -timeStarted.timeIntervalSinceNow\n    var peerRunningTime = NSTimeInterval()\n    context.getBytes(&peerRunningTime)\n    let isPeerOlder = (peerRunningTime > runningTime)\n    invitationHandler(isPeerOlder, mcSession)\n    if isPeerOlder {\n        advertiser.stopAdvertisingPeer()\n    }\n}\n```\n\n## Sending and Receiving\n\nMPC offers several ways to send and receive data, each with their own advantages and trade-offs.\n\n### Sending Data\n\nWhen sending small amounts (up to a few kB) of event-driven data, such as game events (start/pause/quit), use the `sendData(_:toPeers:withMode:error:)` function.\n\nTo help encapsulate transmitted data, the CardsAgainst app defines an enum of possible game events, which can then be used to serialize and de-serialize accompanying data:\n\n```objc\n// Possible Game Events\nenum Event: String {\n    case StartGame = \"StartGame\",\n    Answer = \"Answer\",\n    CancelAnswer = \"CancelAnswer\",\n    Vote = \"Vote\",\n    NextCard = \"NextCard\",\n    EndGame = \"EndGame\"\n}\n\n// Reliably send an event to given peers, optionally with accompanying data\nfunc sendEvent(event: Event, object: AnyObject? = nil, toPeers peers: [MCPeerID] = session.connectedPeers as [MCPeerID]) {\n    if peers.count == 0 {\n        return\n    }\n    var rootObject: [String: AnyObject] = [\"event\": event.rawValue]\n    if let object = object {\n        rootObject[\"object\"] = object\n    }\n    let data = NSKeyedArchiver.archivedDataWithRootObject(rootObject)\n    session.sendData(data, toPeers: peers, withMode: .Reliable, error: nil)\n}\n\n// Usage\nsendEvent(.StartGame, [\"initialData\": \"hello objc.io!\"])\n```\n\nSee CardsAgainst's [`ConnectionManager.swift source`](https://github.com/jpsim/CardsAgainst/blob/master/CardsAgainst/Controllers/ConnectionManager.swift) for more information.\n\n#### Reliable vs. Unreliable Transmissions\n\nMuch like the [TCP/UDP dichotomy][tcp-udp], MPC allows sending data in both reliable and unreliable modes. The [`MCSessionSendDataMode`](https://developer.apple.com/library/IOs/documentation/MultipeerConnectivity/Reference/MCSessionClassRef/index.html#//apple_ref/doc/c_ref/MCSessionSendDataMode) contains the values for both modes.\n\nTo send data with the `.Reliable` mode:\n\n```objc\nlet message = \"Hello objc.io!\"\nlet data = message.dataUsingEncoding(NSUTF8StringEncoding)!\nvar error: NSError? = nil\nif !session.sendData(data, toPeers: peers, withMode: .Reliable, error: &error) {\n    println(\"error: \\(error!)\")\n}\n```\n\nIf you're sending data where each byte is essential to the proper functionality of your game, such as starting or pausing your game, use the `.Reliable` mode.\n\nIf speed is prioritized over accuracy or order of transmissions, such as sending sensor data, then the `.Unreliable` mode may be a better fit. Be sure to benchmark this against [streaming](#streaming) to pick the best option for your needs.\n\n### Sending Files\n\nWhen sending large amounts of data (hundreds of kB to several MB), such as files, the `sendResourceAtURL(_:withName:toPeer:withCompletionHandler:)` function should be used. This allows both the sender and receiver to monitor transfer progress through `NSProgress` objects.\n\nHere's a sample taken from [DeckRocket](https://github.com/jpsim/DeckRocket/blob/96e875f784/OSX/DeckRocket/MultipeerClient.swift#L46-L56):\n\n```objc\npdfProgress = session!.sendResourceAtURL(url, withName: filePath.lastPathComponent, toPeer: peer) { error in\n    dispatch_async(dispatch_get_main_queue()) {\n        self.pdfProgress!.removeObserver(self, forKeyPath: \"fractionCompleted\", context: &ProgressContext)\n        if error != nil {\n            HUDView.show(\"Error!\\n\\(error.localizedDescription)\")\n        } else {\n            HUDView.show(\"Success!\")\n        }\n    }\n}\npdfProgress!.addObserver(self, forKeyPath: \"fractionCompleted\", options: .New, context: &ProgressContext)\n```\n\n### Streaming\n\nFor streaming data, such as sensor readings or continuously updating player position information, use the `startStreamWithName(_:toPeer:error:)` function to write to an `NSOutputStream`. The receiver will be able to read from an `NSInputStream`:\n\n```objc\n// Receiver\npublic func session(session: MCSession!, didReceiveStream stream: NSInputStream!, withName streamName: String!, fromPeer peerID: MCPeerID!) {\n    // Assuming a stream of UInt8's\n    var buffer = [UInt8](count: 8, repeatedValue: 0)\n\n    stream.open()\n\n    // Read a single byte\n    if stream.hasBytesAvailable {\n        let result: Int = stream.read(&buffer, maxLength: buffer.count)\n        println(\"result: \\(result)\")\n    }\n}\n```\n\n## Challenges\n\nAs powerful as MPC is, it comes with its own set of challenges. Following are descriptions of a few that you may encounter.\n\n### Availability\n\nMPC is only available on iOS 7, iOS 8, and OS X 10.10. So forget about using MPC with non-Apple hardware, or with anything but the very latest OS X release. Cross-platform apps and games will need to rely on other [alternatives](#alternatives).\n\n### Reliability\n\nThough Apple has made major improvements to MPC's reliability since its launch with iOS 7, reliability remains a sore point of MPC. Failed connections must be accounted for, and require quite a bit of legwork to cover many edge cases.\n\n### Synchronization and Race Conditions\n\nWriting real-time networking code is a lot like writing local multi-threaded code, except with arbitrary delays thrown in due to the lossy nature of wireless connectivity. Make sure to have appropriate locks around essential transmissions to confirm that every peer has acknowledged a critical event, before assuming that the event has been received and moving on.\n\nGames often need to share state, such as whether or not the game is started or paused, or a player has quit. What happens if a player pauses your game just as another deals a fatal blow to an opponent? Asynchronous game logic contention is one area that MPC leaves up to you, the developer. Using frameworks like GameKit can actually go a long way toward centralizing this logic, but this comes at the expense of some flexibility.\n\n## Alternatives\n\nWriting a complex game in MPC will undoubtedly be challenging. Make sure to explore other options before making a decision.\n\n### GameKit\n\nIt's clear that Apple has put a lot of thought into [GameKit][gamekit]. Though it enforces certain models and architectural paradigms, and requires relinquishing some control over session connectivity details, the framework also abstracts away much of the lower-level inner workings.\n\nBuilding your game in GameKit will allow it to work both in peer-to-peer mode and over traditional networks.\n\n### Websockets\n\nThe WebSocket protocol ([RFC 6455][websockets]) allows bidirectional communication between host and client. Each node requires a new websocket connection. The protocol is built over TCP, and therefore doesn't offer MPC's `.Unreliable` message-sending mode. Unlike MPC, websockets don't offer any network creation or device discovery mechanisms, so both host and client must be connected to the same network. Websockets are often used in conjunction with [Bonjour][bonjour].\n\nWebsockets can be appealing for building a cross-platform game or app, or if a connection with a custom backend is required.\n\nSeveral websocket libraries are available both for Swift ([starscream][starscream]) and Objective-C ([SocketRocket][socketrocket], [jetfire][jetfire]).\n\n## Summary\n\nThis article has hopefully shown that integrating Multipeer Connectivity into your game or app can be a relatively painless process that can greatly reduce user experience friction and delight your users.\n\nFor more information on MPC, the following resources might prove useful.\n\n## Resources\n\n* [Multipeer Connectivity Reference][mpc]\n* [Multipeer Connectivity WWDC 2013 Session][mpc-apple-video]\n* [GameKit Reference][gamekit]\n* [NSHipster Article on Multipeer Connectivity][nshipster]\n* [PeerKit: An open-source Swift framework for building event-driven, zero-config MPC apps][peerkit]\n* [CardsAgainst: An open-source iOS game built with MPC][cardsagainst]\n* [DeckRocket: An open-source presentation remote control app for iOS/OSX built with MPC][deckrocket]\n\n[mpc]: https://developer.apple.com/library/IOs/documentation/MultipeerConnectivity/Reference/MultipeerConnectivityFramework/index.html\n[gamekit]: https://developer.apple.com/LIBRARY/ios/documentation/GameKit/Reference/GameKit_Collection/index.html\n[bonjour]: https://www.apple.com/support/bonjour\n[cardsagainst]: https://github.com/jpsim/CardsAgainst\n[peerkit]: https://github.com/jpsim/PeerKit\n[deckrocket]: https://github.com/jpsim/DeckRocket\n[deckset]: http://www.decksetapp.com\n[cah]: http://cardsagainsthumanity.com\n[MCBrowserViewController]: https://developer.apple.com/library/IOs/documentation/MultipeerConnectivity/Reference/MCBrowserViewController_class\n[leader-election]: http://en.wikipedia.org/wiki/Leader_election\n[tcp-udp]: http://en.wikipedia.org/wiki/User_Datagram_Protocol#Comparison_of_UDP_and_TCP\n[websockets]: http://tools.ietf.org/html/rfc6455\n[starscream]: https://github.com/daltoniam/starscream\n[socketrocket]: https://github.com/square/SocketRocket\n[jetfire]: https://github.com/acmacalister/jetfire\n[mpc-apple-video]: https://developer.apple.com/videos/enterprise/#15\n[nshipster]: http://nshipster.com/multipeer-connectivity\n"
  },
  {
    "path": "2014-11-10-scenekit.md",
    "content": "---\ntitle:  \"Scene Kit\"\ncategory: \"18\"\ndate: \"2014-11-10 09:00:00\"\ntags: article\nauthor:\n  - name: David Rönnqvist\n    url: https://twitter.com/davidronnqvist\n---\n\nScene Kit is a Cocoa-style 3D rendering framework that was introduced for OS X back when Apple was still doing cat names (WWDC 2012). It had a good first release as a general 3D _renderer_, and added powerful features like shader modifiers, constraints, and skeletal animations the year after, when Apple introduced its first non-cat OS X: Mavericks. This year (2014), Scene Kit became even more powerful, with support for particle effects, physics simulation, scripted events, and multi-pass rendering, _and_ (perhaps most important to many people) it was introduced to iOS, and Apple now refers to it as \"casual game ready.\" \n\nFrom the start, I found Scene Kit's biggest strength and differentiator to be its integration with other graphics frameworks like Core Image, Core Animation, and now also Sprite Kit. These are not the things you would normally look for in a game engine, but if you are a hobbyist or otherwise mainly a Cocoa (or Cocoa Touch) developer, then this means that a lot of things should already feel familiar.\n\n## Scene Kit Basics\n\nScene Kit is built on top of OpenGL, as a higher-level framework where lights, geometries, materials, and cameras are objects that you work with in your Objective-C or Swift code. If you've used OpenGL in [its earliest releases][opengl1], before shaders, this terminology might bring back bad memories of a restricted system with limited configurability. Luckily, this is not the case. High-level configuration is enough for most common tasks — even more advanced things like dynamic shadows and [depth of field][dof] effects. \n\nWhere that is not enough, Scene Kit allows you drop down to a lower level and configure the rendering with your own OpenGL shader code ([GLSL][glsl]).\n\n\n### Nodes\n\nIn addition to the lights, geometries, materials, and cameras, Scene Kit uses a hierarchy of nodes[^name] to organize its content. Each node has a position, rotation, and scale relative to its parent node, which in turn is relative to _its_ parent node, all the way up to the root node. To give one of the other objects a position in the 3D world, it is attached to one of the nodes. The node hierarchy is managed using methods like:\n\n[^name]: A hierarchy of nodes like this is commonly called a _scene graph_ in 3D graphics. That's one explanation for the name Scene Kit.\n\n- `addChildNode(_:)` \n- `insertChildNode(_: atIndex:)` \n- `removeFromParentNode()` \n\nThese mirror some of the methods that are used to manage the view hierarchies and layer hierarchies on iOS and OS X. \n\n\n### Geometry Objects\n\nScene Kit comes with built-in geometry objects for simple shapes like boxes, spheres, planes, and cones, but for games, you'll mostly be loading 3D models from a file. You can import (and export) [COLLADA files][collada] by referencing a file by name:\n\n```swift\nlet chessPieces = SCNScene(named: \"chess pieces\") // SCNScene?\n```\n\nIf the scene contained in the file should be displayed as is in its entirety, then it can be used as the scene view's scene. If the scene contains multiple objects, but only some of them should be displayed on the screen, then they can be found, referenced by their names, and added to the scene that's being rendered in the scene view:\n\n```swift\nif let knight = chessPieces.rootNode.childNodeWithName(\"Knight\", recursively: true) {\n    sceneView.scene?.rootNode.addChildNode(knight)\n}\n```\n\nThis is a reference to the original node in the imported file and contains any and all child nodes, as well as the geometry objects (with their materials), lights, and cameras that were attached to those nodes. Asking for the child node with the same name again will get you a reference to the same object. \n\n![Node > Geometry > Material > Textures](/images/issue-18/textures.png)\n\nTo have multiple copies in a scene, for example, to display two knights on a chess board, you would either `copy` the node or `clone` it (for a recursive copy). This makes a copy of the node that references the same geometry object with the same materials. Both copies would still point to the same geometry objects. So to change one piece's material, the geometry object would also have to be copied and have a new material attached to it. Copying a geometry object remains fast and cheap, since the copies still refer to the same vertex data. \n\nImported nodes with geometry objects that are rigged for [skeletal animation][skeletal] have a skinner object, which provides access to the skeletal node hierarchy and manages the relationship between the bones and the geometry object. Individual bones can be moved and rotated; however, a more complex animation modifying multiple bones — for example, a character's walk cycle — would most likely be loaded from a file and added to the object.\n\n### Lights\n\nLights in Scene Kit are completely dynamic. This makes them easy to grasp and generally easy to work with, but it also mean that the lighting they provide is less advanced than that of a fully fledged game engine. There are four different types of lights: ambient, directional, omnidirectional (point lights), and spotlights. \n\nIn many cases, specifying a rotation axis and an angle is not the most intuitive way of pointing a light at an object. In these cases, the node that gives the light its position and orientation can be constrained to look at another node. Adding a \"look at\"-constraint also means that the light will keep pointing at that node, even as it moves:\n\n```swift\nlet spot = SCNLight()\nspot.type = SCNLightTypeSpot\nspot.castsShadow = true\n\nlet spotNode = SCNNode()\nspotNode.light = spot\nspotNode.position = SCNVector3(x: 4, y: 7, z: 6)\n\nlet lookAt = SCNLookAtConstraint(target: knight)\nspotNode.constraints = [lookAt]\n```\n\n![Rotating Knight with dynamic shadow](/images/issue-18/spinning.gif)\n    \n### Animation \n\nPretty much any property on any object in Scene Kit is animatable. Just as with Cocoa (or Cocoa Touch), this means that you can create a `CAAnimation` for that key path (even paths like `\"position.x\"`) and add it to the object. Similarly, you can change the value in between the \"begin\" and \"commit\" of a `SCNTransaction`. These two approaches should be immediately familiar, but weren't built specifically for animations in games:\n\n```swift\nlet move = CABasicAnimation(keyPath: \"position.x\")\nmove.byValue  = 10\nmove.duration = 1.0\nknight.addAnimation(move, forKey: \"slide right\")\n```\n\nScene Kit also supports the action-style animation API from Sprite Kit. This allows you to create sequences of animations and run blocks of code as custom actions together with other animations. Unlike Core Animation, these actions run as part of the game loop and update the model value for every frame, not just the presentation node. \n\nIn fact, if you've worked with Sprite Kit before, Scene Kit should look fairly familiar, but in 3D. As of iOS 8 (the first iOS version that supports Scene Kit) and OS X 10.10, the two frameworks (Scene Kit and Sprite Kit) can work together. On the Sprite Kit side of things, 3D models can be mixed with 2D sprites. On the Scene Kit side of things, Sprite Kit scenes and textures can be used as textures in Scene Kit, and a Sprite Kit scene can be used as a 2D overlay on top of a Scene Kit scene[^twoScenes]. \n\n[^twoScenes]: Yes, having two very similar APIs with two very similar concepts (scenes, nodes, constrains, etc. exist in both Scene Kit and Sprite Kit) can get confusing.\n\n## Writing Games in Scene Kit\n\nActions and textures are not the only things that Scene Kit and Sprite Kit have in common. When it comes to writing a game using Scene Kit, it bears a strong resemblance to its 2D counterpart. In both cases, the game loop goes through the same steps, making callbacks to the delegate:\n\n1. Updating the scene\n2. Applying animations / actions\n3. Simulating physics \n4. Applying constraints\n5. Rendering\n\n![Game Loop](/images/issue-18/gameloop.png)\n\nEach callback is sent exactly once per frame and is used to perform gameplay-related logic like input handling, artificial intelligence, and game scripting.\n\n### Input Handling\n\nScene Kit uses the same input mechanisms for keyboard events, mouse events, touch events, and gesture recognition as regular Cocoa and Cocoa Touch apps, the main difference being that there is only one view, the scene view. For keyboard events, or gestures like pinch, swipe, and rotate, it may be fine just knowing _that_ they happened, but events like clicks, and gestures like taps or pans, are likely to require more information about the event. \n\nFor these cases, the scene view can be hit tested using `-hitTest(_: options:)`. Unlike regular views and layers that only return the subview or sublayer that was hit, Scene Kit returns an array of hit test results for each intersection with a geometry object and a ray from the camera through the specified point. Each hit test result contains basic information about what node the hit geometry object belongs to, as well as detailed information about the intersection (the coordinate of the hit, the surface normal at that point, and the texture coordinates at that point). For many cases, it's enough just knowing what the first node that was hit was:\n\n```swift\nif let firstHit = sceneView.hitTest(tapLocation, options: nil)?.first as? SCNHitTestResult {\n    let hitNode = firstHit.node\n    // do something with the node that was hit...\n}\n```\n\n## Extending the Default Rendering\n\nLight and material configurations, while easy to work with, can only get you so far. If you already have existing OpenGL shaders, you can configure a material to use those for rendering, in order to do something completely custom. However, if you only want to modify the default rendering, Scene Kit exposes four points where snippets of shader code (GLSL) can be injected into the default rendering. At the different entry points, Scene Kit provides access to data like transform matrices, geometry data, sampled textures, and the rendered output color. \n\nFor example, these lines of GLSL in the geometry entry points can be used to twist all points in a geometry object around the x-axis. This is done by defining a function to create a rotation transform and applying such a transform to the positions and normals of the geometry object. This also defines a custom [\"uniform\" variable][uniform] that determines how much the object is twisted:\n\n```glsl\n// a function that creates a rotation transform matrix around X\nmat4 rotationAroundX(float angle)\n{\n    return mat4(1.0,    0.0,         0.0,        0.0,\n                0.0,    cos(angle), -sin(angle), 0.0,\n                0.0,    sin(angle),  cos(angle), 0.0,\n                0.0,    0.0,         0.0,        1.0);\n}\n\n#pragma body\n\nuniform float twistFactor = 1.0;\nfloat rotationAngle = _geometry.position.x * twistFactor;\nmat4 rotationMatrix = rotationAroundX(rotationAngle);\n\n// position is a vec4\n_geometry.position *= rotationMatrix;\n\n// normal is a vec3\nvec4 twistedNormal = vec4(_geometry.normal, 1.0) * rotationMatrix;\n_geometry.normal   = twistedNormal.xyz;\n```\n\nShader modifiers can be attached to either a geometry object or to one of its materials. Both classes are fully key-value coding compliant, which means that you can set values for arbitrary keys. Having declared the \"twistFactor\" uniform in the shader modifier makes Scene Kit observe that key and re-bind the uniform variable whenever the value changes. This means that it can be altered using key-value coding:\n\n```swift\ntorus.setValue(5.0, forKey: \"twistFactor\")\n```\n\nIt can also be animated by creating a `CAAnimation` for that key path:\n\n```swift\nlet twist = CABasicAnimation(keyPath: \"twistFactor\")\ntwist.fromValue = 5\ntwist.toValue   = 0\ntwist.duration  = 2.0\n\ntorus.addAnimation(twist, forKey: \"Twist the torus\")\n```\n\n![Animated twisting torus](/images/issue-18/twist.gif)\n\n### Deferred Shading\n\nThere are some graphic effects that can't be achieved in a single render pass, even in pure OpenGL. Instead, different shaders operate in a sequence to perform post-processing or [deferred shading][deferred]. Scene Kit represents such rendering techniques using the [SCNTechnique class][technique]. It is created with a dictionary that defines the drawing passes, their inputs and outputs, shader files, symbols, etc.\n\nThe first pass is always Scene Kit's default rendering, which outputs both the color and the depth of the scene. If you don't want the colors to be shaded, the materials can be configured to have the \"constant\" lighting model, or all lights in the scene can be replaced with a single ambient light.\n\nFor example, by getting the depth from Scene Kit's initial pass and the normals from a second pass, and performing edge detection on both of them in a third pass, you can draw strong contours both along the outline and along edges:\n\n![Bishop with strong contours](/images/issue-18/bishop.png)\n\n---\n\n**Further Reading**\n\nIf you want to learn more about making games using Scene Kit, I recommend watching the [\"Building a Game with Scene Kit\" video][wwdc game] from this year's WWDC and looking at the [Bananas sample code][bananas].\n\nAdditionally, if you want to learn about Scene Kit in general, I recommend watching [this year's][wwdc 13] and [last year's][wwdc 14] \"What's New in Scene Kit\" videos. If you still want to learn more, you can check out [my upcoming book][book] on the topic.\n\n\n  \n[collada]: https://en.wikipedia.org/wiki/COLLADA\n\n[skeletal]: https://en.wikipedia.org/wiki/Skeletal_animation\n\n[opengl1]: https://www.opengl.org/documentation/specs/version1.1/glspec1.1/node30.html#SECTION005130000000000000000\n[glsl]: https://en.wikipedia.org/wiki/OpenGL_Shading_Language\n[dof]: https://en.wikipedia.org/wiki/Depth_of_field\n[uniform]: https://www.opengl.org/wiki/Uniform_(GLSL)\n[deferred]: https://en.wikipedia.org/wiki/Deferred_shading\n\n[wwdc game]: https://developer.apple.com/videos/wwdc/2014/?id=610\n[bananas]: https://developer.apple.com/library/ios/samplecode/Bananas/Introduction/Intro.html\n\n[technique]: https://developer.apple.com/library/ios/documentation/SceneKit/Reference/SCNTechnique_Class/index.html\n\n[wwdc 13]: https://developer.apple.com/videos/wwdc/2013/?id=500\n[wwdc 14]: https://developer.apple.com/videos/wwdc/2014/?id=609\n\n[book]: http://scenekitbook.com\n"
  },
  {
    "path": "2014-11-10-sound-design.md",
    "content": "---\ntitle:  \"Virtual Soundscapes: The Art of Sound Design\"\ncategory: \"18\"\ndate: \"2014-11-10 07:00:00\"\nauthor:\n  - name: Janie Clayton\n    url: http://twitter.com/redqueencoder\ntags: article\n---\n\nTake a moment to just close your eyes and listen to the world around you. If you are in an office, you probably hear people typing and phones ringing. You might hear the drone of the heater or the air conditioner going on in the background. You hear your coworkers. You hear their footsteps and their conversation about working on a particularly nasty bug.\n\nSound is all around us. And it is such an integral part of our world that we just absorb and accept all the ambient noises that fill our daily lives, without realizing just how many there are. It's no wonder that people who have been born hearing impaired and later received cochlear implants have been driven insane by all the sounds that we subconsciously absorb and process.\n\nBecause it's a large part of our everyday experiences, sound is also becoming increasingly important in game development. But one of the biggest challenges with fully immersive virtual reality is designing a realistic soundscape. If you were in an immersive virtual forest, but didn’t hear the sound of leaves rustling and insects chirping, you wouldn’t feel like it was real. \n\nIn some ways, sound design is a thankless task. If you do everything right, no one will notice, but people sure as heck will notice if you do something wrong. As such, it is painstaking, detail-oriented work that is virtually overlooked. However, the joy that you feel when you nail an awesome sound effect or a realistic soundscape makes up for the lack of recognition you get for the hard work you put into it. Mostly.\n\n## Recording Sound\n\nRecording sound is very similar to functional programming: you want to eliminate side effects. One massive side effect that can ruin your recording is reverb. Reverb is the effect you hear when sound bounces off of reflective surfaces and creates an echo effect. \n\nIf you want a good idea of what reverb is like, walk into a large public bathroom. Bathrooms have lots of nice sound-reflecting surfaces and very little sound absorption. Drop something on the floor and talk really loudly. Listen to how this sounds. This is a very distinctive side effect that, if your game does not take place in a bathroom, would sound incredibly out of place.\n\nReverb is not something you can “fix in post.” There are no audio filters or plug-ins to eliminate reverb. You can add reverb to a recording, but once you compile and render the recording, you are stuck with it. Trying to take reverb out of a sound file is like trying to remove the eggs from the cake you just pulled out of the oven. It really isn’t going to happen, so you need to make sure that you try to isolate your recording from as much of this as possible before you begin.\n\nYou don’t need to go to a professional studio, but if you could, that would be awesome. If you are trying this at home, do what you can to deaden the noise in the room as much as possible. I have seen people cover the walls with blankets. If you want to embrace your inner Martha Stewart, you can repurpose egg cartons by chaining them together and hanging them on the walls. If that seems like too much work, or simply strikes you as bizarre, just be sure to find the smallest room you can.\n\nAlso, make sure your microphone is as close to your sound source as possible. You not only want to avoid recording reverb — you also want to avoid recording noise. Anyone who has been on a conference call where the other team is in a room with one recording device in the middle knows that people trying to talk into a microphone that is 10 feet away sound terrible. There is a lot of noise between the microphone and the person, so everything sounds staticky. \n\nWe can’t completely eliminate noise, but fortunately there are tools that can remove noise if you record your sounds properly. One product on the market, [iZotope’s RX 4](https://www.izotope.com/en/products/audio-repair/rx/), can analyze the noise and remove it from your recording. However, RX 4 costs more than Logic, so even though you can remove noise in post, the cheapest solution is to avoid recording it in the first place. Noise is a side effect. Avoid side effects as much as possible.\n\nAdditionally, when recording a sound, be sure to do some pre-roll and post-roll recording. Pre-roll is recording a couple of seconds of silence before capturing your sound. Post-roll, like pre-roll, is recording several seconds of silence after\nyou finish your recording. This is important, because you want to make sure that you capture all of your sound. Many sounds will have an acoustic tail, and it would be really unfortunate to cut off the end of that tail. Remember that you can always edit more out, but you can't go back and edit more in.\n\n## Microphones\n\nIf you are serious about making the best sound design experience you can for your users, there are a few essential tools that I recommend you invest time and money into.\n\nThe first, and only, indispensable tool that you absolutely must buy is a decent microphone. Using your laptop microphone to record sounds for your game is absolutely unacceptable. Your laptop microphone is sufficient for talking to people on Skype, but it was never designed or intended for professional sound quality or work. Your laptop microphone can’t be tuned or modified. It can’t be controlled or targeted in any meaningful way. You need to have more control over your tools than you can get with the microphone built into your computer.\n\nThere are several kinds of microphones out on the market, which range from $10 to thousands of dollars. The cheapest and most primitive type of microphone is a dynamic coil microphone. If you were an AV nerd in high school and helped set up the sound for school assemblies, the microphones you worked with were most likely dynamic coil microphones. Dynamic coil microphones are very robust. You can drop them off a building or run them over with a car and they will still work. As such, they tend to not be particularly sensitive and won’t pick up on subtleties and nuances. An external dynamic microphone, even if you steal it from your Rock Band setup, is still a vast improvement over your laptop microphone, but it is the minimum viable product.\n\nAnother type of microphone you probably won’t see very often is a ribbon microphone. Ribbon microphones were developed to make up for the weaknesses in dynamic coil microphones. Ribbon microphones are super sensitive, but that increased sensitivity makes them incredibly fragile and expensive. These microphones are great for high-quality vocal recordings, but for our purposes, they are not ideal.\n\nThe type of microphone I recommend you invest in is a condenser microphone. Condenser microphones are the best of both worlds. They are far more sensitive than dynamic coil microphones, but they have comparable sensitivity to ribbon microphones. Condensers are more expensive than dynamic coil microphones, but less expensive than ribbon microphones.\n\nFor convenience purposes, I would recommend buying a microphone that can interface directly with the computer via USB. When I was learning audio engineering, you needed to purchase an external mixer and other hardware to get your microphone to interface with the computer. With the advent of podcasting and home audio/video production, many easy and low-cost solutions have appeared on the market. \n\nA decent USB-connected condenser microphone can be found on Amazon for about 50 bucks. You can pay more for a USB microphone, but I have not really seen any that are more than $150. Considering that a decent, non-USB condenser microphone was more than $500 five years ago, this really is not that bad. A good microphone is worth every penny you invest in it.\n\nAnother thing to keep in mind when picking out a microphone is its polar pattern. Polar patterns are also called pick-up patterns. Not all microphones pick up sounds all around them. The ones that do are called omnidirectional. Omnidirectional microphones are not ideal for our purposes. Even if you are able to isolate your sound source, your microphone will still pick up ambient noise.\n\nA better polar pattern is a cardioid polar pattern. Cardioid patterns are heart shaped and have a dead spot behind them. This pattern really helps you isolate your sound source. There are two flavors of cardioid: super and hyper. Both of these cardioid patterns pick up a small amount of sound behind them, but both are far better than plain vanilla omnidirectional.\n\n## Digital Audio Workstations (DAWs)\n\nIf you are serious about sound design, you will need to invest some time and probably some money into a digital audio workstation (DAW). You have a free DAW included on your Mac, GarageBand. GarageBand has improved greatly over the last few years, but it is still a rather limited piece of software. GarageBand is primarily targeted at people who want to make music, rather than people who want to design sound effects for a game. So, you can spend a lot of time trying to force GarageBand to do a job it wasn’t really designed to do, or you can spend a little more money and buy a much better tool.\n\nI highly recommend purchasing [Logic Pro X](https://www.apple.com/logic-pro/). Logic Pro is Apple’s upgrade to GarageBand. Logic Pro costs $200 and, unfortunately, it does not have upgrade pricing. However, Logic’s price has dropped significantly over the last few years. Back in 2007, Logic 7 cost $1,000 and required an external software dongle. Logic 8 cost half that, and the last two versions of Logic Pro have stabilized at around $200. You could buy the last three versions of Logic for less than the price of Logic 7.\n\nSimilarly, if we jump in the Wayback Machine, we can see that buying any DAW used to be incredibly expensive. The industry standard DAW 10 years ago, [Pro Tools](http://www.avid.com/US/products/pro-tools-software), was prohibitively expensive. If you bought Pro Tools, you also had to buy an external piece of hardware for the software to work. The barebones Pro Tools setup was $2,000 minimum. Pro Tools also did not come with any plugins or functionality. All you could do with a barebones system was record and edit sound. There were no filters, no effects, and no virtual instruments. Everything came separately, and the cost of a tricked-out Pro Tools DAW could run you $10,000 easily.\n\nAll of these things that you had to buy in addition to a Pro Tools rig are already included in Logic Pro. Logic Pro comes with a large library of royalty-free special effect sounds and music loop components. If you don’t know how to play an instrument but would still like to put together your own soundtrack, playing around with the built-in Apple Loops is a good way to explore. GarageBand has some Apple Loops, but not nearly as many.\n\nLogic Pro also includes 20 audio effect plugins to modify pitch, remove noise, and add the reverb that you actually want to include in your sound. These tools are invaluable to crafting a unique set of sounds for your games.\n\nAnother DAW that I have used and enjoyed working with is [Reason](https://www.propellerheads.se/products/reason/). Reason is available as a free download for trial use, but you can’t save any of your projects if you don’t purchase the software. This is still great, because you can download and play around with it to see if you like working with it, without having to invest money up front. Additionally, Reason has a lot of third-party instrument and sound libraries available to it, making it incredibly powerful and versatile.\n\nDAWs are kind of like programming languages. Once you master one, it isn't hard to take that knowledge and apply it to other DAWs. In many ways, the one you choose to work with is all a matter of taste and what clicks for you. Each of them have their own unique set of features, but much of the base functionality is the same. The ones I mentioned are ones I learned and enjoyed using that also fit into an indie game developer budget.\n\nThese are serious tools that take a bit of time to master, but there are a multitude of resources out in the world in the form of books and online tutorials. If you had the patience to master either a programming language or a gaming engine, you should be able to master these tools with some hard work and practice. Once you have an understanding of how all of these tools work, the only limit to what you can do with them is what you can imagine.\n\n## Foley\n\nFoley is the art of reproducing and recording everyday sounds to add into your game project. One of the more famous examples of foley that all geeks should be familiar with is the use of coconuts to reproduce the sound of a horse’s hooves. \n\nWith the above example, it is clear that you do not have to record the exact sound in order to get an approximation of it for your game, and sometimes it's even the better choice. One great example of a time when you would not want to use the direct sound source is when you have a gunshot. If you have ever heard a real gun shooting, you might have realized that it counterintuitively sounds fake. Instead, you want to look for something that creates a nice, sharp popping noise. One common tool used for this is a staple gun, but my personal favorite is to use a sheet of bubble wrap, the kind with the really large bubbles. I freaked out a bunch of people in my distance learning class when I popped one of those right next to the microphone before class one day. In retrospect, that probably wasn't the wisest idea in the world.\n\nThere are many tutorials available for the budding foley artist, but the best tools you have as a foley artist are your ears and your imagination. I had a sound design project where I had to recreate the sound of several objects rolling across a screen. I remembered when I was a child that I had a marble machine that I would drop marbles into and they would roll down several tracks before landing in a bucket at the bottom with a satisfying plop. I bought some marbles and recorded the sound of them traveling around and dropping. I then went into Logic and I pitch shifted the sound of the marbles so that the sound was lower for larger objects and higher for smaller objects.\n\nThink about what something should sound like, and try to think about whether you have any memories of sounds that could work. Keep your ears open for anything you hear or observe that makes interesting sounds. If you are looking for inspiration, [read a bit about how Ben Burtt designed the sound effects in Star Wars.](http://filmsound.org/starwars/)\n\n## Apple Loops and Other Prebuilt Sounds\n\nSince we are all busy game designers and software engineers, we don’t necessarily have a week to spend futzing around with Logic and carefully handcrafting our own custom sounds. That said, there are resources out there for the busy software engineer who just needs the sound of a dinosaur roaring right away.\n\nI have already briefly mentioned Apple Loops. Apple Loops are royalty-free sound snippets that come with both GarageBand and Logic. They used to be sold separately from Apple, but as of Logic 8, all Apple Loops come with Logic.\n\nThese can be used as is without royalty or attribution. Even though there are a few thousand loops to choose from, there will probably only be a few that meet your needs. It is helpful to understand audio filters, so that you can modify these sounds to make them unique, but it will be a little bit like the Taco Bell menu, where you have five ingredients that can only be combined in a finite number of ways. These are a great starting point, but after a while, all of your sounds will be the same, and nothing is going to stand out from anyone else doing the same thing. As such, you will probably want to explore some more specialized sounds.\n\nThere are many websites that have both free, open-source sounds and proprietary sounds that you would pay a nominal licensing fee to use. Two for-pay websites with a good selection of sounds are [Big Fish Audio](http://www.bigfishaudio.com) and [Audio Jungle.](http://audiojungle.net) There are many free sound sites online, but they will not have as good a selection, and you will probably spend a lot more time looking for what you want. Convenience has a cost, so figure out how much time you will save and how much that is worth when making the determination about whether you will make, buy, or find your audio.\n\nA word of warning about online sounds: Make sure you understand the rights you have associated with those sounds. Make sure you have a license that permits you to include the sounds in your project and that you pay for such rights if you find a sound you want to use. The people who are creating these sounds worked very hard on them. We want people to pay for our software, so we should be willing to pay for a sound that can really set our game apart from the rest of the pack.\n\n## Audio Filters\n\nI have mentioned audio filters a few times already in this article, so now seems like a good time to begin explaining what they are and how you can use them in your projects.\n\nThere are a lot of really cool effects filters out there on the market, but generally speaking, you will be using filters to clear out noise and tune your sound more than you will to create really wonky sound effects. Let’s go over some of the more common audio filters you will be seeing and using most frequently.\n\nI have mentioned trying to eliminate reverb from your audio projects, because it can be added in later in a way that you control. One important set of filters you will be dealing with are reverb filters. You can design a reverb pattern around general and specific types of architectures. Some reverb software was created by taking response patterns from specific places like the Sistine Chapel and Grand Central Station. If you are writing a game that takes place in a real location, it is possible to design your reverb to exactly match the location your game takes place in. You usually don’t need this level of detail, but if you are a massive audio geek, knowing this is possible is a really exciting thing.\n\nAdditional audio filters you should familiarize yourself with are high- and low-pass filters. These filters are pretty self-explanatory. A high-pass filter lets higher frequencies go through, and a low-pass filter only lets lower frequencies through.\n\nHumans generally can hear sounds up to around 20,000 Hz. If you've ever wondered why the sample rate for CDs is 44.1 kHz, it has to do with science. There is a formula called the [Nyquist Theorem](http://en.wikipedia.org/wiki/Nyquist–Shannon_sampling_theorem), which states that if you want to accurately capture a sound, you need to sample it at _at least_ twice the highest frequency. Sound is a wave that has both a compression and a rarefaction. If you think back to high school trigonometry when you programmed a sine wave on your graphing calculator, you noticed that the wave traveled above and below the y-axis. If you wanted to measure that wave, you would have needed to make sure to capture both where the wave went above the axis and where it went below it.\n\nAs we age, many of us lose the ability to hear sounds at these higher frequencies, especially if we have destroyed our hearing blasting death metal and cranking the volume on our Call of Duty sessions. However, not everyone loses the high end of his or her hearing. I personally know someone who can hear dog whistles. There are a lot of sounds that most of us can’t hear, but there are people out there who can. It doesn’t hurt to run your sounds through a low-pass filter and filter out anything over 15 kHz, just to clear out some of the garbage that might bother our supernaturally ultrasonic listeners.\n\nLikewise, filtering out some of the wonky lower frequencies can clean up your sound. Utilizing both a high-pass and a low-pass filter is called a band-pass filter. You are specifying that you only want to use frequencies within a contained band of frequencies.\n\nSpeaking of frequencies, I wanted to briefly mention a little bit about pumping the bass. I know that back in the day when we used to listen to our music through stereo systems, the popular thing to do was crank the bass. You might be tempted while creating your sounds to crank the bass in your equalizer, but don’t do that. Here is a pro tip: It is easier to remove frequencies than it is to add them in. If you want to increase the lower frequencies in your sounds, decrease the higher frequencies. Frequencies cancel one another out, and by directly removing the frequencies you don’t want to hear, you are increasing the ones that you do want to hear.\n\nDepending on what DAW you are using, you should have access to a bunch of audio effects plugins. There are plugins that speed up and slow down your sounds, either with pitch shift or without. I used to have a plugin that would 'smear' two sounds together. Another iZotope product, [Trash 2](https://www.izotope.com/en/products/effects-instruments/trash/), lets you selectively distort and mangle your sound in a highly controlled way. There are so many awesome effects plugins out on the market that I really can’t go into all of them. The best way to work with these effects is to just play with them and see what they do. Again, your ears and your imagination are the most valuable tools in your toolbox. If you look for plugins and you find one that looks cool, see if you can have a trial playing around with it to see if you like the sounds you can create with it.\n\n## Realistic vs. Unrealistic Sound Design\n\nI know, you might be wondering why you would want to create unrealistic sound design. Hear me out.\n\nYour approach to sound design is going to be radically different between a platform game like Super Mario Bros. and a cinematic game like Heavy Rain. Trying to create realistic sound effects for Mario bashing his head against a brick isn’t going to work as well as the cute 8-bit sounds created when Mario jumps and collects coins.\n\nMeanwhile, when you are dealing with a cinematic game, it is vitally important to pay a lot of attention to everything going on in your scene to make sure you are including anything that your player might be hearing. If your game takes place in a forest, think about what sounds you hear when you are walking through the woods. If your game is a first-person shooter where your character is running through a hallway, remember to add footsteps and the appropriate amount of reverb.\n\nNote that even within a realistic game, it is sometimes necessary to take some liberties with making everything sound exactly realistic. There are a multitude of various TV shows and films that take place in space. Nearly all of them utilize some kind of sound design, even though space is a vacuum and realistically there should be no sound. Sound is generated in these films because we psychologically expect to hear noise when something explodes. \n\nOne cool thing you can do if you are using AV Foundation for your sounds is use the pan property. The pan property determines how much sound gets directed to either the left or right speaker in the case of stereo sound. If you have a rocket or something than generates sound and travels across the screen, you can tell the program to set the pan to the projectile’s location so that your player can hear the rocket whizzing through his or her head. Setting this property and paying attention to positional sound design really brings your soundscape to the next level. Anything you can do to immerse your player in the virtual world you have created is a good thing.\n\nIn some ways, realistic sound design is simpler than unrealistic sound design. You know what a gunshot is supposed to sound like. You know what a car crash is supposed to sound like. There are a lot of sound libraries out there that provide common realistic sounds. Creating a unique sound style for your game can be incredibly challenging. However, if you are able to accomplish something unique, you will give your game a tremendous boost. Think of how many instantly recognizable sounds came from Star Wars. No one can use any of those sounds without instantly bringing the film to mind.\n\nI highly recommend becoming familiar with synthesizers if you are working with unrealistic sound design. Many highly recognizable sound effects are a series of musical tones. Synthesizers offer you a great deal of customization and flexibility to develop a sound personality for your game. Both Reason and Logic come with several highly capable virtual synthesizers. Synthesizers are powerful, complex tools, but like all such things, take some time and patience to master fully.\n\nAnother tip I want to pass along is to utilize natural sounds. There is a fairly well-known hoax that claims [if you slow down a recording of crickets, it sounds like humans singing.](http://www.snopes.com/critters/gnus/cricketsong.asp) The track passed around was created by layering the sounds multiple times and manipulating the speed and pitch of each layer. Slowed-down crickets may not sound exactly like humans singing, but with a lot of work they did sound radically different.\n\nModifying natural sounds by speeding them up or slowing them down and shifting their pitches up and down can create completely unique sounds that still have enough of a familiar undertone that they don’t sound wholly unrealistic or out of place. The sound effect that the TIE fighters make as they fly by in Star Wars is a modified howler monkey cry. The sound is unusual, but it is also familiar. We don’t think about what its base component is because we are taking it out of its original context. There is a lot of potential and flexibility to take modified animal and insect noises and place them in completely different and unexpected ways. Most of the time, your player won’t find the sound out of place if you did your job right.\n\n## Ambient Sounds\n\nOne alternative you have to composing or commissioning a soundtrack is to generate an ambient soundscape for your game.\n\nThe best example I can think of for this is the game Myst. Myst had some short bits of soundtrack, but one of the big selling points of the game was this idea that you were wandering around this virtual world. Standing on the dock listening to the waves lapping against the shore and hearing the dock creaking under your feet created a far more realistic feel than you would have gotten if the game designers had had a relentless looping soundtrack in the background. \n\nAmbient soundscapes work really well if you have a story-based game. Strategically withdrawing sound during intense moments in your story is a great way to build tension. You know that moment in the movies when everything feels a little too quiet and it makes you uneasy? Sound is as much about what you don’t hear as it is what you do.\n\nOne thing to watch out for when creating ambient soundscapes is to avoid overdoing things. Think back to our introductory example of the ambient sounds in an office. It is really easy to go crazy adding a lot of ringing phones and slurping coffee. Coco Chanel once famously said: “Before you leave the house, look in the mirror and remove one accessory.” Have fun going overboard generating your soundscape, but be sure to go back through and tone it down a lot before you ship your game. A little goes a long way. You want your sound to be subtle and not obtrusive. It is there to enhance the experience, not overwhelm it.\n\n## Sound Mixing\n\nOne problem I have observed, especially in media that has voice acting, is that not enough care has been taken with sound mixing. A major complaint I hear from a lot of people when I tell them I do sound design is that when they are watching something, they can’t hear the dialogue because the soundtrack is too loud.\n\nI know, you have an awesome, kick-ass soundtrack. I know your soundtrack is the most amazing soundtrack ever. I know that you love to blast the killing music that plays during your final boss battle when you are coding like a rockstar. I get it.\n\nPeople are not playing your game to listen to the soundtrack.\n\nYour soundtrack, no matter how awesome, should never overpower the rest of the sound in your game, especially if that sound is dialogue that is necessary for your player to hear to understand what is going on.\n\nRight now, go to your game. Adjust your soundtrack to how loud you think it should be relative to everything else. Do you have it where you think it should be? Good. Now make it half as loud. Now it should be about where it needs to be in order to do its job of enhancing your game rather than overpowering it. You may adjust it upward again if your beta testers tell you they want it louder. If you are not using beta testers, you should be ashamed of yourself.\n\n## Takeaways\n\nIf you looked at the article and determined that it was too long but would still like a few tips to help you along your way, here are a few takeaways from the article:\n\n- Buy decent tools.\n- Take the time to learn how to use them.\n- Noise and reverb are the enemy. Avoid them at all costs.\n- Determine whether you want realistic or cartoon-like sound effects.\n- Don’t let your soundtrack overwhelm the rest of the sound in your game.\n- Close your eyes and open your ears. You can learn how to master every single audio tool on the market and it will all be worthless if you don’t use your imagination to think about what something should sound like.\n\nSound design is an awesome, fulfilling, creative art. Being able to walk into a world that didn't exist before and dictating what it sounds like is an amazing thing to be able to do. I went into programming for the same reasons I love sound design. It gave me a chance to start with nothing and create something that didn't exist before.\n\nWe all got into this business to create new worlds. Too often we focus just on what that world should look like without thinking about what it should sound like. Show your world a little bit of love and give it its own voice.\n"
  },
  {
    "path": "2014-12-08-activity-tracing.md",
    "content": "---\ntitle:  \"Activity Tracing\"\ncategory: \"19\"\ndate: \"2014-12-08 07:00:00\"\ntags: article\nauthor:\n  - name: Florian Kugler\n    url: https://twitter.com/floriankugler\n---\n\n\nTracking down crashes in asynchronous code is often very hard, because the stack trace is confined to the crashed thread and you're missing contextual information. At the same time, writing asynchronous code has become significantly easier with APIs like [libdispatch](/issues/2-concurrency/low-level-concurrency-apis/), operation queues, and [XPC](/issues/14-mac/xpc/).\n\nActivity tracing is a new technology introduced in iOS 8 and OS X 10.10 that aims to alleviate this problem. This year's WWDC had an excellent [session][wwdcsession] about it, but we thought it would be a good idea to give another overview here, since it is not widely known yet.\n\nThe basic idea is that work done in response to user interactions or other events is grouped under an activity, no matter if the work is done synchronously or if it's dispatched to other queues or processes. For example, if the user triggers a refresh in your app, you'll know that this particular user interaction caused a subsequent crash, even if it happens on a different queue and several other code paths could have led to the crashing code as well.\n\nActivity tracing has three different parts to it: activities, breadcrumbs, and trace messages. We'll go into those in more detail below, but here's the gist of it: Activities allow you to trace the crashing code back to its originating event in a cross-queue and cross-process manner. With breadcrumbs, you can leave a trail of meaningful events across activities leading up to a crash. And finally, trace messages allow you to add further detail to the current activity. All this information will show up in the crash report in case anything goes wrong.\n\nBefore we go into more detail, let me just quickly mention a potential pitfall when trying to get activity tracing to work: if the activity messages are not showing up, check the `system.log` for any messages from the `diagnosticd` daemon, like \"Signature Validation Failed\" — you might be running into code signing issues. Also, note that on iOS, activity tracing only works on a real device, and not in the simulator.\n\n\n## Activities\n\nActivities are at the heart of this new technology. An activity groups together the code executing in response to a certain event, no matter on what queues and in what processes the code is executing. This way, if anything goes wrong in the middle, the crash can be traced back to the original event.\n\nActivity tracing is integrated into AppKit and UIKit, so that an activity is started automatically whenever a user interface event is sent through the target-action mechanism. In case of user interactions that don't send events through the responder chain (like a tap on a table view cell), you'll have to initiate an activity yourself.\n\nStarting an activity is very simple:\n\n```objc\n#import <os/activity.h>\n\nos_activity_initiate(\"activity name\", OS_ACTIVITY_FLAG_DEFAULT, ^{\n    // do some work...\n});\n```\n\nThis API executes the block synchronously, and everything you do within the block will be scoped under this activity, even if you dispatch work onto other queues or do XPC calls. The first parameter is the label of the activity, and has to be provided as a constant string (like all string parameters of the activity tracing API).\n\nThe second parameter, `OS_ACTIVITY_FLAG_DEFAULT`, is the activity flag you use to create an activity from scratch. If you want to create a new activity within the scope of an existing activity, you have to use `OS_ACTIVITY_FLAG_DETACHED`. For example, when reacting to an action message of a user interface control, AppKit has already started an activity for you. If you want to start an activity from here that is not the direct result of the user interaction, that's when you'd use a detached activity.\n\nThere are other variants of this API that work in the same away — a function-based one (`os_activity_initiate_f`), and one that consists of a pair of macros:\n\n```objc\nos_activity_t activity = os_activity_start(\"label\", OS_ACTIVITY_FLAG_DEFAULT);\n// do some work...\nos_activity_end(activity);\n```\n\nNote that activities will not show up in crash reports (or other ways of inspecting them) if you don't set at least one trace message. See below for more details on trace messages.\n\n\n## Breadcrumbs\n\nBreadcrumbs are used for what the name suggests: your code leaves a trail of labeled events while it executes in order to provide context across activities in case a crash happens. Adding a breadcrumb is very simple:\n\n```objc\nos_activity_set_breadcrumb(\"event description\");\n```\n\nThe events are stored in a ring buffer that only holds the last 50 events. Therefore, this API should be used to indicate macro-level events, like meaningful user interactions.\n\nNote that this API only has an effect from within the scope of an activity: it marks the current activity as a breadcrumb. This also means that you can only do this once per activity; subsequent calls will be ignored.\n\n\n## Trace Messages\n\nTrace messages are used to add additional information to activities, very similar to how you would use log messages. You can use them to add valuable information to crash reports, in order to more easily understand the root cause of the problem. Within an activity, a very simple trace message can be set like this:\n\n```objc\n#import <os/trace.h>\n\nos_trace(\"my message\");\n```\n\nTrace messages can do more than that though. The first argument to `os_trace` is a format string, similar to what you'd use with `printf` or `NSLog`. However, there are some restrictions to this: the format string can be a maximum of 100 characters long and can contain a placeholder for up to seven *scalar* values. This means that you cannot log strings. If you try to do so, the strings will be replaced by a placeholder.\n\nHere are two examples of using format strings with `os_trace`:\n\n```objc\nos_trace(\"Received %d creates, %d updates, %d deletes\", created, updated, deleted);\nos_trace(\"Processed %d records in %g seconds\", count, time);\n```\n\nOne caveat that I stumbled upon while experimenting with this API is that trace messages don't show up in crash reports if no trace messages are sent from the crashing thread.\n\n\n### Trace Message Variants\n\nThere are several variants to the basic `os_trace` API. First, there's `os_trace_debug`, which you can use to output trace messages that only show up in debug mode. This can be helpful to reduce the amount of trace messages in production, so that you will only see the most meaningful ones, and don't flood the limited ring buffer that's used to storing those messages with less useful information. To enable debug mode, set the environment variable `OS_ACTIVITY_MODE` to `debug`.\n\nAdditionally, there are two more variants of these macros to output trace messages: `os_trace_error` and `os_trace_fault`. The first one can be used to indicate unexpected errors, and the second one to indicate catastrophic failures, i.e. that you're about to crash.\n\nAs discussed above, the standard `os_trace` API only accepts a constant format string of limited length and scalar values. This is done for privacy, security, and performance reasons. However, there are situations where you'd like to see more data when debugging a problem. This is where payload trace messages come in.\n\nThe API for this is `os_trace_with_payload`, and may seem a bit weird at first: similar to `os_trace`, it takes a format string, a variable number of value arguments, and a block with a parameter of type `xpc_object_t`. This block will not be called in production mode, and therefore poses no overhead. However, when debugging, you can store whatever data you want in the dictionary that the block receives as its first and only argument:\n\n```objc\nos_trace_with_payload(\"logged in: %d\", guid, ^(xpc_object_t xdict) {\n    xpc_dictionary_set_string(xdict, \"name\", username);\n});\n```\n\nThe reason that the argument to the block is an XPC object is that activity tracing works with the `diagnosticd` daemon under the hood to collect the data. By setting values in this dictionary using the `xpc_dictionary_set_*` APIs, you're communicating with this daemon. To inspect the payload data, you can use the `ostraceutil` command line utility, which we will look at in more detail below.\n\nYou can use payloads with all previously discussed variants of the `os_trace` macro. Next to `os_trace_with_payload` (which we used above), there's also `os_trace_debug_with_payload`, `os_trace_error_with_payload`, and `os_trace_fault_with_payload`.\n\n\n## Inspecting Activity Tracing\n\nThere are two ways you can get to the output of activity tracing aside from crash reports. First, activity tracing is integrated into the debugger. By typing `thread info` into the LLDB console, you can inspect the current activity and the trace messages from the current thread:\n\n```\n(lldb) thread info\nthread #1: tid = 0x19514a, 0x000000010000125b ActivityTracing2`__24-[ViewController crash:]_block_invoke_4(.block_descriptor=<unavailable>) + 27 at ViewController.m:26, queue = 'com.apple.main-thread', activity = 'crash button pressed', 1 messages, stop reason = EXC_BAD_ACCESS (code=1, address=0x0)\n\n  Activity 'crash button pressed', 0x8e700000005\n\n  Current Breadcrumb: button pressed\n\n  1 trace messages:\n    message1\n```\n\nAnother option is to use the `ostraceutil` command line utility. Executing\n\n```\nsudo ostraceutil -diagnostic -process <pid> -quiet\n```\n\nfrom the command line (replace `<pid>` with the process id) yields the following (shortened) information:\n\n```\nProcess:\n==================\nPID: 16992\nImage_uuid: FE5A6C31-8710-330A-9203-CA56366876E6\nImage_path: [...]\n\nApplication Breadcrumbs:\n==================\nTimestamp: 59740.861604, Breadcrumb ID = 6768, Name = 'Opened theme picker', Activity ID: 0x000008e700000001\nTimestamp: 59742.202451, Breadcrumb ID = 6788, Name = 'button pressed', Activity ID: 0x000008e700000005\n\nActivity:\n==================\nActivity ID: 0x000008e700000005\nActivity Name: crash button pressed\nImage Path: [...]\nImage UUID: FE5A6C31-8710-330A-9203-CA56366876E6\nOffset: 0x1031\nTimestamp: 59742.202350\nReason: none detected\n\nMessages:\n==================\nTimestamp: 59742.202508\nFAULT\nActivity ID: 0x000008e700000005\nTrace ID: 0x0000c10000001ac0\nThread: 0x1951a8\nImage UUID: FE5A6C31-8710-330A-9203-CA56366876E6\nImage Path: [...]\nOffset: 0x118d\nMessage: 'payload message'\n----------------------\nTimestamp: 59742.202508\nRELEASE\nTrace ID: 0x0000010000001aad\nOffset: 0x114c\nMessage: 'message2'\n----------------------\nTimestamp: 59742.202350\nRELEASE\nTrace ID: 0x0000010000001aa4\nThread: 0x19514a\nOffset: 0x10b2\nMessage: 'message1'\n```\n\nThe output is more extensive than the one from the LLDB console, since it also contains the breadcrumb trail, as well as the trace messages from all threads.\n\nInstead of using `ostraceutil` with the `-diagnostic` flag, we can also use the `-watch` flag to put it into a live mode where we can see the trace messages and breadcrumbs coming in as they happen. In this mode, we can also see the payload data of trace messages:\n\n```\n[...]\n----------------------\nTimestamp: 60059.327207\nFAULT\nTrace ID: 0x0000c10000001ac0\nOffset: 0x118d\nMessage: 'payload message'\nPayload: '<dictionary: 0x7fd2b8700540> { count = 1, contents =\n\t\"test-key\" => <string: 0x7fd2b87000c0> { length = 10, contents = \"test-value\" }\n}'\n----------------------\n[...]\n```\n\n\n## Activity Tracing and Swift\n\nAt the time of writing, activity tracing is not accessible from Swift.\n\nIf you want to use it now within a Swift project, you would have to create an Objective-C wrapper around it and make this API accessible in Swift using the bridging header. Note that activity tracing macros expect strings to be constant, i.e. you can't pass a string argument of your wrapper function to the activity tracing API. To illustrate this point, the following doesn't work:\n\n```objc\nvoid sendTraceMessage(const char *msg) {\n    os_trace(msg); // this doesn't work!\n}\n```\n\nOne possible workaround is to define specific helper functions like this:\n\n```objc\nvoid traceLogin(int guid) {\n    os_trace(\"Login: %d\", guid);\n}\n```\n\n\n## Conclusion\n\nActivity tracing is a very welcome addition to our debugging toolkit and makes diagnosing crashes in asynchronous code so much easier. We really should make it a habit to add activities, breadcrumbs, and trace messages to our code.\n\nThe most painful point at this time is the missing Swift integration, at least for those of us who already use Swift in production code. Hopefully it is just a matter of (a not too long) time until this will change.\n\n\n[wwdcsession]: https://developer.apple.com/videos/wwdc/2014/#714\n"
  },
  {
    "path": "2014-12-08-debugging-case-study.md",
    "content": "---\ntitle:  \"Debugging: A Case Study\"\ncategory: \"19\"\ndate: \"2014-12-08 11:00:00\"\nauthor:\n  - name: Peter Steinberger\n    url: https://twitter.com/steipete\ntags: article\n---\n\n\nNobody writes perfect code, and debugging is something every one of us should be able to do well. Instead of providing a random list of tips about the topic, I'll walk you through a bug that turned out to be a regression in UIKit, and show you the workflow I used to understand, isolate, and ultimately work around the issue.\n\n## The Issue\n\nWe received a bug report where quickly tapping on a button that presented a popover dismissed the popover but also the *parent* view controller. Thankfully, a sample was included, so the first part — of reproducing the bug — was already taken care of:\n\n![](/images/issue-19/dismiss-issue-animated.gif)\n\nMy first guess was that we might have code that dismisses the view controller, and we wrongfully dismiss the parent. However, when using Xcode's integrated view debugging feature, it was clear that there was a global `UIDimmingView` that was the first responder for touch input:\n\n![](/images/issue-19/xcode-view-debugging.png)\n\nApple added the [Debug View Hierarchy](https://developer.apple.com/library/ios/recipes/xcode_help-debugger/using_view_debugger/using_view_debugger.html) feature in Xcode 6, and it's likely that this move was inspired by the popular [Reveal](http://revealapp.com/) and [Spark Inspector](http://sparkinspector.com/) apps, which, in many ways, are still better and more feature rich than the Xcode feature.\n\n## Using LLDB\n\nBefore there was visual debugging, the common way to inspect the hierarchy was using `po [[UIWindow keyWindow] recursiveDescription]` in LLDB, which prints out [the whole view hierarchy in text form](https://gist.github.com/steipete/5a3c7a3b6e80d2b50c3b). \n\nSimilar to inspecting the view hierarchy, we can also inspect the view controller hierarchy using `po [[[UIWindow keyWindow] rootViewController] _printHierarchy]`. This is a [private helper](https://github.com/nst/iOS-Runtime-Headers/blob/a8f9f7eb4882c9dfc87166d876c547b75a24c5bb/Frameworks/UIKit.framework/UIViewController.h#L365) on `UIViewController` that Apple silently added in iOS 8:\n\n```\n(lldb) po [[[UIWindow keyWindow] rootViewController] _printHierarchy]\n<PSPDFNavigationController 0x7d025000>, state: disappeared, view: <UILayoutContainerView 0x7b3218d0> not in the window\n   | <PSCatalogViewController 0x7b3100d0>, state: disappeared, view: <UITableView 0x7c878800> not in the window\n   + <UINavigationController 0x8012c5d0>, state: appeared, view: <UILayoutContainerView 0x8012b7a0>, presented with: <_UIFullscreenPresentationController 0x80116c00>\n   |    | <PSPDFViewController 0x7d05ae00>, state: appeared, view: <PSPDFViewControllerView 0x80129640>\n   |    |    | <PSPDFContinuousScrollViewController 0x7defa8e0>, state: appeared, view: <UIView 0x7def1ce0>\n   |    + <PSPDFNavigationController 0x7d21a800>, state: appeared, view: <UILayoutContainerView 0x8017b490>, presented with: <UIPopoverPresentationController 0x7f598c60>\n   |    |    | <PSPDFContainerViewController 0x8017ac40>, state: appeared, view: <UIView 0x7f5a1380>\n   |    |    |    | <PSPDFStampViewController 0x8016b6e0>, state: appeared, view: <UIView 0x7f3dbb90>\n```\n\nLLDB is quite powerful and can also be scripted. Facebook released [a collection of python scripts named Chisel](https://github.com/facebook/chisel) that help a lot with daily debugging. `pviews` and `pvc` are the equivalents for view and view controller hierarchy printing. Chisel's view controller tree is similar, but also displays the view rects. I often use it to inspect the [responder chain](https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/event_delivery_responder_chain/event_delivery_responder_chain.html), and while you could manually loop over `nextResponder` on the object you're interested in, or [add a category helper](https://gist.github.com/n-b/5420684), typing `presponder object` is by far the quickest way.\n\n\n## Adding Breakpoints\n\nLet's first figure out what code is actually dismissing our view controller. The most obvious action is setting a breakpoint on `viewWillDisappear:` to see the stack trace:\n\n```\n(lldb) bt\n* thread #1: tid = 0x1039b3, 0x004fab75 PSPDFCatalog`-[PSPDFViewController viewWillDisappear:](self=0x7f354400, _cmd=0x03b817bf, animated='\\x01') + 85 at PSPDFViewController.m:359, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1\n  * frame #0: 0x004fab75 PSPDFCatalog`-[PSPDFViewController viewWillDisappear:](self=0x7f354400, _cmd=0x03b817bf, animated='\\x01') + 85 at PSPDFViewController.m:359\n    frame #1: 0x033ac782 UIKit`-[UIViewController _setViewAppearState:isAnimating:] + 706\n    frame #2: 0x033acdf4 UIKit`-[UIViewController __viewWillDisappear:] + 106\n    frame #3: 0x033d9a62 UIKit`-[UINavigationController viewWillDisappear:] + 115\n    frame #4: 0x033ac782 UIKit`-[UIViewController _setViewAppearState:isAnimating:] + 706\n    frame #5: 0x033acdf4 UIKit`-[UIViewController __viewWillDisappear:] + 106\n    frame #6: 0x033c46a1 UIKit`-[UIViewController(UIContainerViewControllerProtectedMethods) beginAppearanceTransition:animated:] + 200\n    frame #7: 0x03380ad8 UIKit`__56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 594\n    frame #8: 0x033b47ab UIKit`__40+[UIViewController _scheduleTransition:]_block_invoke + 18\n    frame #9: 0x0327a0ce UIKit`___afterCACommitHandler_block_invoke + 15\n    frame #10: 0x0327a079 UIKit`_applyBlockToCFArrayCopiedToStack + 415\n    frame #11: 0x03279e8e UIKit`_afterCACommitHandler + 545\n    frame #12: 0x060669de CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30\n    frame #20: 0x032508b6 UIKit`UIApplicationMain + 1526\n    frame #21: 0x000a119d PSPDFCatalog`main(argc=1, argv=0xbffcd65c) + 141 at main.m:15\n(lldb) \n```\n\nWith LLDB's `bt` command, you can print the backtrace. `bt all` will do the same, but it prints the state of all threads, and not just the current one.\n\nLooking at the stack trace, we notice that the view controller is already dismissing, as we're called from a scheduled animation, so we need to add a breakpoint earlier. In this case, we are interested in calls to `-[UIViewController dismissViewControllerAnimated:completion:]`. We add a *symbolic breakpoint* to Xcode's breakpoint list and run the sample again. \n\nThe Xcode breakpoint interface is very powerful, allowing you to add [conditions, skip counts, or even custom actions like playing a sound effect and automatically continuing](http://www.peterfriese.de/debugging-tips-for-ios-developers/). We don't need these features here, but they can save quite a bit of time:\n\n```\n(lldb) bt\n* thread #1: tid = 0x1039b3, 0x033bb685 UIKit`-[UIViewController dismissViewControllerAnimated:completion:], queue = 'com.apple.main-thread', stop reason = breakpoint 7.1\n  * frame #0: 0x033bb685 UIKit`-[UIViewController dismissViewControllerAnimated:completion:]\n    frame #1: 0x03a7da2c UIKit`-[UIPopoverPresentationController dimmingViewWasTapped:] + 244\n    frame #2: 0x036153ed UIKit`-[UIDimmingView handleSingleTap:] + 118\n    frame #3: 0x03691287 UIKit`_UIGestureRecognizerSendActions + 327\n    frame #4: 0x0368fb04 UIKit`-[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 561\n    frame #5: 0x03691b4d UIKit`-[UIGestureRecognizer _delayedUpdateGesture] + 60\n    frame #6: 0x036954ca UIKit`___UIGestureRecognizerUpdate_block_invoke661 + 57\n    frame #7: 0x0369538d UIKit`_UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 317\n    frame #8: 0x03689296 UIKit`_UIGestureRecognizerUpdate + 3720\n    frame #9: 0x032a226b UIKit`-[UIWindow _sendGesturesForEvent:] + 1356\n    frame #10: 0x032a30cf UIKit`-[UIWindow sendEvent:] + 769\n    frame #21: 0x032508b6 UIKit`UIApplicationMain + 1526\n    frame #22: 0x000a119d PSPDFCatalog`main(argc=1, argv=0xbffcd65c) + 141 at main.m:15\n```\n\nNow we're talking! As expected, the fullscreen `UIDimmingView` receives our touch and processes it in `handleSingleTap:`, then forwarding it to `UIPopoverPresentationController`'s `dimmingViewWasTapped:`, which dismisses the controller (as it should). However, when we tap quickly, this breakpoint is called twice. Is there a second dimming view? Is it called on the same instance? We only have the assembly on this breakpoint, so calling `po self` will not work.\n\n## Calling Conventions 101\n\nWith some basic knowledge of assembly and function-calling conventions, we can still get the value of `self`. The [iOS ABI Function Call Guide](http://developer.apple.com/library/ios/#documentation/Xcode/Conceptual/iPhoneOSABIReference/Introduction/Introduction.html) and the [Mac OS X ABI Function Call Guide](http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/LowLevelABI/000-Introduction/introduction.html) that is used in the iOS Simulator are both great resources.\n\nWe know that every Objective-C method has two implicit parameters: `self` and `_cmd`. So what we need is the first object on the stack. For the **32-bit** architecture, the stack is saved in `$esp`, so you can use `po *(int*)($esp+4)` to get `self`, and `p (SEL)*(int*)($esp+8)` to get `_cmd` in Objective-C methods. The first value in `$esp` is the return address. Subsequent variables are in `$esp+12`, `$esp+16`, and so on.\n\nThe **x86-64** architecture (iPhone Simulator for devices that have an arm64 chip) offers many more registers, so variables are placed in `$rdi`, `$rsi`, `$rdx`, `$rcx`, `$r8`, `$r9`. All subsequent variables land on the stack in `$rbp`, starting with `$rbp+16`, `$rbp+24`, etc.\n\nThe **armv7** architecture generally places variables in `$r0`, `$r1`, `$r2`, `$r3`, and then moves the rest on the stack `$sp`:\n\n```\n(lldb) po $r0\n<PSPDFViewController: 0x15a1ca00 document:<PSPDFDocument 0x15616e70 UID:amazondynamososp2007_0c7fb1fc6c0841562b090b94f0c1c890 files:1 pageCount:16 isValid:1> page:0>\n\n(lldb) p (SEL)$r1\n(SEL) $1 = \"dismissViewControllerAnimated:completion:\"\n```\n\n**Arm64** is similar to armv7, however, since there are more registers available, the whole range of `$x0` to `$x7` is used to pass over variables, before falling back to the stack register `$sp`.\n\nYou can learn more about stack layout for [x86](http://eli.thegreenplace.net/2011/02/04/where-the-top-of-the-stack-is-on-x86/) and [x86-64](http://eli.thegreenplace.net/2011/09/06/stack-frame-layout-on-x86-64/), and also by reading the [AMD64 ABI Draft](http://www.x86-64.org/documentation/abi.pdf).\n\n## Using the Runtime\n\nAnother technique to track method execution is overriding the methods with a log statement before calling super. However, manually swizzling just to be able to debug more conveniently isn't really time efficient. A while back, I wrote a small library called [*Aspects*](http://github.com/steipete/Aspects) that does exactly that. It can be used in production code, but I mostly use it for debugging and to write test cases. (If you're curious about Aspects, you can [learn more here.](https://speakerdeck.com/steipete/building-aspects))\n\n```objc\n#import \"Aspects.h\"\n\n[UIPopoverPresentationController aspect_hookSelector:NSSelectorFromString(@\"dimmingViewWasTapped:\") \n                                         withOptions:0 \n                                          usingBlock:^(id <AspectInfo> info, UIView *tappedView) {\n    NSLog(@\"%@ dimmingViewWasTapped:%@\", info.instance, tappedView);\n} error:NULL];\n```\n\nThis hooks into `dimmingViewWasTapped:`, which is private — thus we use `NSSelectorFromString`. You can verify that this method exists, and also look up all other private and public methods of pretty much every framework class, by using the [iOS Runtime Headers](https://github.com/nst/iOS-Runtime-Headers). This project uses the fact that one can't really hide methods at runtime to query all classes and create a more complete header than what Apple gives us. (Of course, actually calling a private API is not a good idea — this is just to better understand what's going on.)\n\nWith the log message in the hooked method, we get the following output:\n\n```\nPSPDFCatalog[84049:1079574] <UIPopoverPresentationController: 0x7fd09f91c530> dimmingViewWasTapped:<UIDimmingView: 0x7fd09f92f800; frame = (0 0; 768 1024)>\nPSPDFCatalog[84049:1079574] <UIPopoverPresentationController: 0x7fd09f91c530> dimmingViewWasTapped:<UIDimmingView: 0x7fd09f92f800; frame = (0 0; 768 1024)>\n```\n\nWe see that the object address is the same, so our poor dimming view really is called twice. We can use Aspects again to see on which controller the dismiss is actually called:\n\n```objc\n[UIViewController aspect_hookSelector:@selector(dismissViewControllerAnimated:completion:)\n                          withOptions:0\n                           usingBlock:^(id <AspectInfo> info) {\n    NSLog(@\"%@ dismissed.\", info.instance);\n} error:NULL];\n```\n\n```\n2014-11-22 19:24:51.900 PSPDFCatalog[84210:1084883] <UINavigationController: 0x7fd673789da0> dismissed.\n2014-11-22 19:24:52.209 PSPDFCatalog[84210:1084883] <UINavigationController: 0x7fd673789da0> dismissed.\n```\n\nBoth times, the dimming view calls dismiss on our main navigation controller. UIViewControllers's `dismissViewControllerAnimated:completion:` will forward the view controller dismissal request to its immediate child controller, if there is one, otherwise it will dismiss itself. So the first time, the dismiss request goes to the popover, and the second time, the navigation controller itself gets dismissed.\n\n## Finding a Workaround\n\nWe now know what is happening — so let's move to the *why*. UIKit is closed source, but we can use a disassembler like [Hopper](http://www.hopperapp.com/) to read the UIKit assembly and take a closer look what's going on in `UIPopoverPresentationController`. You'll find the binary under `/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/UIKit.framework`. Use File -> Read Executable to Disassemble... and select this in Hopper, and watch how it crawls through the binary and symbolicates code. The 32-bit disassembler is the most mature one, so you'll get the best results selecting the 32-bit file slice. [IDA by Hex-Rays](https://www.hex-rays.com/products/ida/) is another very powerful and expensive disassembler, which often provides [even better results](https://twitter.com/steipete/status/537565877332639744):\n\n![](/images/issue-19/hopper-dimmingView.png)\n\nSome basics in assembly are quite useful when reading through the code. However, you can also use the pseudo-code view to get something more C-like:\n\n![](/images/issue-19/pseudo-code.png)\n\nReading the pseudo-code is quite eye-opening. There are two code paths — one if the delegate implements `popoverPresentationControllerShouldDismissPopover:`, and one if it doesn't — and the code paths are actually quite different. While the one reacting to the delegate basically has an `if (controller.presented && !controller.dismissing)`, the other code path (that we currently fall into) doesn't, and always dismisses. With that inside knowledge, we can attempt to work around this bug by implementing our own `UIPopoverPresentationControllerDelegate`:\n\n```objc\n- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {\n    return YES;\n}\n```\n\nMy first attempt was to set this to the main view controller that creates the popover. However, that broke `UIPopoverController`. While not documented, the popover controller sets itself as the delegate in `_setupPresentationController`, and taking the delegate away will break things. Instead, I used a `UIPopoverController` subclass and added the above method directly. The connection between these two classes is not documented, and our fix relies on this undocumented behavior; however, the implementation matches the default and exists purely to work around this issue, so it's future-proof code.\n\n## Reporting a Radar\n\nNow please don't stop here. You should always properly document such workarounds, and most importantly, file a radar with Apple. As an additional benefit, this allows you to verify that you actually understood the bug, and that no other side effects from your application play a role — and if you drop an iOS version, it's easy to go back and test if the radar is still valid:\n\n```objc\n// The UIPopoverController is the default delegate for the UIPopoverPresentationController\n// of it's contentViewController.\n//\n// There is a bug when someone double-taps on the dimming view, the presentation controller invokes\n// dismissViewControllerAnimated:completion: twice, thus also potentially dismissing the parent controller.\n//\n// Simply implementing this delegate runs a different code path that properly checks for dismissing.\n// rdar://problem/19067761\n- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {\n    return YES;\n}\n```\n\nWriting radars is actually quite a fun challenge, and doesn't take as much time as you might think. With an example, you'll help out some overworked Apple engineer, and without it, the engineers will most likely push back and not even consider the radar. I managed to create a sample in about 50 LOC, including some comments and the workaround. The Single View Template is usually the quickest way to create an example.\n\nNow, we all know that Apple's RadarWeb application isn't great, however, you don't have to use it. [QuickRadar](http://www.quickradar.com/) is a great Mac front-end that can submit the radar for you, and also automatically sends a copy to [OpenRadar](http://openradar.appspot.com). Furthermore, it makes duping radars extremely convenient. You should download it right away and dupe [rdar://19067761](http://openradar.appspot.com/19067761) if you feel like this bug should be fixed.\n\n\nNot every issue can be solved with such a simple workaround, however, many of these steps will help you find better solutions to issues, or at least improve your understanding of why something is happening. \n\n## References\n\n*  [iOS Debugging Magic (TN2239)](https://developer.apple.com/library/ios/technotes/tn2239/_index.html)\n*  [iOS Runtime Headers](https://github.com/nst/iOS-Runtime-Headers)\n*  [Debugging Tips for iOS Developers](http://www.peterfriese.de/debugging-tips-for-ios-developers/)\n*  [Hopper — a reverse engineering tool](http://www.hopperapp.com/)\n*  [IDA by Hex-Rays](https://www.hex-rays.com/products/ida/)\n*  [Aspects — Delightful, simple library for aspect-oriented programming.](http://github.com/steipete/Aspects)\n*  [Building Aspects](https://speakerdeck.com/steipete/building-aspects)\n*  [Event Delivery: The Responder Chain](https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/event_delivery_responder_chain/event_delivery_responder_chain.html)\n*  [Chisel — a collection of LLDB commands to assist debugging iOS apps](https://github.com/facebook/chisel)\n*  [Where the top of the stack is on x86](http://eli.thegreenplace.net/2011/02/04/where-the-top-of-the-stack-is-on-x86/)\n*  [Stack frame layout on x86-64](http://eli.thegreenplace.net/2011/09/06/stack-frame-layout-on-x86-64)\n*  [AMD64 ABI draft](http://www.x86-64.org/documentation/abi.pdf)\n*  [ARM 64-bit Architecture](http://infocenter.arm.com/help/topic/com.arm.doc.ihi0055b/IHI0055B_aapcs64.pdf)\n*  [Decompiling assembly: IDA vs Hopper](https://twitter.com/steipete/status/537565877332639744)\n"
  },
  {
    "path": "2014-12-08-debugging-checklist.md",
    "content": "---\ntitle: Debugging Checklist\ncategory: \"19\"\ndate: \"2014-12-08 09:00:00\"\ntags: article\nauthor:\n  - name: Chris Eidhof\n    url: http://twitter.com/chriseidhof\n---\n\nFinding bugs can be very time-consuming; almost every experienced developer can\nrelate to having spent days on a single bug. As you become more experienced on a\nplatform, it becomes easier to find bugs. However, some bugs will always be\nhard to find or reproduce. As a first step, it is always useful to find a way\nto reproduce the bug. Once you have a way to reproduce it consistently, you can\nget to the next stage: finding the bug.\n\nThis article tries to sketch a number of common problems that we usually run into when\ndebugging. You could use this as a checklist when you encounter a bug, and\nmaybe by checking some of these things, you'll find that bug way sooner.\nAnd hopefully, some of the techniques help to prevent the bugs in the first place.\n\nWe'll start off with a couple of very common sources of bugs that happen to us a lot.\n\n## Are Your Callbacks on the Right Thread?\n\nOne source of unexpected behavior is when things are happening on the wrong thread. For example, when you update UIKit objects from any other thread than the main thread, things could break. Sometimes updating works, but mostly you will get strange behavior, or even crashes. One thing you can do to mitigate this is having assertions in your code that check whether or not you're on the main thread. Common callbacks that might (unexpectedly) happen on a background thread could be coming from network calls, timers, file reading, or external libraries.\n\nAnother solution is to keep the places where threading happens very isolated. As an example, if you are building a wrapper around an API on the network, you could handle all threading in that wrapper. All network calls will happen on a background thread, but all callbacks could happen on the main thread, so that you'll never have to worry about that occurring in the calling code. Having a simple design really helps.\n\n## Is This Object Really the Right Class?\n\nThis is mostly an Objective-C problem; in Swift, there's a stronger type system with more precise guarantees about the type of an object or value. However, in Objective-C, it's fairly common to accidentally have objects of the wrong class.\n\nFor example, in [Deckset](http://www.decksetapp.com), we were adding a new feature that had to do with fonts. One of the objects had a `fonts` array property, and I assumed the objects in the array were of type `NSFont`. As it turned out, the array contained `NSString` objects (the font names). It took quite a while to figure this out, because, for the most part, things worked as expected. In Objective-C, one way to check this is by having assertions. Another way to help yourself is to encode type information in the name (e.g. this array could have been named `fontNames`). In Swift, these errors can be prevented by having precise types (e.g. `[NSFont]` rather than `[AnyObject]`).\n\nWhen unsure about whether the object is of the right type, you can always print it in the debugger. It is also useful to have assertions that check whether or not an object is the right class using `isKindOfClass:`. In Swift, rather than force casting with the `as` keyword, rely on having optionals and use `as?` to typecast whenever you need to. This will let you minimize the chances of errors.\n\n## Build-Specific Settings\n\nAnother common source of bugs that are hard to find is when there are settings that differ between builds. For example, sometimes optimizations that happen in the compiler could cause bugs in production builds that never show up during debugging. This is relatively uncommon, although there are reports of this happening with the the current Swift releases.\n\nAnother source of bugs is where certain variables or macros are defined differently. For example, some code might be commented out during development. We had an instance where we were writing incorrect (crashing) analytics code, but during development we turned off analytics, so we never saw these crashes when developing the app. \n\nThese kinds of bugs can be hard to detect during development. As such, you should always thoroughly test the release build of your app. Of course, it's even better if someone else (e.g. a QA department) can test it.\n\n## Different Devices\n\nMeanwhile, there are many different devices with different capabilities. If you have only tested on a limited number of devices, this is a potential cause of bugs. The classic scenario is just testing on the simulator without having the real device. But even when you do test with a real device, you need to account for different capabilities. For example, when dealing with the built-in camera, always use methods like `isSourceTypeAvailable:` to check whether you can use a specific input source. You might have a working camera on your device, but it might not be available on the user's device. \n\n## Mutability\n\nMutability is also a common source of bugs that can be very hard to track down. For example, if you share an object between two threads, and they both modify it at the same time, you might get very unexpected behavior. The tough thing about these kinds of bugs is that they can be very hard to reproduce.\n\nOne way to deal with this is to have immutable objects. This way, once you have access to an object, you know that it'll never change its state. There is so much to say about this, but for more information, we'd rather direct you to read the following: [A Warm Welcome to Structs and Value Types](/issues/16-swift/swift-classes-vs-structs/), [Value Objects](/issues/7-foundation/value-objects/), [Object Mutability](https://developer.apple.com/library/mac/documentation/General/Conceptual/CocoaEncyclopedia/ObjectMutability/ObjectMutability.html), and [About Mutability](http://www.bignerdranch.com/blog/about-mutability/).\n\n## Nullability\n\nAs Objective-C programmers, we sometimes make fun of Java programmers because of their `NullPointerException`s. For the most part, we can safely send messages to nil and not have any problems. Still, there are some tricky bugs that might arise out of this. If you are writing Swift instead of Objective-C, you can safely skip most of this section, because Swift optionals are a solution to many of these problems.\n\n### Does the Method You Call Take `nil` Parameters?\n\nThis is a common source of bugs. Some methods will crash when you call them with a nil parameter. For example, consider the following fragment:\n\n```objectivec\nNSString *name = @\"\";\nNSAttributedString *string = [[NSAttributedString alloc] initWithString:name];\n```\n\nIf `name` is nil, this code will crash. The tricky thing is when this is an edge case that you might not discover (e.g. `myObject` is non-nil in most of the cases). When writing your own methods, you can add a custom attribute to inform the compiler about whether or not you expect nil parameters:\n\n```objectivec\n- (void)doSomethingWithRequiredString:(NSString *)requiredString\n                                  bar:(NSString *)optionalString\n        __attribute((nonnull(1)));\n```\n\n(Source: [StackOverflow](http://stackoverflow.com/a/19186298))\n\nAdding this attribute will give a compiler warning when you try to pass in a nil parameter. This is nice, because now you don't have to think about this edge case anymore: you can leverage the compiler infrastructure to have this checked for you.\n\nAnother possible way around this is to invert the flow of messages. For example, you could create a custom category on `NSString` which has an instance method `attributedString`:\n\n```objectivec\n@implementation NSString (Attributes)\n\n- (NSAttributedString*)attributedString {\n\treturn [[NSAttributedString alloc] initWithString:self];\n}\n\n@end\n```\n\nThe nice thing about the above code is that you can now safely construct an `attributedString`. You could write `[@\"John\" attributedString]`, but you can also send this message to nil (`[nil attributedString]`), and rather than a crash, you get a nil result. For some more ideas about this, see Graham Lee's article on [reversing the polarity of the message flow](http://www.sicpers.info/2014/10/reversing-the-polarity-of-the-message-flow/).\n\nIf you want to capture more constraints that need to be true (e.g. a parameter should always be a certain class), you can use [`NSParameterAssert`](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/#//apple_ref/c/macro/NSParameterAssert) as well.\n\n### Are You Sure You Can Send the Message to `nil`?\n\nThis is a rather uncommon source of bugs, but it happened to us in a real app. Sometimes when dealing with scalar values, sending a message to nil might produce an unexpected result. Consider the following innocent-looking snippet of code:\n\n```objectivec\nNSString *greeting = @\"Hello objc.io\";\nNSRange range = [greeting rangeOfString:@\"objc.io\"];\nif (range.location != NSNotFound) {\n  NSLog(@\"Found the keyword!\");\n}\n```\n\nIf `greeting` contains the string `\"objc.io\"`, a message is logged. If `greeting` does not contain this string, no message is logged. But what if greeting is `nil`? Then the `range` will be a struct with zeroes, and the `location` will be zero. Because `NSNotFound` is defined as `-1`, this will log the message. So whenever you deal with scalar values and `nil`, be sure to take extra care. Again, this is not an issue in Swift because of optionals.\n\n## Is There Anything in the Class That's Not Initialized?\n\nSometimes when working with an object, you might end up working with a half-initialized object. Because it's uncommon to do any work in `init`, sometimes you need to call some methods on the object before you can start working with it. If you forget to call these methods, the class might not be initialized completely and weird behavior might occur. Therefore, always make sure that after the designated initializer is run, the class is in a usable state. If you absolutely need your designated initializer to run, and can't construct a working class using just the `init` method, you can still override the `init` method and crash. This way, when you do accidentally instantiate an object using `init`, you'll hopefully find out about it early.\n\n## Key-Value Observing\n\nAnother common source of bugs is when you're using key-value observing (KVO) incorrectly. Unfortunately, it's not that hard to make mistakes, but luckily, there are a couple of ways to avoid them.\n\n### Are You Cleaning Up Your Observers?\n\nAn easy-to-make mistake is adding an observer, but then never cleaning it up. This way, KVO will keep sending messages, but the receiver might have dealloc'ed, so there will be a crash. One way around this is to use a full-blown framework like [ReactiveCocoa](https://github.com/ReactiveCocoa/ReactiveCocoa), but there are some lighter approaches as well.\n\nOne such way is, whenever you create a new observer, immediately write a line in dealloc that removes it. However, this process can be automated. Rather than adding the observer directly, you can create a custom object that adds it for you. This custom object adds the observer and removes it in its own dealloc. The advantage of this is that the lifetime of your observer is the same as the lifetime of the object. This means that creating this object adds the observer. You can then store it in a property, and whenever the containing object is dealloc'ed, the property will automatically be set to nil, thus removing the observer.\nA slightly longer explanation of this technique, including sample code, can be found [here](http://chris.eidhof.nl/posts/lightweight-key-value-observing.html). A tiny library that does this is [THObserversAndBinders](https://github.com/th-in-gs/THObserversAndBinders), or you could look at Facebook's [KVOController](https://github.com/facebook/KVOController).\n\nAnother problem with KVO is that callbacks might arrive on a different thread than you expected (just like we described in the beginning). Again, by using an object to deal with this (as described above), you can make sure that all callbacks get delivered on a specific thread.\n\n### Dependent Key Paths\n\nIf you're observing properties that depend on other properties, you need to make sure that you [register dependent keys](https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/KeyValueObserving/Articles/KVODependentKeys.html). Otherwise, you might not get callbacks when your properties change. A while ago, I created a recursive dependency (a property dependent on itself) in my dependent key declarations, and strange things happened.\n\n## Views\n\n### Outlets and Actions\n\nA common mistake when using Interface Builder is to forget to wire up outlets and actions. This is now often indicated in the code (you can see small circles next to outlets and actions). Also, it's very possible to add unit tests that test whether everything is connected as you expect (but it might be too much of a maintenance burden).\n\nHere, you could also use asserts like [`NSAssert`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/#//apple_ref/c/macro/NSAssert) to verify that your outlets are not nil, in order to make sure you fail fast whenever this happens.\n\n### Retaining Objects\n\nWhen you use Interface Builder, you need to make sure that your object graph that you load from a nib file stays retained. There are [good pointers on this by Apple](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW6). Be sure to read that section and follow the advice, otherwise your objects might either disappear underneath you, or get over-retained. There are differences between plain XIB files and Storyboards; be sure to account for that.\n\n### View Lifecycle\n\nWhen dealing with views, there are many potential bugs that can arise. One common mistake is to work with views when they are not yet initialized. Alternatively, you might work with initialized views that don't have a size yet. The key here is to do things at the right point in [the view lifecycle](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW58). Investing the time in understanding exactly how this works will almost certainly pay off in decreased debugging time.\n\nWhen you port an existing app to the iPad, this might also be a common source of bugs. All of a sudden, you might need to worry about whether a view controller is a child view controller, how it responds to rotation events, and many other subtle differences. Here, auto layout might be helpful, as it can automatically respond to many of these changes.\n\nOne common mistake that we keep making is creating a view, adding some constraints, and then adding it to the superview. In order for most constraints to work, the view needs to be in the superview hierarchy. Luckily, most of the time this will crash your code, so you'll find the bug fast.\n\n## Finally\n\nThe techniques above are hopefully helpful to get rid of bugs or prevent them completely. There is also automated help available: turning on all warning messages in Clang can show you a lot of possible bugs, and running the static analyzer will almost certainly find some bugs (unless you run it on a regular basis).\n"
  },
  {
    "path": "2014-12-08-dtrace.markdown",
    "content": "---\ntitle:  \"DTrace\"\ncategory: \"19\"\ndate: \"2014-12-08 08:00:00\"\ntags: article\nauthor:\n  - name: Daniel Eggert\n    url: http://twitter.com/danielboedewadt\n---\n\nFew have heard of DTrace, a little gem tucked away somewhere inside the OS. It's a debugging power tool — powerful because it's extremely flexible, and probably relatively unknown because it is so very different from other tools out there.\n\nQuite often, real users or testers of your app will see some unexpected behavior. DTrace lets you answer arbitrary questions about the app, on a production version of the app, and without relaunching the app.\n\n\n\n## Dynamic Tracing\n\nAlmost 10 years ago, [Sun Microsystems](https://en.wikipedia.org/wiki/Sun_Microsystems) built DTrace, short for *Dynamic Trace*. And at the end of 2007, Apple integrated it into its [operating system](https://en.wikipedia.org/wiki/Mac_OS_X_Leopard).\n\nDTrace is a dynamic tracing framework that provides *zero disable cost*, i.e. probes in code have no overhead when disabled — we can leave the probes in our code even for production builds. You only pay the cost of the probe when using it.\n\nDTrace is *dynamic*, which means that we can attach to an already running program and detach from it again without interrupting the program itself. No need to recompile or restart.\n\nIn this article, we’ll focus on using DTrace to investigate our own program, but it’s worth mentioning that DTrace is systemwide: a single script could, for example, watch allocations made by *all* processes on the system. Take a look inside `/usr/share/examples/DTTk` for some great examples.\n\n### OS X vs. iOS\n\nAs you might have guessed by now, DTrace is only available on OS X. Apple also uses DTrace on iOS, in order to power tools such as Instruments, but for third-party developers, it is only available on OS X or the iOS simulator.\n\nAt [Wire](https://www.wire.com), DTrace has been very helpful on iOS, even though we're limited to using it in the iOS simulator. If you're reading this article and think real DTrace support on iOS devices is a good idea, please file an [enhancement request](https://bugreport.apple.com/) with Apple.\n\n\n### Probes and Scripts\n\nThere are two parts to DTrace: the DTrace probes, and the DTrace scripts that attach to those probes.\n\n#### Probes\n\nThere are build-in probes, and you can add (so-called static) probes to your code. A probe looks very similar to a normal C function. At Wire, our syncing code has an internal state machine, and we define these two probes:\n\n```\nprovider syncengine_sync {\n    probe strategy_go_to_state(int);\n}\n```\n\nProbes are grouped into so-called *providers*. The `int` argument is the state that we're entering. In our Objective-C (or Swift) code, we simply insert the following:\n\n```objc\n- (void)goToState:(ZMSyncState *)state\n{\n    [self.currentState didLeaveState];\n    self.currentState = state;\n    SYNCENGINE_SYNC_STRATEGY_GO_TO_STATE(state.identifier);\n    [self.currentState didEnterState];\n}\n```\n\nWe'll talk about how to integrate all of this and clean it up [in a bit](#staticProbes).\n\n#### Scripts\n\nWe can now write a small DTrace script that shows us state transitions:\n\n```\nsyncengine_sync*:::strategy_go_to_state\n{\n    printf(\"Transitioning to state %d\\n\", arg0);\n}\n```\n\n(We will go into more detail [below](#DProgrammingLanguage) about how DTrace scripts work.)\n\nIf we save this DTrace into `state.d`, we can then run it using the [`dtrace(1)` command line tool][dtrace1man]:\n    \n```\n% sudo dtrace -q -s state.d\n```\n\nWe will see the following:\n\n```\nTransitioning to state 1\nTransitioning to state 2\nTransitioning to state 5\n```\n\nJust as we'd expect. Nothing too exciting. Hit `^C` to exit DTrace.\n\n<a name=\"ATimingExample\"></a>\n### A Timing Example\n\nSince DTrace is very low cost, it's a perfect fit for measuring performance — even when times measured are very small. The resolution of the timers inside DTrace is in nanoseconds.\n\nIf we extend the trivial example from above, we can output the time spent in each state:\n\n```\nuint64_t last_state;\nuint64_t last_state_timestamp;\n\ndtrace:::BEGIN\n{\n    syncState[4] = \"EventProcessing\";\n    syncState[5] = \"QuickSync1\";\n    syncState[6] = \"QuickSync2\";\n}\n\nsyncengine_sync*:::strategy_go_to_state\n/ last_state_timestamp != 0 /\n{\n    t = (walltimestamp - last_state_timestamp) / 1000000;\n    printf(\"Spent %d ms in state %s\\n\", t, syncState[last_state]);\n}\n\nsyncengine_sync*:::strategy_go_to_state\n{\n    printf(\"Transitioning to state %s\\n\", syncState[arg0]);\n    last_state = arg0;\n    last_state_timestamp = walltimestamp;\n}\n```\n\nThis will output the following:\n\n```\nTransitioning to state QuickSync1\nSpent 2205 ms in state QuickSync1\nTransitioning to state QuickSync2\nSpent 115 ms in state QuickSync2\nTransitioning to state EventProcessing\n```\n\nThere are a few new things in this script. The `dtrace:::BEGIN` clause will run when the script starts. There's a matching `END` probe for when the script exits.\n\nWe also added a predicate, `/ last_state_timestamp != 0 /`, to the first probe.\n\nFinally, we're using variables in the global scope to keep track of what the last state was, and at what point in time we entered it.\n\nThe built-in `walltimestamp` variable returns the current number of nanoseconds since the Unix epoch.\n\nThere's another timestamp variable, `vtimestamp`, which is a virtualized timestamp, also in nanoseconds. It is the amount of time the current thread has been running on a CPU, minus the time spent in DTrace. Finally, `machtimestamp` corresponds to `mach_absolute_time()`.\n\nFor the above script, the order of execution is important. We have two so-called *clauses*, which match the same probe, (`syncengine_sync*:::strategy_go_to_state`). These will run in the order in which they appear in the D program.\n\n### Combining with System-Provided Probes\n\nThe operating system, particularly the kernel, provides thousands of probes, all grouped into various providers. A lot of these are the ones documented by [Oracle's DTrace documentation][oracleDTraceProviders].\n\nWe can use the `ip` provider's `send` probe to check how many bytes are sent over the network before we transition to the next state with this script:\n\n```\nuint64_t bytes_sent;\n\nsyncengine_sync$target:::strategy_go_to_state\n{\n    printf(\"Transitioning to state %d\\n\", arg0);\n    printf(\"Sent %d bytes in previous state\\n\", bytes_sent);\n    bytes_sent = 0;\n}\n\nip:::send\n/ pid == $target /\n{\n    bytes_sent += args[2]->ip_plength;\n}\n```\n\nThis time, we are targeting a specific process — the `ip:::send` would otherwise match all processes on the system, while we're only interested in the `Wire` process. We run this script with the following:\n\n```\nsudo dtrace -q -s sample-timing-3.d -p 198\n```\n\nHere, `198` is the process identifier (aka PID) of the process. We can find this number in the Activity Monitor app, or by using the [`ps(1)` command line tool][ps1man].\n\nWe'll get this:\n\n```\nTransitioning to state 6\nSent 2043 bytes in previous state\nTransitioning to state 4\nSent 581 bytes in previous state\n```\n\n<a name=\"DProgrammingLanguage\"></a>\n## D Programming Language\n\nNote: This is *not* [D by W. Bright and A. Alexandrescu](https://en.wikipedia.org/wiki/D_%28programming_language%29).\n\nMost parts of D are very similar to the C programming language, but the overall structure is different. Each DTrace script consists of multiple so-called *probe clauses*.\n\nIn the above examples, we have seen a few of these *probe clauses*. They all follow this general form:\n\n```\nprobe descriptions\n/ predicate /\n{\n    action statements\n}\n```\n\nThe *predicate* and the *action statement* parts are both optional.\n\n### Probe Descriptions\n\nThe probe description defines what probe the clause matches. These are all of the forms where parts can be left out:\n\n```\nprovider:module:function:name\n```\n\nFor example, `syscall:::` matches all probes by the `syscall` provider. We can use `*` to match any string so that `syscall::*lwp*:entry` matches all `entry` probes by the `syscall` provider, where the function name contains `lwp`.\n\nA probe description can consist of multiple probes, such as this:\n\n```\nsyscall::*lwp*:entry, syscall::*sock*:entry\n{\n    trace(timestamp);\n}\n```\n\n### Predicates\n\nWe can use predicates to limit when the *action statements* are to be run. The predicates are evaluated when the specified probes fire. If the predicate evaluates to non-zero, the *action statements* will run, similar to an `if` statement in C.\n\nWe can list the same probe multiple times with different predicates. If multiples of them match, they will be run in the order that they appear in the D program.\n\n### Actions\n\nThe actions are enclosed in curly braces. The D language is lightweight, small, and simple.\n\nD does not support control flow, such as loops or branches. We can not define any user functions. And variable declaration is optional.\n\nThis limits what we can do. But the simplicity also gives us a lot of flexibility once we know a few common patterns, which we'll look into in the next section. Be sure to also check out the [D Programming Language][DProgrammingLanguage] guide for details.\n\n\n## Common D Programming Patterns\n\nThe following examples will give us a good feel for some of the things we can do.\n\nThis example measures the accumulated time the *App Store* application spends inside any syscall (i.e. a system call, or a call into the kernel):\n\n```\nsyscall:::entry\n/ execname == \"App Store\" /\n{\n    self->ts = timestamp;\n}\n\nsyscall:::return\n/ execname == \"App Store\" && self->ts != 0 /\n{\n    @totals[probefunc] = sum(timestamp - self->ts);\n}\n```\n\nIf we run this and launch the Mac *App Store* application, then exit the D Trace script with `^C`, we will get output like this:\n\n```\ndtrace: script 'app-store.d' matched 980 probes\n^C\n\n  __disable_threadsignal                                         2303\n  __pthread_sigmask                                              2438\n  psynch_cvclrprepost                                            3216\n  ftruncate                                                      3663\n  bsdthread_register                                             3754\n  shared_region_check_np                                         3939\n  getpid                                                         4189\n  getegid                                                        4276\n  gettimeofday                                                   4285\n  flock                                                          4825\n  sigaltstack                                                    4874\n  kdebug_trace                                                   5430\n  kqueue                                                         5860\n  workq_open                                                     6155\n  sigprocmask                                                    6188\n  setrlimit                                                      7085\n  psynch_cvsignal                                                8909\n  \n  [...]\n  \n  stat64                                                      6451260\n  read                                                        6657207\n  fsync                                                       8231130\n  rename                                                      8340468\n  open_nocancel                                               8856035\n  workq_kernreturn                                           15835068\n  getdirentries64                                            17978504\n  bsdthread_ctl                                              25418263\n  open                                                       29503041\n  psynch_mutexwait                                          453338483\n  ioctl                                                    1049412360\n  __semwait_signal                                         1373514528\n  select                                                   1632760820\n  kevent64                                                 3656884980\n```\n\nIn this example, the *App Store* spent 3.6 seconds of CPU time inside the `kevent64` syscall.\n\nThere are two very interesting things inside this small script: thread local variables (`self->ts`) and aggregations.\n\n### Scope of Variable\n\nD has three different scopes for variables: global, thread local, and probe clause local.\n\nA global variable such as `foo` or `bar` is visible throughout the D program.\n\nThread local variables are named `self->foo`, `self->bar`, etc., and are local to the specific thread.\n\nProbe clause local variables are similar to local variables in C or Swift. They can be useful for intermediate results.\n\nIn the script, we use the first probe clause to match when we enter a syscall. We set the thread local variable, `self->ts`, to the current timestamp:\n\n```\nsyscall:::entry\n/ execname == \"App Store\" /\n{\n    self->ts = timestamp;\n}\n```\n\nThe second clause matches when the thread returns from the syscall. It will be the same thread that entered, hence we can be sure that `self->ts` has the expected value, even when multiple threads do system calls at the same time.\n\nWe add `self->ts != 0` to the predicate to be sure that our script works, even if we attach to the application while it is inside a system call. Otherwise, `timestamp - self->ts` would result in a very large value, because `self->ts` would not have been set:\n\n```\nsyscall:::return\n/ execname == \"App Store\" && self->ts != 0 /\n{\n    @totals[probefunc] = sum(timestamp - self->ts);\n}\n```\n\nFor all the nitty-gritty details on variables, be sure to check the [Dynamic Tracing Guide, “Variables.”][DTraceGuideChapter3]\n\n<a name=\"Aggregations\"></a>\n### Aggregations\n\nThis line uses aggregations:\n\n```\n@totals[probefunc] = sum(timestamp - self->ts);\n```\n\nThis is an extremely powerful feature of DTrace.\n\nWe're calling our aggregation variable `totals`. The `@` in front of the name turns it into an aggregation. `probefunc` is a built-in variable — it is the name of the probe function. For `syscall` probes, as in our case, `probefunc` is the name of the system call being made. \n\n`sum` is the aggregation function. In our case, the aggregation is summing up `timestamp - self->ts` for each `probefunc`.\n\nThe [DTrace Guide](https://docs.oracle.com/cd/E19253-01/817-6223/chp-aggs-trunc/index.html) shows this small example script that uses aggregations to print the number of system calls per second of the 10 applications that do the most system calls:\n\n```\n#pragma D option quiet\n\nBEGIN\n{\n    last = timestamp;\n}\n\nsyscall:::entry\n{\n    @func[execname] = count();\n}\n\ntick-10sec\n{\n    trunc(@func, 10);\n    normalize(@func, (timestamp - last) / 1000000000);\n    printa(@func);\n    clear(@func);\n    last = timestamp;\n}\n```\n\nOn a mostly idle OS X, it may show something like this:\n\n\n```\nkextd                                                             7\nntpd                                                              8\nmds_stores                                                       19\ncfprefsd                                                         20\ndtrace                                                           20\nUserEventAgent                                                   34\nlaunchd                                                          42\nSafari                                                          109\ncloudd                                                          115\ncom.apple.WebKi                                                 177\n\nmds                                                               8\nWire                                                              8\nTerminal                                                         10\ncom.apple.iClou                                                  15\ndtrace                                                           20\nsecurityd                                                        20\ntccd                                                             37\nsyncdefaultsd                                                    98\nSafari                                                          109\ncom.apple.WebKi                                                 212\n```\n\nWe see Safari, WebKit, and `cloudd` being active. \n\nHere is the total list of aggregation functions:\n\n```\nFunction Name     | Result \n------------------|---------\ncount             | Number of times called\nsum               | Sum of the passed in values\navg               | Average of the passed in values\nmin               | Smallest of the passed in values\nmax               | Largest of the passed in values\nlquantize         | Linear frequency distribution\nquantize          | Power-of-two frequency distribution\n```\n\nThe `quantize` and `lquantize` functions can be very helpful in providing an overview of what quantities are being passed:\n\n```\nip:::send\n{\n    @bytes_sent[execname] = quantize(args[2]->ip_plength);\n}\n```\n\nThe above will output something like this:\n\n```\ndiscoveryd                                        \n         value  ------------- Distribution ------------- count    \n            16 |                                         0        \n            32 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 2        \n            64 |                                         0        \n\nsyncdefaultsd                                     \n         value  ------------- Distribution ------------- count    \n           256 |                                         0        \n           512 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 4        \n          1024 |                                         0        \n\nkernel_task                                       \n         value  ------------- Distribution ------------- count    \n             8 |                                         0        \n            16 |@@@@@@@@@@@@@@                           37       \n            32 |@@@@@@@@@@@@@@@@@@@@@@@@@@               67       \n            64 |                                         0        \n\ncom.apple.WebKi                                   \n         value  ------------- Distribution ------------- count    \n            16 |                                         0        \n            32 |@@@@@@@@@@@@@@@@                         28       \n            64 |@@@@                                     7        \n           128 |@@@@                                     6        \n           256 |                                         0        \n           512 |@@@@@@@@@@@@@@@@                         27       \n          1024 |                                         0        \n```\n\nBe sure to check out [the samples from the Dynamic Tracing Guide](https://docs.oracle.com/cd/E19253-01/817-6223/chp-aggs-2/index.html) for information on how to use `lquantize`.\n\n### Associative Arrays\n\nDespite the name, arrays in D are more akin to dictionaries in Swift or Objective-C. In addition, they are multidimensional.\n\nWe can define an associative array with this:\n\n```\nint x[unsigned long long, char];\n```\n\nThen, we can assign to it, like so:\n\n```\nBEGIN\n{\n    x[123ull, ’a’] = 456;\n}\n```\n\nFor the Wire app, we want to track the roundtrip times for [NSURLSessionTask][AppleDocNSURLSessionTask] instances. We fire a [statically defined probe](#staticProbes) when we resume such a task, and a second probe when it completes. With that, we can write a simple script like this:\n\n```\nsyncengine_sync$target:::operation_loop_enqueue\n/ arg0 == 4 /\n{\n    start_transport_request_timestamp[arg1] = timestamp;\n}\n\nsyncengine_sync$target:::operation_loop_enqueue\n/ arg0 == 6 && start_transport_request_timestamp[arg1] != 0 /\n{\n    @time[\"time for transport request round-trip\"] = quantize(timestamp - start_transport_request_timestamp[arg1]);\n}\n```\n\nWe pass in the [`taskIdentifer`][AppleDocNSURLSessionTask_taskIdentifier] as `arg1`, and `arg0` is set to 4 when the task is resumed, and 6 when it completes.\n\nAssociative arrays are also very helpful in providing a good description for `enum` values passed into a clause, as we've seen in the [first example, “A Timing Example,” above](#ATimingExample).\n\n## Probes and Providers\n\nLet's take a step back and look at the probes that are available.\n\nWe can get a list of all available providers with this:\n\n```\nsudo dtrace -l | awk '{ match($2, \"([a-z,A-Z]*)\"); print substr($2, RSTART, RLENGTH); }' | sort -u\n```\n\nOn OS X 10.10, there are 79 providers. Many of these are specific to the kernel and system calls.\n\nSome of these providers are part of the original set documented in the [Dynamic Tracing Guide][oracleDTraceProviders]. Let's look at a few of those that are available to us.\n\n### `dtrace` Provider\n\nWe mentioned the `BEGIN` and `END` probes [above](#ATimingExample). The `dtrace:::END` is particularly helpful for outputting summaries when running DTrace in the quiet mode. There's also an `ERROR` probe for when an error occurs.\n\n### `profile` Provider\n\nThe [`profile` provider][profileProvider] can be used for sampling in a way that should be familiar to users of Instruments. \n\nWe can sample the stack depth at 1001 Herz:\n\n```\nprofile-1001\n/pid == $1/\n{\n    @proc[execname] = lquantize(stackdepth, 0, 20, 1);\n}\n```\n\nThe output will look something like this:\n\n```\nSafari                                            \n         value  ------------- Distribution ------------- count    \n           < 0 |                                         0        \n             0 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@     704      \n             1 |@                                        12       \n             2 |@@                                       30       \n             3 |@                                        17       \n             4 |                                         7        \n             5 |                                         6        \n             6 |                                         1        \n             7 |                                         2        \n             8 |                                         1        \n             9 |                                         7        \n            10 |                                         5        \n            11 |                                         1        \n            12 |                                         0        \n```\n\nSimilarly, the `tick-` probes will fire every fixed interval at a high interrupt level. The `profile-` probes will fire on all CPUs, and the `tick-` probes only on a single CPU per interval. We used `tick-10sec` in our [aggregation](#Aggregations) example above.\n\n### `pid` Provider\n\nThe `pid` provider is a bit of a brute force provider. In most cases, we should really use [static probes](#staticProbes) as described below.\n\n`pid` is short for process identifier. It lets us probe on the function entry and exit of a process. This works in *most* cases. Note that function entry and return are not always well defined, particularly due to *tail-call optimization*. And some functions don’t have to create a stack frame, etc.\n\nWhen you can't change the code to add [static probes](#staticProbes), `pid` is a powerful tool.\n\nYou can trace any function that's visible. For example, look at this probe:\n\n```\npid123:libSystem:printf:return\n```\n\nIt would attach to `printf`, returning in the process with the process identifier (PID) 123.\n\n### `objc` Provider\n\nA direct counterpart to the `pid` provider is the `objc` provider. It provides probes for Objective-C method entry and exit. Again, using [static probes](#staticProbes) provides more flexibility.\n\nThe format for `objc` probe specifiers is this:\n\n```\nobjcpid:[class-name[(category-name)]]:[[+|-]method-name]:[name]\n```\n\nAnd\n\n```\nobjc207:NSTableView:-*:entry\n```\n\nwould match entry into all instance methods of `NSTableView` in the process with PID 207. Since the colon symbol `:` is part of the DTrace probe specifier scheme, `:` symbols in Objective-C method names need to be replaced with a question mark, `?`. For example, to match entry into `-[NSDate dateByAddingTimeInterval:]`, we'd use this:\n\n```\nobjc207:NSDate:-dateByAddingTimeInterval?:entry\n```\n\nFor additional details, check the [`dtrace(1)` man page][dtrace1man].\n\n### `io` Provider\n\nTo trace activity related to disk input and output, the [`io` provider][ioProvider] defines six probes:\n\n```\nstart\ndone\nwait-start\nwait-done\njournal-start\njournal-done\n```\n\nThe sample from the [Oracle documentation][ioProvider] shows how this can be put to use:\n\n```\n#pragma D option quiet\n\nBEGIN\n{\n    printf(\"%10s %58s %2s\\n\", \"DEVICE\", \"FILE\", \"RW\");\n}\n\nio:::start\n{\n    printf(\"%10s %58s %2s\\n\", args[1]->dev_statname,\n        args[2]->fi_pathname, args[0]->b_flags & B_READ ? \"R\" : \"W\");\n}\n```\n\nThe above will output something like this:\n\n```\n??                   ??/com.apple.Safari.savedState/data.data  R\n??            ??/Preferences/com.apple.Terminal.plist.kn0E7LJ  W\n??                                            ??/vm/swapfile0  R\n??              ??/Preferences/com.apple.Safari.plist.jEQRQ5N  W\n??           ??/Preferences/com.apple.HIToolbox.plist.yBPXSnY  W\n??       ??/fsCachedData/F2BF76DB-740F-49AF-94DC-71308E08B474  W\n??                           ??/com.apple.Safari/Cache.db-wal  W\n??                           ??/com.apple.Safari/Cache.db-wal  W\n??       ??/fsCachedData/88C00A4D-4D8E-4DD8-906E-B1796AC949A2  W\n```\n\n# `ip` Provider\n\nThere are two probes, `send` and `receive`, inside the [`ip` provider][ipProvider]. They are triggered whenever data is either sent or received over the IP. The arguments `arg0` to `arg5` provide access to the kernel structures related to the IP packet being sent or received.\n\nThis can be put together into very powerful network debugging tools. It will make using `tcpdump(1)` seem like yesteryear's trick. The `ip` provider lets you print exactly the information you are interested in, at the point in time that you're interested in it.\n\nCheck the [documentation][ipProvider] for some excellent examples.\n\n\n<a name=\"staticProbes\"></a>\n## Defining Our Own Static Probes\n\nDTrace lets us create our own probes, and with that, we can unlock the real power of DTrace for our own apps.\n\nThese kinds of probes are called *static probes* in DTrace. We briefly talked about this in the [first examples](#ATimingExample). The [Wire app](https://itunes.apple.com/app/wire/id931134707?mt=12) defines its own provider with its own probe:\n\n```\nprovider syncengine_sync {\n    probe strategy_go_to_state(int);\n}\n```\n\nWe then call this probe in the code:\n\n    \n```objc\n- (void)goToState:(ZMSyncState *)state\n{\n    [self.currentState didLeaveState];\n    self.currentState = state;\n    SYNCENGINE_SYNC_STRATEGY_GO_TO_STATE(state.identifier);\n    [self.currentState didEnterState];\n}\n```\n\nOne might argue that we could have just used the `objc` provider; using our own probe gives us more flexibility. We can later change the Objective-C code without affecting the DTrace probes.\n\nAdditionally, the static probes give us easy access to the arguments. We saw above how we can use this both for timing and logging.\n\nThe power of DTrace static probes is that they give us a stable interface to debug our code, and an interface that exists even in production code. When someone is seeing odd behavior, we can attach a DTrace script to the running app, even for a production build of that app. And the flexibility of DTrace allows us to use the same probes for multiple purposes.\n\nWe can use DTrace as a logging tool. But it also allows us to gather detailed quantitative information about timing, network requests, etc.\n\nThe reason we can leave the probes in production code is that probes are *zero cost*, or to be fair, equivalent to a test-and-branch CPU instruction.\n\nLet's take a look at how we add static probes to our project.\n\n### Provider Description\n\nFirst we need to create a `.d` file that defines the providers and probes. If we create a `provider.d` file with this content, we will have two providers:\n\n```\nprovider syncengine_sync {\n    probe operation_loop_enqueue(int, int, intptr_t);\n    probe operation_loop_push_channel_data(int, int);\n    probe strategy_go_to_state(int);\n    probe strategy_leave_state(int);\n    probe strategy_update_event(int, int);\n    probe strategy_update_event_string(int, char *);\n};\n\nprovider syncengine_ui {\n    probe notification(int, intptr_t, char *, char *, int, int, int, int);\n};\n```\n\nThese providers are `syncengine_sync` and `syncengine_ui`. Within each one, we define a set of probes with their argument types.\n\n### Creating a Header File\n\nWe now need to add the `provider.d` file to an Xcode target. It is important to make sure that the type is set to *DTrace source*. Xcode now automatically processes it when building. During this processing, DTrace creates a corresponding `provider.h` header file that we can include. It is important to add the `provider.d` file to both the Xcode project and the corresponding target.\n\nUnder the hood, Xcode calls the `dtrace(1)` tool:\n\n```\ndtrace -h -s provider.d\n```\n\nThis will generate the corresponding header file. The header file ends up in the [`DERIVED_FILE_DIR][XcodeBuildDerivedFileDir], and can be imported with\n\n```objc\n#import \"provider.h\"\n```\n\nin any source file in the project. Xcode has a built-in, so-called *build rule* for DTrace provider descriptions. Compare [objc.io issue #6, “The Build Process,”](/issue-6/build-process.html) for more information on build rules and the build process in general.\n\n\n### Adding the Probe\n\nFor each static probe, the header file will contain two macros:\n\n```\nPROVIDER_PROBENAME()\nPROVIDER_PROBENAME_ENABLED()\n```\n\nThe first one is the probe itself. The second one will evaluate to 0 when the probe is not enabled.\n\nThe DTrace probes themselves are zero cost when not enabled, i.e. as long as no one attached to a probe, they're for free. Sometimes, however, we may want to pre-calculate / preprocess some data before sending it to a probe. In those rare cases, we can use the `_ENABLED()` macro, like so:\n\n```objc\nif (SYNCENGINE_SYNC_STRATEGY_GO_TO_STATE_ENABLED()) {\n    argument = /* Expensive argument calculation code here */;\n    SYNCENGINE_SYNC_STRATEGY_GO_TO_STATE(argument);\n};\n```\n\n### Wrapping DTrace Probes\n\nCode readability is very important. As we add more and more DTrace probes to our code, we need to make sure the code doesn't get cluttered and incomprehensible due to the probes. After all, the probes are here to help us, not to make things more difficult.\n\nWhat we did was to add another simple wrapper. These wrappers both make the code a bit more readable and also add the `if (…_ENABLED*())` check in one go.\n\nIf we go back to the state machine example, here is our probe macro:\n\n```objc\nSYNCENGINE_SYNC_STRATEGY_GO_TO_STATE(state);\n```\n\nIn order to make that a bit easier on the eye, we created another header which defines the following:\n\n```objc\nstatic inline void ZMTraceSyncStrategyGoToState(int d) {\n    SYNCENGINE_SYNC_STRATEGY_GO_TO_STATE(d);\n}\n```\n\nWith that, we can call the following:\n\n```objc\nZMTraceSyncStrategyGoToState(state);\n```\n\nThat may seem like a small feat, but the [camel case](https://en.wikipedia.org/wiki/CamelCase) blends in better with normal Objective-C and Swift coding style.\n\nIn other cases we went further. If we look at the \n\n```\nprovider syncengine_ui {\n    probe notification(int, intptr_t, char *, char *, int, int, int, int);\n};\n```\n\nwe defined above, we have a long list of arguments. In the Wire app, we use this to log UI notifications.\n\nThis probe has a very long list of arguments. We decided that we wanted to use a single probe for a lot of different notifications that the syncing code sends to the UI to notify it about changes. The first argument, `arg0`, specifies what the notification is, and the second one specifies the `object` of the `NSNotification`. This makes it easy for our DTrace scripts to narrow in on those specific notifications of interest in the given situation.\n\nThe remainder of the arguments are less loosely defined on a per-notification basis, and we have multiple wrapper functions for the individual cases. When we want to pass two `NSUUID` instances, we call a wrapper function like this:\n\n```objc\nZMTraceUserInterfaceNotification_UUID(1, note.object,\n    conversation.remoteIdentifier, participant.user.remoteIdentifier,\n    wasJoined, participant.isJoined, currentState.connectionState, 0);\n```\n\nThis wrapper function is defined as this:\n\n```objc\nstatic inline void ZMTraceUserInterfaceNotification_UUID(int d, NSObject *obj, NSUUID *remoteID1, NSUUID *remoteID2, int e, int f, int g, int h) {\n    if (SYNCENGINE_UI_NOTIFICATION_ENABLED()) {\n        SYNCENGINE_UI_NOTIFICATION(d, (intptr_t) (__bridge void *) obj, remoteID1.transportString.UTF8String, remoteID2.transportString.UTF8String, e, f, g, h);\n    }\n}\n```\n\nAs noted, this serves two purposes. For one, we don't clutter our code with things like `(intptr_t) (__bridge void *)`. Additionally, we don't spend CPU cycles converting an `NSUUID` to an `NSString` and then to a `char const *`, unless we need to because someone is attached to the probe.\n\nAlong this pattern, we can define multiple wrapper / helper functions that funnel into the same DTrace probe.\n\n### DTrace and Swift\n\nWrapping DTrace probes like this also gives us integration between Swift code and DTrace static probes. `static line` function can now be called directly from Swift code:\n\n```swift\nfunc goToState(state: ZMSyncState) {\n    currentState.didLeaveState()\n    currentState = state\n    currentState.didEnterState()\n    ZMTraceSyncStrategyGoToState(state.identifier)\n}\n```\n\n## How DTrace Works\n\nThe D programming language is a compiled language. When we run the `dtrace(1)` tool, it compiles the script we pass to it into byte code. This byte code is then passed down into the kernel. Inside the kernel, there's an interpreter that runs this byte code.\n\nThat's one of the reasons why the programming language was kept simple. No one wants a bug in a DTrace script to put the kernel into an infinite loop and hang the system.\n\nWhen we add static probes to an executable (an app or a framework), these are added to the executable as `S_DTRACE_DOF` (DTrace Object Format) sections and loaded into the kernel when the executable runs. That is how DTrace knows about the static probes.\n\n\n\n\n## Final Words\n\nIt should be obvious that DTrace is extremely powerful and flexible. However, it is important to note that DTrace is not a replacement for tried and true tools such as `malloc_history`, `heap`, etc. As always, use the right tool for the job at hand.\n\nAlso, DTrace is not magic. You still have to understand the problem you're trying to solve.\n\nThat said, DTrace can bring your development skills and abilities to a new level. It allows you to track down problems in production code which are otherwise difficult or even impossible to locate.\n\nIf you have code that has `#ifdef TRACING` or `#if LOG_LEVEL == 1` in it, these are probably good candidates for code to be replaced by DTrace.\n\nBe sure to check out the [Dynamic Tracing Guide][DynamicTracingGuideHTML] ([PDF version][DynamicTracingGuidePDF]). And peek into your system's `/usr/share/examples/DTTk` directory for some inspiration.\n\nHappy debugging!\n\n\n\n[DynamicTracingGuidePDF]: https://docs.oracle.com/cd/E23824_01/pdf/E22973.pdf \"Oracle Solaris Dynamic Tracing Guide\"\n[DynamicTracingGuideHTML]: https://docs.oracle.com/cd/E23824_01/html/E22973/toc.html \"Oracle Solaris Dynamic Tracing Guide\"\n\n[dtrace1man]: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/dtrace.1.html \"dtrace - generic front-end to the DTrace facility\"\n[oracleDTraceProviders]: https://docs.oracle.com/cd/E23824_01/html/E22973/gkyal.html \"Oracle: DTrace Providers\"\n[DProgrammingLanguage]: https://docs.oracle.com/cd/E23824_01/html/E22973/gkwpo.html#scrolltoc \"D Programming Language\"\n[ps1man]: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/ps.1.html\n[DTraceGuideChapter3]: https://docs.oracle.com/cd/E19253-01/817-6223/chp-variables/index.html \"Dynamic Tracing Guide, Variables\"\n[DTraceGuideChapter9]: https://docs.oracle.com/cd/E19253-01/817-6223/chp-aggs/index.html \"Dynamic Tracing Guide, Aggregations\"\n[AppleDocNSURLSessionTask]: https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionTask_class/index.html \"ADC: NSURLSessionTask\"\n[AppleDocNSURLSessionTask_taskIdentifier]: https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionTask_class/index.html#//apple_ref/occ/instp/NSURLSessionTask/taskIdentifier \"-[NSURLSessionTask taskIdentifer]\"\n[XcodeBuildDerivedFileDir]: https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html#//apple_ref/doc/uid/TP40003931-CH3-SW43 \"DERIVED_FILE_DIR\"\n[ioProvider]: https://docs.oracle.com/cd/E19253-01/817-6223/chp-io/index.html \"io Provider\"\n[profileProvider]: https://docs.oracle.com/cd/E19253-01/817-6223/chp-profile/index.html \"profile Provider\"\n[ipProvider]: https://wikis.oracle.com/display/DTrace/ip+Provider \"ip Provider\"\n"
  },
  {
    "path": "2014-12-08-editorial.md",
    "content": "---\ntitle:  \"Editorial\"\ncategory: \"19\"\ndate: \"2014-12-08 12:00:00\"\ntags: editorial\n---\n\n\nWelcome to objc.io issue 19: all about debugging.\n\nWe're all making mistakes, all the time. As such, debugging is a core part of what we do every day, and we've all developed debugging habits — our own way of approaching the all-too-common situation where something is not working as it should.\n\nBut there's always more to learn about debugging. Do you use LLDB to its full potential? Have you disassembled framework code to glance under the covers? Have you ever used the DTrace framework? Do you know about Apple's new activity tracing APIs? We're going to take a look at these topics and more in this issue.\n\nPeter starts off with a [debugging case study](/issues/19-debugging/debugging-case-study/): he walks us through the workflow and the tools he used to track down a regression bug in UIKit, from first report to filed radar. Next, Ari shows us the [power of LLDB](/issues/19-debugging/lldb-debugging/), and how you can leverage it to make debugging less cumbersome. Chris writes about his [debugging checklist](/issues/19-debugging/debugging-checklist/), a list of the many things to consider when diagnosing bugs. Last but not least, Daniel and Florian talk about two powerful but relatively unknown debugging technologies: [DTrace](/issues/19-debugging/dtrace/) and [Activity Tracing](/issues/19-debugging/activity-tracing/).\n\nWe'd love for you to never need all of this — but since that's not going to happen, we at least hope you'll enjoy these articles! :-)\n\nBest from a wintry Berlin,\n\nChris, Daniel, and Florian.\n"
  },
  {
    "path": "2014-12-08-lldb-debugging.md",
    "content": "---\ntitle:  \"Dancing in the Debugger — A Waltz with LLDB\"\ncategory: \"19\"\ndate: \"2014-12-08 10:00:00\"\ntags: article\nauthor:\n  - name: Ari Grant\n    url: https://twitter.com/ari_grant\n---\n\nHave you ever been stuck trying to understand your code and logged the contents of a variable?\n\n```objc\nNSLog(@\"%@\", whatIsInsideThisThing);\n```\n\nOr skipped a function call to simplify the behavior of the program?\n    \n```objc\nNSNumber *n = @7; // theFunctionThatShouldReallyBeCalled();\n```\n\nOr short-circuited a logical check?\n    \n```objc\nif (1 || theBooleanAtStake) { ... }\n```\n\nOr faked the implementation of a function?\n    \n```objc\nint calculateTheTrickyValue {\n  return 9;\n  \n  /*\n   Figure this out later.\n   ...\n}\n```\n\nAnd had to recompile, and start over each time?\n    \nBuilding software is complicated and bugs will always appear. A common fix cycle is to modify the code, compile, run again, and wish for the best.\n\nIt doesn't have to be that way. You can use the debugger! And even if you already know how to inspect values, there is a lot more it is capable of.\n\nThis article intends to challenge your knowledge of debugging, explain the basics in a bit more detail than you likely know, and then show you a collection of fun examples. Let's take it for a spin and see where we end up.\n\n## LLDB\n\n[LLDB](http://lldb.llvm.org/) is an [open-source](http://lldb.llvm.org/source.html) debugger that features a REPL, along with C++ and Python plugins. It comes bundled inside Xcode and lives in the console at the bottom of the window. A debugger allows you to pause a program at a specific moment of its execution, inspect the values of variables, execute custom instructions, and then manipulate the advancement of the program as you see fit. ([Here](http://eli.thegreenplace.net/2011/01/23/how-debuggers-work-part-1.html) is one explanation of how debuggers work in general.)\n\nIt's likely that you have used a debugger before, even if only in Xcode's UI to add breakpoints. But with a few tricks, there are some pretty cool things that you can do. The [GDB to LLDB](http://lldb.llvm.org/lldb-gdb.html) reference is a great bird's-eye view of the available commands, and you might also want to install [Chisel](https://github.com/facebook/chisel), an open-source collection of LLDB plugins that make debugging even more fun!\n\nIn the meantime, let's begin our journey and start with how to print variables in the debugger.\n\n## The Basics\n\nHere is a small, simple program that logs a string. Notice that a breakpoint has been added on line 8, which was made by clicking in the gutter in the source view in Xcode:\n\n![](/images/issue-19/Image_2014-11-20_at_10.01.46_PM.png)\n\nThe program will pause its execution at that line and the console will open, allowing us to interact with the debugger. What shall we type?\n\n### _help_\n\nThe easiest command to try is `help`, which will list all the commands. And if you ever forget what a command does or want to know more, then you can read all the details with `help <command>`, e.g. `help print` or `help thread`. If you ever forget what the `help` command does, then you can try `help help`, but if you know enough to do that, then maybe you haven't entirely forgotten what the command does after all. &#128539;\n\n### _print_\n\nPrinting values is easy; just try the `print` command:\n\n![](/images/issue-19/Image_2014-11-20_at_10.09.38_PM.png)\n\nLLDB actually does prefix matching, so you would be fine to try `prin`, `pri`, or `p`. You can't use `pr`, since LLDB can't disambiguate it from the `process` command (luckily for us, `p` has been disambiguated).\n\nYou'll also notice that the result has a `$0` in it. You can actually use this to reference the result! Try `print $0 + 7` and you'll see `106`. Anything starting with a dollar sign is in LLDB's namespace and exists to help you.\n\n### _expression_\n\nWhat if you want to modify a value? _Modify_, you say? Yes, modify! That's where the handy `expression` command comes in:\n\n![](/images/issue-19/Image_2014-11-20_at_10.15.01_PM.png)\n\nThis doesn't just modify the value in the debugger. It actually modifies the value in the program! If you resume the program at this point, it will print `42 red balloons`. Magic.\n\nNote that from now on, we will be lazy with the number of characters, and replace `print` and `expression` with `p` and `e`, respectively.\n\n### What is the _print_ Command?\n\nHere's a fun expression to consider: `p count = 18`. If we execute that command and then print the contents of `count`, we’ll see that it behaves exactly as if we had run `expression count = 18`.\n\nThe difference is that the `print` command takes no arguments, unlike the `expression` command. Consider `e -h +17`. It is not clear if it means to execute `+17` as input, only with the `-h` flag, or if it intends to compute the difference between `17` and `h`. It finds that hyphen quite confusing indeed; you may not get the result that you like.\n\nLuckily, the solution is quite simple. Use `--` to signify the end of the flags and the beginning of the input. Then if you want the `-h` flag, you would do `e -h -- +17`, and if you want the difference, you would do `e -- -h +17`. Since passing no flags is quite common, there is an alias for `e --`. It is called `print`.\n\nIf you type `help print` and scroll all the way down, it will say:\n\n```\n'print' is an abbreviation for 'expression --'.\n```\n\n### Printing Objects\n\nIf we try\n\n```\np objects\n```\n\nthen the output is a bit verbose:\n\n```\n(NSString *) $7 = 0x0000000104da4040 @\"red balloons\"\n```\n\nIt's even worse if we try to print a more complex structure:\n\n```\n(lldb) p @[ @\"foo\", @\"bar\" ]\n\n(NSArray *) $8 = 0x00007fdb9b71b3e0 @\"2 objects\"\n```\n\nReally, we want to see the `description` method of the object. We need to tell the `expression` command to print the result as an _object_, using the `-O` flag (that's an \"oh\"):\n\n```\n(lldb) e -O -- $8\n<__NSArrayI 0x7fdb9b71b3e0>(\nfoo,\nbar\n)\n```\n\nLuckily, `e -O --` is aliased as `po` (for **p**rint **o**bject), and we can just use that:\n\n```\n(lldb) po $8\n<__NSArrayI 0x7fdb9b71b3e0>(\nfoo,\nbar\n)\n(lldb) po @\"lunar\"\nlunar\n(lldb) p @\"lunar\"\n(NSString *) $13 = 0x00007fdb9d0003b0 @\"lunar\"\n```\n\n### Print Variations\n\nThere are many different formats that you can specify for the `print` command. They are written in the style `print/<fmt>`, or simply `p/<fmt>`. Following are some examples.\n\nThe default format:\n\n```\n(lldb) p 16\n16\n```\n\nHexadecimal:\n\n```\n(lldb) p/x 16\n0x10\n```\n\nBinary (the `t` stands for **t**wo):\n\n```\n(lldb) p/t 16\n0b00000000000000000000000000010000\n(lldb) p/t (char)16\n0b00010000\n```\n\nYou can also do `p/c` for a character, or `p/s` for a string, as a null-terminated `char *`. [Here](https://sourceware.org/gdb/onlinedocs/gdb/Output-Formats.html) is the complete list of formats.\n\n### Variables\n\nNow that you can print objects and simple types, and modify them in the debugger with the `expression` command, let's use some variables to reduce how much typing we need to do. Just as you might declare a variable in C as `int a = 0`, you can do the same thing in LLDB. However, to be used, the variable **must** start with a dollar sign:\n\n```\n(lldb) e int $a = 2\n(lldb) p $a * 19\n38\n(lldb) e NSArray *$array = @[ @\"Saturday\", @\"Sunday\", @\"Monday\" ]\n(lldb) p [$array count]\n2\n(lldb) po [[$array objectAtIndex:0] uppercaseString]\nSATURDAY\n(lldb) p [[$array objectAtIndex:$a] characterAtIndex:0]\nerror: no known method '-characterAtIndex:'; cast the message send to the method's return type\nerror: 1 errors parsing expression\n```\n\nAwww. LLDB couldn't figure out the types involved. This happens at times. Just give it a hint:\n\n```\n(lldb) p (char)[[$array objectAtIndex:$a] characterAtIndex:0]\n'M'\n(lldb) p/d (char)[[$array objectAtIndex:$a] characterAtIndex:0]\n77\n```\n\nVariables make the debugger much easier to work with. Who would have thunk? &#128521;\n\n### Flow Control\n\nWhen you insert a breakpoint in the gutter in the source editor in Xcode (or add a breakpoint through one of the means below), the program will come to a stop when it hits the breakpoint.\n\nThen there are four buttons in the debug bar that you can use to control the flow of execution of the program:\n\n![](/images/issue-19/Image_2014-11-22_at_10.37.45_AM.png)\n\nThe buttons are, in order from left to right: continue, step over, step into, step out.\n\nThe first, continue, will unpause the program and allow it to continue execution normally (perhaps forever, or until it hits another breakpoint). In LLDB, you can execute this command as `process continue`, which is aliased to `continue`, and thus, just `c`.\n\nThe second, step over, will execute a line of code as if it were a black box. If the line you are at is a function call, then it will **not** go inside the function, but instead execute the function and keep going. LLDB makes this available as `thread step-over`, `next`, or `n`.\n\nIf you do want to step inside a function call in order to debug or examine its execution, then use the third button, step in, available in LLDB as `thread step-in`, `step`, and `s`. Notice that `next` and `step` behave the same when the current line of code is not a function call.\n\nMost people know `c`, `n`, and `s`. But then there is the fourth button, step out. If you ever accidentally step into a function when you meant to step over it, then the typical response is to run `n` repeatedly until the function returns. Step out is your savior here. It will continue execution until the next `return` statement (until a stack frame is popped), and then stop again.\n\n#### Example\n\nConsider this partial program:\n\n![](/images/issue-19/Image_2014-11-22_at_10.53.52_AM.png)\n\nSay we run the program, allow it to stop at the breakpoint, and then execute this sequence of commands:\n\n```\np i\nn\ns\np i\nfinish\np i\nframe info\n```\n\nHere, `frame info` will tell you the current line number and source file, among other things; look at `help frame`, `help thread`, and `help process` for more information. So what will the output be? Think about it before reading the answer!\n\n```\n(lldb) p i\n(int) $0 = 99\n(lldb) n\n2014-11-22 10:49:26.445 DebuggerDance[60182:4832768] 101 is odd!\n(lldb) s\n(lldb) p i\n(int) $2 = 110\n(lldb) finish\n2014-11-22 10:49:35.978 DebuggerDance[60182:4832768] 110 is even!\n(lldb) p i\n(int) $4 = 99\n(lldb) frame info\nframe #0: 0x000000010a53bcd4 DebuggerDance`main + 68 at main.m:17\n```\n\nThe reason that it is still on line 17 is because the `finish` command ran until the `return` of the `isEven()` function, and then stopped immediately. Note that even though it is on line 17, it has already executed the line!\n\n#### Thread Return\n\nThere is one more awesome function that you can use to control program flow when debugging: `thread return`. It takes an optional argument, loads that into the return register, and immediately executes the return command, jumping out of the current stack frame. This means that the rest of the function **is not executed**. This could cause problems with ARC's reference counting/tracking, or prevent any cleanup you have inside a function. However, executing this command right at the start of a function is a great way to \"stub\" the function and fake it returning another value.\n\nLet's run a sightly modified set of commands with the same snippet of code above:\n\n```\np i\ns\nthread return NO\nn\np even0\nframe info\n```\n\nThink about it before you read the answer. OK, here's the answer:\n\n```\n(lldb) p i\n(int) $0 = 99\n(lldb) s\n(lldb) thread return NO\n(lldb) n\n(lldb) p even0\n(BOOL) $2 = NO\n(lldb) frame info\nframe #0: 0x00000001009a5cc4 DebuggerDance`main + 52 at main.m:17\n```\n\n## Breakpoints\n\nWe have all used breakpoints as a way to bring a program to a stop, inspect the current state, and hunt down bugs. But if we change our interpretation of breakpoints, a lot more becomes possible.\n\n> A breakpoint allows you to instruct a program when to stop, and then allows the running of commands.\n\nConsider putting a breakpoint at the start of a function, using `thread return` to override the behavior of the function, and then continuing. Now imagine automating this process. Sounds yummy, doesn't it?\n\n### Managing Breakpoints\n\nXcode offers a bunch of tools for creating and manipulating breakpoints. We'll go through each and describe the equivalent commands in LLDB that would create the same breakpoint (yes, you can add breakpoints from *inside* the debugger).\n\nIn the left pane in Xcode, there is a collection of buttons. One looks like a breakpoint. Clicking it opens the breakpoint navigator, a pane where you can manipulate all of your breakpoints at a glance:\n\n![](/images/issue-19/Image_2014-11-22_at_11.38.24_AM.png)\n\nHere you can see all of your breakpoints — `breakpoint list` (or `br li`) in LLDB. You can also click on an individual breakpoint to turn it on or off — `breakpoint enable <breakpointID>` and `breakpoint disable <breakpointID>` in LLDB:\n\n```\n(lldb) br li\nCurrent breakpoints:\n1: file = '/Users/arig/Desktop/DebuggerDance/DebuggerDance/main.m', line = 16, locations = 1, resolved = 1, hit count = 1\n\n  1.1: where = DebuggerDance`main + 27 at main.m:16, address = 0x000000010a3f6cab, resolved, hit count = 1\n\n(lldb) br dis 1\n1 breakpoints disabled.\n(lldb) br li\nCurrent breakpoints:\n1: file = '/Users/arig/Desktop/DebuggerDance/DebuggerDance/main.m', line = 16, locations = 1 Options: disabled\n\n  1.1: where = DebuggerDance`main + 27 at main.m:16, address = 0x000000010a3f6cab, unresolved, hit count = 1\n\n(lldb) br del 1\n1 breakpoints deleted; 0 breakpoint locations disabled.\n(lldb) br li\nNo breakpoints currently set.\n```\n\n### Creating Breakpoints\n\nIn the example we have been using, we clicked on \"16\" in the gutter in the source view to create a breakpoint. To remove it, you can drag the breakpoint out of the gutter and let go of the mouse (it will vanish with a cute poof animation). You can also select a breakpoint in the breakpoint navigator and then press the delete key to remove it.\n\nTo create a breakpoint in the debugger, use the `breakpoint set` command:\n\n```\n(lldb) breakpoint set -f main.m -l 16\nBreakpoint 1: where = DebuggerDance`main + 27 at main.m:16, address = 0x000000010a3f6cab\n```\n\nThe shortest abbreviation you can use is `br`. As it turns out, `b` is an entirely different command (an alias for `_regexp-break`), but it is robust enough to allow the same breakpoint as above:\n\n```\n(lldb) b main.m:17\nBreakpoint 2: where = DebuggerDance`main + 52 at main.m:17, address = 0x000000010a3f6cc4\n```\n\nYou can also put a breakpoint on a symbol (a C function), without having to specify the line number:\n\n```\n(lldb) b isEven\nBreakpoint 3: where = DebuggerDance`isEven + 16 at main.m:4, address = 0x000000010a3f6d00\n(lldb) br s -F isEven\nBreakpoint 4: where = DebuggerDance`isEven + 16 at main.m:4, address = 0x000000010a3f6d00\n```\n\nThese breakpoints will now stop exactly at the start of the function, and this works for Objective-C methods too:\n\n```\n(lldb) breakpoint set -F \"-[NSArray objectAtIndex:]\"\nBreakpoint 5: where = CoreFoundation`-[NSArray objectAtIndex:], address = 0x000000010ac7a950\n(lldb) b -[NSArray objectAtIndex:]\nBreakpoint 6: where = CoreFoundation`-[NSArray objectAtIndex:], address = 0x000000010ac7a950\n(lldb) breakpoint set -F \"+[NSSet setWithObject:]\"\nBreakpoint 7: where = CoreFoundation`+[NSSet setWithObject:], address = 0x000000010abd3820\n(lldb) b +[NSSet setWithObject:]\nBreakpoint 8: where = CoreFoundation`+[NSSet setWithObject:], address = 0x000000010abd3820\n```\n\nIf you want to create a symbolic breakpoint in Xcode's UI, then click the `+` button at the bottom left of the breakpoint navigator:\n\n![](/images/issue-19/Image_2014-11-22_at_11.52.50_AM.png)\n\nThen choose the third option:\n\n![](/images/issue-19/Image_2014-11-22_at_11.54.44_AM.png)\n\nA popover will appear where you can enter in a symbol such as `-[NSArray objectAtIndex:]`, and then the breakpoint will cause the program to stop **any time** that method is called, whether from your code or Apple's!\n\nIf we look at the other options, we can see that there are some enticing options, which are also available for **any** breakpoint if you right click it in Xcode's UI and select the \"Edit Breakpoint\" option:\n\n![](/images/issue-19/Image_2014-11-22_at_11.58.06_AM.png)\n\nHere, the breakpoint has been modified to **only** stop when `i` is `99`. You can also use the \"ignore\" option to tell the breakpoint to not stop the first `n` times it is called (and the condition is true).\n\nAnd then there is that \"Add Action\" button...\n\n### Breakpoint Actions\n\nPerhaps in the example breakpoint above, you want to know the value of `i` every time the breakpoint is hit. We can use the action `p i`, and then when the breakpoint is hit and we enter the debugger, it will execute that command before giving you control:\n\n![](/images/issue-19/Screen_Shot_2014-11-22_at_12.01.32_PM.png)\n\nYou can also add multiple actions, which can be debugger commands, shell commands, or more robust printing:\n\n![](/images/issue-19/Image_2014-11-22_at_12.06.34_PM.png)\n\nYou can see that it printed `i`, then it said that sentence aloud (!), and then printed the custom expression.\n\nHere's what some of this looks like when done in LLDB instead of Xcode's UI:\n\n```\n(lldb) breakpoint set -F isEven\nBreakpoint 1: where = DebuggerDance`isEven + 16 at main.m:4, address = 0x00000001083b5d00\n(lldb) breakpoint modify -c 'i == 99' 1\n(lldb) breakpoint command add 1\nEnter your debugger command(s).  Type 'DONE' to end.\n> p i\n> DONE\n(lldb) br li 1\n1: name = 'isEven', locations = 1, resolved = 1, hit count = 0\n    Breakpoint commands:\n      p i\n\nCondition: i == 99\n\n  1.1: where = DebuggerDance`isEven + 16 at main.m:4, address = 0x00000001083b5d00, resolved, hit count = 0 \n```\n\nAutomation, here we come!\n\n### Continuing after Evaluation\n\nIf you look at the bottom of the edit breakpoint popover, you'll see one more option: *\"Automatically continue after evaluation actions.\"* It's just a checkbox, but it holds immense power. If you check it, the debugger will evaluate all of your commands and then continue running the program. It won't even be apparent that it executed the breakpoint at all (unless the breakpoint fires a lot and your commands take a while, in which case, your program will slow down). \n\nThis checkbox is the same as having the last breakpoint action be `continue`, but having a checkbox just makes it easier. And here it is in the debugger:\n\n```\n(lldb) breakpoint set -F isEven\nBreakpoint 1: where = DebuggerDance`isEven + 16 at main.m:4, address = 0x00000001083b5d00\n(lldb) breakpoint command add 1\nEnter your debugger command(s).  Type 'DONE' to end.\n> continue\n> DONE\n(lldb) br li 1\n1: name = 'isEven', locations = 1, resolved = 1, hit count = 0\n    Breakpoint commands:\n      continue\n\n  1.1: where = DebuggerDance`isEven + 16 at main.m:4, address = 0x00000001083b5d00, resolved, hit count = 0\n```\n\nAutomatically continuing after evaluating a breakpoint allows you to modify your program solely through the use of breakpoints! You could stop at a line, run an `expression` command to change a variable, and then continue.\n\n#### Examples\n\nConsider the infamous \"print-debug\" technique. Instead of\n\n```objc\nNSLog(@\"%@\", whatIsInsideThisThing);\n```\n\nreplace this log statement with a breakpoint that prints the variable and then continues.\n    \nInstead of\n    \n```objc\nint calculateTheTrickyValue {\n  return 9;\n  \n  /*\n   Figure this out later.\n   ...\n}\n```\n\nadd a breakpoint that uses `thread return 9` and then have it continue.\n\nSymbolic breakpoints with actions are really powerful. You can also add them to your friends' Xcode projects and have actions that speak things aloud. See how long it takes them to figure out what is going on. &#128516;\n\n### Full Execution in the Debugger\n\nThere is one more idea to look at before we start dancing. You really can run just about any C/Objective-C/C++/Swift command in the debugger. The one weak spot is that it cannot create new functions... which means no new classes, blocks, functions, C++ classes with virtual methods, etc. Other than that, it can do it all!\n\nWe can malloc some bytes:\n\n```\n(lldb) e char *$str = (char *)malloc(8)\n(lldb) e (void)strcpy($str, \"munkeys\")\n(lldb) e $str[1] = 'o'\n(char) $0 = 'o'\n(lldb) p $str\n(char *) $str = 0x00007fd04a900040 \"monkeys\"\n```\n\nOr we can inspect some memory (using the `x` command) to see **4 bytes** of our new array:\n\n```\n(lldb) x/4c $str\n0x7fd04a900040: monk\n```\n\nWe can also look 3 bytes down (the `x` command requires backticks, since it only takes a memory address and not actually an expression; see `help x` for more information):\n    \n```\n(lldb) x/1w `$str + 3`\n0x7fd04a900043: keys\n```\n\nBut when you are all done, be sure to free the memory so that you don't leak (lol... we are in the debugger):\n\n```\n(lldb) e (void)free($str)\n```\n\n## Let's Dance\n\nNow that we know the basic steps, it's time to dance and do some crazy things. I once wrote a blog post on [looking at the internals of `NSArray`](http://arigrant.com/blog/2014/1/19/adventures-in-the-land-of-nsarray). The post uses a lot of `NSLog` statements, but I actually did all the exploration in the debugger. It may be a fun exercise to see if you can figure out how.\n\n### Poking around without a Breakpoint\n\nWhen an application is running, the debug bar in Xcode's UI shows a pause button instead of a continue one:\n\n![](/images/issue-19/Screen_Shot_2014_11_22_at_1_50_56_PM.png)\n\nClicking that button will pause the app (it runs `process interrupt`, since LLDB is always attached behind the scenes). This will then give you access to the debugger, but it might not look like you can do much, since there are no variables in scope, and there is no specific area of the code to look at.\n\nThat's where things get fun. If you are running an iOS app, you could try this (since globals are available)\n\n```\n(lldb) po [[[UIApplication sharedApplication] keyWindow] recursiveDescription]\n<UIWindow: 0x7f82b1fa8140; frame = (0 0; 320 568); gestureRecognizers = <NSArray: 0x7f82b1fa92d0>; layer = <UIWindowLayer: 0x7f82b1fa8400>>\n   | <UIView: 0x7f82b1d01fd0; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x7f82b1e2e0a0>>\n```\n\nand see the entire hierarchy! [Chisel](https://github.com/facebook/chisel) implements this as `pviews`.\n\n### Updating the UI\n\nThen, given the above output, we could stash the view:\n\n```\n(lldb) e id $myView = (id)0x7f82b1d01fd0\n```\n\nThen modify it in the debugger to change its background color:\n\n```\n(lldb) e (void)[$myView setBackgroundColor:[UIColor blueColor]]\n```\n\nHowever, you won't see any changes until you continue the program again. This is because the changes need to be sent over to the render server and then the display will be updated.\n\nThe render server is actually another process (called `backboardd`), and even though the containing process of what we are debugging is interrupted, `backboardd` is not!\n\nThis means that without continuing, you can execute the following:\n\n```\n(lldb) e (void)[CATransaction flush]\n```\n\nThe UI will update live in the simulator or on the device while you are still in the debugger! [Chisel](https://github.com/facebook/chisel) provides an alias for this called `caflush`, and it is used to implement other shortcuts like `hide <view>`, `show <view>`, and many, many others. All of [Chisel](https://github.com/facebook/chisel)'s commands have documentation, so feel free to run `help show` after installing it to see more information. \n\n### Pushing a View Controller\n\nImagine a simple application with a `UINavigationController` at the root. You could get it pretty easily in the debugger by executing the following:\n\n```\n(lldb) e id $nvc = [[[UIApplication sharedApplication] keyWindow] rootViewController]\n```\n\nThen push a child view controller:\n\n```\n(lldb) e id $vc = [UIViewController new]\n(lldb) e (void)[[$vc view] setBackgroundColor:[UIColor yellowColor]]\n(lldb) e (void)[$vc setTitle:@\"Yay!\"]\n(lldb) e (void)[$nvc pushViewContoller:$vc animated:YES]\n```\n\nFinally, execute the following:\n\n```\n(lldb) caflush // e (void)[CATransaction flush]\n```\n\nYou will see the navigation controller pushed right before your very eyes!\n\n### Finding the Target of a Button\n\nImagine you have a variable in the debugger, `$myButton`, that you got from creating it, grabbing it from the UI, or simply having it as a variable in scope when you are stopped at a breakpoint. You might wonder who receives the actions when you tap on it. Here's how easy it is:\n\n```\n(lldb) po [$myButton allTargets]\n{(\n    <MagicEventListener: 0x7fb58bd2e240>\n)}\n(lldb) po [$myButton actionsForTarget:(id)0x7fb58bd2e240 forControlEvent:0]\n<__NSArrayM 0x7fb58bd2aa40>(\n_handleTap:\n)\n```\n\nNow you might want to add a breakpoint for when that happens. Just set a symbolic breakpoint on `-[MagicEventListener _handleTap:]`, in LLDB or Xcode, and you are all set to go!\n\n### Observing an Instance Variable Changing\n\nImagine a hypothetical case where you have a `UIView` that was somehow having its `_layer` instance variable being overwritten (uh oh!). Since there might not be a method involved, we can't use a symbolic breakpoint. Instead, we want to *watch* when an address is written to.\n\nFirst we would need to find out where in the object the \"_layer\" ivar is:\n\n```\n(lldb) p (ptrdiff_t)ivar_getOffset((struct Ivar *)class_getInstanceVariable([MyView class], \"_layer\"))\n(ptrdiff_t) $0 = 8\n```\n\nNow we know that `($myView + 8)` is the memory address being written to:\n\n```\n(lldb) watchpoint set expression -- (int *)$myView + 8\nWatchpoint created: Watchpoint 3: addr = 0x7fa554231340 size = 8 state = enabled type = w\n    new value: 0x0000000000000000\n```\n\nThis was added to [Chisel](https://github.com/facebook/chisel) as `wivar $myView _layer`.\n\n### Symbolic Breakpoints on Non-Overridden Methods\n\nImagine that you want to know when `-[MyViewController viewDidAppear:]` is called. What would happen if `MyViewController` didn't actually implement that method, but its superclass did? We can try setting a breakpoint and see:\n\n```\n(lldb) b -[MyViewController viewDidAppear:]\nBreakpoint 1: no locations (pending).\nWARNING:  Unable to resolve breakpoint to any actual locations.\n```\n\nSince LLDB is looking for a *symbol*, it won't find it, and your breakpoint will never fire. What you need to do is set a condition, `[self isKindOfClass:[MyViewController class]]`, and then put the breakpoint on `UIViewController`. Normally, setting a condition like this will work, however, here it doesn’t since we don’t own the implementation of the superclass.\n\n`viewDidAppear:` is a method that Apple wrote, and thus, there are no symbols for it; there is no `self` when inside that method. If you wanted to use `self` in a symbolic breakpoint, you would have to know where it is (it could be in the registers or on the stack; in x86 you’ll find it at `$esp+4`). This is a pain though, because there are already at least four architectures you’d have to know (x86, x86-64, armv7, armv64). Oof! You can imagine taking the time to learn the instruction set and [calling convention](http://en.m.wikipedia.org/wiki/Calling_convention) for each one, and then writing a command that will set a breakpoint for you on the correct super class and with the correct condition. Luckily, this has already been done in [Chisel](https://github.com/facebook/chisel), and is called `bmessage`:\n\n\n```\n(lldb) bmessage -[MyViewController viewDidAppear:]\nSetting a breakpoint at -[UIViewController viewDidAppear:] with condition (void*)object_getClass((id)$rdi) == 0x000000010e2f4d28\nBreakpoint 1: where = UIKit`-[UIViewController viewDidAppear:], address = 0x000000010e11533c\n```\n\n### LLDB and Python\n\nLLDB has full, built-in [Python support](http://lldb.llvm.org/python-reference.html). If you type `script` in LLDB, it will open a Python REPL. You can also pass a line of Python to the `script command` and have it executed without entering the REPL:\n\n```\n(lldb) script import os\n(lldb) script os.system(\"open http://www.objc.io/\")\n```\n\nThis allows you to create all sorts of cool commands. Put this in a file, `~/myCommands.py`:\n\n```\ndef caflushCommand(debugger, command, result, internal_dict):\n  debugger.HandleCommand(\"e (void)[CATransaction flush]\")\n```\n\nThen, in LLDB, run the following:\n\n```\ncommand script import ~/myCommands.py\n```\n\nOr, put the line in `/.lldbinit` to have it executed every time LLDB starts. [Chisel](https://github.com/facebook/chisel) is nothing more than a collection of Python scripts that concatenate strings, and then tells LLDB to execute them. Simple, huh?\n\n## Wield the Debugger\n\nThere is a lot that LLDB is capable of. Most of us are used to `p`, `po`, `n`, `s`, and `c`, but there is so much more it can do. Mastering all of its commands (there really are not that many) will give you so much more power in unraveling the runtime behavior of your code, finding bugs, forcing specific execution paths, and even prototyping simple interacts — what would happen if a modal view controller opened right now? Try it!\n\nThis article was meant to show you a glimpse of the full power that it has and encourage you to be a bit more adventurous with what you type into the console.\n\nOpen up LLDB, type `help`, and see the list of all of the commands. How many have you tried? How many do you use?\n\nHopefully `NSLog` doesn't really seem that cool any more. At least it had a run for a while.\n\nHappy debugging!\n"
  },
  {
    "path": "2015-01-13-andy-matuschak.md",
    "content": "---\ntitle:  \"A Generation of Lifelong Learners\"\ncategory: \"20\"\ndate: \"2015-01-13 11:00:00\"\ntags: article, interview\nauthor:\n  - name: Andy Matuschak\n    url: https://twitter.com/andy_matuschak\n---\n\n\n**Andy, thanks for taking the time to sit down with us. Could you tell us a bit about how you got into programming?**\n\nI think, like many people, it was through games. I actually wasn't allowed to have game consoles for a certain period of my youth, and in that period I went to the public library a lot. When I was nine, I found these books that were meant, I think, to teach programming, but in fact they just had huge listings of basic source code. There were entire books that were source listings, and they were source listings for games. So when I would play games I would type in this book of source, and often there would be a syntax error halfway through, and I wouldn't understand the error messages, and wouldn't know how to fix them. So I'd have to start over, and then I could play the game, and after a little bit of doing that I realized that I could change the games, and very rapidly things degraded from there. I learned C++ — I guess I was 10 — because that was what people made games with, and tried to write a [MUD](http://en.wikipedia.org/wiki/MUD) — that was my first big project. It went okay...\n\n**Did you finish it?**\n\nI got it all going happily as a single-player situation, but of course that does not accomplish the multi-user part of a MUD — so I suppose in a way, no. I got distracted by graphics, getting obsessed with GL and 3D rendering and all that.\n\n**And then you kept on making games?**\n\nI was doing game engine stuff for a while, going through all your normal OpenGL tutorials, all the spinning cubes and particle engines and all that stuff, and I started making actual games. And then, it was really a case of a 13-year-old yak shaving. I realized that I didn't have a graphics tool that was good for making the content of the games. So, my roommates and I started making a pixel editor. It was called Pixen. That was my introduction to Cocoa and it was open source. When we hit 1.0, I think I was 14, and we went to the OS X Panther (or maybe Jaguar?) launch party at the Apple store in the mall. They had those at the time! We tried to give away CDs of our pixel editor.\n\n**Is there an alternative reality where you would not have become a programmer? Did you have anything else that you found super interesting?**\n\nIt was going to be engineering of some kind. I was fascinated by electrical engineering and even chemical engineering in little high school experiments, so it was going to be one of those things, and software engineering was what I could tinker with. At the time, I called it programming and not software engineering, and then some people told me, \"No, no, you're supposed to call it computer science!\" Of course, at the time, I didn't understand the interesting cultural distinction that is ongoing between computer science, software engineering, and programming, which I do actually think are useful distinct terms. But we sort of misuse them interchangeably.\n\n**Right. So now you're a software engineer, mostly?**\n\nI'm definitely not a computer scientist, although that's where my training is. I would say that I am sort of a software engineer, and I am a heavily computer science-influenced software engineer. And I spend about half of my time these days on more research stuff, reading papers and books on child development and working through ideas for how we might help that.\n\n**Leaving Apple and going to Khan Academy, helping people to learn and to educate, seems like quite a big jump. Why did it happen? What motivated you to do that?**\n\nWell, there are a few ways I can approach that question... I went to this college, Caltech, that fed into a lot of national science programs, NASA and DARPA and things like that, and so the discussions there were always about human scale impact. And we had the NASA administrator at the time come by and say to us, \"OK. You're the best and brightest. You're going to one of the best research universities in the world, and you are privileged, and you have all of these possibilities open to you. Good for you. Now, you have to use them for good. You have to do something that matters.\"\n\nWhen I got out in the broader software community, I was one of the only people who didn't go into research. I wanted to make things at the time when I graduated. I was disappointed by the impact and the ambition of most commercial software engineering and sales — so I thought long and hard about where I could make an impact. The first step for that was really deciding the metric of what that impact is and what matters. I've been heavily influenced by the work of the physicist [David Deutsch](http://en.wikipedia.org/wiki/David_Deutsch), and, through him, have sort of derived the metric for myself that I want to expand the reach of human knowledge and ability. So I thought about how I might do that.\n\nWe would expand our reach if we lived longer, so there are great opportunities there in biotech and chemical engineering. And we would expand our reach if we had better tools for experimentation. And so fabrication tools and synthesis tools are appealing, virtual reality is appealing, artificial intelligence strongly is appealing along that axis. But eventually I decided that, given my background and my opportunities, making a contribution to education would be my best bet, because if I can create a generation of lifelong learners, then we will have more people trying to solve these problems.\n\n**So you want to build tools for coming generations to learn better, so that they can solve difficult problems going forward?**\n\nYes, that's my hope. A big challenge in learning is about the meta learning. It's a way for people to learn without destroying their self confidence. It's a way for people to learn without destroying their attitude toward education. I would be satisfied with the same efficacy: if the tools I create yield the same math scores for graduates, if only their attitudes toward math were better at the end and they were more interested in continuing to learn later on in life.\n\n**So clearly you think that the schools and universities we have already are not good enough — could you elaborate on this?**\n\nNo, it's not good enough. And it's not good enough along many axes. So the most fundamental axis along which it's not good enough is that most people don't have access to what I just described. And that's, I think, the most important thing to fix and the thing to fix first. And that's why the mission right now is a free education for everybody. And Khan Academy's mission is actually a free *world-class* education for everybody, so we're perhaps focusing on the free and for everybody part first, and then world-class will come. But that gets to the second point, which is that I'm not really satisfied with what counts as world-class right now.\n\nI had so many classmates who had the best education money could buy and were taught by Nobel laureates. Nevertheless, they had awful attitudes toward learning, and considered themselves done as soon as they graduated. They just wanted to go off and find some steady job, where they can kind of turn their brains off, because they'd been so beaten down by the system and their curiosity and intellect had been so damaged.\n\n**That sounds like a big frustration...**\n\nI think so. I think you ask most people what they think of math and they say either, \"Oh, math's boring,\" or \"I'm not good at math. I'm not a math person.\" You get similar responses for sciences. If you ask people about literature, maybe they'll talk about some books whose plots they found interesting, but they used to read in school, and now that's kind of over. So I suppose the biggest change I would like to affect is to change our perception of education being over after college is done, which is not really a technical problem, and that's why I sort of hesitate with the label \"Software Engineer.\" That's definitely where a lot of my expertise is, but my ambitions are bleeding elsewhere.\n\n**With this really big plan, focusing on lifelong learning, do you have a vision where you want to be in 10 years, what you would like to have achieved?**\n\nRight now, the standard means of learning are broadly prescriptive. What I mean by that is that you read a textbook, and you passively absorb that information, or you watch a video, you watch a lecture. You are meant to sit and absorb, and perhaps there's a conversation afterward if you are wealthy. After you graduate, you just sort of have to construct those things for yourself. I'm really interested in so-called constructivist learning environments. That's a term from [Papert](http://en.wikipedia.org/wiki/Seymour_Papert), who was a learning researcher a few decades ago. In a constructivist learning environment, the student creates the knowledge for herself, rather than by reading it from a text and passively absorbing it. In these models, it's a little bit more like an environment that has been constructed, such that the features of the environment will encourage or require you to have understood something in order to manipulate them.\n\nThe example from Papert's research is that he was trying to use [Logo](http://en.wikipedia.org/wiki/Logo_(programming_language)) to teach children not about programming really, but about abstractions and procedural thinking, and so he had a classroom where there was the literal physical Logo turtle. There was a mechanical turtle at the time. It would draw on physical paper. And so surrounding the classroom were drawings that the turtle had made. On some of these drawings were source code, and on many of them were not. And the drawings would change over time, with the students, and the idea is that the student would maybe see a flower and the student would say, \"Oh, I like that drawing. Can I have it?\" And the instructor would say, \"Well, you can have it. You just have to type this code into the thing and then the turtle will draw it.\" And the student will say, \"OK, cool,\" and will have the drawing, but will look at another one, and that one doesn't have the source code.\n\nThese other drawings in the environment are designed so as to require that the student understood something in order to have produced them. So the environment is sculpted in that way. The hypothesis that I'm investigating, and likely will be investigating for many years, is that by allowing people to construct knowledge for themselves, we can hopefully — I will be happy with this — achieve the same learning outcomes, first off. But with much-improved metacognitive outcomes — that is, their self esteem is higher, their attitude toward learning is higher, their interest in learning more is greater.\n\n**Next to the more active learning that the constructivist approach encourages, another major difference between what you're doing at Khan Academy and more traditional lines of education is that you're free to learn what you want to learn and when you want to learn it. What do you think is the impact of this change?**\n\nAnother child development researcher, [Piaget](http://en.wikipedia.org/wiki/Jean_Piaget), has a lot to say about this. He says that one of the very important things that must happen in our development of young minds is the training of autonomy. And autonomy is meant here in a broader sense than what we normally mean when we talk about autonomy. It's not just autonomy in the sense of \"I'm free to play on the playground,\" but autonomy like, \"I am making my own choices — broadly, in general — for all of the things that are going on in my life.\" This is contravened by somebody telling you how to spend your day, or by an appeal to authority. For example, a heteronomous answer to \"Bobby cheated when we played this game\" would be \"Oh, well let me go talk to Bobby.\" The opposite of that, an autonomous answer, would be for the teacher to say \"And what did you say to Bobby?\" I think the training of autonomy is important for many reasons. There is some research that suggests that it provides greater outputs in the short term, but my belief is that it will lead to greater thinkers long term. \n\nI'm skeptical of an entire day being scheduled by someone other than the student. I feel more positively about the idea, though, of an educator modifying the environment of the student, day to day, in a way that is sort of sculpted for the students' progress. For instance, to be a little more specific, I think we can do better than giving the student the entire catalogue of human knowledge, with every element of that catalogue being sort of emphasized to the same degree. If we picture all of human knowledge as books on a bookshelf, we should place the books on the lowest shelf that we think would be best for the toddler coming up to it.\n\n**Is there something you miss from your time working at Apple?**\n\nYeah, I miss the people. That's a big thing. This is actually something I find really frustrating about the industry right now. There are so many brilliant people locked up in these enormous corporations, and occasionally they're being put to good use, but often they're just gears in a machine and their intellectual contributions could be enormous and so much more impactful. I don't just mean working on my projects. I mean it broadly, in general. It makes me sad both because I miss them, on a very sentimental level — I miss having that stimulating conversation with these very experienced people who, for example, were at NeXT. But I also wish that their impact could be greater.\n\n**Can you tell us a bit more about your time at Apple? What was it like to build a framework that's used by so many people? And how did you get there from building games?**\n\nOne of the dots filling this gap was this framework I built when I was 16, called Sparkle. That taught me a little bit about the realities of framework development, namely that you change APIs on people, and then they get angry. It's one of the fundamental realities of framework development, but honestly, I really didn't understand what I was doing until I got to Apple, and that was kind of the point. I finished college, and I had these broader ambitions, but Apple seemed like a great place to train, and it was. It still is — I would still recommend it to anybody for that reason. I worked on a team of people who have been building frameworks for 10+ years, and I guess I was kind of thrown in the deep end. I was forced to really rapidly train and understand many unusual and unique concerns when designing frameworks at that scale. Especially frameworks with binary compatibility concerns, which Sparkle did not have. Luckily, everybody around me was incredibly generous with their time and attention and I got great mentorship. After making many, many mistakes, I understood what the hell I was doing, and then a couple years later I left and may never use those skills again...\n\n**What was the biggest surprise for you in building frameworks at this scale?**\n\nI think binary compatibility was my greatest surprise. I did not understand the difficulties and complexities which it induced. So, to illustrate, those are bugs of the form: on iOS 8, this button has a white background, and on iOS 7, it's transparent. iOS 8 hadn't been released yet, so it must be our fault. You start digging a little bit and you realize that maybe a method was called in iOS 7 which took care of this issue, but this method is not called anymore on iOS 8. Then very rapidly this deteriorates into disassembling third-party code to which you do not have the source, in order to try to figure out what's going on. About half the time, somebody was doing something heinous and this was the strange way in which that manifested, and half the time we had changed something in an unexpected way, and we would need to add some defenses for this reason.\n\n***That sounds like a lot of work...***\n\nYeah, it was sort of detective work.\n\n**It sounds a little bit like what Peter Steinberger is doing in the other direction, disassembling UIKit to fix bugs...**\n\nYeah, for sure. The releases were so secret that we couldn't ask, \"Hey, Facebook guys, why are you doing this?\" We'd have to figure it out. On this note, I absolutely admire the work that Peter and his team are doing. I think that's a great thing, and I would love to see more of that. I would love to see more development of frameworks by the community. Facebook's making great contributions there — GitHub as well.\n\n**I've never thought of the fact that Apple also has to disassemble other people's binaries to figure out bugs.**\n\nOh yeah, and as a third-party developer now, I have to disassemble UIKit on a pretty regular basis, because I'm like, \"UIKit, why the hell?\" And the easiest way to get an answer is to just look at the disassembly.\n\n**There was a ton of new stuff last year. What are you really excited about? You're also excited about Swift?**\n\nYeah, these are pragmatic excitements, I should say. When we talk about what stuff I'm really excited about, it is not in the consumer software engineering industry in general. But as far as helping me get my work done, the category of things I'm most excited about are really interactive prototyping tools. So if you're trying to make an interaction that did not exist before — or even one that sort of vaguely has existed before — there really are no tools for it. We're still broadly designing in Photoshop. When you design in Photoshop, you produce something that looks like you designed in Photoshop. I remember at Apple, I could tell the difference between stuff that was made according to specs that came in from Photoshop, and stuff that was made according to specs that came in via After Effects, and stuff that was made according to specs that came in via live prototypes — it was a huge difference. So there are some tools out there that are trying to do this. I think none of them have gotten it right yet, but I am watching this with great fascination, hoping to contribute as well.\n\n**You mean tools like Framer?**\n\nYeah, Framer is a pretty good example. I'm very interested in things like Form, which was just bought by Google, and Origami, of course, which is interesting. I worked on a prototyping tool internal to Apple that got some things more right and other things less right. Each of these tools has pros and cons. We've actually been compiling a list of \"you might use this tool for this purpose, you might use this tool for this purpose.\" We find that we have to bounce around. I feel that none of them actually excel at any of the purposes except perhaps making things that are very much like things that already exist. \n\n**Do you think storyboards are also moving in this direction?**\n\nWell, I don't think so, but I'll have to lunge out a bit to explain. There are discrete modes of interaction and there are continuous modes of interaction. Something like a mouse click is a discrete form of interaction and something like a gesture is a continuous form of interaction. Similarly, there are discrete states of your application and there are continuous states of your application. So something like scrolling around a scroll view is continuous, and navigating to a different level of a navigational stack is discrete.\n\nStoryboards allow you to manipulate only the discrete and discrete section of that quadrangle. You can make it so that when you tap a thing, then there is a discrete transition to a completely different place. But I find that to be the least interesting kind of interaction, and frankly, I think that our user interfaces are dominated by that kind of interaction primarily because we don't have tools that allow us to think about other kinds.\n\nBut even starting in iOS 7, the sort of discrete modes are blurred a great deal. For example, being able to tap a table view cell and expand something beneath it — it's a very trivial example of something that storyboards would already have difficulty representing.\n\n**We would need more [Bret Victor](http://worrydream.com)s working on that problem.**\n\nWe do, and that's part of why I wish fewer people were making photo-sharing apps.\n\n**You mentioned before that the consumer software engineering space is actually less exciting to you. What excites you outside this space?**\n\nI've been reading a lot of papers in different fields, and I'm still wrapping my head around this. I think the ones that have stood out to me recently have been about child development, or about educational or pedagogical models. One of the most exciting ones I read was a survey in which a classroom of first graders who are five were given no formal math instruction of any kind, and instead they were given a bunch of activities which change day by day that were just placed around their environment, that were games. After a year they were assessed, and even though the children hadn't been taught any formal arithmetic methods, they were all able to answer these questions and had great metacognitive characteristics as well. Things like that are incredibly exciting to me.\n\n**So what's the most important thing you learned since starting at Khan Academy?**\n\nLeading teams is hard...\n\n**Why?**\n\nIt is a distinct skill set in which I had little practice. In the software industry, we throw individual contributing engineers into leadership roles on a pretty regular basis and expect them to excel when, of course, it is a distinct skill set, and we have to recognize that and appreciate that. So, a lot of learning has been required there.\n\n**You mentioned earlier that you had really good mentors at Apple. What made them good and what did you learn from them?**\n\nThe primary thing that made them good was the generosity with their time. That was always a challenge at Apple. Apple had the problem that it culturally does not value mentorship. One is not particularly rewarded for that, and teaching and learning is not really built into the culture.\n\nAnyway, I was fortunate enough to be able to pester a few people into spending a fair amount of time teaching me things, and in certain cases I made mistakes egregious enough to have people spend their time teaching me things without requiring pestering. And let's see, what made it effective? Probably their experience. The industry is an interesting place right now, because it's flooded by new grads, and the new grads are noisy and they're on Twitter and they're the ones that are speaking at conferences. But this industry has been around for a while. And there sure are a whole lot of software engineers with 15 or 20 years of professional experience in the field, and they're not as noisy. You don't read stuff by them all the time. They're mostly locked away in companies, where they can't talk, but they sure do know a lot.\n\n**At Khan Academy, you and your team adopted Swift very quickly. Can you tell us a bit about your experience?**\n\nWe have something like 20,000 lines of Swift code, and there are four contributors to that particular project, all contributing in Swift. And the issue that I have or that I've spoken to is really an issue of practicality versus idealism. I'm sure the issues that I have with the language itself are temporary, and they don't really matter in the grand scheme of things, just in the same sense that any app that you can build in six months probably doesn't matter. So take my complaints with a grain of salt. As far as the long-term health of the ecosystem and of professional software development goes, I think it will be an excellent contribution, and these rocky first months don't particularly matter.\n\nOn the plus side, it has helped us to write clearer code. That's the primary thing that I enjoy when I read a Swift interface. When I read a block of Swift code, I have a much better idea of what's happening and what's not going to happen. I can see return values and the return values tell me more. I can use enums to model more accurately the states of the system, we can use value types in order to constrain and confine and limit dependencies, most critically. More broadly, we can move toward this pattern that I strangely endorse, which is really treating UIKit as a library and not as a framework. \n\nIn terms of problems, the tooling is not there, and that's not a dig on the engineers. I happen to think, actually, that the Swift engineering team is one of Apple's finest engineering teams, but it's not done yet. It's full of bugs, it crashes too often. It generates faulty code occasionally. Most critically for us, it's just really slow. Really, really slow. The lack of incremental compilations is the biggest pain point for me. We're building a UI application, and as much as I'd like to spend all of my time thinking about how to improve learning, at some point, I've got to polish interactions and animations, and doing that with a two-minute turnaround time is really damaging — not only to productivity but actually to just morale. It just makes everybody angry. Everybody's just angry, all the time. And worse, it makes us lazy. We don't want to change stuff — so I would not be surprised if the Swift-implemented UI areas of this application turn out less polished than the areas of the application implemented in Objective-C, just because we can't iterate on it without going insane.\n\nBut that's temporary; I'm not worried about it. They know about this and they'll fix it. It's just that expectations were mismanaged, not out of malice, but I think Apple just didn't know, and hadn't built anything this big.\n\n**What effect do you think Swift will have on Apple's framework APIs? Do you expect something here in the short term?**\n\nI don't actually have insider knowledge here, so this is just speculation, but I think it will be a long process. At least when I was there, the teams spent the majority of their time not maintaining and improving frameworks, but really supporting market features like new screen sizes or support for new hardware. That's what takes most of the time. So it will take a conscious decision to do anything non-trivial, and I don't see that forthcoming.\n\nI think that if we fast-forward 10 years into the future, the APIs are all going to change. I have no doubt — either that will happen or it will be an incredible disappointment. In a year, I think you'll get better annotated headers...\n\n**Or we'll have somebody, like the Reactive Cocoa guys, wrapping it all up in a nice way...**\n\nYeah, that's going to happen in the short term for sure. Facebook's doing it right now, but it's not Swift. It's a C++ thing, with their components project, which I think is valorous. That's a great idea. React is a lovely design. One of the complaints that I have about my time at Apple is that, because the culture doesn't value learning, nobody read. I mean this in the sense that scientists say when they talk about reading. Nobody reads the journals. Nobody reads the papers. Nobody knows what's going on in the broader community. People didn't know about React and all this stuff. That was very frustrating. \n\nAnyway, I think those things are great contributions and I have no doubt that somebody will treat UIKit and other frameworks as libraries and wrap them up in something nicer. However, it won't get great adoption until Apple does it, and it'll forever be behind and feel like an impedance mismatch until Apple does it.\n\n**Since you've left Apple, you've also done more writing and speaking. Do you have any other plans there?**\n\nMainly more of the same. I'd like to publish more open-source stuff. I've been a little bitten by that in the past. When you publish open-source stuff, funny enough, people expect you to maintain it. I think we haven't quite gotten clear the contracts that we expect socially around open-source software. There's a kind of open-source software that has a community around and is well maintained, and then there's a kind of open-source software that's just being shared for educational purposes or for interest, but not really being maintained. I probably only have time for the latter, because, frankly, software engineering is not the interesting part of the problems I'm trying to solve right now. \n"
  },
  {
    "path": "2015-01-13-editorial.md",
    "content": "---\ntitle:  \"Editorial\"\ncategory: \"20\"\ndate: \"2015-01-13 12:00:00\"\ntags: editorial\n---\n\nHappy New Year and welcome to objc.io issue 20!\n\nTo mark the first issue of the year, we did things a bit differently, just like last year. This time, we interviewed a few developers who had interesting stories to tell. We hope you will enjoy reading what they have to say.\n\nWe want to thank [Andy Matuschak](/issues/20-interviews/andy-matuschak/), [Loren Brichter](/issues/20-interviews/loren-brichter/), and [Natasha Murashev](/issues/20-interviews/natasha-murashev/) for being a part of this special issue.\n\nBest from Berlin,\n\nChris, Daniel, and Florian.\n"
  },
  {
    "path": "2015-01-13-loren-brichter.md",
    "content": "---\ntitle:  \"Something Slightly Less Terrible\"\ncategory: \"20\"\ndate: \"2015-01-13 10:00:00\"\ntags: article, interview\nauthor:\n  - name: Loren Brichter\n    url: https://twitter.com/lorenb\n---\n\n**Loren, thanks for taking the time to talk to us — especially during the holidays. What are you up to these days?**\n\nI've been pretty busy over the holidays, mostly with family stuff. I'm finally getting back into real work now.\n\n**What are you currently working on?**\n\nI can't share any specifics yet, but I've been working on some low-level experiments for a while that are starting to come together. It's been nice to take a step back and to reflect on what I'm actually doing for a bit without the pressure to ship something.\n\n**Before you went down the road of programming, did you have other strong interests you could have imagined making a career of?**\n\nI definitely could have been an architect or mechanical engineer, or maybe a carpenter or photographer. I've been spending a lot more time away from glowing rectangles lately, which has been hard, because it's so easy for them to take over my field of view.\n\n**From carpenter to photographer — that's quite a range! With many options, what keeps you in programming?**\n\nPure inertia. I'm decent at it, and I hope I can figure out a way to do some good with these skills.\n\n**What are the most non-obvious career choices you've made?**\n\nI think the obvious ones to me were the ones that weren't so obvious to other people, like repeatedly quitting really stable, well-paying jobs to try something crazy. I'm lucky that some of the things I've made worked out well. It probably wouldn't work out the same way if I did it today.\n\n**Aside from the inertia that keeps you in programming, what about it is appealing to you?**\n\nHonestly, it appeals to me less and less. Maybe it's the Graybeard engineer in me, but the more I learn, the more terrible I think programming is. I'd love to rip everything up and start over. But you can only swim against the tide so far, so it's sometimes satisfying to sift through the garbage and repurpose terrible technologies to make something that is slightly less terrible.\n\n**With the appeal of programming in decline, could you see yourself doing something completely different in the future?**\n\nI'll always do something that involves programming, but it'll probably be something where software is a piece of it, rather than the whole thing.\n\n**So since you're not going to jump ship any time soon, let's talk about the terrible parts of programming...**\n\nIt's not like a boat with a couple of holes that we can patch; it's more like trying to sail across an ocean on a pile of accrued garbage. Sure, some of the stuff floats, and it keeps some other stuff from sinking. A better question might be: which parts are good? And you can only answer that if you look at a thing in isolation. Like, I could say that Rust is good. But even that enshrines the Von Neumann philosophy, and so you can crawl your way down the stack questioning everything, which isn't practical.\n\n**Which way do you think this is going to go? Will it get better?**\n\nI do think things will get better eventually. And then worse again. Then someone else will come along and go, \"Oy what a mess!\" And they'll make something better. And then the cycle starts again. Anyway, it's easy to complain, which is why I've been working on what I've been working on.\n\n**When you're trying to make things \"less terrible,\" do you have any greater goals in mind that guide your decisions? Why do you do all this?**\n\nOh yeah, but it's so abstract that people will think I'm nuts. So for a goal, I'll just say \"build tools to make us more enlightened.\" I mean \"enlightened\" in a Carl Sagan sense, where we are the universe trying to understand itself. And we've long hit the limit of what we can think with our naked brain, so we need to augment it in some way with mind tools. But the tools right now are so complicated that it takes all your mental energy just to try and \"hold\" them, so you have nothing left to actually do something interesting. Or at least they're too complicated for me. I'm not that smart.\n\nPersonally, I'm tired of the trivial app stuff, and the App Store isn't conducive to anything more interesting. I think the next big thing in software will happen outside of it.\n\n**With regard to the next big thing in software, are there any truly interesting developments out there today?**\n\nTotally. Lots of little glimmers of hope: Rust, Swift, TypeScript, asm.js, broad WebGL support — all pretty interesting in a narrow sense.\n\n**What specifically do you like about Swift?**\n\nIt's great to have a modern type system (it'll be nicer once it's finished). And I think the custom operators are ridiculously cute and I hope they're copied.\n\n**Do you already use it in your daily work?**\n\nI don't use it for anything real yet; the tools are still brittle. And until it's open-sourced or cloned, I can't really use it for anything that isn't locked to an Apple platform. Hell, I could at least build Objective-C on Linux.\n\n**You said earlier that you've been spending less time in front of the screen lately. I can imagine that becoming a father has been a major part of this.**\n\nAbsolutely. I'm going to be crazy strict in terms of limiting screen time, which maybe is ironic given what I've done for a living. I'm not sure when it happened, but my perspective on the mobile revolution shifted. It used to be really exciting just to see someone pull out an iPhone. Now it's like, \"Hey kid, stop staring at your phone!\" And apps, apps everywhere! Apps for wiping your butt. I've become an old fogey. Get your apps off my lawn!\n\n**I think many of us could benefit from limiting screen time — do you want to take the opportunity to make a public commitment?**\n\nHaha, no way.\n\n**Fair enough. Can you tell us a little bit more about how you work? You work mostly from home, right?**\n\nYes, every time I try not working from home, I'm miserable. I love working from home.\n\n**How do you structure your day — do you work on a fixed schedule or handle things more flexibly?**\n\nMan, having a kid really threw a wrench into my work schedule. My old cadence was really consistent. I worked without distraction until ridiculously early in the morning, then slept in. Now I have to go to bed at a reasonable time so I'm not a zombie when the little guy gets up in the morning. I feel like I'm starting to figure it out, but yeah, it's harder.\n\n**Do you mostly focus on one project at a time, or are you a multitasker?**\n\nI'd describe my work schedule as cooperatively single-threaded with a heavy context switch cost, so I try to keep time slices on the order of about a week. So I have lots of projects going at once that usually relate to each other in some way, but I only consciously work on one at a time.\n\nI can't consciously multitask at all, but I think my brain works a bit like libdispatch. The subconscious can chew on a lot of stuff in parallel. So when my conscious mind switches back to some other work it put aside earlier, there are usually a couple good ideas waiting for it.\n\n**You're known for building great apps all by yourself, combining many tasks that often would be done by different people. What draws you to this way of working, compared to working in a team?**\n\nWe draw lines between disciplines as a way to divvy up work, the same way you split up programs into modules or objects or some other thing. But whenever you split something in two, the pieces inevitably diverge, and it gets harder to piece them back together when you go to build something. I find that after a while, you actually spend more time dealing with \"glue\" than the actual thing itself, dealing with impedance mismatches or what-not (both at an architectural/engineering level, or at an interpersonal level). So my little cheat is to try and build things in a more holistic way. It's not actually much more work, because you only have to build the parts that actually, you know, *do* something, and there's minimal hacking required to stick them together.\n\n**Clearly though, there are limits to what you can build alone. Some projects will just require more people to get them done...**\n\nAbsolutely, but I think the limit is way further out than people realize. Some remarkable stuff throughout history has been accomplished by individuals — part of the trick is standing on the right shoulders. So sure, the space of ideas I am capable of exploring is smaller than if I were working on a big team, but they're both infinitely big, even if one infinity is smaller than the other. And for me personally, that's a worthwhile tradeoff.\n\n**How do you think teams could be organized in order to minimize the friction you're talking about?**\n\nI don't know. I do know that there are parallels to bad coding architecture at organizational and interpersonal levels, and just as an engineer might be capable of building a thing in a holistic way, a great manager might be able to organize a group of people in a holistic way, by deeply understanding each person and the problem they all need to solve together. I don't have those skills, but I don't think it involves any sort of magic template. If I picked up any wisdom over the last few years, it would be to try to know — like *really know* — what you're doing. It's harder, but it's simpler in the long run.\n\n**So when you're working on a project in a holistic way, is there still a separation between prototyping and building the actual product, or do these two things blend together a lot?**\n\nThey're completely blended, which usually means I end up doing my prototyping in code rather than in some other tool like Quartz Composer or Form. The obvious next step for any of those tools is to stop calling them prototyping tools and let them just create the final product.\n\n**How much time actually goes into the prototyping/experimentation phase versus building the things that end up in the actual product?**\n\nI guess it's 100 percent prototyping until I find the right thing (or run out of time), scrapping tons of stuff along the way, and then that's it — I just call it the final product. I guess I'll go back and rip out vestiges of failed evolutions and clean it up a bit, but that usually doesn't take long.\n\n**I guess the design and development processes are also very much interleaved for you?**\n\nCompletely, since design and prototyping are the same thing and I do that work in the same medium that the final product takes. Once I'm happy with it, I'm done.\n\n**In this process, do you mostly build technology with a specific product idea in mind, or do product ideas flow from technology you're building?**\n\nRecently it's been a weird mix. I have definite product goals in mind (otherwise I'd end up in the weeds), but the stuff I've been actually coding is somewhat abstract. And the more I work on it, the more I see how different products really aren't — or at least shouldn't be — different products at all. It's like hacking apart a grand idea to shove each piece into a vertical silo. The more I think about it, the more I think that \"apps\" are a bad unit of organization of software.\n\n**You've been around in this game for a while, and you've had great successes. How do you keep learning after all these years?**\n\nBy remembering that, at a fundamental level, nobody really knows anything. So in between swings of crippling confusion and daunting awe, my brain is in a nice mushy phase where it's receptive to absorbing stuff. Mostly through books. The ones with the paper.\n\n**What are some of your favorite books — you know, those with paper?**\n\nSome favorites on the shelf next to me: The C Programming Language, Mindstorms, Turtle Geometry, Ender's Game, Schild's Ladder, Advanced Global Illumination, The Theoretical Minimum, Collective Electrodynamics, A New Kind of Science.\n\n**What would your advice be for people who are just starting out in this field?**\n\nRemember that nothing is magic. Even though it seems like you're working at the top of a stack of impenetrable abstractions, they're made by people (who were probably rushed, or drunk, or both). Learn how they work, then figure out how to minimize your dependence on them.\n\n**Looking back on all the things you've built, do you have a \"hack\" that you're especially proud of?**\n\nNope. In hindsight, I think everything I've made stinks.\n\n**Well, let's end on a more positive note then: what do you do to get work out of your head?**\n\nPlaying with my son. It's the best.\n\n"
  },
  {
    "path": "2015-01-13-natasha-murashev.md",
    "content": "---\ntitle:  \"Infinite Things to Learn\"\ncategory: \"20\"\ndate: \"2015-01-13 09:00:00\"\ntags: article, interview\nauthor:\n  - name: Natasha Murashev\n    url: https://twitter.com/NatashaTheRobot\n---\n\n**Thanks for taking the time, Natasha! Could you tell us a little bit of what you currently do in your job and in the developer community?**\n\nBy day I'm an iOS engineer at Capital One, and we’re working on a bunch of exciting native apps in the finance space. Outside of work, I usually wake up really early and do blogging or learning or building something: morning is my time to grow.\n\n**Why did you get into iOS development?**\n\nI always wanted to learn, ever since I started developing. At my last company, I was doing Ruby on Rails. They had a hard time hiring iOS developers, so they asked if anybody wanted to learn iOS and switch. I started thinking about it, so I applied to the Mobile Makers bootcamp. After I got in, I went to my boss, and he let me go for a two-month sabbatical, during which I went to Chicago for an eight-week full-time course. I really liked this experience because I had instructors teaching me best practices and I learned at a much more accelerated pace, as opposed to just fiddling around on my own. When I came back I joined the iOS team.\n\n**Could you tell us a bit more about the bootcamp — what did you learn there?**\n\nWe learned a lot of Objective-C, which was the biggest challenge to me initially, as it seemed pretty intimidating. Every morning we learned new stuff and immediately applied it in the afternoon, building small apps together with others. We built an app every week, and at the end of the bootcamp we had to release an app in the App Store.\n\n**How did you get into programming initially, and why?**\n\nIt started when I came to visit San Francisco for a weekend. I absolutely fell in love with the tech scene. I went to a lot of meetups, and learned about startups, and, it was just a really positive and creative atmosphere. I moved here, and within a year I was working on a startup with some friends. I wasn't able to code at the time, but since I don’t like having my hands tied, I started learning soon after.\n\n**So you picked up enough Rails on your own to land a job as a Rails developer?**\n\nYes. Initially I started with a Stanford course in Java. I just did that online, and then I started learning Rails. I could hack things together and make it work, but I knew that's not how I wanted to program. Around that time, Dev Bootcamp just came out. I applied there and ended up in the second class. That was an an opportunity for me to learn smarter approaches to programming.\n\n**What did you study in college, by the way?**\n\nPsychology, it's actually been the most useful major.\n\nThere's that stereotype of programmers, sitting alone in the basement somewhere. But in real life you have to work with people. Working in a team is a lot about group dynamics and group think. Trying to put in different opinions is very important sometimes, because often times when you all know each other, everyone just agrees on things, and that might not be the right thing. There are also a lot of things around leadership, like caring for other people and developing a relationship that’s outside of just your everyday coding.\n\n**With your background in psychology, do you have any tips how to deal with the ups and downs in programming? Those frustrating moments were you just don't know what's going on?**\n\nThat's actually been the hardest thing about learning programming when I started, because I wasn’t used to that. In school, it was a very clear formula: you study hard and you get an A on the test. Or even in my jobs before programming, it was more of a linear learning process — it always gets better. But then when I started programming, one day I am on top of the world, and then the next day, I feel like I can't program at all anymore. The most important thing has been to learn how to debug, taking one step at a time, and questioning all your assumptions.\n\nApart from that, meditation — focusing on the breathing — helps me. Also just walking away, or taking a break. Sometimes, just going to sleep is the best option. The next day, the problem is solved much easier. To me, this part of programming has become a challenge that I love.\n\n**What do you think about all the new stuff that has come out recently, like Swift and WatchKit, and then iOS 8? What are you excited about?**\n\nEverything? I love learning. That's actually one of my favorite things about being a programmer. I missed that in my other jobs, but in programming there are just many more ups and downs with opportunities to learn. For example, Swift looks really easy on the outside. When it was announced, all the JavaScript developers were like, \"Oh, I’m now an iOS developer.\" However, from working with it, I've found that it’s actually harder than Objective-C. Objective-C is very simple. You can only do so many things. But in Swift there's a whole new world of patterns, and there are no best practices yet — it’s kind of a Wild West. There are so many choices.\n\nWith regard to WatchKit, it's pretty simple at the moment, but the promise of it is really exciting. I actually just got one of those FitBit scales, and the way it syncs data so seamlessly is really magical. I’m excited about that aspect of the watch, of being able to track information and sync it seamlessly.\n\n**If I recall correctly, you were disappointed about it initially, but then got really excited about it, right?**\n\nYeah, it's missing a lot of features that were advertised. For example, there’s no API for the Digital Crown or the taptic feedback. It’s very basic. For showing a map, the system takes a picture of the map and sends it to the watch. However, we had a WatchKit hackathon, and it was pretty cool to see what people were able to do with just the basic stuff that's there already.\n\n**When Swift came out, you suddenly became much more visible in the community. Was this a deliberate choice you made?**\n\nI've actually been blogging for a while. When I started coding, I started my blog. It's my way of solidifying my knowledge. At this point, if I learn something, I have to blog about it. It's just part of my routine. When Swift came out, it was just a natural extension of that. And because Swift is so new, there's a lot more to write about, since there are a lot more struggles on a daily basis that you can't find the answers to.\n\n**Did the visibility you got in the Swift community change something for you?**\n\nI just keep doing what I've been doing. I did start a Swift newsletter, but that's also part of my way of learning. The new part is that I've started doing a lot of speaking engagements. That's been really fun and works very well for solidifying my knowledge. When you’re teaching, you have to really learn your stuff. With the WatchKit hackathon, we gambled a bit, because the release date was not clear yet. In the end, I had to learn WatchKit in three days and then teach it. But I enjoyed the pressure because it forced me to learn it, whereas on my own I’d probably have taken a longer time, and then I wouldn’t learn it as well as when I had to actually be an expert on it after three days.\n\n**Right now, everybody's learning Swift, but maybe in a few years there will be established patterns. Do you think we can keep the community as open as it is now?**\n\nI'm starting to get the feeling with Swift that there will just be more options to solve particular problems in a good way. In Objective-C, it's pretty clear most of the time, but Swift just seems to allow more variety in style. Hopefully that'll open people up for learning different things and to keep their minds open.\n\n**We're almost at the end of the year. Do you have any ambitions or any concrete plans for next year?**\n\nYeah, I do have a list. A lot of it is just keeping healthy. Swift has been so exciting that I've just always been learning and pushing myself. But I'm hoping to get back to a normal sleep schedule soon. I’ve found that when you sleep enough, you actually get a lot more done, although it might feel counterintuitive at times. Another main thing is just to double down on learning. Swift has opened up a whole other world of things I wanna learn. I am definitely missing some pieces in my programming experience, so I think I'll just be focused on learning those pieces, and then figuring out how to best fit them in to write better code.\n\n**Do you have big ambitions for the long term?**\n\nI don't like to think too long term because things change. It's kind of like code: you have to rewrite everything. For me, it's more about being in a good state every day versus having a big goal. Every day when I wake up I want to ask myself, \"Am I happy?\" And I know things that make me happy, like learning, being healthy, and traveling. Those are the big three things that I try to center myself around. I feel that there's still a lot to gain for me by learning more, and then maybe in a year or so, I'll focus more on really building and releasing things.\n\n**Could you also see yourself doing something completely different than programming?**\n\nProbably not. I really enjoy it. Maybe in 10 years, who knows? I don’t know if I can find a field where I have infinite things to learn. I’ve been interested also in learning more hardware stuff, but I think that goes together with software. As long as I keep learning new things, keep challenging my mind, I'm happy to continue what I'm doing. Being a little bit outside my comfort zone makes me very happy.\n\n**Did you have any mentors or people who really inspired you when learning iOS and Rails?**\n\nI learned a lot by pair programming with people who are more experienced than me. It really accelerates the learning process. Sometimes I'd have a problem or a bug and I find one solution that works and I'm really happy with it. But then a senior programmer looks at it from different perspectives — not only if it works, but also if it is a good solution in the long term. And now for Swift, I'm learning a lot from the community, because it’s so new. I'm trying to read more books and to always look for people who are better than me.\n\n**When you were pair programming, were there any big lessons you learned, or was it more about many little details?**\n\nI'm very detail oriented, so I do enjoy the tiny little things and tricks that I learn. For example, my coworker showed me that you could turn on the Clang analyzer in Xcode and get much better warning messages. If he wouldn't have told me, I would not have known to even google for this kind of thing. When you pair program, you learn these tiny things that people take for granted. Some of my blog posts about these tiny things are actually my most popular ones, although I initially assumed that probably everybody except me knew this already.\n\nIn terms of bigger things, one of my coworkers at Capital One showed me how to apply the MVVM pattern in a real world situation. I've heard about it, I heard Facebook talk about it, but I wasn’t really sure how it worked, so it was really cool to have him walk me through an actual implementation of it. Now, I use the MVVM pattern all the time in different apps.\n\n**Is there anything else that you want to share?**\n\nYeah, it's a fun time to be an iOS developer. For anyone that is thinking of switching, it’s challenging, but it’s a very exciting time to be in this space.\n"
  },
  {
    "path": "2015-02-10-camera-capture-on-ios.md",
    "content": "---\ntitle: \"Camera Capture on iOS\"\ncategory: \"21\"\ndate: \"2015-02-10 09:00:00\"\ntags: article\nauthor:\n  - name: Matteo Caldari\n    url: https://twitter.com/matteo\n---\n\nThe iPhone has shipped with a camera since its first model. In the first SDKs, the only way to integrate the camera within an app was by using `UIImagePickerController`, but iOS 4 introduced the AVFoundation framework, which allowed more flexibility.\n\nIn this article, we'll see how image capture with AVFoundation works, how to control the camera, and the new features recently introduced in iOS 8.\n\n## Overview\n\n### AVFoundation vs. `UIImagePickerController`\n\n`UIImagePickerController` provides a very simple way to take a picture. It supports all the basic features, such as switching to the front-facing camera, toggling the flash, tapping on an area to lock focus and exposure, and, on iOS 8, adjusting the exposure just as in the system camera app.\n\nHowever, when direct access to the camera is necessary, the AVFoundation framework allows full control, for example, for changing the hardware parameters programmatically, or manipulating the live preview.\n\n### AVFoundation's Building Blocks\n\nAn image capture implemented with the AVFoundation framework is based on a few classes. These classes give access to the raw data coming from the camera device and can control its components.\n\n- `AVCaptureDevice` is the interface to the hardware camera. It is used to control the hardware features such as the position of the lens, the exposure, and the flash.\n- `AVCaptureDeviceInput` provides the data coming from the device.\n- `AVCaptureOutput` is an abstract class describing the result of a capture session. There are three concrete subclasses of interest to still image capture:\n  - `AVCaptureStillImageOutput` is used to capture a still image.\n  - `AVCaptureMetadataOutput` enables detection of faces and QR codes.\n  - `AVCaptureVideoOutput` provides the raw frames for a live preview.\n- `AVCaptureSession` manages the data flow between the inputs and the outputs, and generates runtime errors in case something goes wrong.\n- The `AVCaptureVideoPreviewLayer` is a subclass of `CALayer`, and can be used to automatically display the live feed generated from the camera. It also has some utility methods for converting points from layer coordinates to those of the device. It looks like an output, but it's not. Additionally, it *owns* a session (the outputs are *owned by* a session).\n\n## Setup\n\nLet's start building the capture. First we need an `AVCaptureSession` object:\n\n```swift\nlet session = AVCaptureSession()\n```\n\nNow we need a camera device input. On most iPhones and iPads, we can choose between the back camera and the front camera — aka the selfie camera. So first we have to iterate over all the devices that can provide video data (the microphone is also an `AVCaptureDevice`, but we'll skip it) and check for the `position` property:\n\n```swift\nlet availableCameraDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)\nfor device in availableCameraDevices as [AVCaptureDevice] {\n  if device.position == .Back {\n    backCameraDevice = device\n  }\n  else if device.position == .Front {\n    frontCameraDevice = device\n  }\n}\n```\n\nThen, once we've found the proper camera device, we can get the corresponding `AVCaptureDeviceInput` object. We'll set this as the session's input:\n\n```swift\nvar error:NSError?\nlet possibleCameraInput: AnyObject? = AVCaptureDeviceInput.deviceInputWithDevice(backCameraDevice, error: &error)\nif let backCameraInput = possibleCameraInput as? AVCaptureDeviceInput {\n  if self.session.canAddInput(backCameraInput) {\n    self.session.addInput(backCameraInput)\n  }\n}\n```\n\nNote that the first time the app is executed, the first call to  `AVCaptureDeviceInput.deviceInputWithDevice()` triggers a system dialog, asking the user to allow usage of the camera. This was introduced in some countries with iOS 7, and was extended to all regions with iOS 8. Until the user accepts the dialog, the camera input will send a stream of black frames.\n\nA more appropriate way to handle the camera permissions is to first check the current status of the authorization, and in case it's still not determined, i.e. the user hasn't seen the dialog, to explicitly request it:\n\n```swift\nlet authorizationStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)\nswitch authorizationStatus {\ncase .NotDetermined:\n  // permission dialog not yet presented, request authorization\n  AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo,\n    completionHandler: { (granted:Bool) -> Void in\n    if granted {\n      // go ahead\n    }\n    else {\n      // user denied, nothing much to do\n    }\n  })\ncase .Authorized:\n  // go ahead\ncase .Denied, .Restricted:\n  // the user explicitly denied camera usage or is not allowed to access the camera devices\n}\n```\n\nAt this point, we have two ways to display the video stream that comes from the camera. The simplest is to create a view with an `AVCaptureVideoPreviewLayer` and attach it to the capture session:\n\n```swift\npreviewLayer = AVCaptureVideoPreviewLayer.layerWithSession(session) as AVCaptureVideoPreviewLayer\npreviewLayer.frame = view.bounds\nview.layer.addSublayer(previewLayer)\n```\n\nThe `AVCaptureVideoPreviewLayer` will automatically display the output from the camera. It also comes in handy when we need to translate a tap on the camera preview to the coordinate system of the device, e.g. when tapping on an area to focus. We'll see the details later.\n\nThe second method is to capture the single frames from the output data stream and to manually display them in a view using OpenGL. This is a bit more complicated, but necessary in case we want to manipulate or filter the live preview.\nTo get the data stream, we just create an `AVCaptureVideoDataOutput`, so when the camera is running, we get all the frames (except the ones that will be dropped if our processing is too slow) via the delegate method, `captureOutput(_:didOutputSampleBuffer:fromConnection:)`, and draw them in a `GLKView`. Without going too deep into the OpenGL framework, we could set up the `GLKView` like this:\n\n```swift\nglContext = EAGLContext(API: .OpenGLES2)\nglView = GLKView(frame: viewFrame, context: glContext)\nciContext = CIContext(EAGLContext: glContext)\n```\n\nNow the `AVCaptureVideoOutput`:\n\n```swift\nvideoOutput = AVCaptureVideoDataOutput()\nvideoOutput.setSampleBufferDelegate(self, queue: dispatch_queue_create(\"sample buffer delegate\", DISPATCH_QUEUE_SERIAL))\nif session.canAddOutput(self.videoOutput) {\n  session.addOutput(self.videoOutput)\n}\n```\n\nAnd the delegate method:\n\n```swift\nfunc captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {\n  let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)\n  let image = CIImage(CVPixelBuffer: pixelBuffer)\n  if glContext != EAGLContext.currentContext() {\n    EAGLContext.setCurrentContext(glContext)\n  }\n  glView.bindDrawable()\n  ciContext.drawImage(image, inRect:image.extent(), fromRect: image.extent())\n  glView.display()\n}\n```\n\nOne caveat: the samples sent from the camera are rotated 90 degrees, because that's how the camera sensor is oriented. The `AVCaptureVideoPreviewLayer` handles this automatically, so in this case, we should apply a rotation transform to the `GLKView`.\n\nWe're almost done. The last component — the `AVCaptureStillImageOutput` — is actually the most important, as it allows us to capture a still image. This is just a matter of creating an instance and adding it to the session:\n\n```swift\nstillCameraOutput = AVCaptureStillImageOutput()\nif self.session.canAddOutput(self.stillCameraOutput) {\n  self.session.addOutput(self.stillCameraOutput)\n}\n```\n\n\n### Configuration\n\nNow that we have all the necessary objects in place, we should find the best configuration for our needs. Again, there are two ways to accomplish this.\nThe simplest — and the most recommended — is to use a session preset:\n\n```swift\nsession.sessionPreset = AVCaptureSessionPresetPhoto\n```\n\nThe `AVCaptureSessionPresetPhoto` selects the best configuration for the capture of a photo, i.e. it enables the maximum ISO and exposure duration ranges; the [phase detection](https://en.wikipedia.org/wiki/Autofocus#Phase_detection) autofocus; and a full resolution, JPEG-compressed still image output.\n\nHowever, if you need more control, the `AVCaptureDeviceFormat` class describes the parameters applicable to the device, such as still image resolution, video preview resolution, the type of autofocus system, ISO, and exposure duration limits. Every device supports a set of formats, listed in the `AVCaptureDevice.formats` property, and the proper format can be set as the  `activeFormat` of the `AVCaptureDevice` (note that you cannot modify a format).\n\n## Controlling the Camera\n\nThe camera built into iPhones and iPads has more or less the same controls as other cameras, with some exceptions: parameters such as focus, exposure duration (the analog of the [shutter speed](/issues/21-camera-and-photos/how-your-camera-works/#shutterspeed) on DSLR cameras), and ISO sensitivity can be adjusted, but the lens aperture is fixed. Since iOS 8, we have access to full manual control of all the adjustments.\n\nWe'll look at the details later, but first, it's time to start the camera:\n\n```swift\nsessionQueue = dispatch_queue_create(\"com.example.camera.capture\\_session\", DISPATCH_QUEUE_SERIAL)\ndispatch_async(sessionQueue) { () -> Void in\n  self.session.startRunning()\n}\n```\n\nAll the actions and configurations done on the session or the camera device are blocking calls. For this reason, it's recommended to dispatch them to a background serial queue. Furthermore, the camera device must be locked before changing any of its parameters, and unlocked afterward. For example:\n\n```swift\nvar error:NSError?\nif currentDevice.lockForConfiguration(&error) {\n  // locked successfully, go on with configuration\n  // currentDevice.unlockForConfiguration()\n}\nelse {\n  // something went wrong, the device was probably already locked\n}\n```\n\n\n### Focus\n\nFocus on an iOS camera is achieved by moving the lens closer to, or further from, the sensor.\n\nAutofocus is implemented with phase detection or contrast detection. The latter, however, is available only for low-resolution, high-FPS video capture (slow motion).\n\nThe enum `AVCaptureFocusMode` describes the available focus modes:\n\n- `Locked` means the lens is at a fixed position.\n- `AutoFocus` means setting this will cause the camera to focus once automatically, and then return back to `Locked`.\n- `ContinuousAutoFocus` means the camera will automatically refocus on the center of the frame when the scene changes.\n\nSetting the desired focus mode must be done after acquiring a lock:\n\n```swift\nlet focusMode:AVCaptureFocusMode = ...\nif currentCameraDevice.isFocusModeSupported(focusMode) {\n  ... // lock for configuration\n  currentCameraDevice.focusMode = focusMode\n  ... // unlock\n  }\n}\n```\n\nBy default, the `AutoFocus` mode tries to get the center of the screen as the sharpest area, but it is possible to set another area by changing the \"point of interest.\" This is a `CGPoint`, with values ranging from `{ 0.0 , 0.0 }` (top left) to `{ 1.0, 1.0 }` (bottom right), and `{ 0.5, 0.5 }` (center of the frame).\nUsually this can be implemented with a tap gesture recognizer on the video preview, and to help with translating the point from the coordinate of the view to the device's normalized coordinates, we can use  `AVVideoCaptureVideoPreviewLayer.captureDevicePointOfInterestForPoint()`:\n\n```swift\nvar pointInPreview = focusTapGR.locationInView(focusTapGR.view)\nvar pointInCamera = previewLayer.captureDevicePointOfInterestForPoint(pointInPreview)\n... // lock for configuration\n\n// set the new point of interest:\ncurrentCameraDevice.focusPointOfInterest = pointInCamera\n// trigger auto-focus on the new point of interest\ncurrentCameraDevice.focusMode = .AutoFocus\n\n... // unlock\n```\n\nNew in iOS 8 is the option to move the lens to a position from `0.0`, focusing near objects, to `1.0`, focusing far objects (although that doesn't mean \"infinity\"):\n\n```swift\n... // lock for configuration\nvar lensPosition:Float = ... // some float value between 0.0 and 1.0\ncurrentCameraDevice.setFocusModeLockedWithLensPosition(lensPosition) {\n  (timestamp:CMTime) -> Void in\n  // timestamp of the first image buffer with the applied lens position\n}\n... // unlock\n```\n\nThis means that the focus can be set with a `UISlider`, for example, which would be the equivalent of rotating the focusing ring on a DSLR. When focusing manually with these kinds of cameras, there is usually a visual aid that indicates the sharp areas. There is no such built-in mechanism in AVFoundation, but it could be interesting to display, for instance, a sort of [\"focus peaking.\"](https://en.wikipedia.org/wiki/Focus_peaking) We won't go into details here, but focus peaking could be easily implemented by applying a threshold edge detect filter (with a custom `CIFilter` or [`GPUImageThresholdEdgeDetectionFilter`](https://github.com/BradLarson/GPUImage/blob/master/framework/Source/GPUImageThresholdEdgeDetectionFilter.h)), and overlaying it onto the live preview in the `captureOutput(_:didOutputSampleBuffer:fromConnection:)` method of `AVCaptureAudioDataOutputSampleBufferDelegate` seen above.\n\n### Exposure\n\nOn iOS devices, the aperture of the lens is fixed (at f/2.2 for iPhones after 5s, and at f/2.4 for previous models), so only the exposure duration and the sensor sensibility can be tweaked to accomplish the most appropriate image brightness. As for the focus, we can have continuous auto exposure, one-time auto exposure on the point of interest, or manual exposure. In addition to specifying a point of interest, we can modify the auto exposure by setting a compensation, known as *target bias*. The target bias is expressed in [*f-stops*](/issues/21-camera-and-photos/how-your-camera-works/#stops), and its values range between `minExposureTargetBias` and `maxExposureTargetBias`, with 0 being the default (no compensation):\n\n```swift\nvar exposureBias:Float = ... // a value between minExposureTargetBias and maxExposureTargetBias\n... // lock for configuration\ncurrentDevice.setExposureTargetBias(exposureBias) { (time:CMTime) -> Void in\n}\n... // unlock\n```\n\nTo use manual exposure, instead we can set the ISO and the duration. Both values must be in the ranges specified in the device's active format:\n\n```swift\nvar activeFormat = currentDevice.activeFormat\nvar duration:CTime = ... // a value between activeFormat.minExposureDuration and activeFormat.maxExposureDuration or AVCaptureExposureDurationCurrent for no change\nvar iso:Float = ... // a value between activeFormat.minISO and activeFormat.maxISO or AVCaptureISOCurrent for no change\n... // lock for configuration\ncurrentDevice.setExposureModeCustomWithDuration(duration, ISO: iso) { (time:CMTime) -> Void in\n}\n... // unlock\n```\n\nHow do we know that the picture is correctly exposed? We can observe the `exposureTargetOffset` property of the `AVCaptureDevice` object and check that it's around zero.\n\n### White Balance\n\nDigital cameras [need to compensate](/issues/21-camera-and-photos/how-your-camera-works/#whiteisnotwhite) for different types of lighting. This means that the sensor should increase the red component, for example, in case of a cold light, and the blue component in case of a warm light. On an iPhone camera, the proper compensation can be automatically determined by the device, but sometimes, as it happens with any camera, it gets tricked by the colors in the scene. Luckily, iOS 8 made manual controls available for the white balance as well.\n\nThe automatic modes work in the same way as the focus and exposure, but there's no point of interest; the whole image is considered. In manual mode, we can compensate for the temperature and the tint, with the [temperature](https://en.wikipedia.org/wiki/Color_temperature) expressed in [Kelvin](https://en.wikipedia.org/wiki/Kelvin). Typical color temperature values go from around 2000–3000 K (for a warm light source like a candle or light bulb) up to 8000 K (for a clear blue sky). The tint ranges from a minimum of −150 (shift to green) to a maximum of 150 (shift to magenta).\n\nTemperature and tint will be used to calculate the proper RGB gain of the camera sensor, thus they have to be normalized for the device before they can be set.\n\nThis is the whole process:\n\n```swift\nvar incandescentLightCompensation = 3_000\nvar tint = 0 // no shift\nlet temperatureAndTintValues = AVCaptureWhiteBalanceTemperatureAndTintValues(temperature: incandescentLightCompensation, tint: tint)\nvar deviceGains = currentCameraDevice.deviceWhiteBalanceGainsForTemperatureAndTintValues(temperatureAndTintValues)\n... // lock for configuration\ncurrentCameraDevice.setWhiteBalanceModeLockedWithDeviceWhiteBalanceGains(deviceGains) {\n        (timestamp:CMTime) -> Void in\n    }\n  }\n... // unlock\n```\n\n### Real-Time Face Detection\n\nThe `AVCaptureMetadataOutput` has the ability to detect two types of objects: faces and QR codes. Apparently [no one uses QR codes](http://picturesofpeoplescanningqrcodes.tumblr.com), so let's see how we can detect faces. We just need to catch the metadata objects the `AVCaptureMetadataOutput` is providing to its delegate:\n\n```swift\nvar metadataOutput = AVCaptureMetadataOutput()\nmetadataOutput.setMetadataObjectsDelegate(self, queue: self.sessionQueue)\nif session.canAddOutput(metadataOutput) {\n  session.addOutput(metadataOutput)\n}\nmetadataOutput.metadataObjectTypes = [AVMetadataObjectTypeFace]\n```\n\n```swift\nfunc captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {\n    for metadataObject in metadataObjects as [AVMetadataObject] {\n      if metadataObject.type == AVMetadataObjectTypeFace {\n        var transformedMetadataObject = previewLayer.transformedMetadataObjectForMetadataObject(metadataObject)\n      }\n    }\n```\n\nCheck out [Engin’s article in this issue](/issues/21-camera-and-photos/face-recognition-with-opencv/) for more on face detection and recognition.\n\n### Capturing a Still Image\n\nFinally, we want to capture the high-resolution image, so we call the `captureStillImageAsynchronouslyFromConnection(connection, completionHandler)` method on the camera device. When the data is read, the completion handler will be called on an unspecified thread.\n\nIf the still image output was set up to use the JPEG codec, either via the session `.Photo` preset or via the device's output settings, the `sampleBuffer` returned contains the image's metadata, i.e. Exif data and also the detected faces — if enabled in the `AVCaptureMetadataOutput`:\n\n```swift\ndispatch_async(sessionQueue) { () -> Void in\n\n  let connection = self.stillCameraOutput.connectionWithMediaType(AVMediaTypeVideo)\n\n  // update the video orientation to the device one\n  connection.videoOrientation = AVCaptureVideoOrientation(rawValue: UIDevice.currentDevice().orientation.rawValue)!\n\n  self.stillCameraOutput.captureStillImageAsynchronouslyFromConnection(connection) {\n    (imageDataSampleBuffer, error) -> Void in\n\n    if error == nil {\n\n      // if the session preset .Photo is used, or if explicitly set in the device's outputSettings\n      // we get the data already compressed as JPEG\n\n      let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)\n\n      // the sample buffer also contains the metadata, in case we want to modify it\n      let metadata:NSDictionary = CMCopyDictionaryOfAttachments(nil, imageDataSampleBuffer, CMAttachmentMode(kCMAttachmentMode_ShouldPropagate)).takeUnretainedValue()\n\n      if let image = UIImage(data: imageData) {\n        // save the image or do something interesting with it\n        ...\n      }\n    }\n    else {\n      NSLog(\"error while capturing still image: \\(error)\")\n    }\n  }\n}\n```\n\nIt's nice to have a sort of visual feedback when the photo is being captured. To know when it starts and when it's finished, we can use KVO with the `isCapturingStillImage` property of the `AVCaptureStillImageOutput`.\n\n\n#### Bracketed Capture\n\nAn interesting feature also introduced in iOS 8 is \"bracketed capture,\" which means taking several photos in succession with different exposure settings. This can be useful when taking a picture in mixed light, for example, by configuring three different exposures with biases at −1, 0, +1, and then merging them with an HDR algorithm.\n\nHere's how it looks in code:\n\n```swift\ndispatch_async(sessionQueue) { () -> Void in\n  let connection = self.stillCameraOutput.connectionWithMediaType(AVMediaTypeVideo)\n  connection.videoOrientation = AVCaptureVideoOrientation(rawValue: UIDevice.currentDevice().orientation.rawValue)!\n\n  var settings = [-1.0, 0.0, 1.0].map {\n    (bias:Float) -> AVCaptureAutoExposureBracketedStillImageSettings in\n\n    AVCaptureAutoExposureBracketedStillImageSettings.autoExposureSettingsWithExposureTargetBias(bias)\n  }\n\n  var counter = settings.count\n\n  self.stillCameraOutput.captureStillImageBracketAsynchronouslyFromConnection(connection, withSettingsArray: settings) {\n    (sampleBuffer, settings, error) -> Void in\n\n    ...\n    // save the sampleBuffer(s)\n\n    // when the counter reaches 0 the capture is complete\n    counter--\n\n  }\n}\n```\n\nIt appears rather similar to the single image capture, but the completion handler is called as many times as the number of elements in the settings array.\n\n\n### Conclusion\n\nWe've seen in detail the basics of how taking a picture in an iPhone app could be implemented (hmm... what about [taking photos with an iPad](http://ipadtography.tumblr.com/)?). You can also check them in action in this [sample project](https://github.com/objcio/issue-21-camera-controls-demo). Finally, iOS 8 is allowing a more accurate capture, especially for power users, thus making the gap between iPhones and dedicated cameras a little bit narrower, at least in terms of manual controls. Anyway, not everybody would like to be using a complicated manual interface for the everyday photo, so use these features responsibly!\n"
  },
  {
    "path": "2015-02-10-core-image-intro.md",
    "content": "---\ntitle:  \"An Introduction to Core Image\"\ncategory: \"21\"\ndate: \"2015-02-10 07:00:00\"\ntags: article\nauthor:\n  - name: Warren Moore\n    url: https://twitter.com/warrenm\n---\n\nThis article is a beginner's introduction to Core Image, an image processing framework for OS X and iOS.\n\nIf you would like to follow along with the code in this article, you can download [the sample project at GitHub](https://github.com/objcio/issue-21-core-image-explorer). The sample project is an iOS app that lists a large selection of system-provided image filters, and it provides a UI for tweaking their parameters and observing the effects.\n\nAlthough the sample code is written in Swift for iOS, the concepts transfer readily to Objective-C and OS X.\n\n## Fundamental Concepts\n\nTo talk about Core Image, we first need to introduce a few fundamental concepts.\n\nA _filter_ is an object that has a number of inputs and outputs and performs some kind of transformation. For example, a blur filter might take an input image and a blur radius and produce an appropriately blurred output image.\n\nA _filter graph_ is a network ([directed acyclic graph](http://en.wikipedia.org/wiki/Directed_acyclic_graph)) of filters, chained together so that the output of one filter can be the input of another. In this way, elaborate effects can be achieved. We'll see below how to connect filters to create a vintage photographic effect.\n\n## Getting Acquainted with the Core Image API\n\nWith these concepts in our toolkit, we can start to explore the specifics of image filtering with Core Image.\n\n### Core Image Architecture\n\nCore Image has a plug-in architecture, which means that it allows users to extend its functionality by writing custom filters that integrate with the system-provided filters. We will not be taking advantage of Core Image's extensibility in this article; I mention it only because it influences the API of the framework.\n\nCore Image is written to make the most of the hardware on which it is running. The actual implementation of each filter, the _kernel_, is written in a [subset](https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CIKernelLangRef/Introduction/Introduction.html) of [GLSL](https://www.opengl.org/documentation/glsl/), the shading language of OpenGL. When multiple filters are connected to form a filter graph, Core Image strings together these kernels to build one efficient program that can be run on the GPU.\n\nWhenever possible, Core Image defers work until the future. Oftentimes, no allocations or processing will take place until the output of the final filter in a filter graph is requested.\n\nIn order to do work, Core Image requires an object called a _context_. The context is the actual workhorse of the framework, allocating the necessary memory and compiling and running the filter kernels that do the image processing. Contexts are very expensive to create, so you will most often want to create one context and use it repeatedly. We will see how to create a context below.\n\n### Querying for Available Filters\n\nCore Image filters are created by name. To get a list of system filters, we ask Core Image for the names of filters in the `kCICategoryBuiltIn` category:\n\n```swift\nlet filterNames = CIFilter.filterNamesInCategory(kCICategoryBuiltIn) as [String]\n```\n\nThe list of filters available on iOS is very nearly a subset of the filters available on OS X. There are 169 built-in filters on OS X, and there are 127 on iOS.\n\n### Creating a Filter by Name\n\nNow that we have a list of available filters, we can create and use a filter. For example, to create a Gaussian blur filter, we pass the filter name to the appropriate `CIFilter` initializer:\n\n```swift\nlet blurFilter = CIFilter(named:\"CIGaussianBlur\")\n```\n\n### Setting Filter Parameters\n\nBecause of Core Image's plug-in structure, most filter properties are not set directly, but with key-value coding (KVC). For example, to set the blur radius of the blur filter, we use KVC to set its `inputRadius` property:\n\n```swift\nblurFilter.setValue(10.0 forKey:\"inputRadius\")\n```\n\nSince this method takes `AnyObject?` (`id` in Objective-C) as its value parameter, it is not particularly type safe. Therefore, setting filter parameters requires some vigilance to ensure that you are passing the expected type.\n\n### Querying Filter Attributes\n\nTo know what input and output parameters are offered by a filter, we can ask for its `inputKeys` and `outputKeys` arrays, respectively. These each return an array of `NSString`s.\n\nTo get more information about each parameter, we can look in the `attributes` dictionary provided by the filter. Each input and output parameter name maps to a dictionary of its own, describing what kind of parameter it is, and its minimum and maximum values, if applicable. For example, here is the dictionary corresponding to the `inputBrightness` parameter of the `CIColorControls` filter:\n\n```swift\ninputBrightness = {\n    CIAttributeClass = NSNumber;\n    CIAttributeDefault = 0;\n    CIAttributeIdentity = 0;\n    CIAttributeMin = -1;\n    CIAttributeSliderMax = 1;\n    CIAttributeSliderMin = -1;\n    CIAttributeType = CIAttributeTypeScalar;\n};\n```\n\nFor numerical parameters, the dictionary will contain `kCIAttributeSliderMin` and `kCIAttributeSliderMax` keys, which bound the expected input quantities. Most parameters also contain a `kCIAttributeDefault` key, which maps to the default value of the parameter.\n\n## Filtering an Image in Practice\n\nThe work of filtering an image consists of three parts: building and configuring a filter graph, sending an image in to be filtered, and retrieving the filtered image. The sections below cover this in detail.\n\n### Building a Filter Graph\n\nBuilding a filter graph consists of instantiating filters to do the kind of work we want to perform, setting their parameters, and wiring them up so that the image data flows through each filter in turn.\n\nIn this section, we will construct a filter graph for producing images in the style of a 19th-century tintype photograph. We will chain together two effects to create this effect: a monochrome filter to simultaneously desaturate and tint the image, and a vignette filter to create a shadow effect that frames the image.\n\nQuartz Composer, available for [download from the Apple Developer website](https://developer.apple.com/downloads/index.action?name=Graphics), is useful for prototyping Core Image filter graphs. Below, we have composed the desired photo filter by wiring together the Color Monochrome filter and the Vignette filter:\n\n![A filter graph built with Quartz Composer, showing intermediate filtered images](/images/issue-21/quartz.png)\n\nOnce we're satisfied with the effect, we can recreate the filter graph in code:\n\n```swift\nlet sepiaColor = CIColor(red: 0.76, green: 0.65, blue: 0.54)\nlet monochromeFilter = CIFilter(name: \"CIColorMonochrome\",\n    withInputParameters: [\"inputColor\" : sepiaColor, \"inputIntensity\" : 1.0])\nmonochromeFilter.setValue(inputImage, forKey: \"inputImage\")\n\nlet vignetteFilter = CIFilter(name: \"CIVignette\",\n    withInputParameters: [\"inputRadius\" : 1.75, \"inputIntensity\" : 1.0])\nvignetteFilter.setValue(monochromeFilter.outputImage, forKey: \"inputImage\")\n\nlet outputImage = vignetteFilter.outputImage\n```\n\nNote that the output image of the monochrome filter becomes the input image of the vignette filter. This causes the vignette to be applied to the tinted monochrome image. Also note that we can specify parameters in the initializer, instead of setting them individually with KVC.\n\n### Creating the Input Image\n\nCore Image filters require that their input image be of type `CIImage`. For iOS programmers who are used to `UIImage`, this may be a little unusual, but the distinction is merited. A `CIImage` is actually a more general entity than a `UIImage`, since a `CIImage` may have infinite extent. Obviously, we can't store an infinite image in memory, but conceptually, this means that you can request image data from an arbitrary region in the 2D plane and get back a meaningful result.\n\nAll of the images we will be using in this article are finite, and it's easy enough to create a `CIImage` from a `UIImage`. In fact, it's just one line of code:\n\n```swift\nlet inputImage = CIImage(image: uiImage)\n```\n\nThere are also convenience initializers for creating `CIImage`s directly from image data or a file URL.\n\nOnce we have a `CIImage`, we can set it as the input image of the filter graph by setting the `inputImage` parameter of the filter:\n\n```swift\nfilter.setValue(inputImage, forKey:\"inputImage\")\n```\n\n### Fetching a Filtered Image\n\nFilters have a property named `outputImage`. As you might guess, it has type `CIImage`. So how do we perform the inverse operation of creating a `UIImage` from a `CIImage`? Well, although we've spent all our time thus far building up a filter graph, now is the time to invoke the power of the `CIContext` and do the actual work of filtering the image.\n\nThe simplest way to create a context is to pass a nil options dictionary to its constructor:\n\n```swift\nlet ciContext = CIContext(options: nil)\n```\n\nTo get an image out of the filter graph, we ask our `CIContext` to create a `CGImage` from a rect in the output image, passing the extent (bounds) of the input image:\n\n```swift\nlet cgImage = ciContext.createCGImage(filter.outputImage, fromRect: inputImage.extent())\n```\n\nThe reason we use the input image's extent is that the output image often has different dimensions than the input image. For example, a blurred image has some extra pixels around its border, due to sampling beyond the edge of the input image.\n\nWe can now create a `UIImage` from this newly created `CGImage`:\n\n```swift\nlet uiImage = UIImage(CGImage: cgImage)\n```\n\nIt is possible to create a `UIImage` directly from a `CIImage`, but this approach is fraught: if you try to display such an image in a `UIImageView`, its `contentMode` property will be ignored. Using an intermediate `CGImage` takes an extra step, but obviates this annoyance.\n\n### Improving Performance with OpenGL\n\nIt's time-consuming and wasteful to use the CPU to draw a `CGImage`, only to hand the result right back to UIKit for compositing. We'd prefer to be able to draw the filtered image to the screen without having to take a trip through Core Graphics. Fortunately, because of the interoperability of OpenGL and Core Image, we can do exactly that.\n\nTo share resources between an OpenGL context and a Core Image context, we need to create our `CIContext` in a slightly different way:\n\n```swift\nlet eaglContext = EAGLContext(API: .OpenGLES2)\nlet ciContext = CIContext(EAGLContext: context)\n```\n\nHere, we create an `EAGLContext` with the OpenGL ES 2.0 feature set. This GL context can be used as the backing context for a `GLKView` or for drawing into a `CAEAGLLayer`. The sample code uses this technique to draw images efficiently.\n\nWhen a `CIContext` has an associated GL context, a filtered image can be drawn with OpenGL using the following call:\n\n```swift\nciContext.drawImage(filter.outputImage, inRect: outputBounds, fromRect: inputBounds)\n```\n\nAs before, the `fromRect` parameter is the portion of the image to draw in the filtered image's coordinate space. The `inRect` parameter is the rectangle in the coordinate space of the GL context into which the image should be drawn. If you want to respect the aspect ratio of the image, you may need to do some math to compute the appropriate `inRect`.\n\n### Forcing Filtering onto the CPU\n\nWhenever possible, Core Image will perform filtering on the GPU. However, it does have the ability to fall back to the CPU. Filtering done on the CPU may have better accuracy, since GPUs often exchange some fidelity for speed in their floating-point calculations. You can force Core Image to run on the CPU by setting the value of the `kCIContextUseSoftwareRenderer` key in the options dictionary to `true` when creating a context.\n\nYou can determine whether a CPU or GPU renderer is in use by setting the `CI_PRINT_TREE` environment variable to `1` in your scheme configuration in Xcode. This will cause Core Image to print diagnostic information every time a filtered image is rendered. This setting is useful for examining the composed image filter tree as well.\n\n## A Tour of the Sample App\n\nThe [sample code](https://github.com/objcio/issue-21-core-image-explorer) for this article consists of an iPhone app that showcases a broad variety of the image filters available in Core Image for iOS.\n\n### Creating a GUI from Filter Parameters\n\nTo demonstrate a maximum number of filters, the sample app takes advantage of the introspective nature of Core Image and generates a user interface for controlling the parameters of the filters it supports:\n\n![Image being tweaked with the Color Controls filter](/images/issue-21/color-controls.png)\n\nThe sample app is restricted to filters that have a single input image, and zero or more numerical inputs. There are some interesting filters that do not fall into this category (notably the compositing and transition filters). Even so, the app gives a good overview of the functionality available in Core Image.\n\nFor each input parameter to the filter, a slider is configured with the minimum and maximum value of the parameter, and its value is set to the default value. When the value of the slider changes, it conveys the change to its delegate, which is a `UIImageView` subclass that holds a `CIFilter` reference.\n\n### Using the Built-In Photo Filters\n\nIn addition to numerous other built-in filters, the sample app demonstrates the photo filters introduced in iOS 7. These filters have no parameters we can tune, but they merit inclusion, since they show how you can emulate the effects in the Photos app for iOS:\n\n![Image processed with the Transfer photo filter](/images/issue-21/photo-filters.png)\n\n## Conclusion\n\nThis article has been a brief introduction to Core Image, a framework for high-performance image processing. We've tried to cover as many features of the framework as practically possible in this short format. You've learned how to instantiate and wire together Core Image filters, get images in and out of filter graphs, and tune parameters to get the desired outcome. You also learned how to access the system-provided photo filters, with which you can emulate the behavior of the Photos app on iOS.\n\nYou now know enough to go out and write your own photo editing applications. With a little more exploration, you'll be able to write your own filters that exploit the amazing power of your Mac or iPhone to perform previously unimagined effects. Go forth and filter!\n\n### References\n\nThe [Core Image Reference Collection](https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CoreImagingRef/index.html) is the canonical set of documentation on Core Image.\n\nThe [Core Image Filter Reference](https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/uid/TP40004346) contains a comprehensive list of the image filters available in Core Image, along with usage examples.\n\nFor a take on writing Core Image code in a more functional style, see Florian Kugler's [article in objc.io issue #16](/issues/16-swift/functional-swift-apis/).\n"
  },
  {
    "path": "2015-02-10-editorial.md",
    "content": "---\ntitle:  \"Editorial\"\ncategory: \"21\"\ndate: \"2015-02-10 12:00:00\"\ntags: editorial\n---\n\nWelcome to objc.io issue 21. This month is all about photos and the iPhone and iPad camera.\n\nThis is an important area for developers. The iPhone has become [the most popular camera in the world](http://www.tuaw.com/2014/06/12/iphone-models-remain-the-most-popular-cameras-on-flickr/), and for many people, the camera is the most loved feature of their smartphone.\n\nWe cover a wide range of topics. Starting with the foundations, Daniel explains [how the camera in your iPhone works](/issues/21-camera-and-photos/how-your-camera-works/), and Oliver discusses some essential [image formats](/issues/21-camera-and-photos/image-formats/) and APIs.\n\nNext, we have a section about the camera and photo library APIs. Matteo writes about [capturing images with the camera](/issues/21-camera-and-photos/camera-capture-on-ios/), including the new manual controls in iOS 8. Saniul walks us through the new [Photos framework](/issues/21-camera-and-photos/the-photos-framework/), and Sam shows how to integrate our apps directly into the photo library via [photo extensions](/issues/21-camera-and-photos/photo-extensions/).\n\nOnce you have captured an image, you will want to process it with optimum performance, and that often means filtering it on the GPU. The easiest way to do that is to use [Core Image](/issues/21-camera-and-photos/core-image-intro/), which is covered by Warren. The next step is writing your own [custom shaders for image processing](/issues/21-camera-and-photos/gpu-accelerated-image-processing/). Many developers dread direct GPU programming, but Janie shows that it isn’t as hard as you might think. Brad then explains how you can use the same concepts to develop powerful [computer vision algorithms](/issues/21-camera-and-photos/gpu-accelerated-machine-vision/). Finally, Engin [introduces us to OpenCV](/issues/21-camera-and-photos/face-recognition-with-opencv/), a very popular open-source library for computer vision.\n\nIn addition to all the contributors to this issue, we had a lot of help from [Ole Begemann](http://oleb.net) this month. He really pulled some strings to make this issue happen. Thanks Ole!\n\nBest from Berlin,\n\nChris, Daniel, and Florian.\n"
  },
  {
    "path": "2015-02-10-face-recognition-with-opencv.md",
    "content": "---\ntitle: \"Face Recognition with OpenCV\"\ncategory: \"21\"\ndate: \"2015-02-10 04:00:00\"\ntags: article\nauthor:\n  - name: Engin Kurutepe\n    url: https://twitter.com/ekurutepe\n---\n\n## A Bit of Background\n\nOpenCV is an open-source computer vision and machine learning library. It contains thousands of optimized algorithms, which provide a common toolkit for various computer vision applications. According to the project's [about page](http://opencv.org/about.html), OpenCV is being used in many applications, ranging from stitching Google's Street View images to running interactive art shows.\n\nOpenCV started out as a research project inside Intel in 1999. It has been in active development since then, and evolved to support modern technologies like OpenCL and OpenGL and platforms like iOS and Android.\n\nIn 1999, [Half-Life](http://en.wikipedia.org/wiki/Half-Life_(video_game)) was released and became extremely popular. [Intel Pentium 3](http://en.wikipedia.org/wiki/Pentium_III) was the state-of-the-art CPU, and 400-500MHz clock speeds were considered fast. And a typical CPU in 2006 when OpenCV 1.0 was released had about the same [CPU performance](http://browser.primatelabs.com/geekbench2/compare/212009/1030202) as the A6 in an iPhone 5. Even though computer vision is traditionally considered to be a computationally intensive application, clearly our mobile devices have already passed the threshold of being able to perform useful computer vision tasks, and can be extremely versatile computer vision platforms with their attached cameras.\n\nIn this article, I will provide an overview of OpenCV from an iOS developer's perspective and introduce a few fundamental classes and concepts. Additionally, I cover how to integrate OpenCV to your iOS projects, and share the basics of Objective-C++. Finally, we'll look at a demo project to see how OpenCV can be used on an iOS device to perform facial detection and recognition.\n\n## Overview of OpenCV\n\n### Concepts\n\nOpenCV is a C++ API consisting of various modules containing a wide range of functions, from low-level image color space conversions to high-level machine learning tools.\n\nUsing C++ APIs for iOS development is not something most of us do daily. You need to use Objective-C++ for the files calling OpenCV methods, i.e. you cannot call OpenCV methods from Swift or Objective-C. The OpenCV [iOS tutorials](http://docs.opencv.org/doc/tutorials/ios/video_processing/video_processing.html#opencviosvideoprocessing) tell you to simply change the file extensions to `.mm` for all classes where you'd like to use OpenCV, including view controllers. While this might work, it is not a particularly good idea. The correct approach is to write Objective-C++ wrapper classes for all OpenCV functionality you would like to use in your app. These Objective-C++ wrappers translate OpenCV's C++ APIs to safe Objective-C APIs and can be used transparently in all Objective-C classes. Going the wrapper route, you will be able to contain C++ code in your project to the wrappers only and most likely save lots of headaches further down the road in resolving hard-to-track compile errors because a C++ header was erroneously included in a wrong file.\n\nOpenCV declares the `cv` namespace, such that classes are prefixed with `cv::`, like `cv::Mat`, `cv::Algorithm`, etc. It is possible to use `using namespace cv` in your `.mm` files in order to be able to drop the `cv::` prefixes for a lot of classes, but you will still need to write them out for classes like `cv::Rect` and `cv::Point`, due to collisions with `Rect` and `Point` defined in `MacTypes.h`. While it's a matter of personal preference, I prefer to use `cv::` everywhere for the sake of consistency.\n\n### Modules\n\nBelow is a list of the most important modules as described in the [official documentation](http://docs.opencv.org/modules/core/doc/intro.html).\n\n- **core**: a compact module defining basic data structures, including the dense multi-dimensional array `Mat`, and basic functions used by all other modules\n- **imgproc**: an image processing module that includes linear and non-linear image filtering, geometrical image transformations (resize, affine and perspective warping, generic table-based remapping), color space conversion, histograms, and so on\n- **video**: a video analysis module that includes motion estimation, background subtraction, and object tracking algorithms\n- **calib3d**: basic multiple-view geometry algorithms, single and stereo camera calibration, object pose estimation, stereo correspondence algorithms, and elements of 3D reconstruction\n- **features2d**: salient feature detectors, descriptors, and descriptor matchers\n- **objdetect**: detection of objects and instances of the predefined classes (for example: faces, eyes, mugs, people, cars, and so on)\n- **ml**: various machine learning algorithms such as K-Means, Support Vector Machines, and Neural Networks\n- **highgui**: an easy-to-use interface for video capturing, image and video codecs, and simple UI capabilities (only a subset available on iOS)\n- **gpu**: GPU-accelerated algorithms from different OpenCV modules (unavailable on iOS)\n- **ocl**: common algorithms implemented using OpenCL (unavailable on iOS)\n- a few more helper modules such as Python bindings and user-contributed algorithms\n\n\n### Fundamental Classes and Operations\n\nOpenCV contains hundreds of classes. Let's limit ourselves to a few fundamental classes and operations in the interest of brevity, and refer to the [full documentation](http://docs.opencv.org/modules/core/doc/core.html) for further reading. Going over these core classes should be enough to get a feel for the logic behind the library.\n\n####`cv::Mat`\n\n`cv::Mat` is the core data structure representing any N-dimensional matrix in OpenCV. Since images are just a special case of 2D matrices, they are also represented by a `cv::Mat`, i.e. `cv::Mat` is the class you'll be working with the most in OpenCV.\n\nAn instance of `cv::Mat` acts as a header for the image data and contains information to specify the image format. The image data itself is only referenced and can be shared by multiple `cv::Mat` instances. OpenCV uses a reference counting method similar to ARC to make sure that the image data is deallocated when the last referencing `cv::Mat` is gone. Image data itself is an array of concatenated rows of the image (for N-dimensional matrices, the data consists of concatenated data arrays of the contained N-1 dimensional matrices). Using the values contained in the `step[]` array, each pixel of the image can be addressed using pointer arithmetic:\n\n```c\nuchar *pixelPtr = cvMat.data + rowIndex * cvMat.step[0] + colIndex * cvMat.step[1]\n```\n\nThe data format for each pixel is retrieved by the `type()` function. In addition to common grayscale (1-channel, `CV_8UC1`) and color (3-channel, `CV_8UC3`) images with 8-bit unsigned integers per channel, OpenCV supports many less frequent formats, such as `CV_16SC3` (16-bit signed integer with 3 channels per pixel) or even `CV_64FC4` (64-bit floating point with 4 channels per pixel).\n\n####`cv::Algorithm`\n\n`Algorithm` is an abstract base class for many algorithms implemented in OpenCV, including the `FaceRecognizer` we will be using in the demo project. It provides an API not unlike `CIFilter` in Apple's Core Image framework, where you can create an `Algorithm` by calling `Algorithm::create()` with the name of the algorithm, and can set and get various parameters using the `get()` and `set()` methods, vaguely similar to key-value coding. Moreover, the `Algorithm` base provides functionality to save and load parameters to/from XML or YAML files.\n\n## Using OpenCV on iOS\n\n### Adding OpenCV to Your Project\n\nYou have three options to integrate OpenCV into your iOS project.\n\n- Just use CocoaPods: `pod \"OpenCV\"`.\n- Download the official [iOS framework release](http://opencv.org/downloads.html) and add the framework to your project.\n- Pull the sources from [GitHub](https://github.com/Itseez/opencv) and build the library on your own according to the instructions [here](http://docs.opencv.org/doc/tutorials/introduction/ios_install/ios_install.html#ios-installation).\n\n### Objective-C++\n\nAs mentioned previously, OpenCV is a C++ API, and thus cannot be directly used in Swift and Objective-C code. It is, however, possible to use OpenCV in Objective-C++ files.\n\nObjective-C++ is a mixture of Objective-C and C++, and allows you to use C++ objects in Objective-C classes. The clang compiler treats all files with the extension `.mm` as Objective-C++, and it mostly works as you would expect, but there are a few precautions you should take when using Objective-C++ in your project. Memory management is the biggest point where you should be extra careful, since ARC only works with Objective-C objects. When you use a C++ object as a property, the only valid attribute is `assign`. Therefore, your `dealloc` should ensure that the C++ object is properly deleted.\n\nThe second important point when using Objective-C++ in your iOS project is leaking the C++ dependencies, if you include C++ headers in your Objective-C++ header. Any Objective-C class importing your Objective-C++ class would be including the C++ headers too, and thus needs to be declared as Objective-C++ itself. This can quickly spread like a forest fire through your project if you include C++ headers in your header files. Always wrap your C++ includes with `#ifdef __cplusplus`, and try to include C++ headers only in your `.mm` implementation files wherever possible.\n\nFor more details on how exactly C++ and Objective-C work together, have a look at [this tutorial](http://www.raywenderlich.com/62989/introduction-c-ios-developers-part-1) by [Matt Galloway](https://twitter.com/mattjgalloway).\n\n## Demo: Facial Detection and Recognition\n\nSo, now that we have an overview of OpenCV and how to integrate it into our apps, let's build a small demo app with it: an app that uses the video feed from the iPhone camera to continuously detect faces and draw them on screen. When the user taps on a face, our app will attempt to recognize the person. The user must then either tap \"Correct\" if our recognizer was right, or tap on the correct person to correct the prediction if it was wrong. Our face recognizer then learns from its mistakes and gets better over time:\n\n![Block diagram of the face detection and recognition system in our demo app](/images/issue-21/blocks-face-recognition-objcio.jpg)\n\nThe source code for the demo app is available on [GitHub](https://github.com/objcio/issue-21-OpenCV-FaceRec).\n\n### Live Video Capture\n\nThe highgui module in OpenCV comes with a class, `CvVideoCamera`, that abstracts the iPhone camera and provides our app with a video feed through a delegate method, `- (void)processImage:(cv::Mat&)image`. An instance of the `CvVideoCamera` can be set up like this:\n\n```objc\nCvVideoCamera *videoCamera = [[CvVideoCamera alloc] initWithParentView:view];\nvideoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionFront;\nvideoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset640x480;\nvideoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;\nvideoCamera.defaultFPS = 30;\nvideoCamera.grayscaleMode = NO;\nvideoCamera.delegate = self;\n```\n\nNow that we have set up our camera with a 30-frames-per-second frame rate, our implementation of `processImage:` will be called 30 times per second. Since our app will detect faces continuously, we should perform our facial detection here. Please note that if the facial detection at each frame takes longer than 1/30 seconds, we will be dropping frames.\n\n### Face Detection\n\nYou don't actually need OpenCV for facial detection, since Core Image already provides the `CIDetector` class. This can perform pretty good facial detection, and it is optimized and very easy to use:\n\n```objc\nCIDetector *faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:context options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}];\n\nNSArray *faces = [faceDetector featuresInImage:image];\n```\n\nThe `faces` array contains a `CIFaceFeature` instance for each detected face in the image. Face features describe the location and the size of the face, in addition to optional eye and mouth positions.\n\nOpenCV, on the other hand, provides an infrastructure for object detection, which can be trained to detect any object you desire. The library comes with multiple ready-to-use detector parameters for faces, eyes, mouths, bodies, upper bodies, lower bodies, and smiles. The detection engine consists of a cascade of very simple detectors (so-called Haar feature detectors) with different scales and weights. During the training phase, the decision tree is optimized with known positive and false images. Detailed information about the training and detection processes is available in the [original paper](http://www.multimedia-computing.de/mediawiki//images/5/52/MRL-TR-May02-revised-Dec02.pdf). Once the cascade of correct features and their scales and weights have been determined in training, the parameters can be loaded to initialize a cascade classifier:\n\n```objc\n//Path to the training parameters for frontal face detector\nNSString *faceCascadePath = [[NSBundle mainBundle] pathForResource:@\"haarcascade_frontalface_alt2\"\n                                                   ofType:@\"xml\"];\n\nconst CFIndex CASCADE_NAME_LEN = 2048;\nchar *CASCADE_NAME = (char *) malloc(CASCADE_NAME_LEN);\nCFStringGetFileSystemRepresentation( (CFStringRef)faceCascadePath, CASCADE_NAME, CASCADE_NAME_LEN);\n\nCascadeClassifier faceDetector;\nfaceDetector.load(CASCADE_NAME);\n```\n\nThe parameter files can be found under the `data/haarcascades` folder inside the OpenCV distribution.\nAfter the face detector has been initialized with the desired parameters, it can be used to detect faces:\n\n```objc\ncv::Mat img;\nvector<cv::Rect> faceRects;\ndouble scalingFactor = 1.1;\nint minNeighbors = 2;\nint flags = 0;\ncv::Size minimumSize(30,30);\nfaceDetector.detectMultiScale(img, faceRects,\n                              scalingFactor, minNeighbors, flags\n                              cv::Size(30, 30) );\n```\n\nDuring detection, the trained classifier is moved across all the pixels in the input image at different scales to be able to detect faces of different sizes. The `scalingFactor` parameters determine how much the classifier will be scaled up after each run. The `minNeighbors` parameter specifies how many positive neighbors a positive face rectangle should have to be considered a possible match; when a potential face rectangle is moved a pixel and does not trigger the classifier any more, it is most likely that it's a false positive. Face rectangles with fewer positive neighbors than `minNeighbors` are rejected. When `minNeighbors` is set to zero, all potential face rectangles are returned. The `flags` parameter is a relic from the OpenCV 1.x API and should always be `0`. And finally, `minimumSize` specifies the smallest face rectangle we're looking for. The `faceRects` vector will contain the frames of detected faces in `img`. The image for the face can then be extracted with the `()` operator on `cv::Mat` simply by calling `cv::Mat faceImg = img(aFaceRect)`.\n\nOnce we have at least one face rectangle, either using a `CIDetector` or an OpenCV `CascadeClassifier`, we can try to identify the person in the image.\n\n### Facial Recognition\n\nOpenCV comes with three algorithms for recognizing faces: Eigenfaces, Fisherfaces, and Local Binary Patterns Histograms (LBPH). Please read the very informative OpenCV [documentation](http://docs.opencv.org/modules/contrib/doc/facerec/facerec_tutorial.html#local-binary-patterns-histograms) if you would like to know how they work and how they differ from each other.\n\nFor the purposes of our demo app, we will be using the LBPH algorithm, mostly because it can be updated with user input without requiring a complete re-training every time a new person is added or a wrong recognition is corrected.\n\nIn order to use the LBPH recognizer, let's create an Objective-C++ wrapper for it, which exposes the following methods:\n\n```objc\n+ (FJFaceRecognizer *)faceRecognizerWithFile:(NSString *)path;\n- (NSString *)predict:(UIImage*)img confidence:(double *)confidence;\n- (void)updateWithFace:(UIImage *)img name:(NSString *)name;\n```\n\nOur factory method creates an LBPH instance like this:\n\n```objc\n+ (FJFaceRecognizer *)faceRecognizerWithFile:(NSString *)path {\n    FJFaceRecognizer *fr = [FJFaceRecognizer new];\n    fr->_faceClassifier = createLBPHFaceRecognizer();\n    fr->_faceClassifier->load(path.UTF8String);\n    return fr;\n}\n```\n\nPrediction can be implemented as follows:\n\n```objc\n- (NSString *)predict:(UIImage*)img confidence:(double *)confidence {\n    cv::Mat src = [img cvMatRepresentationGray];\n    int label;\n    self->_faceClassifier->predict(src, label, *confidence);\n    return _labelsArray[label];\n}\n```\n\nPlease note that we had to convert from `UIImage` to `cv::Mat` through a category method. The conversion itself is quite straightforward and is achieved by creating a `CGContextRef` using `CGBitmapContextCreate` pointing to the `data` pointer of a `cv::Image`. When we draw our `UIImage` on this bitmap context, the `data` pointer of our `cv::Image` is filled with the correct data. What's more interesting is that we are able to create an Objective-C++ category on an Objective-C class and it just works!\n\nAdditionally, the OpenCV face recognizer only supports integers as labels, but we would like to be able to use a person's name as a label, and have to implement a simple conversion between them through an `NSArray` property.\n\nOnce the recognizer predicts a label for us, we present this label to the user. Then it's up to the user to give feedback to our recognizer. The user could either say, \"Yes, that's correct!\" or \"No, this is person Y, not person X.\" In both cases, we can update our LBPH model to improve its performance in future predictions by updating our model with the face image and the correct label. Updating our facial recognizer with user feedback can be achieved by the following:\n\n```objc\n- (void)updateWithFace:(UIImage *)img name:(NSString *)name {\n    cv::Mat src = [img cvMatRepresentationGray];\n    NSInteger label = [_labelsArray indexOfObject:name];\n    if (label == NSNotFound) {\n        [_labelsArray addObject:name];\n        label = [_labelsArray indexOfObject:name];\n    }\n    vector<cv::Mat> images = vector<cv::Mat>();\n    images.push_back(src);\n    vector<int> labels = vector<int>();\n    labels.push_back((int)label);\n    self->_faceClassifier->update(images, labels);\n}\n```\n\nHere again we do the conversion from `UIImage` to `cv::Mat` and from `int` labels to `NSString` labels. We also have to put our parameters into `std::vector` instances, as expected by the OpenCV `FaceRecognizer::update` API.\n\nThis \"predict, get feedback, update cycle\" is known as [supervised learning](http://en.wikipedia.org/wiki/Supervised_learning) in literature.\n\n## Conclusion\n\nOpenCV is a very powerful and multi-faceted framework covering many fields which are still active research areas. Attempting to provide a fully detailed instruction manual in an article would be a fool's errand. Therefore, this article is meant to be a very high-level overview of the OpenCV framework. I attempted to cover some practical tips to integrate OpenCV in your iOS project, and went through a facial recognition example to show how OpenCV can be used in a real project. If you think OpenCV could help you for your project, the official OpenCV documentation is mostly very well written and very detailed. Go ahead and create the next big hit app!\n"
  },
  {
    "path": "2015-02-10-gpu-accelerated-image-processing.md",
    "content": "---\ntitle: \"GPU-Accelerated Image Processing\"\ncategory: \"21\"\ndate: \"2015-02-10 06:00:00\"\nauthor:\n  - name: Janie Clayton\n    url: https://twitter.com/RedQueenCoder\ntags: article\n---\n\nInstagram. Snapchat. Photoshop.\n\nAll of these applications are used to do image processing. Image processing can be as simple as converting a photo to grayscale and as complex as analyzing a video of a crowd for a specific person. In spite of how divergent these applications are, both of these examples go through the same process from creation to rendering.\n\nThere are many ways to process images on your computer or mobile phone, but by far the most efficient is effectively using your Graphics Processing Unit, or GPU. Your phone contains two different processing units, the CPU and the GPU. The CPU is a generalist that has to deal with everything, while your GPU can focus on doing one thing really well, which is doing floating-point math in parallel; it turns out that image processing and rendering is nothing more than doing a lot of floating-point math on the values for the pixels that render to your screen.\n\nBy effectively utilizing your GPU, you can increase graphics-rendering performance on your phone by a hundred fold, if not a thousand fold. Being able to filter high-quality live video on your phone is impractical or even impossible without GPU-based processing.\n\nThe tool we use to take advantage of this power is a shader. A shader is a small, C-based program written in a shading language. There are many shading languages out there on the market, but the one you should focus on if you are doing OS X or iOS development is the OpenGL Shading Language, or GLSL. You can take the concepts from GLSL and apply them to other, more proprietary languages like the Metal Shading Language. The concepts we are going over here even map well to custom kernels in Core Image, although they use a slightly different syntax.\n\nThis process can be incredibly daunting, especially to newer developers. The purpose of this article is to get your feet wet with some foundation information necessary to get you going on your journey to writing your own image processing shaders.\n\n\n\n\n## What Is a Shader?\n\nWe're going to take a short trip in The Wayback Machine to get an overview of what a shader is and how it came to be an integral part of our workflow.\n\nIf you've been doing iOS programming since at least iOS 5, you might be aware that there was a shift in OpenGL programming on the iPhone, from OpenGL ES 1.1 to OpenGL ES 2.0.\n\nOpenGL ES 1.1 did not use shaders — it used what is called a fixed-function pipeline. Instead of creating a separate program to direct the operation of the GPU, there was a set of fixed functions that you used to render objects on the screen. This was incredibly limiting, and you weren't able to get any specialized effects. If you want a good example of how much of a difference shaders can make in a project, [check out this blog post Brad Larson wrote about refactoring his Molecules app using shaders instead of the fixed-function pipeline.](http://www.sunsetlakesoftware.com/2011/05/08/enhancing-molecules-using-opengl-es-20)\n\nOpenGL ES 2.0 introduced the programmable pipeline. The programmable pipeline allowed you to go in and write your own shaders, giving you far more power and flexibility.\n\nThere are two kinds of shader files that you must create in OpenGL ES: vertex shaders and fragment shaders. These shaders are two halves of a whole program. You can't just create one or the other; both must be present to comprise a whole shader program.\n\nVertex shaders customize how geometry is handled in a 2D or 3D scene. A vertex is a point in 2D or 3D space. In the case of image processing, we have four vertices: one for each corner of your image. The vertex shader sets the position of a vertex and sends parameters like positions and texture coordinates to the fragment shader.\n\nYour GPU then uses a fragment shader to perform calculations on each pixel in an object or image, ending with the final color for that pixel. An image, when you get right down to it, is simply a collection of data. The image document contains parameters for the value of each pixel, for each color component, and for the pixel's opacity. Because the equations are the same for each pixel, the GPU is able to streamline the process and do it more efficiently. If you are optimizing your shader properly, you can process image data on the GPU more than 100 times faster than if you were to run the same process on the CPU.\n\nOne issue that has plagued OpenGL developers from the beginning is just being able to render anything on the screen. There is a lot of boilerplate code and setup that needs to be done in order to get a screen that isn't black. The frustration and the inability to test out shaders because of all the hoops developers had to jump through in the past has discouraged a lot of people from even trying to get involved in writing shaders.\n\nFortunately, in the last few years, several tools and frameworks have been made available to take some of the anxiety out of trying out shaders:\n\n- [GPUImage](https://github.com/BradLarson/GPUImage)\n- [ShaderToy](https://www.shadertoy.com/)\n- [Shaderific](http://www.shaderific.com/)\n- Quartz Composer\n\nEach of the shader examples I am going through here comes from the open-source GPUImage framework. If you are more curious about how an OpenGL/OpenGL ES scene is configured to render using shaders, feel free to clone the repository. I will not be going into how to set up OpenGL/OpenGL ES to render using shaders like this, as it is beyond the scope of the article.\n\n\n## Our First Shader Example ##\n\n### The Vertex Shader ###\n\nAlright, enough talking about shaders. Let’s see an actual shader program in action. Here is the baseline vertex shader in GPUImage:\n\n```glsl\nattribute vec4 position;\nattribute vec4 inputTextureCoordinate;\n\nvarying vec2 textureCoordinate;\n\nvoid main()\n{\n    gl_Position = position;\n    textureCoordinate = inputTextureCoordinate.xy;\n}\n```\n\nLet's take this piece by piece:\n\n```glsl\nattribute vec4 position;\n```\n\nLike all languages, the designers of our shading language knew to create special data types for commonly used types, such as 2D and 3D coordinates. These types are vectors, which we will go into more depth with a little later. Back in our application code, we are creating a list of vertices, and one of the parameters we provide per vertex is its position within our canvas. We then have to tell our vertex shader that it needs to take that parameter and that we are going to use it for something. Since this is a C program, we need to remember to use a semicolon at the end of each line of code, so if you are coding in Swift you need to remember to pick the semicolon habit back up.\n\n```glsl\nattribute vec4 inputTextureCoordinate;\n```\n\nAt this point you might be wondering why we are getting a texture coordinate. Didn't we just get our vertex position? Aren't these the same thing?\n\nNo, not necessarily. A texture coordinate is part of a texture map. What this means is that you have the image you want to filter, which is your texture. The upper left-hand corner has a coordinate space of (0, 0). The upper right-hand corner has a coordinate space of (1,0). If we wanted to select a texture coordinate that was inside the image and not at the edges, we would specify that the texture coordinate was something else in our base application, like (.25, .25), which would be located a quarter of the way in and down on our image. In our current image processing application, we want the texture coordinate and the vertex position to line up, because we want to cover the entire length and breadth of our image. There are times where you might want these positions to be different, so it's important to remember that they don't necessarily need to be the same coordinate. Also, the coordinate space for vertices in this example extends from −1.0 to 1.0, where texture coordinates go from 0.0 to 1.0.\n\n```glsl\nvarying vec2 textureCoordinate;\n```\n\nSince the vertex shader is responsible for communicating with the fragment shader, we need to create a variable that will share pertinent information with it. With image processing, the only piece of pertinent information it needs from the vertex shader is what pixel is it currently working on.\n\n```glsl\ngl_Position = position;\n```\n\n`gl_Position` is a built-in variable. GLSL has a few built-in variables, one of which we will see in the fragment shader example. These are special variables that are a part of the programmable pipeline that the API knows to look for and knows how to associate. In this case, we are specifying the vertex position and feeding that from our base program to the render pipeline.\n\n```glsl\ntextureCoordinate = inputTextureCoordinate.xy;\n```\n\nFinally, we are extracting the X and Y positions of the texture coordinate at this vertex. The coordinate was initially fed into the vertex shader with four attributes, but we only care about the first two components of `inputTextureCoordinate`, X and Y. Instead of feeding more attributes than we need to to our fragment shader, we are stripping out the ones we need and assigning them to a variable type that will talk to the fragment shader.\n\nThis vertex shader stays pretty much the same for all of our various image filter programs, so the rest of the shaders we will be focusing on for this article will be fragment shaders.\n\n\n### The Fragment Shader ###\n\nNow that we have gone over our simple vertex shader, let's take a look at the simplest fragment shader you can implement — a passthrough filter:\n\n```glsl\nvarying highp vec2 textureCoordinate;\n\nuniform sampler2D inputImageTexture;\n\nvoid main()\n{\n    gl_FragColor = texture2D(inputImageTexture, textureCoordinate);\n}\n```\n\nThis shader isn’t really changing anything in our image. It’s a passthrough shader, which pretty much means we are inputting each pixel and outputting the exact same one. Let’s also go through this piece by piece:\n\n```glsl\nvarying highp vec2 textureCoordinate;\n```\n\nSince the fragment shader works on each and every pixel, we need a way to determine which pixel/fragment we are currently analyzing. It needs to store both the X and the Y coordinate for the pixel. We are receiving the current texture coordinate that was set up in the vertex shader.\n\n```glsl\nuniform sampler2D inputImageTexture;\n```\n\nIn order to process an image, we are receiving a reference to the image from the application, which we are treating as a 2D texture. The reason this data type is called a `sampler2D` is because we are using the shader to go in and pluck out a point in that 2D texture to process.\n\n```glsl\ngl_FragColor = texture2D(inputImageTexture, textureCoordinate);\n```\n\nThis is our first encounter with a GLSL-specific function: `texture2D` is a function that, just as it sounds, creates a 2D texture. It takes our properties declared above as parameters to determine the exact color of the pixel being analyzed. This is then set to our other built-in variable, `gl\\_FragColor`. Since the only purpose of a fragment shader is to determine what color a pixel is, `gl\\_FragColor` essentially acts as a return statement for our fragment shader. Once the fragment color is set, there is no longer any point in continuing to do anything else in a fragment shader, so if you write any code after this line, it will not be processed.\n\nAs you can see, a vital part of writing shaders is to understand the shading language. Even though the shading language is based on C, there are lots of quirks and nuances that differentiate it from plain, vanilla C.\n\n\n## GLSL Data Types and Operations ##\n\nShaders of all flavors are written in the OpenGL Shading Language (GLSL). GLSL is a simple language derived from C. It is lacking in some of the more advanced features of C, such as dynamic memory management. However, it also contains a lot of specialized functionality to process commonly used mathematical functions in the shading process.\n\nThe Khronos Group, which is responsible for maintaining OpenGL and OpenGL ES, has reference materials for both available through its website. One of the most valuable things you can do for yourself when you are starting out is obtaining the Language Quick Reference cards for OpenGL ES and OpenGL:\n\n- [OpenGL ES](https://www.khronos.org/opengles/sdk/docs/reference_cards/OpenGL-ES-2_0-Reference-card.pdf)\n- [OpenGL](https://www.khronos.org/files/opengl-quick-reference-card.pdf)\n\nThese cards contain a quick and easy way to look over the language for the function or data type information you need to write an OpenGL application.\n\nUse these early. Use them often.\n\n\nThere are quite a few things in even that simple shader that look completely alien, aren’t there? Now that we’ve had a chance to take a look at a very basic shader, it’s time to start explaining what some of its contents are, and why we have them in GLSL.\n\n\n## Inputs, Outputs, and Precision Qualifiers ##\n\nIf you look at our passthrough shader, you will notice that we had one property that was labeled “varying,” and another one that was labeled “uniform.”\n\nThese variables are our inputs and outputs in GLSL. They allow input from our application and communication from the vertex shader to the fragment shader.\n\nThere are actually three labels we can assign to our variables in GLSL:\n\n- Uniforms\n- Attributes\n- Varyings\n\nUniforms are one way for the outside world to communicate with your shaders. Uniforms are designed for input values that aren't going to change within a render cycle. If you are applying a sepia filter and you need to specify the strength of the filter, this is something that isn't going to change within a render pass, so you'd send it in as a uniform. Uniforms can be accessed in both the vertex and the fragment shader.\n\nAttributes are only available in the vertex shader. Attributes are the input values that change with each vertex, such as its position and texture coordinate. The vertex shader takes in these values and either uses them to calculate the position, or passes values based on them along to the fragment shader in varyings.\n\nLast, but not least, we have varyings. Varyings are present in both the vertex and the fragment shader. Varyings are used to pass information from the vertex shader to the fragment shader, and must have matching names in both. Values are written to varyings in the vertex shader and read in the fragment shader. Values written into varyings are interpolated between vertices for each of the between-vertex pixels acted on by a fragment shader.\n\nIf you look back at our simple shader example, we had a varying declared in both the vertex and the fragment shader: `textureCoordinate`. We wrote the value of that varying in the vertex shader. We then passed it to the fragment shader, where it was read and processed.\n\nOne last quick thing to mention before we move on. Look at those variables you created. You will notice that your texture coordinate has an attribute called “highp.” This attribute is setting the precision you need for this variable. Since OpenGL ES was designed to be on systems with limited processing power, precision qualifiers were added for efficiency.\n\nIf you have something that doesn’t have to be very precise, you can indicate that and possibly allow more of these values to be operated on in a single clock cycle. On the other hand, in the case of the texture coordinate, we care a great deal about making sure this is as precise as possible, so we specify that we do indeed need this extra precision.\n\nPrecision qualifiers exist in OpenGL ES because they are geared toward mobile devices. However, they are missing in older versions of desktop OpenGL. Since OpenGL ES is effectively a subset of OpenGL, you can almost always directly port an OpenGL ES project to OpenGL. If you do that, however, you need to remember to strip the precision qualifiers out of your desktop shaders. This is an important thing to keep in mind, especially if you are planning to port your application between iOS and OS X.\n\n## Vectors ##\n\nYou are going to work with a lot of vectors and vector types in GLSL. Vectors are a slightly tricky topic in that they are seemingly straightforward, but since they are so versatile, there is a lot of information out there that can be confusing about them.\n\nIn the context of GLSL, vectors are a specialized data type similar to an array. Each type has a fixed value of elements that it can hold. If you dig in a little further, you can get even more specialized about the exact type of number value the array can hold, but for most purposes, sticking to the generic vector types will work just fine.\n\nThere are three vector types you will see over and over again:\n\n- `vec2`\n- `vec3`\n- `vec4`\n\nThese vector types contain a specified number of floating-point values: `vec2` contains two floating-point values, `vec3` contains three floating-point values, and `vec4` contains four floating-point values.\n\nThese types can be applied to several kinds of data you want to modify and persist in your shaders. One of the more obvious things you would want to keep track of is the X and Y coordinates of your fragment. An (X,Y) would fit quite nicely into the `vec2` data type.\n\nAnother thing that you tend to keep track of in graphics processing is the red, green, blue, and alpha value of each pixel. Those can be nicely stored in a `vec4` data type.\n\n\n## Matrices ##\n\nNow that we have a handle on vectors, let’s move on to matrices. Matrices are very similar to vectors, but they add an additional layer of complexity. Instead of simply being an array of floating-point values, matrices are an array of an array of floating-point values.\n\nAs with vectors, the matrix objects you are going to deal with most often are:\n\n- `mat2`\n- `mat3`\n- `mat4`\n\nWhere `vec2` holds two floating-point values, `mat2` holds the equivalent of two `vec2` objects. You don’t need to pass vector objects into your matrix objects, as long as you account for the correct number of floating-point elements needed to fill the matrix. In the case of the `mat2` object, you would either need to pass in two `vec2` objects or four floating-point values. Since you can name your vectors and you would only be responsible for two objects instead of four, it is highly encouraged for you to encapsulate your numbers in values that you can keep track of more easily. This only gets more complex when you move on to the `mat4` object and you are responsible for 16 numbers instead of 4!\n\nIn our `mat2` example, we have two sets of `vec2` objects. Each `vec2` object represents a row. The first element of each `vec2` represents a column. It’s very important to make sure that you are placing each value in the correct row and column when you are constructing your matrix object, or else the operations you perform on them will not work successfully.\n\nSo now that we have matrices and vectors to fill the matrices, the important question is: “What do we do with these?” We can store points and colors and other bits of information, but how does that get us any closer to making something cool by modifying them?\n\n## Vector and Matrix Operations, AKA Linear Algebra 101 ##\n\nOne of the best resources I have found out there to simply explain how linear algebra and matrices work is the site [Better Explained](http://betterexplained.com/articles/linear-algebra-guide/). One of the quotes I have stolen, er, borrowed from this site is the following:\n\n> The survivors of linear algebra classes are physicists, graphics programmers, and other masochists.\n\nMatrix operations generally aren’t “hard”; they just are not explained with any kind of context, so it’s difficult to conceptualize why on earth anyone would want to work with them. Hopefully by giving a little bit of context insofar as how they are utilized in graphics programming, we can get a sense of how they can help us implement awesome stuff.\n\nLinear algebra allows you to perform an action on many values at the same time. Let’s say you have a group of numbers and you want to multiply each of them by two. You are transforming each number in a consistent way. Since the same operation is being done to each number, you can implement this operation in parallel.\n\nOne example we should be using but seem to be afraid of is that of `CGAffineTransforms`. An affine transform is simply an operation that changes the size, position, or rotation of a shape with parallel sides, like a square or a rectangle.\n\nIt isn’t super important at this juncture to break out the old slide rule and be able to sit down with a pad of graph paper and a pencil and calculate your own transforms. GLSL has a lot of built-in functions that do the messy work of calculating out your transforms for you. It’s just important to have an idea of how these functions are working under the hood.\n\n## GLSL-Specific Functions ##\n\nWe’re not going to go over all of the built-in functions for GLSL in this article, but a good resource for this can be found at [Shaderific](http://www.shaderific.com/glsl-functions). The vast majority of the GLSL functions are derived from basic math operations that are present in the C Math Library, so it isn’t really a good use of time to explain what the sin function does. We’re going to stick to some of the more esoteric functions for the purposes of this article, in order to explain some of the nuances of how to get the best performance out of your GPU.\n\n**`step()`:** One limitation that your GPU has is that it doesn’t really deal well with conditional logic. The GPU likes to take a bunch of operations and just apply them to everything. Branching can lead to significant slowdowns in fragment shaders, particularly on mobile devices. `step()` works around this limitation somewhat by allowing conditional logic without branching. If a variable passed into a `step()` function is less than a threshold value, `step()` returns 0.0. If the variable is greater or equal, it returns 1.0. By multiplying this result times values in your shader, values can be used or ignored based on conditional logic, all without an `if()` statement.\n\n**`mix()`:** The mix function blends two values (such as colors) to a variable degree. If we had two colors of red and green, we could linearly interpolate between them using a `mix()` function. This is commonly used in image processing to control the strength of an effect in response to a uniform set by the application.\n\n**`clamp()`:** One of the consistent aspects of GLSL is that it likes to use normalized coordinates. It wants and expects to receive values between 0.0 and 1.0 for things like color components or texture coordinates. In order to make sure that our values don't stray outside of this very narrow parameter, we can implement the `clamp()` function. The `clamp()` function checks to make sure your value is between 0.0 and 1.0. If your value is below 0.0, it will set its value to 0.0. This is done to avoid any general wonkiness that might arise if you are trying to do calculations and you accidentally receive a negative number or something that is entirely beyond the scope of the equation.\n\n\n## More Complex Shader Examples\n\nI realize that deluge of math must have felt very overwhelming. If you’re still with me, I want to walk through a couple of neat shader examples that will make a lot more sense now that you’ve have a chance to wade into the GLSL waters.\n\n### Saturation Adjustment\n\n![Saturation Filter in Action](/images/issue-21/Saturation.png)\n\nThis is a fragment shader that does saturation adjustment. This shader is based off of code from the book \"[Graphics Shaders: Theory and Practice](http://www.amazon.com/Graphics-Shaders-Theory-Practice-Second/dp/1568814348/ref=sr_1_1?s=books&ie=UTF8&qid=1422557718&sr=1-1&keywords=graphics+shaders+theory+and+practice),\" which I highly recommend to anyone interested in learning more about shaders.\n\nSaturation is the term used to describe how bright and intense a color is. A bright red sweater is far more saturated than the gloomy, gray winter skies in rural Wisconsin.\n\nThere are some optimizations we can utilize in this shader that work with the way that human beings perceive color and contrast. Generally speaking, human beings are far more sensitive to brightness than we are to color. One optimization made over the years to our compression software is to pare back the amount of memory used to store color.\n\nNot only are humans more sensitive to brightness than color, but we are also more responsive to certain colors within the brightness spectrum, specifically green. This means that when you are calculating out ways of compressing your photos or modifying their brightness or color in some way, it’s important to put more emphasis on the green part of the spectrum, because that is the one that we respond to the most:\n\n```glsl\nvarying highp vec2 textureCoordinate;\n\nuniform sampler2D inputImageTexture;\nuniform lowp float saturation;\n\nconst mediump vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);\n\nvoid main()\n{\n   lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);\n   lowp float luminance = dot(textureColor.rgb, luminanceWeighting);\n   lowp vec3 greyScaleColor = vec3(luminance);\n\n\tgl_FragColor = vec4(mix(greyScaleColor, textureColor.rgb, saturation), textureColor.w);\n\n}\n```\n\nLet's go through this fragment shader line by line:\n\n```glsl\nvarying highp vec2 textureCoordinate;\n\nuniform sampler2D inputImageTexture;\nuniform lowp float saturation;\n```\n\nAgain, since this is a fragment shader that is talking to our baseline vertex shader, we do need to declare varyings for our input texture coordinate and our input image texture, in order to receive the information we need to process our filter. We do have a new uniform that we are dealing with in this example, which is saturation. Saturation amount is a parameter we are set up to receive from the user interface. We need to know how much saturation the user wants in order to present the correct amount of color.\n\n```glsl\nconst mediump vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);\n```\n\nThis is where we are setting up a three-component vector to store our color weighting for our luminance extraction. All three of these values must add up to 1.0 so that we can calculate the luminance of a pixel on a scale from 0.0 to 1.0. Notice that the middle number, which represents green, uses 70 percent of the available color weighting, while blue only uses a tenth of that. The blue doesn’t show up as well to us, and it makes more sense to instead weigh toward green for brightness.\n\n```glsl\nlowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);\n```\n\nWe need to capture the color information about our specific pixel by sampling its exact coordinate within our image/texture. Instead of simply returning this value as we did with the passthrough filter, we are going to modify it and change it up a little.\n\n```glsl\nlowp float luminance = dot(textureColor.rgb, luminanceWeighting);\n```\n\nThis line will probably look unfamiliar to anyone who either never took linear algebra or who took it so long ago that dinosaurs were used to ride to school. We are using the dot product function from GLSL. If you remember using a dot symbol to multiply two numbers together in school, you’re on the right track here. The dot product is taking our `vec4` containing the texture color information for the fragment, dropping the last parameter because it won’t be needed, and multiplying it by its corresponding luminance weight. Then it is taking all three of those values and adding them together to figure out the overall luminance of the pixel.\n\n```glsl\nlowp vec3 greyScaleColor = vec3(luminance);\n```\n\nNow we are creating a `vec3` that contains the luminance for all three values. If you only specify one value, the compiler knows enough to set it for each slot in that vector.\n\n```glsl\ngl_FragColor = vec4(mix(greyScaleColor, textureColor.rgb, saturation), textureColor.w);\n```\n\nFinally, we are putting all of our pieces together. In order to determine what the new value of each color is, we are applying our handy dandy mix function that we learned about a little while ago. The mix function is taking the grayscale color we just determined, combining it with the initial texture color, and basing the ratio of the mix on the information we are getting back about the saturation level.\n\nSo here is a nice, handy shader that lets you change your image from color to grayscale and back with only four lines of code in the main function. Not too bad, huh?\n\n### Sphere Refraction\n\nFinally, we’re going to go over a really nifty filter that you can pull out to impress your friends and terrify your enemies. This filter makes it look like there is a glass sphere sitting on top of your image. It's going to be quite a bit more complicated than the previous ones, but I have confidence that we can do it!\n\n![Sphere Refraction Filter in Action!](/images/issue-21/sphereRefraction.png)\n\n\n```glsl\nvarying highp vec2 textureCoordinate;\n\nuniform sampler2D inputImageTexture;\n\nuniform highp vec2 center;\nuniform highp float radius;\nuniform highp float aspectRatio;\nuniform highp float refractiveIndex;\n\nvoid main()\n{\n    highp vec2 textureCoordinateToUse = vec2(textureCoordinate.x, (textureCoordinate.y * aspectRatio + 0.5 - 0.5 * aspectRatio));\n    highp float distanceFromCenter = distance(center, textureCoordinateToUse);\n    lowp float checkForPresenceWithinSphere = step(distanceFromCenter, radius);\n\n    distanceFromCenter = distanceFromCenter / radius;\n\n    highp float normalizedDepth = radius * sqrt(1.0 - distanceFromCenter * distanceFromCenter);\n    highp vec3 sphereNormal = normalize(vec3(textureCoordinateToUse - center, normalizedDepth));\n\n    highp vec3 refractedVector = refract(vec3(0.0, 0.0, -1.0), sphereNormal, refractiveIndex);\n\n    gl_FragColor = texture2D(inputImageTexture, (refractedVector.xy + 1.0) * 0.5) * checkForPresenceWithinSphere;\n}\n```\n\nOnce more, with feeling...\n\n```glsl\nuniform highp vec2 center;\nuniform highp float radius;\nuniform highp float aspectRatio;\nuniform highp float refractiveIndex;\n```\n\nWe are bringing in a few parameters that we need in order to calculate out how much of our image is going to go through the filter. Since this is a sphere, we need a center point and a radius to calculate where the edges of the sphere are. The aspect ratio is determined by the screen size of whatever device you are using, so it can’t be hardcoded, because an iPhone has a different screen ratio than an iPad does. Our user or the programmer will decide what he or she wants the refractive index to be to determine how the refraction looks. The refractive index set in GPUImage is 0.71.\n\n```glsl\nhighp vec2 textureCoordinateToUse = vec2(textureCoordinate.x, (textureCoordinate.y * aspectRatio + 0.5 - 0.5 * aspectRatio));\n```\n\nThe texture coordinates of our image are in a normalized 0.0-1.0 coordinate space. This means that instead of thinking of the phone as being 320 pixels across and 480 pixels high, the screen is one unit long and one unit wide. Since the phone is taller than it is long, we need to calculate an offset ratio for our sphere so that the sphere is round instead of oval:\n\n![We want a correct aspect ratio](/images/issue-21/aspectRatio.png)\n\n```glsl\nhighp float distanceFromCenter = distance(center, textureCoordinateToUse);\n```\n\nWe need to calculate how far away from the center of the sphere our specific pixel is. We are using the `distance()` function built into GLSL, which takes the Pythagorean distance between the center coordinate and the aspect-ratio-corrected texture coordinate.\n\n```glsl\nlowp float checkForPresenceWithinSphere = step(distanceFromCenter, radius);\n```\n\nHere is where we are going to figure out if our fragment resides within the sphere. We are checking to see how far away we are from the center of the sphere and what the radius is. If our distance is shorter than the radius, then the fragment exists within the sphere and this variable is set to 1.0. If, however, the distance from the center is longer than the radius, the fragment does not live within the sphere and this gets set to 0.0:\n\n![Pixels are either inside or outside the sphere](/images/issue-21/distanceFromCenter2.png)\n\n```glsl\ndistanceFromCenter = distanceFromCenter / radius;\n```\n\nNow that we have determined which pixels exist within the sphere, we are going to move on to calculating what to do with the ones that do exist in the sphere. Again, we need to normalize our distance from the center. Rather than creating a whole new variable, which adds to the overhead in our program, we are going to reset the `distanceFromCenter` variable. By dividing it by the radius, we are making our math calculations easier in the next few lines of code.\n\n```glsl\nhighp float normalizedDepth = radius * sqrt(1.0 - distanceFromCenter * distanceFromCenter);\n```\n\nSince we are trying to emulate a glass sphere, we need to figure out how “deep” the sphere is. The virtual sphere, for all intents and purposes, is extending a distance up from the image surface toward the viewer in the z-axis. This is going to be used to help the computer figure out how to model the pixels that exist within the sphere. Also, since a sphere is round, there will be different depths for the sphere depending upon how far away you are from the center. The center of the sphere will refract light differently than the edges, due to the different orientations of the surface:\n\n![How deep is the sphere?](/images/issue-21/normalizedDepth.png)\n\n```glsl\nhighp vec3 sphereNormal = normalize(vec3(textureCoordinateToUse - center, normalizedDepth));\n```\n\nAgain, we are back to normals, huzzah. To describe the orientation of the sphere surface at a point, we take the distance of the current pixel from the center of the sphere in X and Y, and combine those components with the sphere depth we calculated. We then normalize the resulting vector to have a length of one.\n\nThink about when you are using something like Adobe Illustrator. You create a triangle in Illustrator, but it's too small. You hold down the option key and you resize the triangle, except now it's too big. You then scale it down to get it to be the exact size you want:\n\n![What's the angle?](/images/issue-21/sphereNormal.png)\n\n```glsl\nhighp vec3 refractedVector = refract(vec3(0.0, 0.0, -1.0), sphereNormal, refractiveIndex);\n```\n\n`refract()` is a fun GLSL function. `refract()` is taking in the sphere normal we just created and using the refractive index to calculate how light passing through a sphere of this type would look at any given point.\n\n```glsl\ngl_FragColor = texture2D(inputImageTexture, (refractedVector.xy + 1.0) * 0.5) * checkForPresenceWithinSphere;\n```\n\nFinally, after jumping through all these hoops, we have gathered together all of the pieces we need to figure out what color to use for the fragment. The refracted light vector is used to find which location on the input image to read from, but because the coordinates in that vector range from −1.0 to 1.0, we adjust that to lie within the 0.0–1.0 texture coordinate space.\n\nWe then multiply our effect by the value we got from our sphere bounds check. If our fragment doesn’t lie within the sphere, a transparent pixel (0.0, 0.0, 0.0, 0.0) is written. If the fragment is present within the sphere, the effect is applied and the calculated color returned. This allows us to avoid expensive conditional logic for the shader.\n\n## Debugging Shaders\n\nDebugging shaders is not a straightforward task. In your normal program, if the program crashes, you can set a breakpoint. That isn't really possible to do on an operation that gets called in parallel millions of times a second. It also isn't feasible to use `printf()` statements in your shaders to debug what is going wrong, because where would the output even go? Given that your shaders seem to be living in a black box, how do you crack them open to find out why they aren't working?\n\nYou have one output at your disposal: our old friend `gl\\_FragColor`. `gl\\_FragColor` gives you an output that, with a little lateral thinking, you can use to debug your code.\n\nAll colors you see on the screen are represented by a series of numbers, which are a percentage of the amount of red, green, blue, and opacity each individual pixel contains. You can use this knowledge to test each part of your shader as you construct it to make sure that it is performing the way you would like. Instead of getting back a printed value, you would get back a color with a specific associated value that you can reverse engineer.\n\nIf you want to know the value of one of your variables that is between zero and one, you can set it to part of the `vec4` that gets passed to the `gl\\_FragColor`. Let's say you set it to the first part, which is the red value. That value will be converted and rendered to the screen, at which point you can examine it to determine what the original value was that was passed in.\n\nYou can then capture these values in a couple of ways. The output image from your shader could be captured and written to disk as an image (preferably in an uncompressed format). This image could then be pulled into an application like Photoshop and the pixel colors examined.\n\nFor faster turnaround, you could render your image to the screen in an OS X application, or an iOS one running in the Simulator. To analyze these rendered views, there is a tool included in your Utilities folder in your Applications folder called \"Digital Color Meter.\" If you hover your mouse over any pixel on your desktop, it will show you the exact RGB component of that pixel. Since RGB values in Digital Color Meter and Photoshop are from 0 to 255 instead of 0 to 1, you need to divide the specific value you want by 255 to get an approximate value of what the initial passed-in value was.\n\nLet's look back at our sphere refraction shader. We wouldn't want to try to write the whole shader without doing any debugging on it. We have the specific chunk of code to determine if the pixel we are currently looking at is within the circle or not. That block of code ends with a `step()` function that sets the value of a pixel to either 0.0 or 1.0.\n\nIf you passed a `vec4` to the `gl\\_FragColor`, where the red value was whatever the `step()` function value was, and the other two colors were set to 0.0, you should see a red circle on a black screen if your code is working properly. If the whole screen is either black or red, then something has gone terribly wrong.\n\n## Performance Tuning\n\nPerformance tuning and profiling are incredibly important things to do, especially if you are trying to target your application to run smoothly on older iOS devices.\n\nProfiling your shader is important, because you can't always be sure how performant something will be. Shader performance changes in nonintuitive ways. You might find a great optimization on Stack Overflow that does nothing to speed up your shader because you didn't optimize where the actual bottleneck was in your processing code. Even switching a couple of lines of code in your project can vastly increase or decrease the amount of time it takes for your frame to render.\n\nWhen profiling, I recommend measuring frame rendering time, rather than focusing on frames per second (FPS). Frame rendering time increases or decreases linearly with the performance of your shader, which makes it easy to see the impact you're having. FPS is the inverse of frame time, and it can be harder to understand when tuning. Lastly, if you're capturing from the iPhone's video camera, it will adjust incoming FPS depending upon the lighting in your scene, which can lead to incorrect measurements if you rely on that.\n\nThe frame rendering time is the amount of time it takes for the frame to begin processing until it completely finishes and is rendered to the screen or to a final image. Many mobile GPUs use a technique called \"deferred rendering,\" where rendering instructions are batched up and executed only as needed. Therefore, it's important to measure the entire rendering operation, rather than operations in the middle, because they may run in a different order than you expect.\n\nOptimizations can also vary wildly from device to device, desktop, and mobile. You may need to profile on multiple classes of devices. For example, the GPUs in mobile iOS devices have grown increasingly more powerful. The CPU on an iPhone 5S is approximately 10 times faster than the CPU on the iPhone 4, however its GPU is hundreds of times faster.\n\nIf you are testing your applications on devices with an A7 chip or higher, you are going to get vastly different results than you would with an iPhone 5 or lower. [Brad Larson profiled how long a Gaussian Blur took on various iOS devices and has clearly demonstrated a dramatic leap forward in processing times on newer devices:](http://www.sunsetlakesoftware.com/2013/10/21/optimizing-gaussian-blurs-mobile-gpu)\n\niPhone Version | Frame Rendering Time in Milliseconds\n-------------- | ------------------------------------\niPhone 4       | 873\niPhone 4S      | 145\niPhone 5       | 55\niPhone 5S      | 3\n\n\nThere is a tool that you can download, [Imagination Technologies PowerVR SDK](http://community.imgtec.com/developers/powervr/), that will profile your shader and let you know the best- and worst-case performance for your shader rendering. It's important to get the number of cycles necessary to render your shader as low as possible to keep your frame rate high. If you want to hit a target of 60 frames per second, you only have 16.67 milliseconds to get all of your processing done.\n\nHere are some easy ways to help you hit your target:\n\n- **Eliminate conditional logic.** Sometimes it's necessary to include conditional logic, but try to keep it to a minimum. Using workarounds like the `step()` function can help you avoid expensive conditional logic in your shaders.\n- **Reduce dependent texture reads.** Dependent texture reads occur when a texture is sampled in a fragment shader from a texture coordinate that wasn't passed in directly as a varying, but was instead calculated in the fragment shader. These dependent texture reads can't take advantage of optimizations in caching that normal texture reads do, leading to much slower reads. For example, if you want to sample from nearby pixels, rather than calculate the offset to the neighboring pixel in your fragment shader, it's best to do this calculation in the vertex shader and have the result be passed along as a varying. A demonstration of this is present in [Brad Larson's article](/issues/21-camera-and-photos/gpu-accelerated-machine-vision/), in the case of Sobel edge detection.\n- **Make your calculations as simple as possible.** If you can avoid an expensive operation and get an approximate value that is good enough, you should do so. Expensive calculations include calling trigonometric functions (like `sin()`, `cos()`, and `tan()`).\n- **Shift work over to the vertex shader, if it makes sense.** Our previous talk about dependent texture reads is a situation where it would make sense to move texture coordinate calculations to the vertex shader. If a calculation would have the same result across your image, or would linearly vary across it, look at moving that calculation into the vertex shader. Vertex shaders run once per vertex, whereas fragment shaders execute once per pixel, so a calculation performed in the former will run fewer times.\n- **Use appropriate precision on mobile devices.** On certain mobile devices, it can be much faster to work with lower precision values in vectors. The addition of two `lowp vec4`s can often be done in a single clock cycle on these devices, where the addition of two `highp vec4`s can take four clock cycles. This is less important on desktop GPUs and more recent mobile GPUs, though, as they don't have the same optimizations for low precision values.\n\n## Conclusions and Resources\n\nShaders seem kind of scary at first, but they are nothing more than modified C programs. Everything involved in creating a shader is stuff that most of us have dealt with at one point or another, just in a different context.\n\nOne thing I would highly recommend for anyone trying to get into shaders is to refamiliarize yourself with trigonometry and linear algebra. The biggest stumbling block I encountered when working with this was that I didn't remember a lot of the math I learned in high school, because I hadn't used it in a really long time.\n\nThere are some books I would recommend if your math is a little rusty:\n\n- [3D Math Primer for Graphics and Game Development](http://www.amazon.com/Math-Primer-Graphics-Game-Development/dp/1568817231/ref=sr_1_1?ie=UTF8&qid=1422837187&sr=8-1&keywords=3d+math+primer+for+graphics+and+game+development)\n- [The Nature of Code](http://natureofcode.com)\n- [The Computational Beauty of Nature](http://www.amazon.com/Computational-Beauty-Nature-Explorations-Adaptation/dp/0262561271/ref=sr_1_1?s=books&ie=UTF8&qid=1422837256&sr=1-1&keywords=computational+beauty+of+nature)\n\nThere are also countless books out there about GLSL and how some very specific shaders were created by prominent members of our industry:\n\n- [Graphics Shaders: Theory and Practice](http://www.amazon.com/Graphics-Shaders-Theory-Practice-Second/dp/1568814348/ref=sr_1_1?s=books&ie=UTF8&qid=1422837351&sr=1-1&keywords=graphics+shaders+theory+and+practice)\n- [The OpenGL Shading Language](http://www.amazon.com/OpenGL-Shading-Language-Randi-Rost/dp/0321637631/ref=sr_1_1?s=books&ie=UTF8&qid=1422896457&sr=1-1&keywords=opengl+shading+language)\n- [OpenGL 4 Shading Language Cookbook](http://www.amazon.com/OpenGL-Shading-Language-Cookbook-Second/dp/1782167021/ref=sr_1_2?s=books&ie=UTF8&qid=1422896457&sr=1-2&keywords=opengl+shading+language)\n- [GPU Gems](http://http.developer.nvidia.com/GPUGems/gpugems_part01.html)\n- [GPU Pro: Advanced Rendering Techniques](http://www.amazon.com/GPU-Pro-Advanced-Rendering-Techniques/dp/1568814720/ref=sr_1_4?s=books&ie=UTF8&qid=1422837427&sr=1-4&keywords=gpu+pro)\n\nAlso, again, [GPUImage](https://github.com/BradLarson/GPUImage) is an open-source resource to get a look at some really cool shaders. One good way to learn about shaders is to take a shader you find interesting and go through it line by line, looking up any part of it that you don't understand. GPUImage also has a [shader designer](https://github.com/BradLarson/GPUImage/tree/master/examples/Mac/ShaderDesigner) application on the Mac side that lets you test out shaders without having to set up the OpenGL code.\n\nLearning how to effectively implement shaders in your code can give you a huge performance boost. Not only that, but shaders also allow you to do things that were not possible before.\n\nLearning shaders takes some tenacity and some curiosity, but they aren't impossible. If a 33-year-old recovering journalism major could confront her math fear to tackle shaders, so can you.\n"
  },
  {
    "path": "2015-02-10-gpu-accelerated-machine-vision.md",
    "content": "---\ntitle:  \"GPU-Accelerated Machine Vision\"\ncategory: \"21\"\ndate: \"2015-02-10 05:00:00\"\nauthor:\n  - name: Brad Larson\n    url: https://twitter.com/bradlarson\ntags: article\n---\n\n\nWhile the proliferation of cameras attached to mobile computers has been a boon for photography, there is much more that this can enable. Beyond simply capturing the outside world, the right software running on more powerful hardware can allow these computers to understand the things their cameras see.\n\nThis small bit of understanding can enable some truly powerful applications, such as barcode scanning, document recognition and imaging, translation of written words, real-time image stabilization, and augmented reality. As processing power, camera fidelity, and algorithms advance, this machine vision will be able to solve even more important problems.\n\nMany people regard machine vision as a complex discipline, far outside the reach of everyday programmers. I don't believe that's the case. I created an open-source framework called [GPUImage](https://github.com/BradLarson/GPUImage) in large part because I wanted to explore high-performance machine vision and make it more accessible.\n\nGPUs are ideally suited to operate on images and video because they are tuned to work on large collections of data in parallel, such as the pixels in an image or video frame. Depending on the operation, GPUs can process images hundreds or even thousands of times faster than CPUs can.\n\nOne of the things I learned while working on GPUImage is how even seemingly complex image processing operations can be built from smaller, simpler ones. I'd like to break down the components of some common machine vision processes, and show how these processes can be accelerated to run on modern GPUs.\n\nEvery operation analyzed here has a full implementation within GPUImage, and you can try each one yourself by grabbing the project and building the `FilterShowcase` sample application either for OS X or iOS. Additionally, all of these operations have CPU-based (and some GPU-accelerated) implementations within the OpenCV framework, which Engin Kurutepe talks about in [his article within this issue](/issues/21-camera-and-photos/face-recognition-with-opencv/).\n\n## Sobel Edge Detection\n\nThe first operation I'll describe may actually be used more frequently for cosmetic image effects than machine vision, but it's a good place to start. Sobel edge detection is a process where edges (sharp transitions from light to dark, or vice versa) are found within an image.[^1] The strength of an edge around a pixel is reflected in how bright that pixel is in the processed image.\n\nFor example, let's see a scene before and after Sobel edge detection:\n\n![Original image](/images/issue-21/MV-Chair.png)\n![Sobel edge detection image](/images/issue-21/MV-Sobel.png)\n\nAs I mentioned, this is often used for visual effects. If the colors of the above are inverted, with the strongest edges represented in black instead of white, we get an image that resembles a pencil sketch:\n\n![Sketch filtered image](/images/issue-21/MV-Sketch.png)\n\nSo how are these edges calculated? The first step in this process is a reduction of a color image to a luminance (grayscale) image. Janie Clayton explains how this is calculated in a fragment shader within [her article](/issues/21-camera-and-photos/gpu-accelerated-image-processing/), but basically the red, green, and blue components of each pixel are weighted and summed to arrive at a single value for how bright that pixel is.\n\nSome video sources and cameras provide YUV-format images, rather than RGB. The YUV color format splits luminance information (Y) from chrominance (UV), so for these types of inputs, a color conversion step can be avoided. The luminance part of the image can be used directly.\n\nOnce an image is reduced to its luminance, the edge strength near a pixel is calculated by looking at a 3×3 array of neighboring pixels. An image processing calculation performed over a block of pixels involves what is called a convolution kernel. Convolution kernels consist of a matrix of weights that are multiplied with the values of the pixels surrounding a central pixel, with the sum of those weighted values determining the final pixel value.\n\nThese kernels are applied once per pixel across the entire image. The order in which pixels are processed doesn't matter, so a convolution across an image is an easy operation to parallelize. As a result, this can be greatly accelerated by running on a programmable GPU using fragment shaders. As described in [Janie's article](/issues/21-camera-and-photos/gpu-accelerated-image-processing/), fragment shaders are C-like programs that can be used by GPUs to perform incredibly fast image processing.\n\nThis is the horizontal kernel of the Sobel operator:\n\n<style type=\"text/css\">\n  table.border td {\n      border: 1px solid #ccc;\n  }\n  td.center {\n    padding-left: 1em;\n    padding-right: 1em;\n    text-align: center;\n  }\n</style>\n<table class=\"border\">\n  <tr>\n    <td class=\"center\">−1</td><td class=\"center\">0</td><td class=\"center\">+1</td>\n  </tr>\n  <tr>\n    <td class=\"center\">−2</td><td class=\"center\">0</td><td class=\"center\">+2</td>\n  </tr>\n  <tr>\n    <td class=\"center\">−1</td><td class=\"center\">0</td><td class=\"center\">+1</td>\n  </tr>\n</table>\n\nTo apply this to a pixel, the luminance is read from each surrounding pixel. If the input image has been converted to grayscale, this can be sampled from any of the red, green, or blue color channels. The luminance of a particular surrounding pixel is multiplied by the corresponding weight from the above matrix and added to the total.\n\nHow this works to find an edge in a direction is that it looks for differences in luminance (brightness) on the left and right sides of a central pixel. If you have two equally bright pixels on the left and right of the center one (a smooth area in the image), the product of their intensities and the negative and positive weights will cancel out, and no edge will be detected. If there is a difference between the brightness of pixels on the left and right (an edge), one brightness will be subtracted from the other. The greater the difference, the stronger the edge measured.\n\nThe Sobel operator has two stages, the horizontal kernel being the first. A vertical kernel is applied at the same time, with the following matrix of weights:\n\n<table class=\"border\">\n  <tr>\n    <td class=\"center\">−1</td><td class=\"center\">−2</td><td class=\"center\">−1</td>\n  </tr>\n  <tr>\n    <td class=\"center\">0</td><td class=\"center\">0</td><td class=\"center\">0</td>\n  </tr>\n  <tr>\n    <td class=\"center\">+1</td><td class=\"center\">+2</td><td class=\"center\">+1</td>\n  </tr>\n</table>\n\nThe final weighted sum from each operator is tallied, and the square root of the sums of their squares is obtained. The squares are used because the values might be negative or positive, but we want their magnitude, not their sign. There's also a handy built-in GLSL function that does this for us.\n\nThat combined value is then used as the luminance for the final output image. Sharp transitions from light to dark (or vice versa) become bright pixels in the result, due to the Sobel kernels emphasizing differences between pixels on either side of the center.\n\nThere are slight variations to Sobel edge detection, such as Prewitt edge detection,[^2] that use different weights for the horizontal and vertical kernels, but they rely on the same basic process.\n\nAs an example for how this can be implemented in code, the following is an OpenGL ES fragment shader that performs Sobel edge detection:\n\n```glsl\nprecision mediump float;\n\nvarying vec2 textureCoordinate;\nvarying vec2 leftTextureCoordinate;\nvarying vec2 rightTextureCoordinate;\n\nvarying vec2 topTextureCoordinate;\nvarying vec2 topLeftTextureCoordinate;\nvarying vec2 topRightTextureCoordinate;\n\nvarying vec2 bottomTextureCoordinate;\nvarying vec2 bottomLeftTextureCoordinate;\nvarying vec2 bottomRightTextureCoordinate;\n\nuniform sampler2D inputImageTexture;\n\nvoid main()\n{\n   float bottomLeftIntensity = texture2D(inputImageTexture, bottomLeftTextureCoordinate).r;\n   float topRightIntensity = texture2D(inputImageTexture, topRightTextureCoordinate).r;\n   float topLeftIntensity = texture2D(inputImageTexture, topLeftTextureCoordinate).r;\n   float bottomRightIntensity = texture2D(inputImageTexture, bottomRightTextureCoordinate).r;\n   float leftIntensity = texture2D(inputImageTexture, leftTextureCoordinate).r;\n   float rightIntensity = texture2D(inputImageTexture, rightTextureCoordinate).r;\n   float bottomIntensity = texture2D(inputImageTexture, bottomTextureCoordinate).r;\n   float topIntensity = texture2D(inputImageTexture, topTextureCoordinate).r;\n\n   float h = -bottomLeftIntensity - 2.0 * leftIntensity - topLeftIntensity + bottomRightIntensity + 2.0 * rightIntensity + topRightIntensity;\n   float v = -topLeftIntensity - 2.0 * topIntensity - topRightIntensity + bottomLeftIntensity + 2.0 * bottomIntensity + bottomRightIntensity;\n   float mag = length(vec2(h, v));\n\n   gl_FragColor = vec4(vec3(mag), 1.0);\n}\n```\n\nThe above shader has manual names for the pixels around the center one, passed in from a custom vertex shader, due to an optimization to reduce dependent texture reads on mobile devices. After these named pixels are sampled in a 3×3 grid, the horizontal and vertical Sobel kernels are applied using hand-coded calculations. The 0-weight entries are left out, in order to simplify these calculations. The GLSL `length()` function calculates a Pythagorean hypotenuse between the results of the horizontal and vertical kernels. That magnitude value is then copied into the red, green, and blue channels of the output pixel to produce a grayscale indication of edge strength.\n\n## Canny Edge Detection\n\nSobel edge detection can give you a good visual measure of edge strength in a scene, but it doesn't provide a yes/no indication of whether a pixel lies on an edge or not. For such a decision, you could apply a threshold of some sort, where pixels above a certain edge strength are considered to be part of an edge. However, this isn't ideal, because it tends to produce edges that are many pixels wide, and choosing an appropriate threshold can vary with the contents of an image.\n\nA more involved form of edge detection, called Canny edge detection,[^3] might be what you want here. Canny edge detection can produce connected, single-pixel-wide edges of objects in a scene:\n\n![Canny edge detection image](/images/issue-21/MV-Canny.png)\n\nThe Canny edge detection process consists of a sequence of steps. First, like with Sobel edge detection (and the other techniques we'll discuss), the image needs to be converted to luminance before edge detection is applied to it. Once a grayscale luminance image has been obtained, a slight [Gaussian blur](http://www.sunsetlakesoftware.com/2013/10/21/optimizing-gaussian-blurs-mobile-gpu) is used to reduce the effect of sensor noise on the edges being detected.\n\nAfter the image has been prepared, the edge detection can be performed. The specific GPU-accelerated process used here was originally described by Ensor and Hall in \"GPU-based Image Analysis on Mobile Devices.\"[^4]\n\nFirst, both the edge strength at a given pixel and the direction of the edge gradient are determined. The edge gradient is the direction in which the greatest change in luminance is occurring. This is perpendicular to the direction the edge itself is running.\n\nTo find this, we use the Sobel kernel described in the previous section. The magnitude of the combined horizontal and vertical results gives the edge gradient strength, which is encoded in the red component of the output pixel. The horizontal and vertical Sobel results are then clamped to one of eight directions (corresponding to the eight pixels surrounding the central pixel), and the X component of that direction is encoded in the green component of the pixel. The Y component is placed into the blue component.\n\nThe shader used for this looks like the Sobel edge detection one above, only with the final calculation replaced with this code:\n\n```glsl\n\tvec2 gradientDirection;\n\tgradientDirection.x = -bottomLeftIntensity - 2.0 * leftIntensity - topLeftIntensity + bottomRightIntensity + 2.0 * rightIntensity + topRightIntensity;\n\tgradientDirection.y = -topLeftIntensity - 2.0 * topIntensity - topRightIntensity + bottomLeftIntensity + 2.0 * bottomIntensity + bottomRightIntensity;\n\n\tfloat gradientMagnitude = length(gradientDirection);\n\tvec2 normalizedDirection = normalize(gradientDirection);\n\tnormalizedDirection = sign(normalizedDirection) * floor(abs(normalizedDirection) + 0.617316); // Offset by 1-sin(pi/8) to set to 0 if near axis, 1 if away\n\tnormalizedDirection = (normalizedDirection + 1.0) * 0.5; // Place -1.0 - 1.0 within 0 - 1.0\n\n\tgl_FragColor = vec4(gradientMagnitude, normalizedDirection.x, normalizedDirection.y, 1.0);\n```\n\nTo refine the Canny edges to be a single pixel wide, only the strongest parts of the edge are kept. For that, we need to find the local maximum of the edge gradient at each slice along its width.\n\nThis is where the gradient direction we calculated in the last step comes into play. For each pixel, we look at the nearest neighboring pixels both forward and backward along this length, and compare their calculated gradient strength (edge intensity). If the current pixel's gradient strength is greater than those of the ones forward and backward along the gradient, we keep that pixel. If the strength is less than either of the neighboring pixels, we reject that pixel and turn it to black.\n\nA shader to do this appears as follows:\n\n```glsl\nprecision mediump float;\n\nvarying highp vec2 textureCoordinate;\n\nuniform sampler2D inputImageTexture;\nuniform highp float texelWidth;\nuniform highp float texelHeight;\nuniform mediump float upperThreshold;\nuniform mediump float lowerThreshold;\n\nvoid main()\n{\n    vec3 currentGradientAndDirection = texture2D(inputImageTexture, textureCoordinate).rgb;\n    vec2 gradientDirection = ((currentGradientAndDirection.gb * 2.0) - 1.0) * vec2(texelWidth, texelHeight);\n\n    float firstSampledGradientMagnitude = texture2D(inputImageTexture, textureCoordinate + gradientDirection).r;\n    float secondSampledGradientMagnitude = texture2D(inputImageTexture, textureCoordinate - gradientDirection).r;\n\n    float multiplier = step(firstSampledGradientMagnitude, currentGradientAndDirection.r);\n    multiplier = multiplier * step(secondSampledGradientMagnitude, currentGradientAndDirection.r);\n\n    float thresholdCompliance = smoothstep(lowerThreshold, upperThreshold, currentGradientAndDirection.r);\n    multiplier = multiplier * thresholdCompliance;\n\n    gl_FragColor = vec4(multiplier, multiplier, multiplier, 1.0);\n}\n```\n\nHere, `texelWidth` and `texelHeight` are the distances between neighboring pixels in the input texture, and `lowerThreshold` and `upperThreshold` set limits on the range of edge strengths we want to examine in this.\n\nAs a last step in the Canny edge detection process, pixel gaps in the edges are filled in to complete edges that might have had a few points failing the threshold or non-maximum suppression tests. This cleans up the edges and helps to make them continuous.\n\nThis last step looks at all the pixels around a central pixel. If the center was a strong pixel from the previous non-maximum suppression step, it remains a white pixel. If it was a completely suppressed pixel, it stays as a black pixel. For middling grey pixels, the neighborhood around them is evaluated. Each one touched by more than one white pixel becomes a white pixel. If not, it goes to black. This fills in the gaps in detected edges.\n\nAs you can tell, the Canny edge detection process is much more involved than Sobel edge detection, but it can yield nice, clean lines tracing around the edges of objects. This gives a good starting point for line detection, contour detection, or other image analysis, and can also be used to produce some interesting aesthetic effects.\n\n## Harris Corner Detection\n\nWhile the previous edge detection techniques can extract some information about an image, the result is an image with visual clues about the locations of edges, not higher-level information about what is present in a scene. For that, we need algorithms that process the pixels within a scene and return more descriptive information about what is shown.\n\nA popular starting point for object detection and matching is feature detection. Features are points of interest in a scene — locations that can be used to uniquely identify structures or objects. Corners are commonly used as features, due to the information contained in the pattern of abrupt changes in lighting and/or color around a corner.\n\nOne technique for detecting corners was proposed by Harris and Stephens in \"A Combined Corner and Edge Detector.\"[^5] This so-called Harris corner detector uses a multi-step process to identify corners within scenes.\n\nAs with the other processes we've talked about, the image is first reduced to luminance. The X and Y gradients around a pixel are determined using a Sobel, Prewitt, or related kernel, but they aren't combined to yield a total edge magnitude. Instead, the X gradient strength is passed along in the red color component, the Y gradient strength in the green color component, and the product of the X and Y gradient strengths in the blue color component.\n\nA Gaussian blur is then applied to the result of that calculation. The values encoded in the red, green, and blue components are extracted from that blurred image and used to populate the variables of an equation for calculating the likelihood that a pixel is a corner point:\n\nR = I<sub>x</sub><sup>2</sup> × I<sub>y</sub><sup>2</sup> − I<sub>xy</sub> × I<sub>xy</sub> − k × (I<sub>x</sub><sup>2</sup> + I<sub>y</sub><sup>2</sup>)<sup>2</sup>\n\nHere, I<sub>x</sub> is the gradient intensity in the X direction (the red component in the blurred image), I<sub>y</sub> is the gradient intensity in Y (the green component), I<sub>xy</sub> is the product of these intensities (the blue component), k is a scaling factor for sensitivity, and R is the resulting \"cornerness\" of the pixel. Alternative implementations of this calculation have been proposed by Shi and Tomasi[^6] and Noble,[^7] but the results tend to be fairly similar.\n\nLooking at this equation, you might think that the first two terms should cancel themselves out. That's where the Gaussian blur of the previous step matters. By blurring the X, Y, and product of X and Y values independently across several pixels, differences develop around corners and allow for them to be detected.\n\nHere we start with a test image drawn from [this question on the Signal Processing Stack Exchange site](http://dsp.stackexchange.com/questions/401/how-to-detect-corners-in-a-binary-images-with-opengl):\n\n![Harris corner detector test image](/images/issue-21/MV-HarrisSquares.png)\n\nThe resulting cornerness map from the above calculation looks something like this:\n\n![Harris cornerness intermediate image](/images/issue-21/MV-HarrisCornerness.png)\n\nTo find the exact location of corners within this map, we need to pick out local maxima (pixels of highest brightness in a region). A non-maximum suppression filter is used for this. Similar to what we did with the Canny edge detection, we now look at pixels surrounding a central one (starting at a one-pixel radius, but this can be expanded), and only keep a pixel if it is brighter than all of its neighbors. We turn it to black otherwise. This should leave behind only the brightest pixels in a general region, or those most likely to be corners.\n\nFrom that, we now can read the image and see that any non-black pixel is a location of a corner:\n\n![Harris corners](/images/issue-21/MV-HarrisCorners.png)\n\nI'm currently doing this point extraction stage on the CPU, which can be a bottleneck in the corner detection process, but it may be possible to accelerate this on the GPU using histogram pyramids.[^8]\n\nThe Harris corner detector is but one means of finding corners within a scene. Edward Rosten's FAST corner detector, as described in \"Machine learning for high-speed corner detection,\"[^9] is a higher-performance corner detector that may also outpace the Harris detector for GPU-bound feature detection.\n\n## Hough Transform Line Detection\n\nStraight lines are another large-scale feature we might want to detect in a scene. Finding straight lines can be useful in applications ranging from document scanning to barcode reading. However, traditional means of detecting lines within a scene haven't been amenable to implementation on a GPU, particularly on mobile GPUs.\n\nMany line detection processes are based on a Hough transform, which is a technique where points in a real-world, Cartesian coordinate space are converted to another coordinate space. Calculations are then performed in this alternate coordinate space and the results converted back into normal space to determine the location of lines or other features. Unfortunately, many of these proposed calculations aren't suited for being run on a GPU because they aren't sufficiently parallel in nature, and they require intense mathematical operations, like trigonometry functions, to be performed at each pixel.\n\nIn 2011, Dubská, *et al.*[^10] [^11] proposed a much simpler, more elegant way of performing this coordinate space transformation and analysis — one that was ideally suited to being run on a GPU. Their process relies on a concept called parallel coordinate space, which sounds completely abstract, but I'll show how it's actually fairly simple to understand.\n\nLet's take a line and pick three points within it:\n\n![An example line](/images/issue-21/MV-ParallelCoordinateSpace.png)\n\nTo transform this to parallel coordinate space, we'll draw three parallel vertical axes. On the center axis, we'll take the X components of our line points and draw points at 1, 2, and 3 steps up from zero. On the left axis, we'll take the Y components of our line points and draw points at 3, 5, and 7 steps up from zero. On the right axis, we'll do the same, only we'll make the Y values negative.\n\nWe'll then connect the Y component points to the corresponding X-coordinate component on the center axis. That creates a drawing like the following:\n\n![Points transformed into parallel coordinate space](/images/issue-21/MV-ParallelCoordinateTransform.png)\n\nYou'll notice that the three lines on the right intersect at a point. This point determines the slope and intercept of our line in real space. If we had a line that sloped downward, we'd have an intersection on the left half of this graph.\n\nIf we take the distance from the center axis and call that u (2 in this case), take the vertical distance from zero and call that v (1/3 in this case), and then label the width between our axes d (6, based on how I spaced the axes in this drawing), we can calculate slope and Y-intercept using the following equations:\n\nslope = −1 + d/u<br>\nintercept = d × v/u\n\nThe slope is thus 2, and the Y-intercept 1, matching what we drew in our line above.\n\nGPUs are excellent at this kind of simple, ordered line drawing, so this is an ideal means of performing line detection on the GPU.\n\nThe first step in detecting lines within a scene is finding the points that potentially indicate a line. We're looking for points on edges, and we want to minimize the number of points we're analyzing, so the previously described Canny edge detection is an excellent starting point.\n\nAfter the edge detection, the edge points are read and used to draw lines in parallel coordinate space. Each edge point has two lines drawn for it, one between the center and left axes, and one between the center and right axes. We use an additive blending mode so that pixels where lines intersect get brighter and brighter. The points of greatest brightness in an area indicate lines.\n\nFor example, we can start with this test image:\n\n![Sample image for line detection](/images/issue-21/MV-HoughSampleImage.png)\n\nAnd this is what we get in parallel coordinate space (I've shifted the negative half upward to halve the Y space needed):\n\n![Hough parallel coordinate space](/images/issue-21/MV-HoughParallel.png)\n\nThose bright central points are where we detect lines. A non-maximum suppression filter is then used to find the local maxima and reduce everything else to black. From there, the points are converted back to line slopes and intercepts, yielding this result:\n\n![Hough transform line detection](/images/issue-21/MV-HoughLines.png)\n\nI should point out that the non-maximum suppression is one of the weaker points in the current implementation of this within GPUImage. It causes lines to be detected where there are none, or multiple lines to be detected near strong lines in a noisy scene.\n\nAs mentioned earlier, line detection has a number of interesting applications. One particular application this enables is one-dimensional barcode reading. An interesting aspect of this parallel coordinate transform is that parallel lines in real space will always appear as a series of vertically aligned dots in parallel coordinate space. This is true no matter the orientation of the parallel lines. That means that you could potentially detect standard 1D barcodes at any orientation or position by looking for a specific ordered spacing of vertical dots. This could be a huge benefit for blind users of mobile phone barcode scanners who cannot see the box or orientation they need to align barcodes within.\n\nPersonally, the geometric elegance of this line-drawing process is something I find fascinating and wanted to present to more developers.\n\n## Summary\n\nThese are but some of the many machine vision operations that have been developed in the last few decades, and only a small portion of the ones that can be adapted to work well on a GPU. I personally think there is exciting and groundbreaking work left to be done in this area, with important applications that can improve the way of life for many people. Hopefully, this has at least provided a brief introduction to the field of machine vision and shown that it's not as impenetrable as many developers believe.\n\n[^1]: I. Sobel. An Isotropic 3x3 Gradient Operator, Machine Vision for Three-Dimensional Scenes, Academic Press, 1990.\n\n[^2]: J.M.S. Prewitt. Object Enhancement and Extraction, Picture processing and Psychopictorics, Academic Press, 1970.\n\n[^3]: J. Canny. A Computational Approach To Edge Detection, IEEE Trans. Pattern Analysis and Machine Intelligence, 8(6):679–698, 1986.\n\n[^4]: A. Ensor, S. Hall. GPU-based Image Analysis on Mobile Devices. Proceedings of Image and Vision Computing New Zealand 2011.\n\n[^5]: C. Harris and M. Stephens. A Combined Corner and Edge Detector. Proc. Alvey Vision Conf., Univ. Manchester, pp. 147-151, 1988.\n\n[^6]: J. Shi and C. Tomasi. Good features to track. Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, pages 593-600, June 1994.\n\n[^7]: A. Noble. Descriptions of Image Surfaces. PhD thesis, Department of Engineering Science, Oxford University 1989, p45.\n\n[^8]: G. Ziegler, A. Tevs, C. Theobalt, H.-P. Seidel. GPU Point List Generation through HistogramPyramids. Research Report, Max-Planck-Institut fur Informatik, 2006.\n\n[^9]: E. Rosten and T. Drummond. Machine learning for high-speed corner detection. European Conference on Computer Vision 2006.\n\n[^10]: M. Dubská, J. Havel, and A. Herout. [Real-Time Detection of Lines using Parallel Coordinates and OpenGL](http://medusa.fit.vutbr.cz/public/data/papers/2011-SCCG-Dubska-Real-Time-Line-Detection-Using-PC-and-OpenGL.pdf). Proceedings of SCCG 2011, Bratislava, SK, p. 7.\n\n[^11]: M. Dubská, J. Havel, and A. Herout. [PClines — Line detection using parallel coordinates](http://medusa.fit.vutbr.cz/public/data/papers/2011-CVPR-Dubska-PClines.pdf). 2011 IEEE Conference on Computer Vision and Pattern Recognition (CVPR), p. 1489- 1494.\n"
  },
  {
    "path": "2015-02-10-how-your-camera-works.md",
    "content": "---\ntitle:  \"How Your Camera Works\"\ncategory: \"21\"\ndate: \"2015-02-10 11:00:00\"\ntags: article\nauthor:\n  - name: Daniel Eggert\n    url: https://twitter.com/danielboedewadt\n---\n\n\n\nClick! Your camera just turned photons into bits. You took a photo with your iPhone.\n\nLet's say you're outside and looking around you. The sun, 150 million kilometers away, is emitting [photons](http://en.wikipedia.org/wiki/Photon). These travel from the sun to our cozy planet in about eight minutes. Some of these photons hit the things around you and get reflected and hit the retina inside your eyes, and this triggers your brain to create an image, a visual representation of your surroundings.\n\n[Photography](https://en.wikipedia.org/wiki/View_from_the_Window_at_Le_Gras) is about capturing that image. It was invented some 200 years ago. And even before then, for thousands upon thousands of years, humans tried to capture what they saw by making [drawings](https://en.wikipedia.org/wiki/Cave_painting).\n\nMost of us carry a camera with us almost every minute of the day; today's smartphones are some of the [most used cameras](http://petapixel.com/assets/uploads/2015/01/rankings.jpg). Before the era of digital photography, photography would record light onto paper or film. Today, photography turns light into bits and bytes.\n\nThis article will go through some of what's making this happen — how a smartphone camera works.\n\n\n\n<a name=\"ShutterSpeedApertureISO\"></a>\n## Shutter Speed, ISO, Aperture\n\nBefore we dive into the magic of turning photons into a JPEG file, we will take a look at some of the general concepts of how a photo comes to life. These concepts are as true today as they were back in the days of film photography. Not so long ago, almost all photography was done with film. A biochemical process affected by light captured the image, while today we're using a digital image sensor. But since everything else having to do with taking pictures is based on the same principles, a lot of what was true for film photography with [bulky film-based cameras](http://en.wikipedia.org/wiki/120_film) still applies when we're shooting images with an iPhone.\n\n### Quantities of Light\n\nThe process of capturing a single image is sometimes referred to as an exposure. [Exposure](http://en.wikipedia.org/wiki/Exposure_%28photography%29) also refers to the amount of light per unit area. This amount of light needs to be within a certain range. If we don't capture enough light, the image will be *underexposed* — the image is drowning in the inherent noise floor of the image sensor or film. If we capture too much light, the image will be *overexposed* — the image sensor/film is too saturated and can no longer differentiate between different amounts of light, meaning all areas will seem to have the same exposure.\n\nWhen taking a photo, we must adjust the camera in such a way that the amount of light is sufficiently high but not too high. Here are samples of the same scene, underexposed and overexposed. The right-hand side shows the same image with the exposure adjusted with Pixelmator. This is to show that there is no way to fix an image once it is severely overexposed or underexposed:\n\n| unadjusted | \"adjusted\" |\n|------------|------------|\n| ![underexposed](/images/issue-21/underexposed.jpg) | ![underexposed](/images/issue-21/underexposed_adjusted.jpg) |\n| ![overexposed](/images/issue-21/overexposed.jpg) | ![overexposed](/images/issue-21/overexposed_adjusted.jpg) |\n\nIn the underexposed image, even after trying to make it brighter, the dark regions of the image are \"stuck\" as black, and there is no way to make out that the pens in the image actually have different colors. The overexposed image has large regions that are stuck at the same level of white/gray. Note how the pattern on the fabric band and the coins is completely lost.\n\n### Stops\n\nThere are three things that affect the amount of light of an exposure: shutter speed, ISO value, and aperture. We will go through these in a bit.\n\nIn photography, a change to any one of these three which either doubles or halves the amount of light is called “one stop.” For each of these three (shutter speed, ISO, aperture), a single stop corresponds to a different numerical change. But if we adjust the shutter speed by one stop, we need to compensate by adjusting either the ISO or the aperture by exactly one stop, too, in order to get the same amount of light. We'll look at this in detail shortly.\n\nThe tricky part is that all three (shutter speed, ISO, and aperture) also affect other aspects of the exposure. And there are countless combinations of these three parameters that result in the same amount of light. Let's take a closer look.\n\n<a name=\"ShutterSpeed\"></a>\n### Shutter Speed\n\nWhen we capture an image, the image sensor captures light for a specific amount of time. This duration is called the shutter speed, because it describes how fast the shutter opens and closes.\n\nA shutter speed of e.g. 1/50 second lets the image sensor capture light for 1/50 s (= 0.02 s or 20 ms). If we change the shutter speed to 1/25 s (40 ms), the image sensor will capture light for twice as long, and it will capture twice the amount of photons, i.e. twice the amount of light.\n\nA so-called *stop* for the shutter speed either doubles or halves the shutter speed. Going from 1/50 s to 1/25 s is an adjustment by one stop.\n\nThe iPhone 6 can adjust the shutter speed from 1/8000 s up to 1/2 s. We can change the shutter speed to adjust the amount of light, but it will also affect motion blur of the image. If the camera and the scene that we're taking a photo of are completely still, we can make the exposure time (shutter speed) arbitrarily long, but more often than not, things around us are moving. And particularly with an iPhone, the camera itself is also moving.\n\nWhen things move fast enough such that they will not be at a single spot on our image sensor during the entire time of the exposure, the resulting image will be [blurred](http://en.wikipedia.org/wiki/Motion_blur). We generally want things to be sharp and without blur, so we often need a shutter speed around 1/100 s or faster/shorter. For action shots of things that move at a fast speed, we may have to choose an even faster shutter speed. But we can also intentionally use a long shutter speed to blur things and show their motion. This works best when the camera is fixed on a tripod or something similar to make sure stationary objects in the scene are still sharp.\n\n<a name=\"ISO\"></a>\n### ISO\n\nThe ISO value is also called the [film speed](http://en.wikipedia.org/wiki/Film_speed). It's a measure of how sensitive the image sensor is to light, and hence how noisy the exposure will be. The exact details are obviously a lot more complicated, and Wikipedia will give you a more detailed explanation.\n\nThe iPhone 6 can adjust the ISO of its camera from ISO 32 up to ISO 1600. A *stop* corresponds to either doubling or halving the ISO value. Every time we double the ISO (e.g. from ISO 100 to ISO 200) we only need half the amount of light. We *pay* for this by increasing the amount of noise in the photo.\n\nAt ISO 32, the images off the iPhone sensor will have the least amount of noise, but also need the most amount of light. At ISO 1600, the iPhone's image sensor will only need 1/50 (0.02) the amount of light, but the image will be a lot more noisy.\n\nHere are two samples of the same scene taken with the iPhone 6 mounted on a tripod. The images show a small cutout of the full image. The left one is taken at ISO 32 and exposed for 1/3 s. The right one is taken at ISO 1600 and exposed for 1/180 s. The amount of light captured is about the same, but the left one has way less noise than the right one. Without the tripod, we wouldn't have been able to take a sharp photo with an exposure time of 1/3 s, though:\n\n| ISO 32 | ISO 1600 |\n|--------|----------|\n| [![ISO 32, 1/3 s](/images/issue-21/1-3s_ISO_32.jpg)](/images/issue-21/1-3s_ISO_32.jpg) | [![ISO 1600, 1/180 s](/images/issue-21/1-180s_ISO_1600.jpg)](/images/issue-21/1-180s_ISO_1600.jpg) |\n\n\n\n<a name=\"Aperture\"></a>\n### Aperture\n\nFinally, the aperture of a camera (more specifically of the camera's lens) is a measure of how large the opening is through which light reaches the image sensor. The aperture is specified as an [f-number](http://en.wikipedia.org/wiki/F-number), such as ƒ/5.6 — the number 5.6 is the ratio of the focal length to the effective diameter of the aperture (the opening).\n\nThe f-numbers can be a bit confusing. A *stop* with f-numbers corresponds to a multiplication or division by the square root of 2 (√₂ ≃ 1.4). At ƒ/4, we will get half the light that we'd get at ƒ/2.8.\n\nWhen using the iPhone camera, things are a lot easier because the aperture is fixed. An iPhone 6 has a fixed aperture of ƒ/2.2.\n\nIn addition to the amount of light, aperture also affects the [depth of field](http://en.wikipedia.org/wiki/Depth_of_field). This relates to [focus](#Focus). The optical system in a camera will render things sharply within a range of distances away from the camera. As we change the aperture, this range becomes wider or narrower. We can use this to achieve nice effects. But sadly, we cannot adjust the aperture on the iPhone.\n\n\n### Combining\n\nOn the iPhone, we can only adjust the ISO and the shutter speed. We can hence trade noise (affected by the ISO) against motion blur/sharpness while maintaining the same level of exposure.\n\nThat explains why photos at night often look worse than those taken during the day: At night there's a lot less light. In order to still have an acceptable shutter speed, the auto exposure will bump up the ISO, probably to the maximum of what the camera allows. And even that may not be enough to achieve enough light, so the auto exposure will also lower the shutter speed. This combination results in more noise in the image, and the image being blurred.\n\nSome [iOS apps](http://campl.us/) let you adjust the exposure manually. This can be done by either adjusting the [EV/exposure value](http://en.wikipedia.org/wiki/Exposure_value), which will still use the auto exposure logic of the camera, but lets you adjust the desired level of exposure. An EV value of −1 will result in an image that is one stop underexposed, according to the auto exposure logic. The auto exposure logic will still automatically pick some combination of ISO and shutter speed (since the aperture is fixed on the camera).\n\nAnother option for adjusting the exposure is [shutter priority](http://en.wikipedia.org/wiki/Shutter_priority) (often denoted with an *S*). This lets us directly set the desired shutter speed, while the auto exposure will try to compensate for the exposure by automatically adjusting the ISO.\n\nFinally, we can do a fully manual exposure (often denoted with an *M*) by adjusting both the shutter speed and the ISO. When doing this, it's often convenient to use auto exposure first, and use the values it picks as a starting point.\n\nCameras that allow you to adjust the aperture have something called [aperture priority](http://en.wikipedia.org/wiki/Aperture_priority) (often denoted with an *A*), which is conceptually the same as shutter priority, only that we manually control the aperture and let the auto exposure logic pick the shutter speed (and ISO level, unless it is fixed).\n\nThere are several strategies for picking a good auto exposure. iOS has an auto exposure logic that looks at the overall image and makes a guess as to what you want to be exposed well. If parts of the image are very bright and others very dark, the camera cannot expose everything well. The auto exposure has to pick something, and if it, for example, finds faces, it will pick those. The built-in iOS camera app lets you tap on something in the image to hint to the auto exposure that this part of the image should be exposed well. That will cause the auto exposure to adjust itself. The camera app also lets you adjust the exposure value by dragging up or down on the screen. But to explicitly set the shutter speed and/or ISO, you need to use a different app.\n\n<a name=\"Focus\"></a>\n## Focus\n\nA camera can only render sharply items that are within a certain range of distances from the camera. Items in the range are *in focus*. Items too close or too far away will be blurred; they're *out of focus*.\n\nMost cameras, including the iPhone camera, have autofocus (AF). The camera will make a guess as to which part of the image should be in focus and adjust its focus accordingly. The built-in iOS camera app allows users to tap on something to make it focus on that part of the image — and some apps even allow users to manually set the focus.\n\n\n<a name=\"Optics\"></a>\n## Optical Components\n\nThe camera lens consists of multiple optical components. These direct and focus the light. When changing the focus of the camera, this is done by physically moving parts in the lens.\n\nModular cameras, such as [SLRs](http://en.wikipedia.org/wiki/Single-lens_reflex_camera), let you switch between different lenses. And even fixed lenses, such as the one in the iPhone, let you adjust the optical part of the camera by clipping external lenses on in front of the built-in one.\n\nThe key property of a lens system is its [focal length](http://en.wikipedia.org/wiki/Focal_length) — its magnification or angle of view. A [wide-angle lens](http://en.wikipedia.org/wiki/Wide-angle_lens) has low magnification, and lets the camera see a large area. A [long-focus lens](http://en.wikipedia.org/wiki/Long-focus_lens), particularly a [telephoto lens](http://en.wikipedia.org/wiki/Telephoto_lens), has a narrow angle of view, and it shows a small fraction of what a wide-angle lens would, due to its magnification.\n\nThe lens affects other aspects of the image too. It can introduce various unwanted [distortions](http://en.wikipedia.org/wiki/Distortion_(optics)) to the captured image, which affect both geometry and color (c.f. [chromatic aberration](http://en.wikipedia.org/wiki/Chromatic_aberration)).\n\n\n<a name=\"Sensor\"></a>\n## Bucket Full of Light\n\nNow we know how the basics work. But how does the camera actually capture an image?\n\nInside the camera of your iPhone, there's an image sensor. This is the part that's equivalent to the retina in our eyes. The image sensor converts light or photons into an electrical signal.\n\nThe image sensor consists of a huge number of individual pixel sensors that are lined up into a huge rectangle. We can think of each pixel sensor as a charge bucket. As photons hit the photodiode of the pixel sensor, they slowly build up a charge in that pixel's bucket. As a result, each pixel has its own tiny bucket of electrons. The charge, in turn, depends on the number of photons — and ultimately the intensity of the light hitting that particular spot.\n\nSince we have a two-dimensional array of these pixel sensors, we now have a two-dimensional array of charges that reflect the intensity of the light at all these positions. On the iPhone 6, there are eight million of these tiny pixel sensors with their corresponding charge buckets.\n\nAt this point, we need two things: First, we need to be able to reset the charge. Second, we need to be able to read out the charges once the pixel sensors have been exposed to light. The reset can be done globally for all pixels. But as for the eight million small charges, we want to be able to turn them into voltage levels individually.\n\nDigital cameras usually shift out rows of pixels. The image sensor will read the charge of the first electron bucket in the row, and then all buckets transfer their charge to the adjacent one. The first electron bucket now holds the charge that was in the second, which can now be read. Repeating this, one by one, all pixel values are read off the sensor's row.\n\nThe bucket or pixel sensor that is being read will have its value converted to a digital value through an [analog-to-digital converter](https://en.wikipedia.org/wiki/Analog-to-digital_converter) (ADC). The output of the ADC is a digital number for each pixel sensor corresponding to the amount of light that hit it. Finally, these values are then passed on to a digial image processor. We'll talk a bit more about the image processing below.\n\n\n### Pixel Size Matters\n\nWith this, we can already start to understand why the number of megapixels don't matter for image quality. Or rather: what's very important for the quality of the image is the size of the individual pixel. These pixel sensors are tiny. On an iPhone 6, they're 1.5 µm ([microns or micrometers](https://en.wikipedia.org/wiki/Micrometre)) on each side. On prosumer DSLR cameras, they can be as large as 9 µm on each side.\n\nTwo things happen as the size increases. First, the larger the pixel, the more light will hit it, and the more charge will build up. The more charge we have, the lower the noise in the readout. Imagine you're listening to music next to a busy street. If all you have is the built-in speaker of your phone, you may barely be able to make out the music. If you have a large stereo set, the noise of the street will disappear. The same for the charge in the pixels vs. the noise in the image sensor. Larger pixels are good. A 9 µm image pixel will capture 36 times as many photons as a 1.5 µm pixel.\n\nThe second thing is that larger pixels are less affected by bleed. The image sensor is a [semiconductor](https://en.wikipedia.org/wiki/Semiconductor) made out of [silicon](https://en.wikipedia.org/wiki/Silicon), just like the CPU and RAM. As light hits the sensor, it will, to some extent, bleed into neighboring pixels in a similar fashion to light hitting frosted glass. As pixels get smaller and smaller, the amount of light bleeding into neighboring pixels increases: the value for each pixel is affected more and more by the light that is actually hitting its neighboring pixels, but bleeds into this pixel.\n\n\n\n<a name=\"Shutter\"></a>\n### Shutter\n\nFilm cameras use a mechanical [shutter](https://en.wikipedia.org/wiki/Shutter_%28photography%29), a delicate mechanism that would open in front of the film, and then close after the time specified by the [shutter speed](#ShutterSpeed) has expired. Larger digital cameras still use mechanical shutters, but smartphones and other small digital cameras use an electronic shutter.\n\nMany of these, including iOS devices, use a so-called [rolling shutter](https://en.wikipedia.org/wiki/Rolling_shutter), which reads out image data line by line. Since lines are not read out at the same time, but in turn, this can lead to odd artifacts in images when objects in the scene move at a fast speed. Some of these are [quite funny](https://www.flickr.com/photos/sorenragsdale/3192314056/).\n\n\n\n<a name=\"Color\"></a>\n## Color\n\nNow we know how the iPhone measures how much light hits each pixel. But this would only result in a black and white photo. Color photos require additional technologies. Before we dive into those, let's take a look at what color is. We will sidetrack a bit to scratch the surface of what is known as color science.\n\nIt may seem too obvious that a deep green forest is deep green, and that a bright yellow bike is bright yellow. But what is this thing called “color?” When working with computers, we might be tempted to answer that a particular color is just a combination of certain amounts of red, green, and blue. But in reality, things are more complicated.\n\nSome people ([CIE](https://en.wikipedia.org/wiki/International_Commission_on_Illumination)) try to define color, but end up with confusing words like these:\n\n> Color is the attribute of visual perception consisting of any combination of chromatic and achromatic content. This attribute can be described by chromatic color names such as yellow, orange, brown, red, pink, green, blue, purple, etc., or by achromatic color names such as white, grey, black, etc., and qualified by bright, dim, light, dark or by combinations of such names.\n>\n> Note: Perceived color depends on the spectral distribution of the color stimulus, on the size, shape, structure and surroundings of the stimulus area, on the state of adaptation of the observer’s visual system, and on the person’s experience of prevailing and similar situations of observation.\n\nThey define color recursively by referring to color itself, or *chromatic*, which is just another word for color.\n\n### Visual Perception\n\nThe important takeaway from the above is: \"Color is a visual perception.\" Someone has to be looking at the thing for there to be color. Color doesn't exist outside our perception. You need a light source and something that reflects this light. And that light then has to be seen in order for there to be color.\n\nNewton first discovered that light is a spectrum. Light consists of wavelengths roughly in the range of 380-720 nm. This is visible light. But we see light at different wavelengths differently.\n\nThe human eye has photo detectors. Some of these are so-called cones. There are three different *kinds* of cones: S, M, and L. Each kind responds differently to different wavelengths of light. These cones are sometimes referred to as red, green, and blue photoreceptors, although that's not really true. It would be more accurate to call them reddish, slightly less reddish, and blueish. As this graph shows, there's a quite a bit of overlap between their response curves:\n\n[![Simplified human cone response curves. Vanessaezekowitz at en.wikipedia [GFDL (http://www.gnu.org/copyleft/fdl.html), CC BY 3.0-2.5-2.0-1.0 (http://creativecommons.org/licenses/by/3.0-2.5-2.0-1.0) or CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons](/images/issue-21/Cones_SMJ2_E.svg)](http://commons.wikimedia.org/wiki/File%3ACones_SMJ2_E.svg)\n\nA light source such as a lightbulb has a certain spectrum of light — a certain intensity at each wavelength. An object, e.g. a bike's frame, will in turn reflect or absorb different wavelengths with different intensities. For each cone, we can multiply (in fact, integrate over the wavelength) the light source's spectrum with the object's reflectance spectrum and the cone's response curve. For each cone, that will result in a single value. The resulting S, M, and L cones' three *stimulus* values are the perceived color. Our brain interprets this combination of the values for reddish, slightly less reddish, and blueish cones as a color. The perceived color, however, does not only depend on these three values, but also spatial and temporal information.\n\n### Specifying a Color\n\nWe now know how color is formed, but how can we specify a given color? What if we want to describe a particular red that a bicycle has?\n\nIt turns out, we need something called a color space for that. We can think of a color space as a unit of measurement.\n\nWhen someone asks \"How fast can an elephant run?\", an answer like \"18\" or \"2.8\" would not make sense. We need to qualify it with a unit of measurement such as \"18 km/h.\" The same goes for a color. When we ask: \"What color is this bicycle?\", we similarly need a \"unit of measurement\" in order to quantify our answer. This unit of measurement is a color space.\n\nWhile the details are complicated, the main concept of color spaces is quite easy to understand: If we use three sources of light — a red, a green, and a blue source of light — we can mix these together to yield various colors of light. If we want to match, say, the color of a banana that's reflecting light from our desk lamp, we might end up with the values 10, 8, and 4. The color of the sky may end up being 4, 8, and 10. These values depend on exactly which light sources (which primaries) we picked for our three light sources that we're dialing in to give us those three values: 4, 8, and 10. If we had picked a different set of lamps (i.e. primaries), the values 11, 9, and 2 may have resulted in exactly the same color. The primaries we pick define our color space.\n\nWhen asked \"What color is this bicycle?\", we can say that with a specific set of primary light sources, these have to be adjusted to 23 percent, 45 percent, and 53 percent intensity. Someone else with the same set of primaries can then recreate that color.\n\nThe de facto default color space used by modern computers, and iOS, is the [sRGB](https://en.wikipedia.org/wiki/SRGB) color space. Its three primaries are defined in [ITU-R BT.709](http://www.itu.int/rec/R-REC-BT.709/en). There are different ways to define a color space than through three primaries. In the end, though, the concept remains the same.\n\nIt is worth pointing out that most color spaces can only represent a subset of the visible colors. Let's take a look at the diagram of the [sRGB](https://en.wikipedia.org/wiki/SRGB) color space:\n\n[![The CIE 1931 color space. By CIExy1931.svg: Sakurambo derivative work: GrandDrake (CIExy1931.svg) [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0), CC BY-SA 2.5-2.0-1.0 (http://creativecommons.org/licenses/by-sa/2.5-2.0-1.0) or GFDL (http://www.gnu.org/copyleft/fdl.html)], via Wikimedia Commons](/images/issue-21/CIExy1931_Rec_709.svg)](http://commons.wikimedia.org/wiki/File%3ACIExy1931_Rec_709.svg)\n\nThe horseshoe-shaped diagram represents all colors visible to the human eye. The black triangle shows the colors that can be represented by the [sRGB](https://en.wikipedia.org/wiki/SRGB) color space. The three corners are the primaries of the color space, i.e. the colors that consist of just one of the primaries at full intensity. The colors outside the triangle cannot be represented by the sRGB color space, even though the human eye can perceive them. And on a side note: All the colors outside the triangle in this diagram appear to be very similar to those on the edge of the triangle. That's because the *real* colors outside the triangle cannot be represented by the sRGB color space that the image file is using. It can only represent colors within the sRGB space and has to fall back to the one closest to the real color.\n\nWhen we pick a color using hex values, say `#dde834`, or use UIKit's API `UIColor(red:0.867, green:0.910, blue:0.204, alpha:1.000)`, we implicitly say 86.7 percent of the sRGB red primary, 91 percent of the sRGB green primary, and 20.4 percent of the sRGB blue primary.\n\nOne of the reasons to have RGB color spaces other than sRGB is that some of these have a larger gamut, i.e. can represent more of the visible colors. An example of a color space that is not based on RGB primaries is the [CIELUV color space](https://en.wikipedia.org/wiki/CIELUV) (aka. CIELAB). It also has three components: an *L* component for lightness, and *u* and *v* components (sometimes *a* and *b*) for the *color-opponent dimensions*.\n\nBe sure to check out the [Wikipedia article on color spaces](https://en.wikipedia.org/wiki/Color_space) for more information. And play around with the [ColorSync Utility.app](file:///Applications/Utilities/ColorSync%20Utility.app) that's part of OS X.\n\n\n\n<a name=\"WhiteBalance\"></a>\n### White Is Not White\n\nOne additional thing that makes color tricky is that our brains do a lot of processing to make things look \"right\" — a huge part of this is what's referred to as white balancing. We all know what white or gray is. When we see something as gray, it's, more often than not, anything but gray. But our brains \"figure out\" that due to the lighting conditions, it ought to be gray. Hence we see it as such.\n\nThe light reflected from a white building will be vastly different in the light of the morning sun than in the evening. But our brains figure out that the building did not turn from one color into another. This may seem obvious, because we're used to our brains doing this all the time.\n\nOur brains are capable of chromatic adaption — it is color balancing for us by independently regulating the sensitivity of each cone type. In addition to that, our brains pull some very complicated tricks beyond just simple adjustments. How the brain interprets the color signals from the cones depends on local, spatial, and temporal effects. [Akiyoshi's illusion pages](http://www.ritsumei.ac.jp/~akitaoka/index-e.html) show some mind-boggling examples of what our brains are up to. When it comes to photos and color, the important takeaway is that a camera cannot simply capture the color it *sees*, because that would look very wrong to us.\n\nWhen we take a photo with a camera, the camera is not as clever as our brains are. But cameras have an automatic white balance algorithm that tries to figure out what the neutral color/gray in the scene is. The camera's image processor will then try to adjust all colors in the photo accordingly. Sometimes this works, sometimes it fails. When it fails, it's most often due to strange lighting conditions, for example, if part of the scene is lit by one kind of light, and another lit by a different kind of light source.\n\nWith all of this in mind, let's move on and take a look at how our digital cameras *see* these colors.\n\n\n\n<a name=\"Bayer\"></a>\n## The Digital Color Sensor\n\nThe pixel sensors in and of themselves do not differentiate between different wavelengths. By putting color filters in front of them, the pixel sensors will have distinct response curves based on the wavelength of the light.\n\nIf we use green, red, and blue color filters, some pixel sensors will only receive green light, some will only receive red light, and some only blue light (in reality, they'll have response curves similar to how our eyes' cones do). Today, cameras almost exclusively use a so-called [Bayer filter](https://en.wikipedia.org/wiki/Bayer_filter). With such a filter, there are twice as many green pixel sensors as there are red or blue. It looks like this:\n\n[![A bayer pattern on a sensor in isometric perspective/projection. By en:User:Cburnett [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or GPL (http://www.gnu.org/licenses/gpl.html)], via Wikimedia Commons](/images/issue-21/Bayer_pattern_on_sensor.svg)](http://commons.wikimedia.org/wiki/File%3ABayer_pattern_on_sensor.svg)\n\nThe digital camera now has color information, but it also looses a lot of information. It only knows what the level of red is for 1/4 of all pixels. Likewise for blue. For green, it knows what the level is for 1/2 of all pixels. In other words: 66 percent of the data is missing. The process of recreating full color information for all pixels is called debayering. This involves advanced algorithms which will interpolate the existing information into a full-resolution color image.\n\nA simple interpolation would cause a lot of artifacts, and over the years, more and more advanced methods have been developed by various debayering algorithms to work around these. As an example of a problem, most debayering algorithms rely heavily upon the green pixels for luminosity. As a result, almost all debayering algorithms perform poorly in areas that are mostly red. Here's a cutout of an image taken with an iPhone 6:\n\n[![iPad cover and booklet](/images/issue-21/red_debayer.jpg)](/images/issue-21/red_debayer.jpg)\n\nNote how the letters C and T of the large “PRODUCT” written on the inside of the iPad cover are extremely fuzzy compared to the black text in the booklet. It seems like the denoise part of the debayering is getting confused by the lack of green information in the part of the image that's the iPad cover.\n\n\n\n### Compensating for Imperfections\n\nAside from recreating color information, the image processor inside the camera will also do a handful of other adjustments to the image.\n\nAs image sensors have more and more pixels, the likelihood of some of these pixels being defect increases. Quite often, the camera will have a list of pixels (sometimes entire rows) which are misbehaving. During the image processing of the raw image data, the image processor will fix up any dead pixels.\n\nImage sensors have pixels that are outside the area that receives visible light. These image sensor pixels will always be black. Their readout value, however, is not 0. But these pixels allow the image processor to adjust the image's *black level* by subtracting the value of those pixels from the ones that contain the actual image. This way, the image processor can also compensate for most inter-row variations in the image sensor (and/or ADC).\n\n\n### File Formats\n\nThe final step of a digital camera is to write the image data to a file. In just about all cases, digital photos are saved as JPEGs. [JPEG compression](https://en.wikipedia.org/wiki/JPEG) modifies the image data in order to heavily compress it. The pixel data in an iPhone 6 image takes up almost 23 MB (3.264 x 2.448 x 3 = 23.970.816), while the JPEG files for such an image are typically 1.5 to 2.5 MB. Read more about JPEG compression in [issue #3](/issues/3-views/moving-pixels-onto-the-screen/#JPEG).\n\nSome digital cameras allow the user to save a so-called RAW file. The camera's image processor will still have done some work on the raw image data coming off the sensor, but the file will contain something very close to the real pixel values. The benefit of this is that we can do the debayering at a later point.\n\nFor example, the debayering built into OS X is more powerful than that of most [DSLRs](https://en.wikipedia.org/wiki/Digital_single-lens_reflex_camera). Working on a RAW file of a [supported camera](https://www.apple.com/aperture/specs/raw.html) allows us to make more adjustments to the images without losing image quality, as we would when working on the data contained in the JPEG file. The RAW image processing of Core Image will apply various user-defined adjustments to the image as part of the debayering. When working on JPEG images, adjustments can only be done after the fact.\n\nCheck out `CoreImage/CIRAWFilter.h` in the OS X SDK, and [WWDC 2014 session 514](https://developer.apple.com/videos/wwdc/2014/#514) at 32:40 for more details.\n\n## Final Words\n\nDigital cameras of today are the result of decades of research and engineering. We've barely scratched the surface of all the technologies involved. But with this, we hope you will be able to get more out of your (iPhone) camera... and make better photo apps for iOS and OS X.\n"
  },
  {
    "path": "2015-02-10-image-formats.md",
    "content": "---\ntitle:  \"Image Formats\"\ncategory: \"21\"\ndate: \"2015-02-10 10:00:00\"\ntags: article\nauthor:\n  - name: Oliver Mason\n    url: https://twitter.com/ojmason\n---\n\n\n## Storing Data\n\nStoring text on a computer is easy. We have letters/characters as a fundamental unit, and there is a fairly straightforward mapping from a number to the character it encodes. This is not the case with graphical information. There are different ways to represent images, all with different pros and cons.\n\nText is also linear, and one-dimensional. Leaving aside the question of what the text direction is, all we need is to know what the next item in the sequence is.\n\nImages are more complicated. For a start, they are two-dimensional, so we need to think about identifying where in our image a particular value is. Then, we need to consider what a value is. Depending on what we want to capture, there are different ways of encoding graphical data. The most intuitive way these days seems to be as bitmap data, but that would not be very efficient if you wanted to deal with a collection of geometrical figures. A circle can be represented by three values (two coordinates and the radius), whereas a bitmap would not only be much larger in size, but also only a rough approximation.\n\nThis, then, leads us to the first distinction between different image formats: bitmaps versus vector images. While bitmaps store values in a grid, vector formats store instructions for drawing an image. This is obviously much more efficient when dealing with sparse images that can be reduced to a few geometric shapes; it does not really work well for photographic data. An architect designing a house would want to use a vector format. Vector formats do not have to be restricted to line drawings, as gradient or pattern fills can also be represented, so a realistic rendering of the resulting house could still be produced from a line drawing fairly efficiently.\n\nA brick pattern would be more easily stored as a bitmap, so in this case we might have a hybrid format. An example for a very common hybrid format is [PostScript](https://en.wikipedia.org/wiki/PostScript), (or the nowadays more popular follow-up, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format)), which is basically a description language for drawing images. The target is mainly paper, but NeXT and Adobe developed [Display PostScript](http://en.wikipedia.org/wiki/Display_PostScript) as an instruction set for drawing on screens. PostScript is also capable of placing letters, and even bitmaps, which makes it a very versatile format.\n\n### Vector Images\n\nA big advantage of vector formats is scaling. As an image is a set of drawing instructions, it is generally independent of size. If you want to enlarge your circle, you simply scale up the radius before drawing it. With a bitmap, this is not easily possible. For starters, scaling to any factor that is not a multiple of two involves mangling the image, and the individual elements would simply increase in size, leading to a blocky image. As we do not know that the image is a circle, we cannot smooth the circular lines properly, and it will not look as good as a line drawn to scale. This is why vector images are very useful as graphical assets on devices with different pixel density. The same icon that looks OK on a pre-Retina iOS device will not look as crisp when scaled up twice for display on a Retina iPhone — just like an iPhone-only app does not look as good when run in 2x mode on an iPad.\n\nThere is support for PDF assets in Xcode 6, but it seems to be rather sketchy at the moment, and still creates bitmap images at compile time on iOS. The most common vector image format is [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics), and there is a library for rendering SVG files on iOS, [SVGKit](https://github.com/SVGKit/SVGKit).\n\n### Bitmaps\n\nMost image work deals with bitmaps, and from now on, we will focus on how they can be handled. The first question is how to represent the two dimensions. All formats use a sequence of rows for that, whereas pixels are stored in a horizontal sequence. Most formats then store rows sequentially, but that is by no means the only way — interleaved formats, where the rows are not in strict sequential order, are also common. Their advantage is that a better preview of the image can be shown when it is partially loaded. With increasing data transmission speeds, that is now less of an issue than it was in the early days of the web.\n\nThe simplest way to represent bitmaps is as binary pixels: a pixel is either on or off. We can then store eight pixels in a byte, which is very efficient. However, we can only have two colors, one for each state of a bit. While this does not sound very useful in the age of millions of colors, there is one application where this is still all that is needed: masking. Image masks can, for example, be used for transparency, and in iOS they are used in tab bar icons (even though the actual icons are not one-pixel bitmaps).\n\nFor adding more colors, there are two basic options: a look-up table, or actual color values. [GIF](https://en.wikipedia.org/wiki/GIF) images have a color table (or a palette), which can store up to 256 colors. The values stored in the bitmap are indices into this look-up table, which specifies their respective color. As a result, GIFs are limited to 256 colors only. This is fine for simple line drawings or diagrams with filled colors, but not really enough for photos, which require a greater color depth. A further improvement is that of [PNG](https://en.wikipedia.org/wiki/Portable_Network_Graphics) files, which can either use a palette or separate channels, both supporting a variety of color depths. In a channel, the color components of each pixel (red, green, and blue [RGB], sometimes adding opacity/alpha [RGBA]) are specified directly.\n\nGIF and PNG are best for images with large areas of identical color, as they use compression algorithms (mainly based on run-length encoding) to reduce the storage requirements. The compression is lossless, which means that the image quality is not affected by the process.\n\nAn example for an image format that has lossy compression is [JPEG](https://en.wikipedia.org/wiki/JPEG). When creating JPEG images, it is often possible to specify a parameter for the quality/compression ratio, and here, a better compression leads to a deterioration in the image quality. JPEG is not suited for images with sharp contrasts (such as line drawings), as the compression leads to artifacts around such areas. This can clearly be seen when a screenshot containing rendered text is saved in JPEG format: the resulting image will have stray pixels around the letters. However, this is not a problem with most photos, and photos are the main use case for JPEGs.\n\nTo summarize: for scalability, vector formats (such as SVG) are best. Line drawings with sharp contrast and limited amount of colors work best for GIF or PNG (where PNG is the more powerful format), and for photos, you should use JPEG. Of course, these are not unbreakable laws, but generally lead to the best results in terms of quality/image size.\n\n\n\n## Handling Image Data\n\nThere are several classes for handling bitmap data in iOS: `UIImage` (UIKit), `CGImage` (Core Graphics), and `CIImage` (Core Image). There is also `NSData` for holding the actual data before creating one of those classes. Getting a `UIImage` is easy enough using the `imageWithContentsOfFile:` method, and for other sources we have `imageWithCGImage:`, `imageWithCIImage:`, and `imageWithData:`. This large number of different-but-similar classes seems somewhat superfluous, but it is partly caused by optimizing aspects of image storage for different purposes across the different frameworks, and it is generally possible to easily convert between the different types.\n\n\n## Capturing an Image from the Camera\n\nTo get an image from the camera, we need to set up an `AVCaptureStillImageOutput` object. We can then use the `captureStillImageAsynchronouslyFromConnection:completionHandler:` method. Its handler is a block that is called with a `CMSampleBufferRef` parameter. We can convert this into an `NSData` object with `AVCaptureStillImageOutput`'s `jpegStillImageNSDataRepresentation:` class method, and then, in turn, we use the method `imageWithData:` (mentioned above) to get to a `UIImage`. There are a number of parameters that can be tweaked in the process, such as exposure control or the focus setting, low-light boost, flash, and even the ISO setting (from iOS 8 only). The settings are applied to an `AVCaptureDevice`, which represents the camera on devices that have one.\n\n\n## Manipulating Images Programmatically\n\nA straightforward way to manipulate images is to use UIKit's `UIGraphicsBeginImageContext` function. You can then draw in the current graphics context, and also include images directly. In one of my own apps, Stereogram, I use this to place two square images next to each other, and add a region above them with two dots for focusing. The code for that is as follows:\n\n```objc\n-(UIImage*)composeStereogramLeft:(UIImage *)leftImage right:(UIImage *)rightImage\n{\n    float w = leftImage.size.width;\n    float h = leftImage.size.height;\n    UIGraphicsBeginImageContext(CGSizeMake(w * 2.0, h + 32.0));\n    [leftImage drawAtPoint:CGPointMake(0.0, 32.0)];\n    [rightImage drawAtPoint:CGPointMake(w, 32.0)];\n    float leftCircleX = (w / 2.0) - 8.0;\n    float rightCircleX = leftCircleX + w;\n    float circleY = 8.0;\n    [[UIColor blackColor] setFill];\n    UIRectFill(CGRectMake(0.0, 0.0, w * 2.0, 32.0));\n\n    [[UIColor whiteColor] setFill];\n    CGRect leftRect = CGRectMake(leftCircleX, circleY, 16.0, 16.0);\n    CGRect rightRect = CGRectMake(rightCircleX, circleY, 16.0, 16.0);\n    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:leftRect];\n    [path appendPath:[UIBezierPath bezierPathWithOvalInRect:rightRect]];\n    [path fill];\n    UIImage *savedImg = UIGraphicsGetImageFromCurrentImageContext();\n    UIGraphicsEndImageContext();\n    return savedImg;\n}\n```\n\nAfter placing the images on the canvas and adding two filled circles, we can turn the graphics context into a `UIImage` with a single method call. The output of that method looks as follows:\n\n![Stereogram output](/images/issue-21/stereogram-output.jpg)\n\nIt is composed of the two photos taken from slightly different camera positions, and a black strip with two centered white dots to aid the viewing process.\n\nIt is a bit more complex if we want to mess with the actual pixel values. With a stereogram, we have two photos next to each other, and we need to align our eyes (or go cross-eyed) to see the 3D effect. An alternative to this is a so-called [anaglyph](http://www.3dtv.at/knowhow/anaglyphcomparison_en.aspx), a red/green image that you use colored 3D glasses to look at. (The function listed below implements the Optimized Anaglyphs method on the linked page.)\n\nFor working with individual pixels in this way, we have to create a context with `CGBitmapContextCreate`, which includes a color space (such as RGB). We can then iterate over the bitmaps (left and right photos), and get at the individual color channel values. For example, we keep the green and blue values of one image as they were, and merge the green and blue values of the other photo into the red value:\n\n```objc\nUInt8 *rightPtr = rightBitmap;\nUInt8 *leftPtr = leftBitmap;\nUInt8 r1, g1, b1;\nUInt8 r2, g2, b2;\nUInt8 ra, ga, ba;\n\nfor (NSUInteger idx = 0; idx < bitmapByteCount; idx += 4) {\n    r1 = rightPtr[0]; g1 = rightPtr[1]; b1 = rightPtr[2];\n    r2 = leftPtr[0]; g2 = leftPtr[1]; b2 = leftPtr[2];\n\n    // r1/g1/b1 is the right hand side photo, which is merged in\n    // r2/g2/b2 is the left hand side photo which the other is merged into\n    // ra/ga/ba is the merged pixel\n\n    ra = 0.7 * g1 + 0.3 * b1;\n    ga = b2;\n    ba = b2;\n\n    leftPtr[0] = ra;\n    leftPtr[1] = ga;\n    leftPtr[2] = ba;\n    rightPtr += 4; // move to the next pixel (4 bytes, includes alpha value)\n    leftPtr += 4;\n}\nCGImageRef composedImage = CGBitmapContextCreateImage(_leftContext);\nUIImage *retval = [UIImage imageWithCGImage:composedImage];\nCGImageRelease(composedImage);\nreturn retval;\n```\n\nWith this method, we have full access to the actual pixels, and can do with them whatever we like. However, it is worth checking whether there are already filters available via [Core Image](/issues/21-camera-and-photos/core-image-intro/), as they will be much easier to use and generally more optimized than any processing of individual pixel values.\n\n\n## Metadata\n\nThe standard format for storing information about an image is [Exif](https://en.wikipedia.org/wiki/Exchangeable_image_file_format) (Exchangeable image file format). With photos, this generally captures date and time, shutter speed and aperture, and GPS coordinates if available. It is a tag-based system, based on [TIFF](https://en.wikipedia.org/wiki/Tagged_Image_File_Format) (Tagged Image File Format). It has a lot of faults, but as it's the de facto standard, there isn't really a better alternative. As is often the case, there are other methods available which are better designed, but not supported by the cameras we all use.\n\nUnder iOS, it is possible to access the Exif information using `CGImageSourceCopyPropertiesAtIndex`. This returns a dictionary containing all the relevant information. However, do not rely on any information being attached. Due to the complexities of vendor-specific extensions to the convention (it's not a proper standard), the data is often missing or corrupted, especially when the image has passed through a number of different applications (such as image editors, etc.). Usually the information is also stripped when an image is uploaded to a web server; some of it can be sensitive, for example, GPS data. For privacy reasons, this is often removed. Apparently the NSA is harvesting Exif data in its [XKeyscore program](https://en.wikipedia.org/wiki/XKeyscore).\n\n\n## Summary\n\nHandling images can be a fairly complex issue. Image processing has been around for some time, so there are many different frameworks concerned with different aspects of it. Sometimes you have to dig down into C function calls, with the associated manual memory management. Then there are many different sources for images, and they can all handle certain edge cases differently. The biggest problem on iOS, however, is memory: as cameras and screen resolutions get better, images grow in size. The iPhone 5s has an 8-megapixel camera; if each pixel is stored in 4 bytes (one each for the three color channels, plus one for opacity), that totals 32 MB. Add a few working copies or previews of the image, and we quickly run into trouble when handling multiple images or slideshows. Writing to the file system is also not very fast, so there are a lot of optimizations necessary to ensure your iOS app runs smoothly.\n"
  },
  {
    "path": "2015-02-10-photo-extensions.md",
    "content": "---\ntitle:  \"Photo Extensions\"\ncategory: \"21\"\ndate: \"2015-02-10 07:30:00\"\ntags: article\nauthor:\n  - name: Sam Davies\n    url: https://twitter.com/iwantmyrealname\n---\n\n\nWhen iOS 8 came out, Apple introduced extensions to the world, with\n[six extension points](https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/index.html#//apple_ref/doc/uid/TP40014214-CH20-SW2)\navailable. These offer unprecedented access to the operating system, with the\nPhoto Editing extension allowing developers to build functionality into the\nsystem Photos app.\n\nThe user workflow for Photo Editing extensions is not entirely simple. From\nselecting the photo you want to edit, it takes three taps to launch the\nextension, one of which is on a tiny, unintuitive button:\n\n![Image Editing Extension User Workflow](/images/issue-21/user_workflow.png)\n\nNevertheless, these kinds of extensions offer a fantastic opportunity for\ndevelopers to offer a seamless experience to users, thereby creating a consistent\napproach to managing photos.\n\nThis article will talk briefly about how to create extensions, and their\nlifecycle, before moving on to look at the editing workflow in more\ndetails. It will conclude by looking at some common concerns and scenarios\nassociated with creating Photo Editing extensions.\n\nThe _Filtster_ project, which accompanies this article, demonstrates how you\ncan set up your own image editing extension. It represents a really simple image\nfiltering process using a couple of Core Image filters. You can access the\n[complete _Filtster_ project on GitHub](https://github.com/objcio/issue-21-photo-extensions-Filtster).\n\n## Creating an Extension\n\nAll types of iOS extensions have to be contained in a fully functional iOS app,\nand this includes Photo Editing extensions. This can mean that you have to\ndo a lot of work to get your amazing new custom Core Image filter in the hands of\nsome users. It remains to be seen how strict Apple is on this, since most apps\nin the App Store with custom image editing extensions existed before the\nintroduction of iOS 8.\n\nTo create a new image editing extension, add a new target to an existing iOS\napp project. There is a template target for the extension:\n\n![Image Editing Extension Template](/images/issue-21/xcode-target-template-photo-extension.png)\n\nThis template consists of three components:\n\n1. __Storyboard.__ Image editing extensions can have an almost completely custom UI. The system provides only a toolbar across the top, containing _Cancel_ and\n_Done_ buttons:\n\n    ![Cancel/Done Buttons](/images/issue-21/cancel_done.png)\n\n    Although the storyboard doesn't have size classes enabled by default, the system will respect them should you choose to activate them. Apple highly recommends using Auto Layout for building Photo Editing extensions, although there is no obvious reason why you couldn't perform manual layout. Still, you're flying in the face of danger if you decide to ignore Apple's advice.\n\n2. __Info.plist.__ This specifies the extension type and accepted media types,\nand is common to all extension types. The `NSExtension` key has a\ndictionary containing all the extension-related configurations:\n\n    ![Extension plist](/images/issue-21/extension_plist.png)\n\n    The  `NSExtensionPointIdentifier` entry informs the system that this is a Photo Editing extension with a value of `com.apple.photo-editing`. The only key that is specific to photo editing is `PHSupportedMediaTypes`, and this is related to what types of media the extension can operate on. By default, this is an array with a single entry of `Image`, but you have the option of adding `Video`.\n\n3. __View Controller.__ This adopts the `PHContentEditingController` protocol,\nwhich contains methods that form the lifecycle of an image editing extension.\nSee the next section for further detail.\n\nNotably missing from this list is the ability to provide the imagery for the\nicon that appears in the extension selection menu:\n\n![Extension Icon](/images/issue-21/extension_icon.png)\n\nThis icon is provided by the App Icon image set in the host app's asset catalog.\nThe documentation is a little confusing here, as it implies that you have to\nprovide an icon in the extension itself. However, although this is possible, the\nextension will not honor the selection. This point is somewhat moot, as Apple\nspecifies that the icon associated with an extension should be identical to that\nof the container app.\n\n## Extension Lifecycle\n\nThe Photo Editing extension is built on top of the Photos framework, which means\nthat edits aren't destructive. When a photo asset is edited, the original file\nremains untouched, while a rendered copy is saved. It addition, semantic\ndetails about how to recreate the edit are saved as adjustment data. This data\nmeans that the edit can be completely recreated from the original image. When\nyou implement image editing extensions, you are responsible for constructing your\nown adjustment data objects.\n\nThe `PHAdjustmentData` class represents these edit parameters, and has two\nformat properties (`formatIdentifier` and `formatVersion`) that are used to\ndetermine compatibility of an editing extension with a previously edited image.\nBoth properties are strings, and the `formatIdentifier` should be in reverse-DNS\nform. These two properties give you the flexibility to create a suite of image\nediting apps and extensions, each of which can interpret the editing results from\nthe others. There is also a `data` property that is of type `NSData`. This can\nbe used however you wish to store the details of how your extension can resume\nediting.\n\n### Beginning Editing\n\nWhen a user chooses to edit an image using your extension, the system will instantiate your view controller and initiate the photo editing lifecycle. If the photo has previously been edited, this will first call the `canHandleAdjustmentData(_:)` method, at which point you are provided a `PHAdjustmentData` object. As such, it's important to figure out in advance whether or not your extension can handle the previous edit data. This determines what the framework will send to the next method in the lifecycle.\n\nOnce the system has decided to supply either the original image or one\ncontaining the rendered output from previous edits, it then calls the\n`startContentEditingWithInput(_:, placeholderImage:)`. The input is an object of\ntype `PHContentEditingInput`, which contains metadata such as location, creation\ndata, and media type about the original asset, alongside the details you need to\nedit the asset. In addition to the path of the full-size input\nimage, the input object also contains a `displaySizedImage` that represents the\nsame image data, but is scaled appropriately for the screen. This means that the\ninteractive editing phase can operate at a lower resolution, ensuring that your\nextension remains responsive and energy efficient.\n\nThe following shows an implementation of this method:\n\n```swift\nfunc startContentEditingWithInput(contentEditingInput: PHContentEditingInput?,\n                                  placeholderImage: UIImage) {\n  input = contentEditingInput\n  filter.inputImage = CIImage(image: input?.displaySizeImage)\n  if let adjustmentData = contentEditingInput?.adjustmentData {\n    filter.importFilterParameters(adjustmentData.data)\n  }\n\n  vignetteIntensitySlider.value = Float(filter.vignetteIntensity)\n  ...\n}\n```\n\nThe above implementation stores the `contentEditingInput`, since it's required to\ncomplete the edit, and imports the filter parameters from the\nadjustment data.\n\nIf your `canHandleAdjustmentData(_:)` method returned `true`, then the images\nprovided to `startContentEditingWithInput(_:, placeholderImage:)` will be\noriginal, and the extension will have to recreate the edited image from the\nprevious adjustment data. If this is a time-consuming process, then the\n`placeholderImage` is an image of the rendered previous edit that can be used\ntemporarily.\n\nAt this stage, the user interacts with the UI of the extension to control the\nediting process. Since the extension has a view controller, you can use any of\nthe features of UIKit to implement this. The sample project uses a Core Image\nfilter chain to facilitate editing, so the UI is handled with a custom `GLKView`\nsubclass to reduce the load on the CPU.\n\n### Cancelation\n\nTo finish editing, users can select either the _Cancel_ or _Done_ buttons\nprovided by the Photos UI. If the user decides to cancel with unsaved edits,\nthen the `shouldShowCancelConfirmation` property should be overridden to return\n`true`:\n\n![Confirm Cancellation](/images/issue-21/confirm_cancel.png)\n\nIf the cancelation is requested, then the `cancelContentEditing` method is\ncalled to allow you to clear up any temporary data that you've created.\n\n### Commit Changes\n\nOnce the user is happy with his or her edits and taps the _Done_ button, a call is\nmade to `finishContentEditingWithCompletionHandler(_:)`. At this point, the full-size image needs to be edited with the settings that are currently applied to\nthe display-sized image, and the new adjustment data needs saving.\n\nAt this point, you can obtain the full-size image using the `fullSizeImageURL` on\nthe `PHContentEditingInput` object provided at the beginning of the editing\nprocess.\n\nTo complete editing, the supplied callback should be invoked with a\n`PHContentEditingOutput` object, which can be created from the input. This\noutput object also includes a `renderedContentURL` property that specifies\nwhere you should write the output JPEG data:\n\n```swift\nfunc finishContentEditingWithCompletionHandler(completionHandler: ((PHContentEditingOutput!) -> Void)!) {\n  // Render and provide output on a background queue.\n  dispatch_async(dispatch_get_global_queue(CLong(DISPATCH_QUEUE_PRIORITY_DEFAULT), 0)) {\n    // Create editing output from the editing input.\n    let output = PHContentEditingOutput(contentEditingInput: self.input)\n\n    // Provide new adjustments and render output to given location.\n    let adjustmentData = PHAdjustmentData(formatIdentifier: self.filter.filterIdentifier,\n      formatVersion: self.filter.filterVersion, data: self.filter.encodeFilterParameters())\n    output.adjustmentData = adjustmentData\n\n    // Write the JPEG data\n    let fullSizeImage = CIImage(contentsOfURL: self.input?.fullSizeImageURL)\n    UIGraphicsBeginImageContext(fullSizeImage.extent().size);\n    self.filter.inputImage = fullSizeImage\n    UIImage(CIImage: self.filter.outputImage)?.drawInRect(fullSizeImage.extent())\n\n    let outputImage = UIGraphicsGetImageFromCurrentImageContext()\n    let jpegData = UIImageJPEGRepresentation(outputImage, 1.0)\n    UIGraphicsEndImageContext()\n\n    jpegData.writeToURL(output.renderedContentURL, atomically: true)\n\n    // Call completion handler to commit edit to Photos.\n    completionHandler?(output)\n  }\n}\n```\n\nOnce the call to the `completionHandler` has returned, you can clear up any\ntemporary data and files ready for the extension to return.\n\n\n## Common Concerns\n\nThere are a few areas associated with creating an image editing extension that\ncan be a little complicated. The topics in this section address the most\nimportant of these.\n\n### Adjustment Data\n\nThe `PHAdjustmentData` is a simple class with just three properties, but to get\nthe best use from it, discipline is required. Apple suggests using reverse-DNS\nnotation to specify the `formatIdentifier`, but then you are left to decide how\nto use the `formatVersion` and `data` properties yourself.\n\nIt's important that you can determine compatibility between different versions\nof your image editing framework, so an approach such as [semantic versioning](http://semver.org/)\noffers the flexibility to manage this over the lifetime of your products. You\ncould implement your own parser, or look to a third-party framework such as\n[SemverKit](https://github.com/nomothetis/SemverKit) to provide this\nfunctionality.\n\nThe final aspect of the adjustment data is the `data` property itself,\nwhich is just an `NSData` blob. The only advice that Apple offers here is that\nit should represent the settings to recreate the edit, rather than the edit\nitself, since the size of the `PHAdjustmentData` object is limited by the Photos\nframework.\n\nFor non-complex extensions (such as _Filtster_), this can be as simple as an\narchived dictionary, which can be written as follows:\n\n```swift\npublic func encodeFilterParameters() -> NSData {\n  var dataDict = [String : AnyObject]()\n  dataDict[\"vignetteIntensity\"] = vignetteIntensity\n  ...\n  return NSKeyedArchiver.archivedDataWithRootObject(dataDict)\n}\n```\n\nThis is then reinterpreted with the following:\n\n```swift\npublic func importFilterParameters(data: NSData?) {\n  if let data = data {\n    if let dataDict = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? [String : AnyObject] {\n      vignetteIntensity = dataDict[\"vignetteIntensity\"] as? Double ?? vignetteIntensity\n      ...\n    }\n  }\n}\n```\n\nHere, these two methods are on the shared `FiltsterFilter` class, which is also\nresponsible for determining compatibility of the adjustment data:\n\n```swift\npublic func supportsFilterIdentifier(identifier: String, version: String) -> Bool {\n  return identifier == filterIdentifier && version == filterVersion\n}\n```\n\nIf you have more complex requirements, you could create a custom settings\nclass, which adopts the `NSCoding` protocol to allow it to be archived in a\nsimilar manner.\n\nA user can chain incompatible photo edits together — if the adjustment\ndata is not understood by the current extension, the pre-rendered image will be\nused as input. For example, you can crop an image using the system crop tool\nbefore using your custom Photo Editing extension. Once you have saved the edited\nimage, the associated adjustment data will only contain details of the most\nrecent edit. You could store adjustment data from the previous, incompatible edit\nin your output adjustment data, allowing you to implement a revert function for\njust your phase of the filter chain. The revert function provided by the Photos\napp will remove all the edits, returning the photo to its original state:\n\n![Revert Edits](/images/issue-21/revert.png)\n\n\n### Code/Data Sharing\n\nPhoto Editing extensions are distributed as an embedded binary inside a\ncontainer app, which Apple has stated must be a functioning app. Since you're\ncreating a Photo Editing extension, it is likely that the app will offer the\nsame functionality. You're therefore likely to want to share code and data\nbetween the app extension and the container app.\n\nSharing code is achieved by creating a Cocoa Touch Framework target, a new\nfunctionality available in iOS 8. Then you can add shared functionality, such as\nthe filter chain and custom view classes, and use them from both the app and\nthe extension.\n\nNote that since the framework will be used from an app extension, you must\nrestrict the APIs it can use on the Target Settings page:\n\n![Restrict Framework API](/images/issue-21/app_extension_api.png)\n\nSharing data is a less obvious requirement, and in many cases it won't exist.\nHowever, if necessary, you can create a shared container, which is\nachieved by adding both the app and extension to an app group associated with\nyour developer profile. The shared container represents a shared space on disk\nthat you can use in any way you wish, e.g. `NSUserDefaults`, `SQLite`, or file\nwriting.\n\n### Debugging and Profiling\n\nDebugging is reasonably well-supported in Xcode, although there are some\npotential sticking points. Selecting the extension's scheme and selecting run\nshould build it and then let you select which app to run. Since photo\nediting extensions can only be activated from within the system Photos app, you\nshould select the Photos app:\n\n![Select App](/images/issue-21/select_app.png)\n\nIf this instead launches your container app, then you can edit the extension's\nscheme to set the executable to _Ask on Launch_.\n\nXcode then waits for you to start the Photo Editing extension before attaching\nto it. At this point, you can debug as you do with standard iOS apps. The\nprocess of attaching the debugger to the extension can take quite a long time,\nso when you activate the extension, it can appear to hang. Running in release mode\nwill allow you to evaluate the extension startup time.\n\nProfiling is similarly supported, with the profiler attaching as the extension\nbegins to run. You might once again need to update the scheme associated with\nthe extension to specify that Xcode should ask which app should run as profiling\nbegins.\n\n\n### Memory Restrictions\n\nExtensions are not full iOS apps and therefore are permitted restricted access\nto system resources. More specifically, the OS will kill an extension if it uses\ntoo much memory. It's not possible to determine the exact memory limit, since memory management is handled privately by iOS, however it is definitely dependent on factors such as the device, the host app, and the memory pressure from other apps. As such, there are no hard\nlimits, but instead a general recommendation to minimize the memory footprint.\n\nImage processing is a memory-hungry operation, particularly with the resolution\nof the photos from an iPhone camera. There are several things you can do to keep\nthe memory usage of your Photo Editing extension to a minimum.\n\n- __Work with the display-sized image:__ When beginning the edit process, the\nsystem provides an image suitably scaled for the screen. Using this instead of\nthe original for the interactive editing phase will require significantly less\nmemory.\n- __Limit the number of Core Graphics contexts:__ Although it might seem like the way\nto work with images, a Core Graphics context is essentially just a big chunk of\nmemory. If you need to use contexts, then keep the number to a minimum. Reuse them\nwhere possible, and decide whether you're using the best approach.\n- __Use the GPU:__ Whether it be through Core Image or a third-party framework such\nas GPUImage, you can keep memory down by chaining filters together and\neliminating the requirement for intermediate buffers.\n\nSince image editing is expected to have high memory requirements, it seems that\nthe extensions are given a little more leeway than other extension types. During\nad hoc testing, it appears to be possible for an image editing extension to use\nmore than 100 MB. Given that an uncompressed image from an 8-megapixel camera is\napproximately 22 MB, most image editing should be achievable.\n\n\n## Conclusion\n\nPrior to iOS 8, there was no way for a third-party developer to provide\nfunctionality to the user anywhere other than within his or her own app. Extensions\nhave changed this, with the Photo Editing extension in particular allowing you\nto put your code right into the heart of the Photos app. Despite the slightly\nconvoluted multi-tap workflow, the Photo Editing extension uses the power of the\nPhotos framework to provide a coherent and integrated user experience.\n\nResumable editing has traditionally been reserved for use in desktop photo\ncollection applications such as Aperture or Lightroom. Creating a common\narchitecture for this on iOS with the Photos framework offers great\npotential, and allowing the creation of Photo Editing extensions takes this even\nfurther.\n\nThere are some complexities associated with creating Photo Editing extensions,\nbut few of these are unique. Creating an intuitive interface and designing image processing algorithms are both as challenging for image editing extensions as they are for complete image editing apps.\n\nIt remains to be seen how many users are aware of these third-party image editing\nextensions, but they have the potential to increase your app's exposure.\n"
  },
  {
    "path": "2015-02-10-the-photos-framework.md",
    "content": "---\ntitle:  \"The Photos Framework\"\ncategory: \"21\"\ndate: \"2015-02-10 08:00:00\"\nauthor:\n  - name: Saniul Ahmed\n    url: https://twitter.com/saniul\ntags: article\n---\n\n## Introduction\n\nEvery day, [more photos are taken with the iPhone](https://www.flickr.com/cameras#brands) than any other camera. Displays on iOS devices get better every year, but even back in the pre-Retina era [when the iPad was introduced](http://youtu.be/_KN-5zmvjAo?t=17m7s), one of its killer uses was just displaying user photos and exploring the photo library. Since the camera is one of the iPhone's most important and popular features, there is a big demand for apps and utilities that make use of the wealth of users' photo libraries.\n\nUntil the summer of 2014, developers used the [AssetsLibrary Framework](https://developer.apple.com/library/ios/documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/#//apple_ref/doc/uid/TP40009722-CH1-SW57) to access the ever-growing photo libraries of users. Over the years, Camera.app and Photos.app have changed significantly, adding new features, including a way of organizing photos by moments. Meanwhile, the AssetsLibrary framework lagged behind.\n\nWith iOS 8, Apple has given us PhotoKit, a modern framework that's more performant than AssetsLibrary and provides features that allow applications to work seamlessly with a device's photo library.\n\n## Outline\n\nWe'll start with a bird's-eye view of the [framework's object model](#PhotoKit-Object-Model): the entities and the relationships between them, fetching instances of those entities, and working with the fetch results.\n\nAdditionally, we'll cover [the asset metadata](#Photo-metadata) that wasn't available to developers when using AssetsLibrary.\n\nThen we'll discuss [loading the assets' image data](#Photo-Loading): the process itself, multitudes of available options, some gotchas, and edge cases.\n\nFinally, we'll talk about [observing changes](#The-Times-They-Are-A-Changin) made to the photo library by external actors and learn how to make and commit [our own changes](#Wind-of-Change).\n\n\n<a name=\"PhotoKit-Object-Model\"></a>\n## PhotoKit Object Model\n\nPhotoKit defines an entity graph that models the objects presented to the user in the stock Photos.app. These photo entities are lightweight and immutable. All the PhotoKit objects inherit from the abstract `PHObject` base class, whose public interface only provides a `localIdentifier` property.\n\n`PHAsset` represents a single asset in the user's photo library, providing the [metadata](#Photo-metadata) for that asset.\n\nGroups of assets are called asset collections and are represented by the `PHAssetCollection` class. A single asset collection can be an album or a moment in the photo library, as well as one of the special \"smart albums.\" These include collections of all videos, recently added items, user favorites, all burst photos, [and more](https://developer.apple.com/library/ios/documentation/Photos/Reference/PHAssetCollection_Class/index.html#//apple_ref/c/tdef/PHAssetCollectionSubtype). `PHAssetCollection` is a subclass of `PHCollection`.\n\n`PHCollectionList` represents a group of `PHCollection`s. Since it is a `PHCollection` itself, a collection list can contain other collection lists, allowing for complex hierarchies of collections. In practice, this can be seen in the Moments tab in the Photos.app: Asset --- Moment --- Moment Cluster --- Moment Year.\n\n### Fetching Photo Entities\n\n#### Fetching vs. Enumerating\n\nThose familiar with the AssetsLibrary framework might remember that to be able to find assets with specific properties, one has to *enumerate* through the user's library and collect the matching assets. Granted, the API provided some ways of [narrowing down the search domain](https://developer.apple.com/library/ios/documentation/AssetsLibrary/Reference/ALAssetsGroup_Class/index.html#//apple_ref/occ/instm/ALAssetsGroup/setAssetsFilter:), but it still remains quite unwieldy.\n\nIn contrast, PhotoKit entity instances are *fetched*. Those familiar with Core Data will recognize the approaches and concepts used and described here.\n\n#### Fetch Request\n\nFetches are made using the class methods of the entities described above. Which class/method to use depends on the problem domain and how you're representing and traversing the photo library. All of the fetch methods are named similarly: `class func fetchXXX(..., options: PHFetchOptions) -> PHFetchResult`. The `options` parameter gives us a way of filtering and ordering the returned results, similar to `NSFetchRequest`'s `predicate` and `sortDescriptors` parameters.\n\n#### Fetch Result\n\nYou may have noticed that these fetch methods aren't asynchronous. Instead, they return a `PHFetchResult` object, which allows access to the underlying collection of results with an interface similar to `NSArray`. It will dynamically load its contents as needed and cache contents around the most recently requested value. This behavior is similar to the result array of an `NSFetchRequest` with a set `batchSize` property. There is no way to parametrize this behavior for `PHFetchResult`, but the [documentation promises](https://developer.apple.com/library/ios/documentation/Photos/Reference/PHFetchResult_Class/index.html) “optimal performance even when handling a large number of results.”\n\nThe `PHFetchResult`s returned by the fetch methods will not be updated automatically if the photo library contents match the request change. Observing changes and processing updates for a given `PHFetchResult` are [described in a later section](#The-Times-They-Are-A-Changin).\n\n\n## Transient Collections\n\nYou might find that you have designed a component that operates on an asset collection, and yet you would like to be able to use it with an arbitrary set of assets. PhotoKit provides an easy way to do that using transient asset collections.\n\nTransient asset collections are created explicitly by you from an array of `PHAsset` objects or from a `PHFetchResult` containing assets. This is done using the `transientAssetCollectionWithAssets(...)` and `transientAssetCollectionWithFetchResult(...)` factory methods on `PHAssetCollection`. The objects vended by these methods can be used just like any other `PHAssetCollection`. Despite that, these collections aren't saved to the user's photos library and thus aren't displayed in the Photos.app.\n\nSimilarly to asset collections, you can create transient collection lists by using the `transientCollectionListWithXXX(...)` factory methods on `PHCollectionList`.\n\nThis can turn out to be very useful when you need to combine results from two fetch requests.\n\n\n<a name=\"Photo-metadata\"></a>\n## Photo Metadata\n\nAs mentioned in the beginning of this article, PhotoKit provides some additional metadata about user assets that wasn't available (or at least not as easily available) in the past when using ALAssetsLibrary.\n\n### HDR and Panorama Photos\n\nYou can use a photo asset's `mediaSubtypes` property to find out if the underlying image was captured with HDR enabled and whether or not it was shot in the Camera.app's Panorama mode.\n\n### Favorite and Hidden Assets\n\nTo find out if an asset was marked as favorite or was hidden by the user, just inspect the `favorite` and `hidden` properties of the `PHAsset` instance.\n\n### Burst Mode Photos\n\n`PHAsset`'s `representsBurst` property is `true` for assets that are representative of a burst photo sequence (multiple photos taken while the user holds down the shutter). It will also have a `burstIdentifier` value which can then be used to fetch the rest of the assets in that burst sequence via `fetchAssetsWithBurstIdentifier(...)`.\n\nThe user can flag assets within a burst sequence; additionally, the system uses various heuristics to mark potential user picks automatically. This metadata is accessible via `PHAsset`'s `burstSelectionTypes` property. This property is a bitmask with three defined constants: `.UserPick` for assets marked manually by the user, `.AutoPick` for potential user picks, and `.None` for unmarked assets.\n\n![.AutoPick Example](/images/issue-21/photos-burst-example.jpg)\n\nThe screenshot shows how Photos.app automatically marks potential user picks in a burst sequence.\n\n\n<a name=\"Photo-Loading\"></a>\n## Photo Loading\n\nOver the years of working with user photo libraries, developers created hundreds (if not thousands) of tiny pipelines for efficient photo loading and display. These pipelines dealt with request dispatching and cancelation, image resizing and cropping, caching, and more. PhotoKit provides a class that does all this with a convenient and modern API: `PHImageManager`.\n\n### Requesting Images\n\nImage requests are dispatched using the `requestImageForAsset(...)` method. The method takes in a `PHAsset`, desired sizing of the image and other options (via the `PHImageRequestOptions` parameter object), and a results handler. The returned value can be used to cancel the request if the requested data is no longer necessary.\n\n#### Image Sizing and Cropping\n\nCuriously, the parameters regarding the sizing and cropping of the result image are spread across two places. The `targetSize` and `contentMode` parameters are passed directly into the `requestImageForAsset(...)` method. The content mode describes whether the photo should be aspect-fitted or aspect-filled into the target size, similar to UIView's `contentMode`. Note: If the photo should not be resized or cropped, pass `PHImageManagerMaximumSize` and `PHImageContentMode.Default`.\n\nAdditionally, `PHImageRequestOptions` provides means of specifying *how* the image manager should resize. The `resizeMode` property can be set to `.Exact` (when the result image must match the target size), `.Fast` (more efficient than .Exact, but the resulting image might differ from the target size), or `.None`. Furthermore, the `normalizedCroppingMode` property lets us specify how the image manager should crop the image. Note: If `normalizedcroppingMode` is provided, set `resizeMode` to `.Exact`.\n\n#### Request Delivery and Progress\n\nBy default, the image manager will deliver a lower-quality version of your image before delivering the high-quality version if it decides that's the optimal strategy to use. You can control this behavior through the `deliveryMode` property; the default behavior described above is `.Opportunistic`. Set it to `.HighQualityFormat` if you're only interested in the highest quality of the image available and if longer load times are acceptable. Use `.FastFormat` to load the image faster while sacrificing the quality.\n\nYou can make the `requestImage...` method synchronous using the `synchronous` property on `PHImageRequestOptions`. Note: When `synchronous` is set to `true`, the `deliveryMode` property is ignored and considered to be set to `.HighQualityFormat`.\n\nWhen setting these parameters, it is important to always consider that some of your users might have iCloud Photo Library enabled. The PhotoKit API doesn't necessarily distinguish photos available on-device from those available in the cloud — they are all loaded using the same `requestImage` method. This means that every single one of your image requests may potentially be a slow network request over the cellular network. Keep this in mind when considering using `.HighQualityFormat` and/or making your requests synchronous. Note: If you want to make sure that the request doesn't hit the network, set `networkAccessAllowed` to `false`.\n\nAnother iCloud-related property is `progressHandler`. You can set it to a [`PHAssetImageProgressHandler`](https://developer.apple.com/library/ios/documentation/Photos/Reference/PHImageRequestOptions_Class/index.html#//apple_ref/doc/c_ref/PHAssetImageProgressHandler) block that will be called by the image manager when downloading the photo from iCloud.\n\n#### Asset Versions\n\nPhotoKit allows apps to make non-destructive adjustments to photos. For edited photos, the system keeps a separate copy of the original image and the app-specific adjustment data. When fetching assets using the image manager, you can specify which version of the image asset should be delivered via the result handler. This is done by setting the `version` property: `.Current` will deliver the image with all adjustments applied to it; `.Unadjusted` delivers the image before any adjustments are applied to it; and `.Original` delivers the image in its original, highest-quality format (e.g. the RAW data, while `.Unadjusted` would deliver a JPEG).\n\nYou can read more about this aspect of the framework in Sam Davies' [article on Photo Extensions](/issues/21-camera-and-photos/photo-extensions/).\n\n#### Result Handler\n\nThe result handler is a block that takes in a `UIImage` and an `info` dictionary. It can be called by the image manager multiple times throughout the lifetime of the request, depending on the parameters and the request options.\n\nThe `info` dictionary provides information about the current status of the request, such as:\n\n* Whether the image has to be requested from iCloud (in which case you're going to have to re-request the image if you initially set `networkAccessAllowed` to `false`) — `PHImageResultIsInCloudKey`.\n* Whether the currently delivered `UIImage` is the degraded form of the final result. This lets you display a preview of the image to the user while the higher-quality image is being downloaded — `PHImageResultIsDegradedKey`.\n* The request ID (convenience for canceling the request), and whether the request has already been canceled — `PHImageResultRequestIDKey` and `PHImageCancelledKey`.\n* An error, if an image wasn't provided to the result handler — `PHImageErrorKey`.\n\nThese values let you update your UI to inform your user and, together with the `progressHandler` discussed above, hint at the loading state of their images.\n\n### Caching\n\nAt times it's useful to load some images into memory prior to the moment when they are going to be shown on the screen, for example when displaying a screen with a large number of asset thumbnails in a scrolling collection. PhotoKit provides a `PHImageManager` subclass that deals with that specific use case – `PHImageCachingManager`.\n\n`PHImageCachingManager` provides a single key method – `startCachingImagesForAssets(...)`. You pass in an array of `PHAsset`s, the request parameters and options that should match those you're going to use later when requesting individual images. Additionally, there are methods that you can use to inform the caching manager to stop caching images for a list of specific assets and to stop caching all images.\n\nThe `allowsCachingHighQualityImages` property lets you specify whether the image manager should prepare images at high quality. When caching a short and unchanging list of assets, the default `true` value should work just fine. When caching while quickly scrolling in a collection view, it is better to set it to `false`.\n\nNote: In my experience, using the caching manager can be detrimental to scrolling performance when the user is scrolling extremely fast through a large asset collection. It is extremely important to tailor the caching behavior for the specific use case. The size of the caching window, when and how often to move the caching window, the value of the `allowsCachingHighQualityImages` property — these parameters should be carefully tuned and the behavior tested with a real photo library and on target hardware. Furthermore, consider setting these parameters dynamically based on the user's actions.\n\n### Requesting Image Data\n\nFinally, in addition to requesting plain old `UIImages`, `PHImageManager` provides another method that returns the asset data as an `NSData` object, its universal type identifier, and the display orientation of the image. This method returns the largest available representation of the asset.\n\n\n<a name=\"The-Times-They-Are-A-Changin\"></a>\n## The Times They Are A-Changin'\n\nWe have discussed requesting metadata of assets in the user's photo library, but we haven't covered how to keep our fetched data up to date. The photo library is essentially a big bag of mutable state, and yet the photo entities covered in the first section are immutable. PhotoKit lets you receive notifications about changes to the photo library together with all the information you need to correctly update your cached state.\n\n### Change Observing\nFirst, you need to register a change observer (conforming to the `PHPhotoLibraryChangeObserver` protocol) with the shared `PHPhotoLibrary` object using the `registerChangeObserver(...)` method. The change observer's `photoLibraryDidChange(...)` method will be called whenever another app or the user makes a change in the photo library **that affects any assets or collections that you fetched prior to the change**. The method has a single parameter of type `PHChange`, which you can use to find out if the changes are related to any of the fetched objects that you are interested in.\n\n### Updating Fetch Results\n\n`PHChange` provides methods you can call with any `PHObject`s or `PHFetchResult`s whose changes you are interested in tracking – `changeDetailsForObject(...)` and `changeDetailsForFetchResult(...)`. If there are no changes, these methods will return `nil`, otherwise you will be vended a `PHObjectChangeDetails` or `PHFetchResultChangeDetails` object.\n\n`PHObjectChangeDetails` provides a reference to an updated photo entity object, as well as boolean flags telling you whether the object's image data was changed and whether the object was deleted.\n\n`PHFetchResultChangeDetails` encapsulates information about changes to a `PHFetchResult` that you have previously received after a fetch. `PHFetchResultChangeDetails` is designed to make updates to a collection view or table view as simply as possible. Its properties map exactly to the information you need to provide in a typical collection view update handler. Note that to update `UITableView`/`UICollectionView` correctly, you must process the changes in the correct order: **RICE** – **r**emovedIndexes, **i**nsertedIndexes, **c**hangedIndexes, **e**numerateMovesWithBlock (if `hasMoves` is `true`). Furthermore, the `hasIncrementalChanges` property of the change details can be set to `false`, meaning that the old fetch result should just be replaced by the new value as a whole. You should call `reloadData` on your `UITableView`/`UICollectionView` in such cases.\n\nNote: There is no need to make change processing centralized. If there are multiple components of your application that deal with photo entities, then each of them could have its own `PHPhotoLibraryChangeObserver`. The components can then query the `PHChange` objects on their own to find out if (and how) they need to update their own state.\n\n\n<a name=\"Wind-of-Change\"></a>\n## Wind of Change\n\nNow that we know how to observe changes made by the user and other applications, we should try making our own!\n\n### Changing Existing Objects\n\nPerforming changes on the photo library using PhotoKit boils down to creating a change request object linked to one of the assets or asset collections and setting relevant properties on the request object or calling appropriate methods describing the changes you want to commit. This has to happen within a block submitted to the shared `PHPhotoLibrary` via the `performChanges(...)` method. Note: You should be prepared to handle failure in the completion block passed to the `performChanges` method. This approach provides safety and relative ease of use, while working with state that can be changed by multiple actors, such as your application, the user, other applications, and photo extensions.\n\nTo modify assets, create a [`PHAssetChangeRequest`](https://developer.apple.com/library/ios/documentation/Photos/Reference/PHAssetChangeRequest_Class/index.html#//apple_ref/occ/cl/PHAssetChangeRequest). You can then modify the creation date, the asset's location, and whether or not it should be hidden and considered a user's favorite. Additionally, you can delete the asset from the user's library.\n\nSimilarly, to modify asset collections or collection lists, create a [`PHAssetCollectionChangeRequest`](https://developer.apple.com/library/ios/documentation/Photos/Reference/PHAssetCollectionChangeRequest_Class/index.html#//apple_ref/occ/cl/PHAssetCollectionChangeRequest) or a [`PHCollectionListChangeRequest`](https://developer.apple.com/library/ios/documentation/Photos/Reference/PHCollectionListChangeRequest_Class/index.html#//apple_ref/occ/cl/PHCollectionListChangeRequest). You can then modify the collection title, add or remove members of the collection, or delete the collection altogether.\n\nNote that before your changes are committed to the user's library, an alert might be shown to acquire explicit authorization from the user.\n\n### Creating New Objects\n\nCreating new assets is done similarly to changing existing assets. Just use the appropriate `creationRequestForAssetFromXXX(...)` factory method when creating the change request, and pass the asset image data (or a URL) into it. If you need to make additional changes related to the newly created asset, you can use the creation change request's `placeholderForCreatedAsset` property. It returns a placeholder that can be used in lieu of a reference to a \"real\" `PHAsset`.\n\n\n## Conclusion\n\nWe have discussed the basics of PhotoKit, but there is still a lot to be discovered. You should learn more by [poking around the sample code](https://developer.apple.com/library/ios/samplecode/UsingPhotosFramework/Introduction/Intro.html#//apple_ref/doc/uid/TP40014575), watching the [WWDC session video](https://developer.apple.com/videos/wwdc/2014/?id=511), and just diving in and writing some code of your own! PhotoKit enabled a new world of possibilities for iOS developers, and we are sure to see more creative and clever products built on its foundation in the coming months and years.\n\n\n\n\n\n\n"
  },
  {
    "path": "2015-03-10-artsy.md",
    "content": "---\ntitle:  \"Artsy\"\ncategory: \"22\"\ndate: \"2015-03-10 11:00:00\"\ntags: article\nauthor:\n  - name: Orta Therox\n    url: https://twitter.com/orta\n  - name: Ash Furrow\n    url: https://twitter.com/ashfurrow\n  - name: Laura Brown\n    url: https://twitter.com/1aurabrown\n  - name: Eloy Durán\n    url: https://twitter.com/alloy\n\n---\n\nThe Artsy mobile team is small, especially in contrast to the other teams in this issue of objc.io. Despite this, we’re notable for our impact on the community. Members of our iOS development team are — and have been — involved in almost all major open-source projects in the Cocoa community. \n\nArtsy is striving toward a technology culture of what we call [Open Source by Default](http://code.dblock.org/2015/02/09/becoming-open-source-by-default.html). We’re [not the only ones](http://todogroup.org/blog/why-we-run-an-open-source-program-walmart-labs/). Something being open source may sound like a purely technical distinction, but it affects culture more than you might think. Open source isn’t just a setting on a GitHub repository — it’s a sentiment shared by everyone on our team. \n\nOur team intrinsically believes in the ideas of open source. Though our individual motivations vary — from a conviction in the power of education to a determination to help others see from giants’ shoulders — we all maintain the value of open source. \n\nEveryone on our team believes that unless there is a good reason to keep something secret, it should be open. That doesn’t just apply to code, either. We hold regular meetings on Google Hangouts On Air and invite anyone to join and ask questions. Our team is also [organized](https://github.com/artsy/mobile) using an open-source repository. \n\n***\n\nOur small team makes a conscious effort to contribute significantly to the Cocoa developer community. We do this in three ways:\n\n1. We actively try to release components of our application that may be useful to others as open-source libraries. \n\n2. We use others’ libraries while sending feedback, bug reports, and pull requests. \n\n3. We encourage all team members to be active in the broader community, through speaking, blog posts, or books.\n\nThere are well-understood reasons to reduce the size and complexity of any given codebase. By writing small, well-tested components, we increase the overall stability and cohesion of our apps. Additionally, by writing reusable libraries, we reduce effort when building multiple apps.\n\nArtsy has three main iOS applications, along with some smaller side projects, so not having to reinvent the wheel with each new project saves time. Improvements or bug fixes to a library written for one app are easily shared with the others. We’ve done well in leveraging this strategy of reuse so far, but there is still room to improve. \n\nDividing our applications into small, reusable pieces has a lot of technical advantages, but it also allows us to create distinct owners for the different components. When we do this, we need to make sure that we consciously spread knowledge to other team members. On both libraries and main codebases, we use peer code reviews on pull requests, ensuring that teammates are familiar with as much of our code as possible. This is really important; when a member of our team was suddenly and unexpectedly summoned for three months of jury duty, we didn’t have to panic because we were not in a situation where that developer was the only person who was familiar with certain parts of our codebase.\n\nBeyond practical improvements to our team management, encouraging developers to explore different aspects of iOS development helps them grow as people and professionals. While we are certainly specialists in iOS, exposure to [other languages](https://github.com/orta/cocoapods-keys/pull/31) helps us stay sharp, relax, and discover new ways to solve familiar problems. \n\nWe work remotely; our team has not yet been all in the same room together. By operating like an open-source community, we embrace — not fight against — the asynchronicity and flexibility inherent within our team structure.\n\n***\n\nAt the start of 2015, we finished open sourcing the Artsy iOS app, [eigen](http://github.com/artsy/eigen). This is a process that took many months; we needed to take considered, incremental steps both to prove that there was business value in open sourcing our consumer-facing app, and to disprove any concerns around letting others see how the sausage is made. This wasn’t a hard decision for our company, because sharing knowledge is a core value that everyone in our company believes in.\n\nThe first steps toward open sourcing eigen were already complete; we had open sourced as much generic code from the application as we could. Most of our time preparing was spent ensuring that sensitive details in our git history and GitHub issues didn’t leak. In the end, we decided that creating a new repository with a fresh history and issue list was the safest route. \n\nEarlier, we said that being open source by default means that everything stays open unless there is a good reason to keep it secret. The code we do share isn’t what makes Artsy unique or valuable. There is code at Artsy that will necessarily stay closed forever. Practical examples include the use of a [commercially licensed](https://github.com/artsy/eigen/blob/e52f3e1fa30f7bae6fb1d6332f37e5309463df41/Podfile#L63-L67) font or the recommendation engine that powers [The Art Genome Project](https://www.artsy.net/theartgenomeproject). \n\nAfter setting the gears in motion to open an existing app, the next step was to build an app entirely in the open — from scratch. This gave us the opportunity to figure out how we could deal with project management, tooling issues, and actual development in the open. The experience felt very similar to working on a community-run open-source project. \n\nWe didn’t lay out a strict roadmap during initial development (something we’ve since fixed). This was partially due to a rush to complete the project on time, as well as lots of communication in our private Slack channel. Over time, we’ve tried to move more and more of our interactions into the public repository. \n\nDeveloping a complex iOS application in the open is something that not many companies have done. As a result, there were gaps in tooling that we’ve had to address. The most significant was keeping our API tokens a secret. In the process of solving this problem, we’ve made a [new](https://github.com/orta/cocoapods-keys), more secure way to store these keys. \n\nArtsy is a design-driven company, and our designers are frequent collaborators during the development process. With very little help, our designers were able to adjust to working in the open. They were already familiar with using GitHub to provide feedback asynchronously, so moving their remaining contributions to the open was fairly easy. They have similar ideals about giving back to the design community and consider working in the open to be as much a step forward for themselves as for us developers.\n\n***\n\nNow that we’ve covered the hand-wavey, philosophical stuff, let’s talk about the nitty gritty of day-to-day work in the open. \n\nWorking in the open isn’t so different from typical software development. We open issues, submit pull requests, and communicate over GitHub. When we see an opportunity to create a new library, the developer responsible for that library creates it under his or her own GitHub account, not Artsy’s. \n\nThis encourages a sense of product ownership. If a team member has a question about this library six months down the road, it's clear who to turn to. Likewise, that developer now feels personal ownership of that code, helping to cultivate a fulfilling and joyful work environment. Finally, developers know that the code they make belongs to them, and they can continue to use it after leaving Artsy. \n\nLet’s take a look at an example.\n\nLike responsible developers, we try and automate as much of our jobs as possible. That includes using continuous integration to minimize the introduction of new bugs and regressions. Currently, we use [Travis](https://travis-ci.org) for our CI needs, but other solutions exist. \n\nMany of our [tests](https://github.com/artsy/eigen/tree/master/Artsy%20Tests) are based on Facebook’s [iOS Snapshot Test Case](https://github.com/facebook/ios-snapshot-test-case) library. This allows us to construct a view (or view controller), simulate some interaction, and take a snapshot of the view hierarchy for reference. Later, when running our tests, we perform the same setup and snapshot the same view class. This time, we compare the generated snapshot to the earlier reference file. \n\nA problem we were facing involved the use of Travis. Sometimes, snapshot tests would fail remotely on CI but pass locally. Snapshot test failures leave behind reference and failed images that you can compare with [Kaleidoscope](http://www.kaleidoscopeapp.com) to determine the problem. However, on CI, we didn’t have access to these snapshots files. What could we do?\n\nDuring a weekend hike in Vienna, Orta and Ash discussed possible solutions to this problem. We ended up building a tool called [Second Curtain](https://github.com/ashfurrow/second_curtain) that would parse `xcodebuild` output. If it detects any failures, the failing snapshot and the reference snapshot are uploaded to an S3 bucket where they can be [diff’d visually](https://eigen-ci.s3.amazonaws.com/snapshots/50119516/index.html). This tool was the first time Ash had built something non-trivial in Ruby, giving him the chance to improve our tooling and to expand his knowledge.\n\n***\n\nPeople often ask why we operate in the open as we do. We’ve already discussed our technical motivations, as well as the sense of purpose it gives individual team members, but honestly, working in the open is just smart business. For example, we’re working on a single open-source library to handle [authentication](https://github.com/artsy/Artsy_Authentication) with our API. Not only does this make writing apps against our API easier for us, but it makes it easier for everyone else. Huzzah!\n\nThere is a tendency, particularly in iOS developer communities, to eschew third-party dependencies. Developers fall victim to \"Not Invented Here\" syndrome and end up wasting valuable hours of development time tackling the same problems others have already solved for them. On our mobile team, we vehemently reject NIH syndrome and instead favor the philosophy of “Proudly Discovered Elsewhere” (a phrase we appropriated from [another developer](https://twitter.com/jasonbrennan), naturally).\n\nIn our field, personal and professional growth are expected from employers; however, these processes are not just checkboxes on some HR form. If you’re doing them right, they should be daily activities that are intrinsic to your work. We accomplish this by working as an open-source team. \n\nBy developing our software in the open, the larger developer community is able to offer feedback on our work and we get to work with awesome developers all around the world. Sharing knowledge with others also helps cultivate an amazing workplace culture — one we’ve become known for — which in turn attracts great developers to work with. You can never have too much of a reputation, after all. \n\nWe often codify the knowledge we’ve gained on our blog or in presentations at conferences. Not only do these artifacts help to spread our knowledge to developers outside Artsy, but they also facilitate in bringing new members of our mobile team up to speed with how we work and the tools we use. We’ve even shared our experience sharing our knowledge, one of our more meta endeavors. \n\nWorking in the open isn’t all rainbows and puppy dogs, but the problems we’ve encountered have been minor. A common concern in open sourcing a codebase is the potential embarrassment of allowing others to see less-than-perfect code. However, we believe the best way to improve is by opening oneself up to critique. Critique is not a reason to feel embarrassed — and really, every developer understands that when you have to ship on a deadline, sometimes hacky code makes it into production. \n\nWe do know that some of our competitors use our code, because they offer their fixes back to us. We do the same. In reality, the biggest challenge to a business open sourcing a project is the obligation of stewardship. This responsibility is mostly a matter of working with people and needs to be managed correctly — especially if a project gains enough popularity. Most projects don’t get large enough for their stewardship to become burdensome.\n\nIt’s easy to release code and then either abandon the project or ignore the community, but we gladly accept the obligations tied to running a project in the open.\n\nThe extra time necessary to create and maintain open-source code often helps us unwind and relax from our day-to-day tasks, and over-communicating on GitHub has helped the remote members of our team. \n\nIn our experience working as a small team in a company that believes in open source, any risks or costs associated with Open Source by Default are negligible. If actual disadvantages even exist, they are far, far outweighed by the technical advantages, business prospects, and personal joy of working in the open. \n\n***\n\nThere is a fundamental difference between releasing a library to the open and creating an open-source library — the same difference between the technical distinction of open source and the mentality of open source. The difference is summed up by answering the following question:\n\nAre you opening code only for your benefit, or are you doing it for the sake of everyone?\n"
  },
  {
    "path": "2015-03-10-dropbox.md",
    "content": "---\ntitle:  \"The Art of Code Review: A Dropbox Story\"\ncategory: \"22\"\ndate: \"2015-03-10 9:00:00\"\ntags: article\nauthor:\n  - name: Ashley Nelson-Hornstein\n    url: https://twitter.com/ashleynh\n---\n\nEvery single line of code in the Dropbox for iOS app began as a bug or feature added to [Maniphest](http://phabricator.org/applications/maniphest/), our task management system. When an engineer picks a task, responsibilities begin before a line of code is written. [Phabricator](http://phabricator.org/), the platform that contains our code review tools, has a lot of benefits, but it's not very good for evaluating interactions among objects at scale. To combat this, our engineers know who will be reviewing their tasks before they begin working.[^1] For reviewees, this ensures that they have a [rubber duck](http://en.wikipedia.org/wiki/Rubber_duck_debugging) on the team who has background on the reasons for the change and can assist with design decisions. For reviewers, this helps to break up the time spent evaluating a change into phases that occur over the project's lifecycle. Unsurprisingly, our experience has shown that upfront planning can help avoid iteration during review. Planning for the change can be as simple as a quick chat in front of a whiteboard, or as thorough as producing an architectural document. It's up to the team members assigned to decide what's best.\n\nAs work begins on the task, the engineer keeps our [style guide](https://dl.dropboxusercontent.com/s/5utnlwhr18ax05c/style-guide.html?dl=0) in mind. It's a rather large amalgamation of best practices and consistency rules, but it exists to take the guesswork out of how we code and make reviewing code easier.[^2] Because the project is large, nobody on the engineering team has a perfect mapping or understanding of the entire app. So our engineer will rely on teammates to help piece together how functionality behaves, correlating conversations with the logic read in the code.\n\nAt some point while working on this task, our engineer will likely have to make a non-obvious tradeoff or unpopular choice. The best time to capture this reasoning is the moment it happens — in preparation for future explanation to the reviewer. To account for this being easier said than done, our engineers are encouraged to make use of `//TODO`, `//HAX`, and `//FIXME` comments in the project. `//TODO` and `//FIXME` are self-explanatory — though the latter generates a compiler warning and must be resolved before our next release. `//HAX` comments are where things get interesting. We typically use them to cite a non-obvious workaround to an Apple API.[^3] Our comments are prefixed with the date and name of the person who wrote the comment,[^4] and we're always thankful for this additional context.[^5]\n\nAs the development journey continues, our engineer will be tempted by spotting what looks like a [quick improvement](https://www.youtube.com/watch?v=4F4qzPbcFiA) to an existing feature. Inevitably, this out-of-scope improvement will lead down a rabbit hole of realization that there are plenty of underlying consequences for making this \"quick fix.\" This is a classic case of DoingTooMuch™. The only cure for our engineer is to file a new task for the improvement, and refocus on the assigned task.\n\nIf our engineer has gotten this far, hooray! The requirements of the task have been resolved. However, writing the code is but one aspect of this process. Now, the work begins to [land the changeset](http://cdn.visualnews.com/wp-content/uploads/2011/10/realartistsship-iphone.jpg).\n\nOur engineer will use the command-line tool [Arcanist](http://phabricator.org/applications/arcanist/) to start the process of uploading the diff to [Differential](http://phabricator.org/applications/differential/), our code review tool. During this process, scripts and unit tests will run. The scripts format our code, helping us focus on the functional changes of the diff and removing the cognitive load of stylistic nitpicks. Specifically, we use [clang-format](https://github.com/travisjeffery/ClangFormat-Xcode) to enforce pointer spacing and line breaks, while a [homegrown script](https://www.dropbox.com/s/71etvp8smmh8xvi/sort_imports.py?dl=0) auto-sorts our imports alphabetically. The nice thing about both of these scripts is that the changes are made like magic, but our engineer is able to double-check before committing the changes.\n\nAfter the code has been auto formatted, existing unit tests will run against the diff. Of course, any failures need to be resolved by our engineer before proceeding.\n\nOnce the diff has been uploaded, but before it's been sent for review, our engineer has a few fields to fill out. First, an outline of the goals for the diff and how those goals were met needs to be written. Next, our engineer needs to attach a test plan. Our engineer did create a test plan, correct? Thought about all of the edge cases that could cause the code to break? Created a modular enough design to even be able to consider unit testing? If the answer to any of these questions is a bewildered \"No,\" it's time for our engineer to close that Differential browser tab and open Xcode back up.\n\nNow, with our engineer's well-thought-out test plan, the diff is ready to be submitted for review.\n\nAt this point, our focus moves to the engineer reviewer, who will work hard to give constructive feedback in a helpful manner. The use of [memes](https://www.dropbox.com/s/qf9iqkjedzo20ob/Meme.png?dl=0) will help. So will remembering that a fellow engineer, who is invested in the outcome of the diff and worked hard at solving the problem, is on the receiving end. Tone matters. Be nice.\n\nBecause our engineer reviewer has been involved since the inception of the task assignment, hopefully larger structural questions like \"Is this code as modular as it could be?\" or \"Does this code avoid unnecessary duplication?\" will be answered with \"Absolutely!\" If so, our engineer reviewer will dig deeper into the changes, completing a thorough evaluation that may include patching and trying out the change. Unless a change is obvious, rarely is a diff accepted without comment or a change request. So our focus moves back to the engineer who submitted the diff.\n\nWhile reading the diff comments, our engineer will remember that the feedback received does not reflect on him or her as a person. Code is hard to get right on projects of any size, and it's particularly difficult on larger projects. Reviews facilitate a discussion between engineers that provide an opportunity for growth. A thorough code review process requires significant engineering effort, but it's emblematic of a culture that has effective communication.\n\nAfter typically a few iterations of code review, depending on the size of the diff, our engineer's code is ready to land.[^6] Bask in our engineer's prideful feeling that every single line now being added to the Dropbox for iOS app began as a task in Maniphest. Now, let's go pick another [task](http://image.slidesharecdn.com/beplum-stevejobs-131221124654-phpapp02/95/best-of-steve-jobs-8-638.jpg?cb=1387651669).\n\n[^1]: Everyone on our team reviews code. New hires are typically ramped up on smaller changesets before they help out on reviews of larger tasks.\n\n[^2]: Though this doesn't stop us from having a property vs. ivar debate whenever a new team member joins.\n\n[^3]: Included in the citation is generally a link to a third-party source or radar, and specific repro steps. \n\n[^4]: `//HAX:(ashleynh) 2015-03-09` for example\n\n[^5]: Hello 👋 iOS 7\n\n[^6]: This is a great time for both our reviewee and reviewer engineers to add to their running lists of \"common issues to consider\" when submitting or reviewing future diffs.\n"
  },
  {
    "path": "2015-03-10-editorial.md",
    "content": "---\ntitle:  \"Editorial\"\ncategory: \"22\"\ndate: \"2015-03-10 13:00:00\"\ntags: editorial\n---\n\nWelcome to issue 22 of objc.io. This month, developers from some well-known companies in our community share with us how they work.\n\nBrent Simmons starts off with a tour of how app development is done [at The Omni Group](/issues/22-scale/omni-group/). Next, the team at Artsy writes about the positive impact [working in the open](/issues/22-scale/artsy/) has had on the company's culture. Kyle Van Essen describes how Square has [scaled its engineering process](/issues/22-scale/square/) to a quickly growing team and increased quality requirements. Ashley Nelson-Hornstein walks us through the [code review process](/issues/22-scale/dropbox/) at Dropbox. Finally, Adam Ernst [introduces us to Components](/issues/22-scale/facebook/), a React-inspired library the News Feed team at Facebook developed to make the UI code easier to write and more maintainable.\n\nWe’d like to thank all our contributors. And like last month, [Ole Begemann](http://oleb.net) helped coordinate with the authors. Thanks Ole!\n\nBest from Berlin,\n\nChris, Daniel, Florian, and Ole.\n"
  },
  {
    "path": "2015-03-10-facebook.md",
    "content": "---\ntitle:  \"React-Inspired Views\"\ncategory: \"22\"\ndate: \"2015-03-10 8:00:00\"\ntags: article\nauthor:\n  - name: Adam Ernst\n    url: https://twitter.com/adamjernst\n---\n\nUser interfaces can be hard to get right in any application. Combining display and interaction in a little rectangle on the user's screen seems simple, but even for small applications, it's easy to end up with a tangled mess of view code. In complex products with many contributing engineers, like Facebook's News Feed, these views can be *especially* hard to develop and maintain over time.\n\nRecently I've been working on a library called Components to make views simpler on iOS. It emphasizes a one-way data flow from [immutable models](https://code.facebook.com/posts/340384146140520/making-news-feed-nearly-50-faster-on-ios/) to immutable \"components\" that describe how views should be configured. It's heavily inspired by the [React Javascript library](http://facebook.github.io/react/) that has become popular on the web. Just like React, which abstracts away manipulation of the DOM using a concept called the \"virtual DOM,\" Components abstracts away direct manipulation of `UIView` hierarchies.\n\nIn this post, I'll focus on some of the benefits of switching to Components for rendering News Feed on iOS and share lessons I've learned that you can apply to your own apps.\n\n### No Layout Math\n\nSuppose we have four subviews and want to stack them vertically, stretching them the full width horizontally. The classic way of doing this is to implement `-layoutSubviews` and `-sizeThatFits:`, which clocks in at around [52 lines of code](https://gist.github.com/adamjernst/c7bd7e5f98de5dc82e3a). There's a bunch of math and it's not immediately obvious at first glance that the code is vertically stacking views. There is a lot of duplication between the two methods, so it's easy for them to get out of sync in future refactors.\n\nIf we switch to Apple's Auto Layout APIs, we can do a little better: [34 lines of code](https://gist.github.com/adamjernst/2d52beb72506863f0ac5). There is no longer any math or duplication — hurrah! But we've traded that for a different set of problems: Auto Layout is hard to set up,[^1] is difficult to debug,[^2] and suffers from poor runtime performance on complex hierarchies.[^3]\n\nComponents draws inspiration from the [CSS Flexbox specification](http://www.w3.org/TR/css3-flexbox/) for its layout system. I won't get into the nuts and bolts; check out [Mozilla's fine tutorial](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes) to learn more. Flexbox makes layout so much easier that the equivalent in Components weighs in at only 18 lines of code. There is no math and no string-based visual format language.\n\nHere's how you'd do the same vertically stacked layout in Components. To the unfamiliar eye, the syntax looks pretty weird — it'll be explained shortly:\n\n```objc\n@implementation FBStoryComponent\n\n+ (instancetype)newWithStory:(FBStory *)story\n{\n  return [super newWithComponent:\n          [FBStackLayoutComponent\n           newWithView:{}\n           size:{}\n           style:{.alignItems = FBStackLayoutAlignItemsStretch}\n           children:{\n             {[FBHeaderComponent newWithStory:story]},\n             {[FBMessageComponent newWithStory:story]},\n             {[FBAttachmentComponent newWithStory:story]},\n             {[FBLikeBarComponent newWithStory:story]},\n           }]];\n}\n\n@end\n```\n\n### But All Those Braces!\n\nRight. We use Objective-C++. [Aggregate initialization](http://en.cppreference.com/w/cpp/language/aggregate_initialization) gives us a great way to specify style structs in a terse and type-safe manner. Here are a few examples of other valid `style:` values:\n\n```objc\nstyle:{} // default values\nstyle:{.justifyContent = FBStackLayoutJustifyContentCenter}\nstyle:{\n  .direction = FBStackLayoutDirectionHorizontal,\n  .spacing = 10,\n}\n```\n\nUsing STL containers like `std::vector` and `std::unordered_map` give us more type safety than Objective-C's corresponding containers. We also get the benefits of stack allocation for temporary view model data structures, boosting performance.\n\nThere are some other stylistic oddities in Components (the use of `+newWith...` instead of `-initWith...` for brevity, nonstandard indentation) that make sense with more context — a subject for another blog post. Back to the good stuff.\n\n### Declarative, Not Imperative\n\nEven with the completely new syntax, it is pretty easy to observe what is happening in the Components version of our stacking view. There's one key reason why: it's *declarative*, not imperative.\n\nMost iOS view code reads like a series of instructions:\n\n- Create a header view.\n- Store it to the `_headerView` ivar.\n- Add it to the view.\n- Add constraints that equate the header's left and right sides to the superview's.\n- *... do similarly for other views*\n- Add more constraints that stack the views.\n\nComponents code is declarative:\n\n- A story is rendered with these four components stacked vertically and stretched horizontally.\n\nThink about the distinction as the difference between a list of materials and directions to workers, and a full blueprint. To stretch the analogy a bit, the architect shouldn't run around a building site telling the workers exactly how to do their jobs — it would be far too chaotic. Declarative techniques focus on *what needs to be done*, not *how* it should be done; as a result, you can focus on intentions instead of specific implementation details.\n\nWith Components, there are no local variables or properties to keep track of. You don't need to jump around between the places where views are created, constraints are added, and views are configured with a model. Everything is right there in front of you.\n\nMy advice to you: always prefer declarative style to imperative code. It's easier to read and maintain.\n\n### Composition over Inheritance\n\nHere's a quick quiz: what does the code below do?\n\n```objc\n- (void)loadView {\n  self.view = [self newFeedView];\n}\n\n- (UIView *)newFeedView {\n  return [[FBFeedView alloc] init];\n}\n```\n\nWith inheritance, it could do anything. Maybe `-newFeedView` was overridden in a subclass to return a completely different view. Maybe `-loadView` was overridden to call a different method. In large codebases, proliferating subclasses make it difficult to read code and understand what it is actually doing.[^4] Problems from inheritance cropped up often in News Feed before we used Components. For example, `FBHorizontalScrollerView` had many subclasses that overrode different methods, making the superclass difficult to read or refactor.\n\nComponents are always composed, never subclassed. Think of them as little building blocks that can be plugged together to make something great.\n\nBut heavy use of composition results in deep hierarchies, and deep `UIView` hierarchies slow scrolling to a crawl. So it's particularly handy that a component may specify that no view should be created for it at all.[^5] In practice, most components don't need a view. Take the `FBStackLayoutComponent` example from earlier; it stacks and flexes its children, but it doesn't need a `UIView` in the hierarchy to perform this task.\n\nEven though Feed's *component* hierarchy is dozens of layers deep, the resulting *view* hierarchy is only about three layers deep. We get all the benefits of using lots of composition but don't have to pay the cost.\n\nIf there's one lesson I learned from scaling a large codebase, it's this: avoid inheritance! Find ways to use composition or other patterns instead.\n\n### Automatic Recycling\n\nA key part of using `UITableView` is cell recycling: a small set of `UITableViewCell` objects are reused to render each row as you scroll. This is key to blazing-fast scroll performance.\n\nUnfortunately, it's really hard to get everything right when recycling complex cells in a codebase shared by many engineers. Before adopting Components, we once added a feature to fade out part of a story but forgot to reset `alpha` upon recycling; other stories were randomly faded out too! In another case, forgetting to reset the `hidden` property properly resulted in random missing or overlapping content.\n\nWith Components, you never need to worry about recycling; it's handled by the library. Instead of writing imperative code to correctly configure a recycled view that may be in *any* state, you declare the state you want the view to be in. The library figures out the minimal set of changes that need to be made to the view.\n\n### Optimize Once, Benefit Everywhere\n\nSince all view manipulation is handled by Components code, we can speed up everything at once by optimizing a single algorithm. It's a lot more rewarding to optimize one place and see the results everywhere than to confront 400 subclasses of `UIView` and think \"This is going to be a big project….\"\n\nFor example, we were able to add an optimization that ensured we don't call property setters (like `-setText:`) when reconfiguring views unless the value has actually changed. This led to a boost in performance, even though most setters are efficient when the value hasn't changed. Another optimization ensured that we never reorder views (by calling `-exchangeSubviewAtIndex:withSubviewAtIndex:`) unless absolutely necessary, since this operation is relatively expensive.\n\nBest of all, these optimizations don't require anyone to change the way code is written. Instead of taking time to learn about expensive operations and how to avoid them, developers can focus on getting work done — a big organizational benefit.\n\n### The Challenge of Animation\n\nNo framework is a silver bullet. One challenging aspect of reactive UI frameworks is that animations can be more difficult to implement in comparison to traditional view frameworks.\n\nA reactive approach to UI development encourages you to be explicit about transitioning between states. For example, imagine a UI that truncates text but allows the user to tap a button to expand inline and see all the text. This is easily modeled with two states: `{Collapsed, Expanded}`.\n\nBut if you want to animate the expansion of the text, or let the user drag and drop to control exactly how much text is visible, it's not possible to represent the UI with only two states. There are hundreds of states corresponding to exactly how much text is visible at any point in the animation. The very fact that reactive frameworks force you to reason about state changes upfront is exactly what makes it difficult to model these animations.\n\nWe've developed two techniques to manage animations in Components:\n\n- Static animations can be expressed declaratively using an API called `animationsFromPreviousComponent:`. For example, a component may specify that it should be faded in when it appears for the first time.\n- Dynamic animations are handled by providing an \"escape hatch\" back to traditional imperative and mutable code. You won't get the benefits of declarative code and explicit state management, but you'll have all the power of UIKit at your disposal.\n\nOur hope is to develop powerful tools for expressing even dynamic animations in a simple and declarative way — we're just not there yet.\n\n### React Native\n\nAt Facebook, we recently announced [React Native](https://code.facebook.com/videos/786462671439502/react-js-conf-2015-keynote-introducing-react-native-/), a framework that uses the React Javascript library to manipulate `UIView` hierarchies in native apps instead of DOM elements on web pages. It may surprise you to hear that the Components library I'm describing is *not* React Native, but a separate project.\n\nWhy the distinction? It's simple: React Native hadn't been invented yet when we rebuilt News Feed in Components. Everyone at Facebook is excited about the future of React Native, and it's already been used to power both [Mobile Ads Manager](https://www.facebook.com/business/news/ads-manager-app) and [Groups](http://newsroom.fb.com/news/2014/11/introducing-the-facebook-groups-app/).\n\nAs always, there are tradeoffs; for example, Components' choice of Objective-C++ means better type safety and performance, but React Native's use of Javascript allows live reload while running an app under development. These projects often share ideas to bring both forward.\n\n### AsyncDisplayKit\n\nSo what about [AsyncDisplayKit](http://asyncdisplaykit.org), the UI framework developed to power Facebook's [Paper](https://www.facebook.com/paper)? It adds the ability to perform measurement and rendering on background threads, freeing you from UIKit's main thread shackles.\n\nPhilosophically, AsyncDisplayKit is much closer to UIKit than to React. Unlike React, AsyncDisplayKit doesn't emphasize declarative syntax, composition, or immutability.\n\nLike AsyncDisplayKit, Components performs all component creation and layout off the main thread. (This is easy because both our model objects and components themselves are completely immutable — no race conditions!)\n\nAsyncDisplayKit enables complex gesture-driven animations, precisely the area that is a weak spot for Components. This makes the choice easy: If you're designing a complex gesture-driven UI, AsyncDisplayKit might be right for you. If your interface looks more like Facebook's News Feed, Components could be a good fit.\n\n### The Future of Components\n\nThe Components library has been adopted successfully in all feeds in the app (News Feed, Timeline, Groups, Events, Pages, Search, etc.) and is rapidly making its way to other parts of the Facebook app. Building UIs using simple, declarative, composable components is a joy.\n\nYou might think some of the ideas behind Components sound crazy. [Give it five minutes](https://signalvnoise.com/posts/3124-give-it-five-minutes) as you think it over; you may have to challenge some assumptions, but these ideas have worked well for us and might benefit you too. If you want to learn more, [watch this QCon talk](http://www.infoq.com/presentations/facebook-ios-architecture), which explains some more detail behind Components. The [Why React?](http://facebook.github.io/react/docs/why-react.html) blog post and the resources it links to are another great reference.\n\nI'm excited to share the code behind Components with the community, and we're preparing to do so soon. If you have thoughts to share, [I'd love to hear from you](mailto:adamjernst@fb.com) any time — especially if you have ideas about animations!\n\n_Postscript: ComponentKit was open sourced at F8. [Check it out on Github.](https://github.com/facebook/componentkit)_\n\n[^1]: Interface Builder makes Auto Layout easier, but since XIBs are impractical to merge, you can't use them with large teams.\n\n[^2]: There is no shortage of [articles](http://www.informit.com/articles/article.aspx?p=2041295) and [blog posts](https://medium.com/@NSomar/auto-layout-best-practices-for-minimum-pain-c130b2b1a0f6) about debugging Auto Layout.\n\n[^3]: We prototyped a very simplified version of News Feed that was powered by Auto Layout and it was challenging to get it to 60fps.\n\n[^4]: objc.io has [covered this topic before](/issues/13-architecture/subclassing/), and the [Wikipedia article](http://en.wikipedia.org/wiki/Composition_over_inheritance) also does a good job of covering it.\n\n[^5]: Similarly, in React, not every component results in the creation of a DOM element.\n"
  },
  {
    "path": "2015-03-10-omni-group.md",
    "content": "---\ntitle:  \"Inside Omni\"\ncategory: \"22\"\ndate: \"2015-03-10 12:00:00\"\ntags: article\nauthor:\n  - name: Brent Simmons\n    url: https://twitter.com/brentsimmons\n---\n\nThe Omni Group is an employee-owned company where people bring their dogs to work.\n\nIn other words: when you think about managing large projects, think about culture *first*. It almost doesn’t matter what the details are — how we’re organized, what we use for source control management, and so on — because a great culture makes for a happy team that will figure out how to work together. And Omni has done a *great* job with that.\n\nOmni’s culture deserves an article of its own, but I’m not going to get into culture much. Instead, this is an engineering-centric tour of the details of how we manage our apps.\n\n## Flat Organization\n\nAll engineers report to Tim Wood, CTO and founder. Each product has a project manager.\n\nTeams for the various apps are fluid. They don’t change often or willy-nilly, but they do change.\n\n## In-House Communication\n\nPeople talk face-to-face, since everybody works at the office. There are meetings from time to time, some regularly scheduled, some not. Everybody attends the weekly all-hands meeting, which lasts for around 20 minutes, where section heads and project managers report on how things are going. It’s followed by the weekly engineering meeting, which also lasts around 20 minutes. Most of the engineering reporting is about things of general interest (as opposed to catching up on what each person is doing, though there’s some of that too).\n\nFace-to-face communication is helped by Omni’s core hours: people are expected to be in the office 11 a.m. to 4 p.m. every day, so you know that you can find people during that period. (Otherwise, you can come in and leave as early or late as you want, as long as you put in a full week of work.)\n\nThen there’s email, including several mailing lists, and our internal chatroom-plus-direct-messages thing. (We recently replaced Messages and Jabber with a system that actually remembers history. And allows animated GIFs.)\n\n## Bugs\n\nEvery engineering organization larger than zero people revolves around its bug tracker. Omni uses a housemade Mac app named OmniBugZapper (OBZ) that has the features you’d expect. It’s not polished the same way the public apps are, but it is nevertheless an effective tool.\n\nA typical workflow goes like this: you look at the bugs in your current milestone, pick one with a high priority, and then open it so that people can see you’re working on it.\n\nOnce it’s finished, you add a note to the bug saying what you did to fix it, and perhaps add a note on how to test it, and you include the SCM revision number.\n\nYou switch the status to Verify — and then a tester takes over and makes sure the bug really is fixed. It could get reopened if it’s not actually fixed. If the fix creates a new bug, then a new bug is added to OBZ.\n\nOnce a bug in Verify status passes testing, it gets marked as Fixed.\n\n(Worth noting: the relationship between engineers and testers is not adversarial, as I’ve heard it is in some companies. There are moments when you may think the testers want to kill you with work — but that’s normal and as it should be. It means they’re doing a great job. We all have the exact same goal, which is to ship great apps.)\n\nThere are some bug fixes that require an engineer to verify, but these are relatively rare; most bugs are verified by testing. (The engineer who verifies a bug can’t be the same engineer who fixed it.)\n\nSome bugs are never intended to be fixed or verified. There are discussion bugs, where we talk about a change or addition, and there are reference bugs, which document behavior and appearance of a feature.\n\n## Milestones\n\nOmniBugZapper has the concept of milestones, of course, and it has a Milestones Monitor window where you can see progress and status of current milestones.\n\nEach milestone has triage, planned, and verify categories. Bugs are triaged and become planned, or put off till later.\n\nThe process of deciding which bugs and features go into which milestone and into which release is collaborative, and everybody who wants to participate does participate. That said, project managers make many (if not most) of the decisions. For the bigger issues, there is often more discussion, and we often reach consensus — but we don’t make design decisions by voting, and CEO Ken Case has the ultimate authority. Ken creates the roadmap.\n\n## SCM\n\nWe use Subversion. All the apps and the website are in one giant repository. I wouldn’t be surprised if everybody’s working copy is just a partial version, rather than the entire thing.\n\nYou might think Subversion is unfrozen caveman engineer stuff, and it wouldn’t surprise you to learn that people have thought about switching. But Subversion gets the job done, and there’s something to be said for the simplicity of managing one big repository for everything.\n\nWe have a number of scripts that help with this. For instance, when I want to get the latest changes to OmniFocus, I type <code>./Update OmniFocus</code> and it updates my working copy (I usually do this once a day, first thing). I don’t have OmniGraffle in my working copy, since I haven’t had a need to look at it. But I could get it by typing <code>./Update OmniGraffle</code>.\n\nSubversion may not make branching as easy as Git and Mercurial do, but it’s not like it’s crazily difficult, either. We make a branch when an app gets close to release, in order to protect it from other changes. People make private branches and directories whenever they want to for whatever reason.\n\nCommit messages are sent via email to engineers and everybody else who wants them.\n\n## Crashes\n\nSince apps sit at the top of a mountain of system frameworks that have their own bugs, and since apps run not on an ideal machine but on actual people’s computers, there’s no way to guarantee that an app will never crash.\n\nBut it’s our job to make sure that *our* code doesn’t crash — and that if we note a crashing bug in system frameworks, we find a way to work around it.\n\nWe have stats and graphs that show us how long an app goes, on average, before crashing. There’s another homemade app called OmniCrashSorter, where we can look at symbolicated crash logs, including exception backtraces and any notes from the user about what was happening when it crashed.\n\nHere’s the thing about crashes: unfortunately, apps never crash for the people writing the code (this seems to be a natural law of software development). This makes crash logs from users — and steps to reproduce — absolutely critical. So we collect these and make them easy to get to.\n\nAnd: we crash on exceptions, on purpose. Since our apps autosave, it’s safer to crash rather than try to recover and possibly corrupt data.\n\n## Code\n\nWe have a small style guide, on our internal wiki, and I’ve already internalized it so I forget what it says.\n\nExcept for this one thing. Methods should start like this:\n\n<code>- (void)someMethod;</code><br />\n<code>{</code>\n\nIt may not be widely known that that semicolon is allowed in Objective-C. It is.\n\nOne of the points of this style is that it makes it easy to copy a prototype to the header file or class extension. Another is that you can select the entire line, cmd-E, and then do a find to look it up in the header file (or .m file if you’re going the other direction).\n\nI don’t love this. To me — a compulsive simplifier — the semicolon is an extra thing, and all extra things must be carved away. (How satisfying to imagine my X-ACTO blade slowly drawing a line around the ; and then, with a northeast flick, throwing it in the air, off the side of the desk, where it then flutters into the recycling bin.)\n\nBut this is just me idly complaining. The point — which I’m on board with, completely — is that we *have* a style guide, and everybody uses it, and we can read each other’s code without being tempted to argue the fine points of semicolon placement. We don’t get tempted to waste time reformatting existing code just to match our tastes.\n\n\n### Shared Frameworks\n\nAll of Omni’s apps are one big app, in a way; there are lots of shared frameworks they depend on. A bunch of them are open source, and you can [read about them](http://www.omnigroup.com/developer/) and [get the code from GitHub](https://github.com/omnigroup/OmniGroup). There are additional internal frameworks — some used by every app, some used by just some apps.\n\nShared frameworks make it easier to develop a bunch of different apps, and they make it easier to switch teams, since so much will be the same.\n\nThere’s a downside, of course, which is that a change to a framework could break a bunch of apps all at once. But the only way to deal with that is to deal with it. Bugs are bugs.\n\n(Since we do a branch when an app gets close to release, we have protection against framework changes during those last few weeks of development.)\n\n### ARC\n\nNew code is usually ARC code. There is plenty of older code that hasn’t been converted — and that’s mostly fine, because making changes to working, debugged code is something you do only when you need to. But sometimes it’s worth doing the conversion. (I’ve done some and will do more. I think it’s easier to write crash-free code using ARC.)\n\n### Swift\n\nThough a bunch of engineers have written Swift code, Swift has yet to appear in any apps or frameworks.\n\nThis could change tomorrow, or it might take a year. Or two. Or it might have changed by the time you’re reading this.\n\n### Tests\n\nOmniFocus has unit tests that cover the model classes; other apps have more or less similar test coverage. The problem we face is the same problem other OS X and iOS developers face, which is that so much of each app is UI, and doing automated UI testing is difficult. Our solution for our Mac apps is AppleScript-based tests. (That’s one of the best reasons for making sure an app supports AppleScript, and writing tests is a good way to make sure that the support makes sense and works.)\n\nTests will never be quite as important to Cocoa developers as to Ruby, JavaScript, and Python developers, since the compiler and static analyzer catch so many things that the compilers for scripting languages don’t catch.\n\nBut they’re important nevertheless.\n\n### Assertions\n\nYou can see a bunch of the assertions we use — OBASSERT_NOT_REACHED, OBPRECONDITION, OBASSERT, and friends — [in our repository](https://github.com/omnigroup/OmniGroup/blob/master/Frameworks/OmniBase/assertions.h).\n\nWe use these to document assumptions and intentions. They’re for later versions of ourselves and for other engineers, and we use them liberally.\n\nThe downside to so many assertions is that you get failures, and you have to figure out why. Why is the code not working as expected? Is the assertion just wrong, or does it need to be extended or modified?\n\nThere are moments when I look at a bunch of console spew and wonder if it’s a good idea. It is.\n\n## Builds\n\n### Xcode Organization\n\nEach app has a workspace file that includes OS X and iOS projects and embeds the various frameworks that it uses.\n\n### Config Files\n\nWe use .xcconfig files pretty heavily. You can see a bunch of them [in our repository](https://github.com/omnigroup/OmniGroup/tree/master/Configurations).\n\nThis is one of those things I haven’t used in the past, and haven’t had to even look at in my several months at Omni. They just work.\n\n### Debug Builds\n\nWith OmniFocus, debug builds use a separate database and set of preferences, so developers don’t have to subject their real data to the contortions they put debug data through.\n\n(Our other apps are document-based apps, so the exact same issue doesn’t apply, but some apps aside from OmniFocus also use separate app IDs for the debug builds.)\n\n### Static Analyzer\n\nAnalysis is set to deep, even for debug builds. This is as it should be.\n\n### Automated Builds\n\nWe have a build server, of course, and we’re alerted when builds break. There’s another in-house Mac app, OmniAutoBuild, where we can see the status of the various builds and see where they broke when they break.\n\nBuilding full, releasable applications is done with scripts. And we can set flags so that builds go to the staging server, so external beta testers can download the latest test versions.\n\niOS betas go out through TestFlight.\n\n## No Magic\n\nI wish I could say there are some secret incantations — I could just tell you what they are.\n\nBut, instead, managing large projects at Omni is like you think it is. Communication, defined broadly — talking in person, chatting, using OmniBugZapper, using assertions, doing code reviews, following coding guidelines — is the big thing.\n\nThe next thing is automation: make computers do the things computers do best.\n\nBut, again, the zeroth thing — the thing that comes before choosing a bug tracker or SCM system or anything — is company culture. Build one based on trust and respect, with great people, and they’ll want to work together on large projects, and they’ll make good decisions, and they’ll learn from the bad ones.\n\nThe good news is that it’s all just work.\n\nAnd lunches. Work *and* lunches. We all eat together. It makes a difference.\n\nP.S. Many thanks to the folks at Omni who read drafts of this article and provided feedback: [Rachael Worthington](https://twitter.com/nothe), [Curt Clifton](https://twitter.com/curtclifton), [Jim Correia](https://twitter.com/jimcorreia), [Tim Ekl](https://twitter.com/timothyekl), [Tim Wood](https://twitter.com/tjw), and [Ken Case](https://twitter.com/kcase). Anything weird or wrong is my fault, not theirs.\n"
  },
  {
    "path": "2015-03-10-square.md",
    "content": "---\ntitle:  \"Scaling Square Register\"\ncategory: \"22\"\ndate: \"2015-03-10 10:00:00\"\ntags: article\nauthor:\n  - name: Kyle Van Essen\n    url: https://twitter.com/kyleve\n---\n\nOver the six-year history of [Square Register](https://squareup.com/register), the codebase and the company have undergone significant changes, as the app has grown from a simple credit card terminal to a full-featured point-of-sale system. The company has grown from 10 people to more than 1,000, and we’ve had to scale quickly. Here are some of the things we’ve learned and the processes we’ve implemented along the way.\n\n## The Team\nAs we grew, we realized that once an engineering team reaches a certain size, it’s ineffective to organize the team by platform. Instead, we have “full-stack” teams that are responsible for specific sets of features within the app. These teams consist of iOS engineers, Android engineers, and server engineers. This gives teams more freedom and creates improved focus on building a deeper, more comprehensive product. We’ve organized feature-oriented teams around restaurants, retail stores, international support, hardware, and core components (to name a few). Giving full vertical ownership to a group allows those engineers to make more holistic technical decisions, and it gives them a tangible sense of ownership over the product.\n\n## Our Release Process\n\nBefore 2014, Register releases followed the waterfall methodology; we decided on a large set of features to build, set a future deadline (three to six months), and then worked to build these features.\n\nThis process did not scale well. Waterfall became laborious and slow as we added features and engineers to the product. Since all features developed during the release had to ship together, a single delayed or buggy feature would delay the entire release. To ensure that teams continued to stay autonomous, we looked for a different, more efficient approach.\n\n### All Aboard the Release Train\nTo stay efficient, we always want to make sure our processes match our size. Starting in 2014, we adopted a new model consisting of “release trains.” Release trains optimize for feature team autonomy while enabling continuous shipping. This means individual features can be released when they’re ready, without having to wait for other work to be completed.\n\nSwitching to release trains required changes to our workflow:\n\n- **Incremental Development** — Features are developed incrementally, rather than in long-lived isolated feature branches.\n- **Isolation & Safety** — New features must be behind a server-controlled feature flag. The feature flag remains disabled outside of development until the feature is ready to ship.\n- **No Regressions** — If a change introduces a regression in an existing feature, the regression must be fixed immediately.\n- **Target a Timeframe** — Instead of attempting to ship a feature in a specific version, teams instead target a release timeframe that contains two to three features.\n\nThis means that our master branch stays in a stable state. This is where the train part comes in.\n\n1. **Branch** — At the beginning of every month, a release branch is created off of the master branch.\n2. **Board the Train** — If a feature is ready to ship (very few issues remaining), its feature flag is enabled. If it is not, it must wait for the next train.\n3. **Test and Fix** — The rest of the month is spent fixing bugs in the release branch. If a team is not shipping anything in the train, it will continue to work on the master branch.\n4. **Merge** — The changes in the release branch are continuously merged back down into the master branch.\n5. **Ship** — We ship that release branch to the App Store at the end of the month.\n6. **Repeat** — We repeat the process every month after that.\n\nThis has several benefits:\n\n- There is no more than one month of code change between each release, leading to fewer bugs.\n- Only bug fixes go into the train’s release branch. This means longer “bake time” to prove that changes are stable.\n- There’s less need to ship bug fix releases; most bug fixes can wait until the next train.\n- By building and merging features incrementally, we avoid large disruptive merges that destabilize the master branch.\n- Regressions or high-priority bugs on the master branch are not acceptable. Fixing these are the team’s highest priority.\n- There’s less pressure to ship features on a specific date. Instead of having to wait months for the next release, the feature can be included in next month’s train. This means that teams don’t need to rush through their work. They simply ship when they’re comfortable with the quality of their features. This improves team productivity, morale, and code quality.\n\nAt the beginning of 2015, we refined this process even more: Release branches are now cut and shipped on two-week intervals. That means teams will have 26 opportunities to ship this year. Compared with just three or four releases per year in 2013 and earlier, this is a huge win. More opportunities to ship means more features delivered to customers.\n\n## Our Engineering Process\nSquare merchants rely on Register to run their businesses. As such, it needs to be reliable at all times. We have rigorous processes for ensuring quality at the design, implementation, and testing phases.\n\n### Large Engineering Changes Require Design and Review\n_“Writing is nature’s way of letting you know how sloppy your thinking is” –Guindon_\n\nThis is one of my favorite quotes, and it applies to building software too! If the software you’re building exists only in your head, that software will be flawed. The image in your head is very ambiguous and ephemeral; it’s constantly changing, and thus needs to be written down to be clarified and perfected.\n\nEvery large change at Square must go through an engineering design review. This sounds daunting if you’ve never done it before, but it’s actually quite easy! The process generally involves writing up the following in a design document:\n\n- **Goals** — What are you trying to accomplish? What are the customer-facing effects of your change?\n- **Non-Goals** — What aren’t you trying to accomplish? What are your boundaries?\n- **Metrics** — How will you measure success or failure?\n- **Investigations** — What other solutions (if any) did you investigate? Why didn’t you choose them?\n- **Choice** — What have you decided to do? Why have you decided to do it?\n- **Design** — What are the details of your chosen design? Include an API overview, technical details, and (potentially) some example headers, along with anything else you think will be useful. This is where you sell the design to yourself and your fellow engineers.\n\nWe then include two to four reviewers who should review the document, ask questions, and make a final decision. These reviewers should be familiar with the system you’re extending.\n\nThis may seem like a lot of work, but it’s well worth it. The end result will be a design that’s more robust and easier to understand. We consistently see fewer bugs and less complexity when a change goes through design review. Plus, as a side effect, we end up with peer-reviewed documentation of the system. Neat!\n\n### Our Code Review Process\nOur code review process is rigorous for a few reasons:\n\n- **App Store Timing** — If we do ship a bug, the App Store review process delays delivering fixes to customers by about a week.\n- **Register Is Critical** — Finding bugs is important because Register is a critical piece of restaurants, retail shops, and so on.\n- **Large App** — Catching bugs post-merge in a large application like Register is difficult.\n\nWhat is our process for pull requests? Every PR must:\n\n- **Be Tracked** — Pair a PR with a JIRA issue.\n- **Be Described** — There must be a clear description of the what and why behind the change.\n- **Be Consumable** — Pull request diffs must be 500 lines or less. No large diffs are allowed. Reviewers will overlook bugs if a change is much larger than 500 lines.\n- **Be Focused** — Do not pair a refactor or rename with a new feature. Do them separately.\n- **Be Self-Reviewed** — All PR authors are expected to do a self-review of their change before adding outside reviewers. This is meant to catch stray NSLogs, missing tests, incomplete implementations, and so on.\n- **Have Two Specific Approvers** — One of these reviewers must be an owner of the component being changed. We require explicitly listed reviewers to ensure engineers know exactly what is and isn’t in their review queue.\n- **Be Tested** — Include tests that demonstrate the stability and correctness of the change. Non-tested pull requests are rejected.\n\nSimilarly, reviewers are expected to:\n\n- **Be Clear** — Comments must be clear and concise. For new engineers, reviewers should include examples to follow.\n- **Explain** — Don’t just say “change X to Y”; also explain why the change should occur.\n- **Not Nitpick Code Style** — This is what automated style formatters are for (more on this later).\n- **Document** — Each code review comment must be marked as one of the following:\n— Required _(“This must be fixed before merge.”)_\n— Nice to have _(“This should be fixed eventually.”)_\n— A personal preference _(“I would do this, but you don’t have to.”)_\n— A question _(“What does this do?”)_\n- **Be Helpful** — Reviewers must enter a code review in a helpful mindset. It is the job of a reviewer to help code be merged safety, not to block it.\n\nBefore merging, all tests must pass. We block pull requests from being merged until a successful run of our unit tests and our automated integration tests (which use [KIF](https://github.com/kif-framework/KIF)).\n\n## Some Process Tips\nWe’ve begun doing the following things to help streamline and accelerate the Register development process.\n\n### Document Common Processes as Much as Possible\nOne thing we learned as the Register team grew was how poorly “word-of-mouth” knowledge scales. This isn’t a problem if you’re only onboarding a few engineers a year, but it quickly becomes time-consuming if you’re onboarding a few engineers a month, especially if they’re only on the project temporarily (e.g. a server engineer helping to build a particular feature). It becomes important to have an up-to-date document containing the standards and practices of the team. What should this document include?\n\n- Code review guidelines (for submitters and reviewers)\n— _“How many reviewers do I need? When can I merge this?”_\n- Design review guidelines\n— _“How should I design this feature?”_\n- Testing guidelines\n— _“How do I test this? What testing frameworks do we use?”_\n- Module/component owners\n— _“Who can I talk to about X? Who built it?”_\n- Issue tracking guidelines\n— _“Where do I look up and track what I have to do?”_\n\nYou’ll likely notice a pattern here: anything that can be answered in 10 minutes or less should be clearly documented.\n\n### Automate as Many Inefficiencies as Possible\nManual processes that take a couple of minutes with a few engineers can take much longer with many engineers. Any time you see something trivial that eats up a lot of time, automate it if possible.\n\n#### We Automated Our Style Guide\nOne of our biggest “automate it” wins recently has been our Objective-C style guide: We now use [clang-format](http://clang.llvm.org/docs/ClangFormat.html) to automatically format all code committed into Register and its submodules. This eliminates code review comments along the lines of “missing newline” or “too much whitespace,” meaning reviewers can focus on things that actually improve the quality of the product.\n\nWe merge many pull requests each day. These “style nit” comments used to take anywhere from 10–20 minutes per pull request (between the reviewer and the author). That means we’re saving two or more hours a day from style guide automation alone. That’s 10 hours a week. It adds up quickly!\n\n#### We Automated Visibility into Code Reviews\nAnother example of automation saving time is our new “Pull Request Status” email that gets sent out daily.\n\nBefore this email existed, 10 to 15 of us would crowd around a stand-up table for 10 minutes each morning and assign reviewers to open pull requests. Instead, we now send out a morning email containing a list of all open PRs, along with who is assigned to review them. No more meeting needed. This means we’re getting back more than 2 hours of engineering time per day, or 10 hours per week.\n\nAnother benefit of this daily PR status email is that we can easily track what’s happening with reviews: How long they take, which engineers are contributing the most, and which are reviewing the most. This helps to reveal time allocation issues which may be slowing the team down (e.g. Is one engineer doing half of the team’s reviews?).\n\n### Centralize on a Single Bug Tracker\nIt’s impossible to ship a bug-free product if your bugs are split across multiple trackers. It’s incredibly important to have one place where we can go to see everything pertaining to the current release: the number of bugs, the number of open bugs per engineer (is anyone overloaded?), and the overall trend of bugs (are we fixing them faster than they’re being opened?).\n\n## Maintaining Quality in a Shared Codebase\nWhen only a few engineers are working on a project, it’s easy to maintain quality: all engineers know the codebase well, and they all feel strong ownership over it. As a team scales to 5, 10, 20, or more engineers, maintaining this quality bar becomes more difficult. It’s important to ensure every component and feature has an explicit owner who is responsible for maintaining its quality.\n\n### Every Component Needs an Owner\nIn Register, we recently decided to have explicit owners for each logical component of the app. These owners are documented in a list for anyone to easily look up. What is a component? It might be a framework, it might be a customer-facing feature, or it might be some combination of the two. The exact separation isn’t important; what’s important is to ensure that every line of code in your app is owned by someone. What do these owners do?\n\n- They review and approve code changes and design documents.\n- They know the “hard parts” and how to work around them.\n- They can provide an overview for engineers new to the component.\n- They ensure quality is always increasing.\n\nWe’ve seen great results from electing explicit owners for components: code quality is consistently higher (and the bug rate is lower) in components which have owners versus those that are implicitly owned by everyone.\n\n### Keep the Master Branch Shippable\nThis is another recent change for us: We’ve started enforcing a strict “no regressions” rule on the master branch. The benefit of this? Our master branch is now always very stable. If anyone finds a bug, there’s no question if it should be reported or not. It also reduces QA load, as less time is spent figuring out if issues should be filed, if they’re duplicates, etc. If a bug is found, an issue is filed.\n\nThis policy goes hand in hand with the release train model: At nearly any time, we can cut a release branch from the master branch and be just a few days from shipping to the App Store. This is incredibly valuable for an app as large as Register, and it helps us move as fast as possible.\n\nKeeping the master branch in a shippable state also helps avoid the “broken windows” problem as we scale; fixing bugs as they’re discovered ensures engineers hold themselves to a higher standard.\n\n### Build for Testability from the Beginning\nIt’s incredibly important to ensure every component within Register is built and designed with testability in mind. Without this, we would need to expand manual QA efforts exponentially: two features can interact in four ways, three features can interact in eight ways, etc. Obviously, this isn’t reasonable, reliable, or scalable.\n\nAs we’re working through the engineering design for a feature, we’re constantly asking ourselves: “Is this testable? Am I making automated testing easy?”\n\nBuilding for testability also has an additional benefit: It introduces a second consumer of all APIs (i.e. the tests themselves). This means engineers are forced to spend more time thinking through the design of an API, making sure it’s useful in more than one case. The result is that it will be easier for other engineers to reuse the API, saving time in the future.\n\nFor us, testing isn’t an option; it’s a requirement. If you’re committing code to Register, you need to include tests.\n\n### The Importance of CI on Pull Requests\nA mental exercise: If an engineering organization has 365 engineers, each engineer only has to break the master branch once a year for it to be broken every single day. This obviously wouldn’t be acceptable, and would slow down and frustrate the engineering team greatly.\n\nWhat’s an easy way to prevent the master branch from breaking? By not merging broken code in the first place, of course! This is where pull request CI comes in. Every Register pull request has a CI job that is kicked off for new commits. Around 15 minutes later, the engineer submitting the PR can feel confident that he or she is not introducing any regressions.\n\nThis has been incredibly valuable as we onboard new engineers into the codebase. They can commit code without worrying that they’re going to introduce master-breaking changes.\n\n## Some Observations as the Team Has Grown\nThese are some personal observations I’ve made as the Register iOS team has grown and expanded around me over the last three years.\n\n### Not Everything Will Be Perfect\nIn a large app, you’ll have a lot of code. Some of this code will be old. But old doesn’t have to mean bad. As long as you have good test coverage, old code will continue to work just fine. Don’t spend time “cleaning up” code that is fulfilling its needs and isn’t slowing anyone down. The best you’d be able to do during this cleanup is not break anything. Spend this time building new features instead.\n\n### Make Time to Learn Outside of Your Codebase\nIn a big codebase, it’s very easy to spend all your time working within it, and never learning from outside influences.\n\nHow do you fix this? Take time during the week (I set aside an hour every day) to learn from resources outside of your codebase. What can you learn from? Watch talks that sound interesting. Read papers on subjects you find interesting. You’ll be surprised by the parallels and benefits you’ll begin drawing back into your day-to-day work. Sometimes these little things make the biggest difference.\n\n### Addressing Tech Debt Takes Time\nThere’s rarely an immediate solution to anything, and this includes technical debt. Don’t let yourself get frustrated if addressing tech debt takes a long time, especially in a large codebase.\n\nThink about accumulating tech debt like gaining weight: you don’t gain 100 pounds overnight; it happens gradually. Like losing weight, it also takes a great deal of time and effort to eliminate tech debt — there is never an instantaneous solution. Track your progress while paying it off, and make sure it’s progressing downward at a reasonable pace.\n\n## That's All, Folks\nIf you have any questions, feel free to reach out to me at [k@squareup.com](mailto:k@squareup.com). Thanks for reading!\n\n(Thanks to [Connor Cimowsky](https://twitter.com/connorcimowsky), [Charles Nicholson](https://twitter.com/c_nich), [Shuvo Chatterjee](https://twitter.com/shuvster), [Ben Adida](https://twitter.com/benadida), [Laurie Voss](https://twitter.com/seldo), and [Michael White](https://twitter.com/mwwhite) for reviewing.)\n"
  },
  {
    "path": "2015-04-13-capturing-video.md",
    "content": "---\ntitle:  Capturing Video on iOS\ncategory: \"23\"\ndate: \"2015-04-13 11:00:00\"\ntags: article\nauthor:\n  - name: Adriaan Stellingwerff\n    url: https://twitter.com/astellingwerff\n\n---\n\nWith processing power and camera hardware improving with every new release, using iPhones to capture video is getting more and more interesting. They’re small, light, and inconspicuous, and the quality gap with professional video cameras has been narrowing to the point where, in certain situations, an iPhone is a real alternative.\nThis article discusses the different options to configure a video capture pipeline and get the most out of the hardware.\nA sample app with implementations of the different pipelines is available on [GitHub](https://github.com/objcio/VideoCaptureDemo).\n\n\n## `UIImagePickerController`\n\nBy far the easiest way to integrate video capture in your app is by using `UIImagePickerController`. It’s a view controller that wraps a complete video capture pipeline and camera UI.\n\nBefore instantiating the camera, first check if video recording is supported on the device:\n\n```objc\nif ([UIImagePickerController\n       isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {\n    NSArray *availableMediaTypes = [UIImagePickerController\n      availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];\n    if ([availableMediaTypes containsObject:(NSString *)kUTTypeMovie]) {\n        // Video recording is supported.\n    }\n}\n```\n\nThen create a `UIImagePickerController` object, and define a delegate to further process recorded videos (e.g. to save them to the camera roll) and respond to the user dismissing the camera:\n\n```objc\nUIImagePickerController *camera = [UIImagePickerController new];\ncamera.sourceType = UIImagePickerControllerSourceTypeCamera;\ncamera.mediaTypes = @[(NSString *)kUTTypeMovie];\ncamera.delegate = self;\n```\n\nThat’s all the code you need for a fully functional video camera.\n\n### Camera Configuration\n\n`UIImagePickerController` does provide some additional configuration options.\n\nA specific camera can be selected by setting the `cameraDevice` property. This takes a `UIImagePickerControllerCameraDevice` enum. By default, this is set to `UIImagePickerControllerCameraDeviceRear`, but it can also be set to `UIImagePickerControllerCameraDeviceFront`. Always check first to make sure the camera you want to set is actually available:\n\n```objc\nUIImagePickerController *camera = …\nif ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {\n    [camera setCameraDevice:UIImagePickerControllerCameraDeviceFront];\n}\n```\n\nThe `videoQuality` property gives some control over the quality of the recorded video. It allows you to set a specific encoding preset, which affects both the bitrate and the resolution of the video. There are six presets:\n\n```objc\nenum {\n   UIImagePickerControllerQualityTypeHigh             = 0,\n   UIImagePickerControllerQualityTypeMedium           = 1,  // default  value\n   UIImagePickerControllerQualityTypeLow              = 2,\n   UIImagePickerControllerQualityType640x480          = 3,\n   UIImagePickerControllerQualityTypeIFrame1280x720   = 4,\n   UIImagePickerControllerQualityTypeIFrame960x540    = 5\n};\ntypedef NSUInteger  UIImagePickerControllerQualityType;\n```\nThe first three are relative presets (low, medium, and high). The encoding configuration for these presets can be different for different devices, with high giving you the highest quality available for the selected camera. The other three are resolution-specific presets (640x480 VGA, 960x540 iFrame, and 1280x720 iFrame).\n\n### Custom UI\n\nAs mentioned before, `UIImagePickerController` comes with a complete camera UI right out of the box. However, it is possible to customize the camera with your own controls by hiding the default controls and providing a custom view with the controls, which will be overlaid on top of the camera preview:\n\n```objc\nUIView *cameraOverlay = …\npicker.showsCameraControls = NO;\npicker.cameraOverlayView = cameraOverlay;\n```\n\nYou then need to hook up the controls in your overlay to the control methods of the `UIImagePickerController` (e.g. `startVideoCapture` and `stopVideoCapture`).\n\n\n## AVFoundation\n\nIf you want more control over the video capture process than `UIImagePickerController` provides, you will need to use AVFoundation.\n\nThe central AVFoundation class for video capture is `AVCaptureSession`. It coordinates the flow of data between audio and video inputs and outputs:\n\n![AVCaptureSession setup](/images/issue-23/AVCaptureSession.svg)\n\nTo use a capture session, you instantiate it, add inputs and outputs, and start the flow of data from the connected inputs to the connected outputs:\n\n```objc\nAVCaptureSession *captureSession = [AVCaptureSession new];\nAVCaptureDeviceInput *cameraDeviceInput = …\nAVCaptureDeviceInput *micDeviceInput = …\nAVCaptureMovieFileOutput *movieFileOutput = …\nif ([captureSession canAddInput:cameraDeviceInput]) {\n    [captureSession addInput:cameraDeviceInput];\n}\nif ([captureSession canAddInput:micDeviceInput]) {\n    [captureSession addInput:micDeviceInput];\n}\nif ([captureSession canAddOutput:movieFileOutput]) {\n    [captureSession addOutput:movieFileOutput];\n}\n\n[captureSession startRunning];\n```\n\n(For simplicity, dispatch queue-related code has been omitted from the above snippet. Because all calls to a capture session are blocking, it’s recommended to dispatch them to a background serial queue.)\n\nA capture session can be further configured with a `sessionPreset`, which indicates the quality level of the output. There are 11 different presets:\n\n```objc\nNSString *const  AVCaptureSessionPresetPhoto;\nNSString *const  AVCaptureSessionPresetHigh;\nNSString *const  AVCaptureSessionPresetMedium;\nNSString *const  AVCaptureSessionPresetLow;\nNSString *const  AVCaptureSessionPreset352x288;\nNSString *const  AVCaptureSessionPreset640x480;\nNSString *const  AVCaptureSessionPreset1280x720;\nNSString *const  AVCaptureSessionPreset1920x1080;\nNSString *const  AVCaptureSessionPresetiFrame960x540;\nNSString *const  AVCaptureSessionPresetiFrame1280x720;\nNSString *const  AVCaptureSessionPresetInputPriority;\n```\nThe first one is for high-resolution photo output.\nThe next nine are very similar to the `UIImagePickerControllerQualityType` options we saw for the `videoQuality` setting of `UIImagePickerController`, with the exception that there are a few additional presets available for a capture session.\nThe last one (`AVCaptureSessionPresetInputPriority`) indicates that the capture session does not control the audio and video output settings. Instead, the `activeFormat` of the connected capture device dictates the quality level at the outputs of the capture session. In the next section, we will look at devices and device formats in more detail.\n\n### Inputs\n\nThe inputs for an `AVCaptureSession` are one or more `AVCaptureDevice` objects connected to the capture session through an `AVCaptureDeviceInput`.\n\nWe can use `[AVCaptureDevice devices]` to find the available capture devices. For an iPhone 6, they are:\n\n```\n(\n    “<AVCaptureFigVideoDevice: 0x136514db0 [Back Camera][com.apple.avfoundation.avcapturedevice.built-in_video:0]>”,\n    “<AVCaptureFigVideoDevice: 0x13660be80 [Front Camera][com.apple.avfoundation.avcapturedevice.built-in_video:1]>”,\n    “<AVCaptureFigAudioDevice: 0x174265e80 [iPhone Microphone][com.apple.avfoundation.avcapturedevice.built-in_audio:0]>”\n)\n```\n\n#### Video Input\n\nTo configure video input, create an `AVCaptureDeviceInput` object with the desired camera device and add it to the capture session:\n\n```objc\nAVCaptureSession *captureSession = …\nAVCaptureDevice *cameraDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];\nNSError *error;\nAVCaptureDeviceInput *cameraDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:cameraDevice error:&error];\nif ([captureSession canAddInput:input]) {\n    [captureSession addInput:cameraDeviceInput];\n}\n```\n\nIf any of the capture session presets discussed in the previous section are sufficient, that’s all you need to do. If they aren’t, because, for instance, you want to capture at high frame rates, you will need to configure a specific device format. A video capture device has a number of device formats, each with specific properties and capabilities. Below are a few examples (out of a total of 22 available formats) from the back-facing camera of an iPhone 6:\n\n| Format | Resolution | FPS     | HRSI      | FOV    | VIS | Max Zoom  | Upscales | AF | ISO        | SS                | HDR |\n|--------|------------|---------|-----------|--------|-----|----------|----------|----|------------|-------------------|-----|\n| 420v   | 1280x720   | 5 - 240 | 1280x720  | 54.626 | YES |49.12 | 1.09     | 1  | 29.0 - 928 | 0.000003-0.200000 | NO  |\n| 420f   | 1280x720   | 5 - 240 | 1280x720  | 54.626 | YES |49.12 | 1.09     | 1  | 29.0 - 928 | 0.000003-0.200000 | NO  |\n| 420v   | 1920x1080  | 2 - 30  | 3264x1836 | 58.040 | YES | 95.62 | 1.55     | 2  | 29.0 - 464 | 0.000013-0.500000 | YES |\n| 420f   | 1920x1080  | 2 - 30  | 3264x1836 | 58.040 | YES | 95.62 | 1.55     | 2  | 29.0 - 464 | 0.000013-0.500000 | YES |\n| 420v   | 1920x1080  | 2 - 60  | 3264x1836 | 58.040 | YES | 95.62 | 1.55     | 2  | 29.0 - 464 | 0.000008-0.500000 | YES |\n| 420f   | 1920x1080  | 2 - 60  | 3264x1836 | 58.040 | YES | 95.62 | 1.55     | 2  | 29.0 - 464 | 0.000008-0.500000 | YES |\n\n- Format = pixel format\n- FPS = the supported frame rate range\n- HRSI = high-res still image dimensions\n- FOV = field of view\n- VIS = the format supports video stabilization\n- Max Zoom = the max video zoom factor\n- Upscales = the zoom factor at which digital upscaling is engaged\n- AF = autofocus system (1 = contrast detect, 2 = phase detect)\n- ISO = the supported ISO range\n- SS = the supported exposure duration range\n- HDR = supports video HDR\n\nFrom the above formats, you can see that for recording 240 frames per second, we would need the first or the second format, depending on the desired pixel format, and that 240 frames per second isn’t available if we want to capture at a resolution of 1920x1080.\n\nTo configure a specific device format, you first call `lockForConfiguration:` to acquire exclusive access to the device’s configuration properties. Then you simply set the capture format on the capture device using `setActiveFormat:`. This will also automatically set the preset of your capture session to `AVCaptureSessionPresetInputPriority`.\n\nOnce you set the desired device format, you can configure specific settings on the capture device within the constraints of the device format.\n\nFocus, exposure, and white balance for video capture are managed in the same way as for image capture described in [“Camera Capture on iOS”](/issues/21-camera-and-photos/camera-capture-on-ios/) from Issue #21. Aside from those, there are some video-specific configuration options.\n\nYou set the **frame rate** using the capture device’s `activeVideoMinFrameDuration` and `activeVideoMaxFrameDuration` properties, where the frame duration is the inverse of the frame rate. To set the frame rate, first make sure the desired frame rate is supported by the device format, and then lock the capture device for configuration. To ensure a constant frame rate, set the minimum and maximum frame duration to the same value:\n\n```objc\nNSError *error;\nCMTime frameDuration = CMTimeMake(1, 60);\nNSArray *supportedFrameRateRanges = [device.activeFormat videoSupportedFrameRateRanges];\nBOOL frameRateSupported = NO;\nfor (AVFrameRateRange *range in supportedFrameRateRanges) {\n    if (CMTIME_COMPARE_INLINE(frameDuration, >=, range.minFrameDuration) &&\n        CMTIME_COMPARE_INLINE(frameDuration, <=, range.maxFrameDuration)) {\n        frameRateSupported = YES;\n    }\n}\n\nif (frameRateSupported && [device lockForConfiguration:&error]) {\n    [device setActiveVideoMaxFrameDuration:frameDuration];\n    [device setActiveVideoMinFrameDuration:frameDuration];\n    [device unlockForConfiguration];\n}\n```\n\n**Video stabilization** was first introduced on iOS 6 and the iPhone 4S. With the iPhone 6, a second, more aggressive and fluid stabilization mode — called cinematic video stabilization — was added. This also changed the video stabilization API (which so far hasn’t been reflected in the class references; check the header files instead). Stabilization is not configured on the capture device, but on the `AVCaptureConnection`. As not all stabilization modes are supported by all device formats, the availability of a specific stabilization mode needs to be checked before it is applied:\n\n```objc\nAVCaptureDevice *device = ...;\nAVCaptureConnection *connection = ...;\n\nAVCaptureVideoStabilizationMode stabilizationMode = AVCaptureVideoStabilizationModeCinematic;\nif ([device.activeFormat isVideoStabilizationModeSupported:stabilizationMode]) {\n    [connection setPreferredVideoStabilizationMode:stabilizationMode];\n}\n```\n\nAnother new feature introduced with the iPhone 6 is **video HDR** (High Dynamic Range), which is “streaming high-dynamic-range video as opposed to the more traditional method of fusing a bracket of still images with differing EV values into a single high dynamic range photo.”[^1] It is built right into the sensor. There are two ways to configure video HDR: by directly enabling or disabling it through the capture device’s `videoHDREnabled` property, or by leaving it up to the system by using the `automaticallyAdjustsVideoHDREnabled` property.\n\n\n[^1]: [Technical Note: New AV Foundation Camera Features for the iPhone 6 and iPhone 6 Plus](https://developer.apple.com/library/ios/technotes/tn2409/_index.html#//apple_ref/doc/uid/DTS40015038-CH1-OPTICAL_IMAGE_STABILIZATION)\n\n#### Audio Input\n\nThe list of capture devices presented earlier only contained one audio device, which seems a bit strange given that an iPhone 6 has three microphones. The microphones are probably treated as one device because they are sometimes used together to optimize performance. For example, when recording video on an iPhone 5 or newer, the front and back microphones will be used together to provide directional noise reduction.[^2]\n\nIn most cases, the default microphone configurations will be the desired option. The back microphone will automatically be used with the rear-facing camera (with noise reduction using the front microphone), and the front microphone with the front-facing camera.\n\nBut it is possible to access and configure individual microphones, for example, to allow the user to record live commentary through the front-facing microphone while capturing a scene with the rear-facing camera. It is done through `AVAudioSession`.\nTo be able to reroute the audio, the audio session first needs to be set to a category that supports this. Then we need to iterate through the audio session’s input ports and through the port’s data sources to find the microphone we want:\n\n```objc\n// Configure the audio session\nAVAudioSession *audioSession = [AVAudioSession sharedInstance];\n[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];\n[audioSession setActive:YES error:nil];\n\n// Find the desired input port\nNSArray* inputs = [audioSession availableInputs];\nAVAudioSessionPortDescription *builtInMic = nil;\nfor (AVAudioSessionPortDescription* port in inputs) {\n    if ([port.portType isEqualToString:AVAudioSessionPortBuiltInMic]) {\n        builtInMic = port;\n        break;\n    }\n}\n\n// Find the desired microphone\nfor (AVAudioSessionDataSourceDescription* source in builtInMic.dataSources) {\n    if ([source.orientation isEqual:AVAudioSessionOrientationFront]) {\n        [builtInMic setPreferredDataSource:source error:nil];\n        [audioSession setPreferredInput:builtInMic error:&error];\n        break;\n    }\n}\n```\n\nIn addition to setting up a non-default microphone configuration, you can also use the `AVAudioSession` to configure other audio settings, like the audio gain and sample rate.\n\n[^2]: [Technical Q&A: AVAudioSession - Microphone Selection](https://developer.apple.com/library/ios/qa/qa1799/_index.html)\n\n#### Permissions\n\nOne thing to keep in mind when accessing cameras and microphones is that you will need the user’s permission. iOS will do this once automatically when you create your first `AVCaptureDeviceInput` for audio or video, but it’s cleaner to do it yourself. You can then use the same code to alert users when the required permissions have not been granted. Trying to record video and audio when the user hasn’t given permission will result in black frames and silence.\n\n### Outputs\n\nWith the inputs configured, we now focus our attention on the outputs of the capture session.\n\n#### `AVCaptureMovieFileOutput`\n\nThe easiest option to write video to file is through an `AVCaptureMovieFileOutput` object. Adding it as an output to a capture session will let you write audio and video to a QuickTime file with a minimum amount of configuration:\n\n```objc\nAVCaptureMovieFileOutput *movieFileOutput = [AVCaptureMovieFileOutput new];\nif([captureSession canAddOutput:movieFileOutput]){\n    [captureSession addOutput:movieFileOutput];\n}\n\n// Start recording\nNSURL *outputURL = …\n[movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];\n```\n\nA recording delegate is required to receive callbacks when actual recording starts and stops. When recording is stopped, the output usually still has some data to write to file and will call the delegate when it’s done.\n\nAn `AVCaptureMovieFileOutput` object has a few other configuration options, such as stopping recording after a certain duration, when a certain file size is reached, or when the device is crossing a minimum disk space threshold. If you need more than that, e.g. for custom audio and video compression settings, or because you want to process the audio or video samples in some way before writing them to file, you will need something a little more elaborate.\n\n\n#### `AVCaptureDataOutput` and `AVAssetWriter`\n\nTo have more control over the video and audio output from our capture session, you can use an `AVCaptureVideoDataOutput` object and an`AVCaptureAudioDataOutput` object instead of the `AVCaptureMovieFileOutput` discussed in the previous section.\n\nThese outputs will capture video and audio sample buffers respectively, and vend them to their delegates. The delegate can either apply some processing to the sample buffer (e.g. add a filter to the video) or pass them on unchanged. The sample buffers can then be written to file using an `AVAssetWriter` object:\n\n![Using an AVAssetWriter](/images/issue-23/AVAssetWriter.svg)\n\nYou configure an asset writer by defining an output URL and file format and adding one or more inputs to receive sample buffers. Because the writer inputs will be receiving data from the capture session’s outputs in real time, we also need to set the `expectsMediaInRealTime` attribute to YES:\n\n```objc\nNSURL *url = …;\nAVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:url fileType:AVFileTypeMPEG4 error:nil];\nAVAssetWriterInput *videoInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:nil];\nvideoInput.expectsMediaDataInRealTime = YES;\nAVAssetWriterInput *audioInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeAudio outputSettings:nil];\naudioInput.expectsMediaDataInRealTime = YES;\nif ([assetWriter canAddInput:videoInput]) {\n    [assetWriter addInput:videoInput];\n}\nif ([assetWriter canAddInput:audioInput]) {\n    [assetWriter addInput:audioInput];\n}\n```\n\n(As with the capture session, it is recommended to dispatch asset writer calls to a background serial queue.)\n\nIn the code sample above, we passed in `nil` for the output settings of the asset writer inputs. This means that the appended samples will not be re-encoded. If we do want to re-encode the samples, we need to provide a dictionary with specific output settings. Keys for audio output settings are defined [here](https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVFoundationAudioSettings_Constants/index.html), and keys for video output settings are defined [here](https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVFoundation_Constants/index.html#//apple_ref/doc/constant_group/Video_Settings).\n\nTo make things a bit easier, both the `AVCaptureVideoDataOutput` class and the `AVCaptureAudioDataOutput` class have methods called `recommendedVideoSettingsForAssetWriterWithOutputFileType:` and `recommendedAudioSettingsForAssetWriterWithOutputFileType:`, respectively, that produce a fully populated dictionary of keys and values that are compatible with an asset writer. An easy way to define your own output settings is to start with this fully populated dictionary and adjust the properties you want to override. For example, increase the video bitrate to improve the quality of the video.\n\nAs an alternative, you can also use the `AVOutputSettingsAssistant` class to configure output settings dictionaries, but in my experience, using the above methods is preferable; the output settings they provide are more realistic for things like video bitrates. Additionally, the output assistant appears to have some other shortcomings, e.g. it doesn’t change the video bitrate when you change the expected video frame rate.\n\n\n#### Live Preview\n\nWhen using `AVFoundation` for video capture, we will have to provide a custom user interface.\nA key component of any camera interface is the live preview. This is most easily implemented through an `AVCaptureVideoPreviewLayer` object added as a sublayer to the camera view:\n\n```objc\nAVCaptureSession *captureSession = ...;\nAVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];\nUIView *cameraView = ...;\npreviewLayer.frame = cameraView.bounds;\n[cameraView.layer addSublayer:previewLayer];\n```\n\nIf you need more control, e.g. to apply filters to the live preview, you will instead need to add an `AVCaptureVideoDataOutput` object to the capture session and display the frames onscreen using OpenGL, as discussed in [“Camera Capture on iOS”](/issues/21-camera-and-photos/camera-capture-on-ios/) from Issue #21.\n\n\n## Summary\n\nThere are a number of different ways to configure a pipeline for video capture on iOS — from the straightforward `UIImagePickerController`, to the more elaborate combination of  `AVCaptureSession` and `AVAssetWriter`. The correct option for your project will depend on your requirements, such as the desired video quality and compression, or the camera controls you want to expose to your app’s users.\n"
  },
  {
    "path": "2015-04-13-core-image-video.md",
    "content": "---\ntitle:  \"Core Image and Video\"\ncategory: \"23\"\ndate: \"2015-04-13 10:00:00\"\ntags: article\nauthor:\n  - name: Daniel Eggert\n    url: https://twitter.com/danielboedewadt\n  - name: Chris Eidhof\n    url: https://twitter.com/chriseidhof\n\n---\n\nIn this article, we'll look into applying Core Image effects to live video. We'll look at two examples: first, we'll apply effects to the camera image. Second, we'll apply effects to an existing movie file. It's also possible to do offline rendering, where you render the result back into a video instead of directly on the screen. The full code of both examples is available [here](https://github.com/objcio/core-image-video).\n\n## Quick Recap\n\nPerformance is very important when it comes to video. And it's important to understand how things work under the hood — how Core Image does its work — in order to be able to deliver that performance. It's important to do as much work on the GPU as possible, and minimize the transferring of data between GPU and CPU. After the examples, we'll look into the details of this.\n\nTo get a feeling for Core Image, it's good to read Warren's article: [An Introduction to Core Image](/issues/21-camera-and-photos/core-image-intro/). We'll use the functional wrappers around `CIFilter` as described in [Functional Core Image](/issues/16-swift/functional-swift-apis/). To understand more about AVFoundation, have a look at [Adriaan's article](/issues/23-video/capturing-video/) in this issue and the [Camera Capture](/issues/21-camera-and-photos/camera-capture-on-ios/) article in Issue #21.\n\n## Harnessing OpenGL ES\n\nCore Image can run on both the CPU and the GPU. We'll go into more detail about what to be aware of [below](#cpuvsgpu). In our case, we want to use the GPU, and we do that as follows.\n\nWe start by creating a custom `UIView` that allows us to render Core Image's results directly into OpenGL. We can use a `GLKView` and initialize it with a new `EAGLContext`. We need to specify OpenGL ES 2 as the rendering API. In both examples, we want to trigger the drawing ourselves (rather than having `-drawRect:` called for us), so when initializing the `GLKView`, we need to set `enableSetNeedsDisplay` to false. This then puts the burden on us to call `-display` when we have a new image available.\n\nIn this view, we also keep a reference to a `CIContext`. This context provides the connection between our Core Image objects and the OpenGL context. We create it once and keep it around. The context allows Core Image to do behind-the-scenes optimizations, such as caching and reusing resources like textures. It is therefore important to reuse the same context.\n\nThe context has a method, `-drawImage:inRect:fromRect:`, which draws a `CIImage`. If you want to draw an entire image, it's easiest to use the image's `extent`. Note however, that it might be infinitely large, so make sure you either crop it beforehand or provide a finite rectangle. A caveat: Because we're dealing with Core Image, the drawing's destination rectangle is specified in pixels, not in points. As most new iOS devices are in Retina, we need to account for this when drawing. If we want to fill up our entire view, it's easiest to take the bounds and scale it up by the screen's scale.\n\nFor a full code example, take a look at [CoreImageView.swift](https://github.com/objcio/core-image-video/blob/master/CoreImageVideo/CoreImageView.swift) in our sample project.\n\n## Getting Pixel Data from the Camera\n\nFor an overview of how AVFoundation works, see [Adriaan's article](/issues/23-video/capturing-video/) and the [Camera Capture](/issues/21-camera-and-photos/camera-capture-on-ios/) article by Matteo. For our purposes, we want to get raw sample buffers from the camera. Given a camera, we do this by creating an `AVCaptureDeviceInput` object. Using an `AVCaptureSession`, we can connect it to an `AVCaptureVideoDataOutput`. This video data output has a delegate object that conforms to the `AVCaptureVideoDataOutputSampleBufferDelegate` protocol. This delegate will receive a message for each frame:\n\n```swift\nfunc captureOutput(captureOutput: AVCaptureOutput!,\n                   didOutputSampleBuffer: CMSampleBuffer!,\n                   fromConnection: AVCaptureConnection!) {\n```\n\nWe will use this to drive our image rendering. In our sample code, we have wrapped up the configuration, initialization, and delegate object into a simple interface called `CaptureBufferSource`. You can initialize it with a camera position (either front or back), and a callback, which, for each sample buffer, gets a callback with the buffer and the transform for that camera:\n\n```swift\nsource = CaptureBufferSource(position: AVCaptureDevicePosition.Front) {\n   (buffer, transform) in\n   ...\n}\n```\n\nWe need to transform the image data from the camera. The pixel data from the camera is always in the same orientation, no matter how we turn the iPhone. In our case, we lock the UI in portrait orientation, and we want the image onscreen to align with the image the camera sees. For that, we need the back-facing camera's image to be rotated by -π/2. The front-facing camera needs to be both rotated by -π/2 and mirrored. We express this as a `CGAffineTransform`. Note that our transform would be different if the UI had a different orientation (e.g. landscape). Note also that this transformation is very cheap, because it is done inside the Core Image rendering pipeline.\n\nNext, to convert a `CMSampleBuffer` into a `CIImage`, we first need to convert it into a `CVPixelBuffer`. We can write a convenience initializer that does this for us:\n\n```swift\nextension CIImage {\n    convenience init(buffer: CMSampleBuffer) {\n        self.init(CVPixelBuffer: CMSampleBufferGetImageBuffer(buffer))\n    }\n}\n```\n\nNow, we can process our image in three steps. First, we convert our `CMSampleBuffer` into a `CIImage`, and apply a transform so the image is rotated correctly. Next, we apply a `CIFilter` to get a new `CIImage` out. We use the style in [Florian's article](/issues/16-swift/functional-swift-apis/) for creating filters. In this case, we use a hue adjust filter, and pass in an angle that depends on time. Finally, we use our custom view defined in the previous section to render our `CIImage` using a `CIContext`. This flow is very simple, and looks like this:\n\n```swift\nsource = CaptureBufferSource(position: AVCaptureDevicePosition.Front) {\n  [unowned self] (buffer, transform) in\n    let input = CIImage(buffer: buffer).imageByApplyingTransform(transform)\n    let filter = hueAdjust(self.angleForCurrentTime)\n    self.coreImageView?.image = filter(input)\n}\n```\n\nWhen you run this, you might be surprised by the lack of CPU usage. The great thing about this setup is that all the hard work happens on the GPU. Even though we create a `CIImage`, apply a filter, and create a new `CIImage` out of it, the resulting image is a *promise*: it is not computed until it actually renders. A `CIImage` object can be many things under the hood: it can be pixel data on the GPU, it can be a description of how to create pixel data (for example, when using a generator filter), or it can be created directly from OpenGL textures.\n\nHere's a video of the result:\n\n<video controls=\"1\">\n  <source src=\"/images/issue-23/camera.m4v\"></source>\n</video>\n\n## Getting Pixel Data from a Movie File\n\nAnother thing we can do is filter a movie through Core Image. Instead of camera frames, we now generate pixel buffers from each movie frame. We will take a slightly different approach here. While the camera pushed frames to us, we use a pull-driven approach for the movie: using a display link, we ask AVFoundation for a frame at a specific time.\n\nA display link is an object that sends us messages every time a frame needs to be drawn, and sends this synchronously with the display's refresh rate. This is often used for [custom animations](/issues/12-animations/interactive-animations/), but can be used to play and manipulate video as well. The first thing we will do is create an `AVPlayer` and a video output:\n\n```swift\nplayer = AVPlayer(URL: url)\nvideoOutput = AVPlayerItemVideoOutput(pixelBufferAttributes: pixelBufferDict)\nplayer.currentItem.addOutput(videoOutput)\n```\n\nThen we set up our display link. Doing that is as simple as creating a `CADisplayLink` object and adding it to the run loop:\n\n```swift\nlet displayLink = CADisplayLink(target: self, selector: \"displayLinkDidRefresh:\")\ndisplayLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes)\n```\n\nNow, the only thing left to do is to fetch a video frame on each `displayLinkDidRefresh:` invocation. First, we take the current time and convert it into a timescale in terms of the item we're currently playing. Then we ask the `videoOutput` if there's a new pixel buffer available for the current time, and if there is one, we copy it over and call our callback method:\n\n```swift\nfunc displayLinkDidRefresh(link: CADisplayLink) {\n    let itemTime = videoOutput.itemTimeForHostTime(CACurrentMediaTime())\n    if videoOutput.hasNewPixelBufferForItemTime(itemTime) {\n        let pixelBuffer = videoOutput.copyPixelBufferForItemTime(itemTime, itemTimeForDisplay: nil)\n        consumer(pixelBuffer)\n    }\n}\n```\n\nThe pixel buffer that we get from a video output is a `CVPixelBuffer`, which we can directly convert into a `CIImage`. Like in the sample above, we will filter this image. In this case, we'll combine multiple filters: we use a kaleidoscope effect, and then use gradient mask to combine the original image with the filtered image. The result is slightly funky:\n\n<video controls=\"1\">\n  <source src=\"/images/issue-23/video.m4v\"></source>\n</video>\n\n## Getting Creative with Filters\n\nEverybody knows the popular photo effects. While we can apply these to video, too, Core Image can do much more.\n\nThe thing that Core Image calls a *filter* comes in different categories. Some of these are of the traditional type that take an input image and produce an output image. But others take two (or more) input images and combine them to generate the output image. Others again take no input images at all, but generate an image based on parameters.\n\nBy combining these various types, we can created unexpected effects.\n\n### Combining Images\n\nIn our example, we use something like this:\n\n![Combining filters](/images/issue-23/combining-filters.svg)\n\nThe example above pixelates a circular part of an image.\n\nIt’s possible to create interaction, too. We could use touch events to change to position of the generated circle.\n\nThe [Core Image Filter Reference](https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html) lists all available filters by category. Note that some of these are only available on OS X.\n\nThe generators and gradient filters produce images without an input. They are rarely useful on their own, but can be very powerful when used as masks, such as with `CIBlendWithMask` in our example.\n\nThe composite operation and `CIBlendWithAlphaMask` and `CIBlendWithMask` allow combining two images into one.\n\n<a name=\"cpuvsgpu\"></a>\n## CPU vs. GPU\n\nOur article from Issue #3, [Getting Pixels onto the Screen](/issues/3-views/moving-pixels-onto-the-screen/), describes the *graphics stack* of both iOS and OS X. The important thing to note is the notion of the CPU vs. the GPU, and how data moves between the two.\n\nWhen working on live video, we face performance challenges.\n\nFirst, we need to be able to process all image data within the time we have for each frame. The 24 fps (frames per second) cat movie we are using in our samples gives us 41 ms (1/24 seconds) to decode, process, and render all 1 million pixels in each frame.\n\nSecond, we need to be able to get all these pixels from the CPU to the GPU. The bytes from the movie file that we read off disk will end up in the CPU's domain. But the data needs to be on the GPU in order to be visible on the display.\n\n### Avoiding Roundtrips\n\nOne very fatal yet easily overseen problem happens when the code moves the image data back and forth between the CPU and GPU multiple times during the render pipeline. It is important to make sure that the pixel data moves in one direction only: from the CPU to the GPU. Even better is if the data stays on the GPU entirely.\n\nIf we want to render at 24 fps, we have 41 ms; if we render at the full 60 fps, we only have 16 ms. If we accidentally download a pixel buffer from the GPU to the CPU, and then upload it back to the GPU, we are moving 3.8 MB of data in each direction for a fullscreen iPhone 6 image. That's going to break our frame rate.\n\nWhen we're using a CVPixelBuffer, we want a flow like this:\n\n![Flow of image data](/images/issue-23/flow.svg)\n\nThe `CVPixelBuffer` is CPU based (see below). We wrap it with a `CIImage`. Building our filter chain does not move any data around; it simply builds a recipe. Once we draw the image, we're using a Core Image context based on the same EAGL context as the GLKView that will display the image. The EAGL context is GPU based. Note how we only cross the GPU-CPU boundary once. That's the crucial part.\n\n### Work and Target\n\nThe Core Image context can be created in two ways: as a GPU context using an `EAGLContext` or as a CPU-based context.\n\nThis defines where Core Image will do its work — where pixel data will be processed. Independent of that, both GPU- and CPU-based contexts can render to the CPU using the `createCGImage(…)`, `render(_, toBitmap, …)` and `render(_, toCVPixelBuffer, …)`, methods, as well as related methods.\n\nIt's important to understand how this moves pixel data between the CPU and GPU, or keeps all data on either the CPU and GPU. Moving data across this boundary comes at a cost.\n\n### Buffers and Images\n\nIn our sample, we are using a few different *buffers* and *images*. This can be a bit confusing. The reason for this is quite simply that different frameworks have different uses for these “images.” Here's a very quick overview to show which ones are CPU- and GPU-based, respectively:\n\n\n| Class          | Description    |\n|----------------|----------------|\n| CIImage        | These can represent two things: image data or a recipe to generate image data. |\n|                | The output of a CIFilter is very lightweight. It's just a description of how it is generated and does not contain any actual pixel data.\n|                | If the output is image data, it can be either raw pixel `NSData`, a `CGImage`, a `CVPixelBuffer`, or an OpenGL texture. |\n| CVImageBuffer  | This is an abstract superclass of `CVPixelBuffer` (CPU) and `CVOpenGLESTexture` (GPU). |\n| CVPixelBuffer  | A Core Video pixel buffer is CPU based. |\n| CMSampleBuffer | A Core Media sample buffer wraps either a `CMBlockBuffer` or a `CVImageBuffer`, in addition to metadata.\n| CMBlockBuffer  | A Core Media block buffer is CPU based. |\n\nNote that `CIImage` has quite a few convenience methods to, for example, load an image from JPEG data or a `UIImage` directly. Behind the scenes, these will use a `CGImage`-based `CIImage`.\n\n## Conclusion\n\nCore Image is a great tool for manipulating live video. When you have the proper setup, the performance is great — just make sure there are no roundtrips between the GPU and CPU. By using filters creatively, you can achieve very interesting effects that go way beyond a simple hue or sepia filter. All this code can be easily abstracted, and a solid understanding of where the different objects live (on the GPU or CPU) helps in getting the necessary performance.\n"
  },
  {
    "path": "2015-04-13-editorial.md",
    "content": "---\ntitle:  \"Editorial\"\ncategory: \"23\"\ndate: \"2015-04-13 13:00:00\"\ntags: editorial\n---\n\nWelcome to issue 23 of objc.io. This month is all about video.\n\nPlaying, recording, and editing video happens a lot on the iPhone. It is amazing that a pocket-sized device allows us to do all this, and much more.\n\nWhen dealing with video, there are many aspects that need to be considered. Adriaan writes about [capturing video on iOS](/issues/23-video/capturing-video/), and shows us a couple of different pipelines for video capturing. Daniel and Chris write about applying [Core Image effects to live video](/issues/23-video/core-image-video/) from the camera or a static file. Finally, Carola and Felix write about [Video Toolbox](/issues/23-video/videotoolbox/), a low-level framework that's very powerful, but relatively unknown.\n\nBest from Berlin,\n\nChris, Daniel, and Florian.\n"
  },
  {
    "path": "2015-04-13-videotoolbox.md",
    "content": "---\ntitle:  \"Video Toolbox and Hardware Acceleration\"\ncategory: \"23\"\ndate: \"2015-04-13 09:00:00\"\ntags: article\nauthor:\n  - name: Felix Paul Kühne\n    url: https://twitter.com/feepk\n  - name: Carola Nitz\n    url: https://twitter.com/_caro_n\n---\n\nThe process of decoding a video on OS X and iOS is complex.\n\nIn order to get a grasp on what is happening on our machines, we first need to understand the underlying concepts. Only then can we talk about implementation details.\n\n## A Brief History of Hardware Accelerated Decoding on Macs\n\nCPU load is expensive and codecs are complex. When video decoding with software on computers became popular, it was revolutionary. With the introduction of QuickTime 1.0 and its C-based API in the early 90s, you were able to have a thumbnail-sized video playing, with up to 32,768 possible colors per pixel decoded solely by the CPU. Up until this point, only specialized computers with certain graphics hardware were able to play color video.\n\n![QuickTime 1.0](/images/issue-23/qt1_1.gif) \n[Image source](http://www.emaculation.com/forum/viewtopic.php?t=5060)\n\nBy the end of the century, DVDs had been introduced using the then state-of-the-art [MPEG-2 video codec](http://en.wikipedia.org/wiki/MPEG-2). Subsequently, Apple added DVD drives to its PowerBooks and ran into an issue: the combination of G3 PowerPC CPUs and batteries was not efficient enough to play a full DVD on a single charge. The solution was to add a dedicated decoding chip by [C-Cube](http://en.wikipedia.org/wiki/C-Cube) to the motherboard for all the heavy lifting. This chip can be found on the Wallstreet, Lombard, and Pismo PowerBook generations, as well as on their professional desktop equivalents.\n\nApple never exposed a public API, so DVD playback was limited to its own application until the introduction of Mac OS X in 2001, which initially did not include a DVD playback application at all.[^2]\n\nBy the early 2000s, CPUs and batteries were evolving in a way that they could reasonably decode MPEG-2 video without help from a dedicated chip — at least as long as there wasn't any further demanding process running on the machine (like Mail.app, which constantly checked a remote server).\n\nIn the mid 00s, a new kid arrived on the block and remains the dominant video codec on optical media, digital television broadcasting, and online distribution: [H.264/AVC/MPEG-4 Part 10](http://en.wikipedia.org/wiki/H.264). MPEG’s fourth generation video codec introduced radical improvements over previous generations with dramatically decreased bandwidth needs. However, this came at a cost: increased CPU load and the need for dedicated decoding hardware, especially on embedded devices like the iPhone. Without the use of the provided DSPs, battery life suffers — typically by a factor of 2.5 — even with the most efficient software decoders.\n\nWith the introduction of QTKit — a Cocoa Wrapper of the QuickTime API — on OS X 10.5 Leopard, Apple delivered an efficient software decoder. It was highly optimized with handcrafted assembly code, and further improved upon in OS X 10.6 by the deployment of the clang compiler as demonstrated at WWDC 2008. What Apple didn't mention back then is the underlying ability to dynamically switch to hardware accelerated decoding depending on availability and codec profile compatibility. This feature was based on two newly introduced private frameworks: Video Toolbox and Video Decode Acceleration (VDA).\n\nIn 2008, Adobe Flash Video switched from its legacy video codecs[^3] to H.264 and started to integrate hardware accelerated decoding in its cross-platform playback applications. On OS X, Adobe's team discovered that the needed underlying technology was actually there and deployed by Apple within QTKit-based applications, but it was not available to the outside world. This changed in OS X 10.6.3 with a patch against the 10.6 SDK, which added a single header file for VDA.\n\nWhat’s inside VDA? It is a small framework wrapping the H.264 hardware decoder, which may or may not work. This is based on whether or not it can handle the input buffer of the encoded data that is provided. If the decoder can handle the input, it will return the decoded frames. If it can't, the session creation will fail, or no frames will be returned with an undocumented error code. There are four different error states available, none of them being particularly verbose.[^4] There is no official documentation beyond the header file and there is no software fallback either.\n\nFast-forward to the present: in iOS 8 and OS X 10.8, Apple published the full Video Toolbox framework. It is a beast!\nIt can compress and decompress video at real time or faster. Hardware accelerated video decoding and encoding can be enabled, disabled, or enforced at will. However, documentation isn’t staggering: there are 14 header files, and Xcode’s documentation reads “Please check the header files.”\n\nFurthermore, without actual testing, there is no way of knowing the supported codec, codec profile, or encoding bitrate set available on any given device. Therefore, a lot of testing and device-specific code is needed. Internally, it appears to be similar to the modularity of the original QuickTime implementation. The external API is the same across different operating systems and platforms, while the actual available feature set varies.\n\nBased on tests conducted on OS X 10.10, a Mac typically supports H.264/AVC/MPEG-4 Part 10 (until profile 100 and up to level 5.1), MPEG-4 Part 2, H.263, MPEG-1 and MPEG-2 Video and Digital Video (DV) in software, and usually both H.264 and MPEG-4 Part 2 in hardware.\n\nOn iOS devices with an [A4 up to A6 SoC](http://en.wikipedia.org/wiki/Apple_system_on_a_chip), there is support for H.264/AVC/MPEG-4 Part 10 (until profile 100 and up to level 5.1), MPEG-4 Part 2, and H.263.\n\nThe A7 SoC added support for H.264’s profile 110, which allows the individual color channels to be encoded with 10 bits instead of 8 bits, allowing a greater level of color detail. This feature is typically used in the broadcasting and content creation industries.\n\nWhile the A8 seems to include basic support for the fifth-generation codec HEVC / H.265, as documented in the FaceTime specifications for the equipped devices, it is not exposed to third-party applications. This is expected to change in subsequent iOS releases,[^5] but might be limited to future devices.\n\n## Video Toolbox\n\n### When Do You Need Direct Access to Video Toolbox?\n\nOut of the box, Apple’s SDKs provide a media player that can deal with any file that was playable by the QuickTime Player. The only exception is for contents purchased from the iTunes Store which are protected by FairPlay2, the deployed Digital Rights Management. In addition, Apple's SDKs include support for Apple’s scaleable HTTP streaming protocol [HTTP Live Streaming (HLS)](http://en.wikipedia.org/wiki/HTTP_Live_Streaming) on iOS and OS X 10.7 and above. HLS typically consists of small chunks of H.264/AAC video stored in the [MPEG-TS container format](http://en.wikipedia.org/wiki/MPEG_transport_stream), and playlists that allow the client application to switch dynamically between different versions, depending on the available hardware.\n\nIf you have control over the encoding and production of the content that is going to be displayed on iOS or OS X devices, then you can use the default playback widgets. Depending on the deployed device and operating system version, the default player will behave correctly, enable hardware accelerated decoding, or transparently fall back on a software solution in case of an error. This is also true for the high-level frameworks AVKit and AVFoundation. Of course, all of them are backed by Video Toolbox, but this is nothing that need concern the average developer.\n\nHowever, there are more container formats than just MP4 — for example, MKV. There are also further scaleable HTTP streaming protocols developed by Adobe and Microsoft, like DASH or Smooth Streaming, which also deploy a similar set of video codecs, but different container formats and intrinsically different protocols. Supporting custom container formats or protocols is a breeze with Video Toolbox. It accepts raw video streams as input and also allows on-device encoding of raw or already encoded video. The result is then made accessible for further processing, be it storage in a file or active streaming to the network. Video Toolbox is an elegant way to achieve performance on par with Apple’s native solutions, and its usage is described in [WWDC 2014 Session #513, \"Direct Access to Video Encoding and Decoding](https://developer.apple.com/videos/wwdc/2014/#513),\" as well as the very basic [VideoTimeLine sample code project](https://developer.apple.com/devcenter/download.action?path=/wwdc_2014/wwdc_2014_sample_code/usingVideoToolboxtodecodecompressedsamplebuffers.zip).\n\nA final word on Video Toolbox deployment on iOS devices. It was introduced as a private framework in iOS 4 and was recently made public in iOS 8. When building applications with a deployment target less than 8.0, including Video Toolbox won't lead to any problems, since the actual symbols stayed the same and the API is virtually unchanged. However, any worker session creation will be terminated with the undocumented error -12913, as the framework is not available for sandboxed applications on previous OS releases due to security concerns.\n\n### Basic Concepts of Video Toolbox Usage\n\nVideo Toolbox is a C API depending on the CoreMedia, CoreVideo, and CoreFoundation frameworks and based on sessions with three different types available: compression, decompression, and pixel transfer. It derives various types from the CoreMedia and CoreVideo frameworks for time and frame management, such as CMTime or CVPixelBuffer.\n\nTo illustrate the basic concepts of Video Toolbox, the following paragraphs will describe the creation of a decompression session along with the needed structures and types. A compression session is essentially very similar to a decompression session, while in practice, a pixel transfer session should be rarely needed.\n\nTo initialize a decompression session, Video Toolbox needs to know about the input format as part of a `CMVideoFormatDescriptionRef` structure, and — unless you want to use the undocumented default — your specified output format as plain CFDictionary reference. A video format description can be obtained from an AVAssetTrack instance or created manually with `CMVideoFormatDescriptionCreate` if you are using a custom demuxer. Finally, decoded data is provided through an asynchronous callback mechanism. The callback reference and the video format description are required by `VTDecompressionSessionCreate`, while setting the output format is optional.\n\n### What Is a Video Format Description?\n\nIt is an opaque structure describing the encoded video. It includes a [FourCC](http://en.wikipedia.org/wiki/FourCC) indicating the used codec, the video dimensions, and a dictionary documented as `extensions`. What are those? On OS X, hardware accelerated decoding is optional and disabled by default. To enable it, the `kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder` key must be set, optionally combined with `kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder` set to fail if hardware accelerated playback is not available. This is not needed on iOS, as accelerated decoding is the only available option. Furthermore, `extensions` allows you to forward metadata required by modern video codecs such as MPEG-4 Part 2 or H.264 to the decoder.[^6] Additionally, it may contain metadata to handle support for non-square pixels with the `CVPixelAspectRatio` key.\n\n### The Video Output Format\n\nThis is a plain CFDictionary. The result of a decompression session is a raw, uncompressed video image. To optimize for speed, it is preferable to have the hardware decoder output's native [chroma format](http://en.wikipedia.org/wiki/Chroma_subsampling), which appears to be `kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange`. However, a number of further formats are available, and transformation is performed in a rather efficient way using the GPU. It can be set using the `kCVPixelBufferPixelFormatTypeKey` key. We also need to set the dimensions of the output using `kCVPixelBufferWidthKey` and `kCVPixelBufferHeightKey` as plain integers. An optional key worth mentioning is `kCVPixelBufferOpenGLCompatibilityKey`, which allows direct drawing of the decoded image in an OpenGL context without copying the data back and forth between the main bus and the GPU. This is sometimes referenced as a *0-copy pipeline*, as no copy of the decoded image is created for drawing.[^7]\n\n### Data Callback Record\n\n`VTDecompressionOutputCallbackRecord` is a simple structure with a pointer to the callback function invoked when frame decompression (`decompressionOutputCallback`) is complete. Additionally, you need to provide the instance of where to find the callback function (`decompressionOutputRefCon`). The `VTDecompressionOutputCallback` function consists of seven parameters:\n\n* the callback's reference value\n* the frame's reference value\n* a status flag (with undefined codes)\n* info flags indicating synchronous / asynchronous decoding, or whether the decoder decided to drop the frame\n* the actual image buffer\n* the presentation timestamp\n* the presentation duration\n\nThe callback is invoked for any decoded or dropped frame. Therefore, your implementation should be highly optimized and strictly avoid any copying. The reason is that the decoder is blocked until the callback returns, which can lead to decoder congestion and further complications. Additionally, note that the decoder will always return the frames in the decoding order, which is absolutely not guaranteed to be the playback order. Frame reordering is up to the developer.\n\n### Decoding Frames\n\nOnce your session is created, feeding frames to the decoder is a walk in the park. It is a matter of calling `VTDecompressionSessionDecodeFrame` repeatedly, with a reference to the session and a sample buffer to decode, and optionally with advanced flags. The sample buffer can be obtained from an `AVAssetReaderTrackOutput`, or alternatively, it can be created manually from a raw memory block, along with timing information, using `CMBlockBufferCreateWithMemoryBlock`, `CMSampleTimingInfo`, and `CMSampleBufferCreate`.\n\n## Conclusion\n\nVideo Toolbox is a low-level, highly efficient way to speed up video processing in specific setups. The higher level framework AVFoundation allows decompression of supported media for direct display and compression directly to a file. Video Toolbox is the tool of choice when support of custom file formats, streaming protocols, or direct access to the codec chain is required. On the downside, profound knowledge of the involved video technology is required to master the sparsely documented API. Regardless, it is the way to go to achieve an engaging user experience with better performance, increased efficiency, and extended battery life.\n\n## References\n\n- [Apple Sample Code](https://developer.apple.com/devcenter/download.action?path=/wwdc_2014/wwdc_2014_sample_code/usingVideoToolboxtodecodecompressedsamplebuffers.zip)\n- [WWDC 2014 Session #513](https://developer.apple.com/videos/wwdc/2014/#513)\n\n[^2]: DVD Player as we know it was introduced in OS X 10.1. Until then, third-party tools like VLC media player were the only playback option for DVDs on OS X.\n[^3]: Sorenson Spark, On2 TrueMotion VP6\n[^4]: `kVDADecoderHardwareNotSupportedErr`, `kVDADecoderFormatNotSupportedErr`, `kVDADecoderConfigurationError`, `kVDADecoderDecoderFailedErr`\n[^5]: Apple published a job description for a management position in HEVC development in summer 2013.\n[^6]: Namely the `kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms` *ESDS* or respectively *avcC*.\n[^7]: Remember, \"memcpy is murder.\" Mans Rullgard, Embedded Linux Conference 2013, [http://free-electrons.com/blog/elc-2013-videos/](http://free-electrons.com/blog/elc-2013-videos/)\n"
  },
  {
    "path": "2015-05-11-audio-api-overview.md",
    "content": "---\ntitle: \"Audio API Overview\"\ncategory: \"24\"\ndate: \"2015-05-11 8:00:00\"\ntags: article\nauthor:\n  - name: Daniel Eggert\n    url: https://twitter.com/danielboedewadt\n  - name: Florian Kugler\n    url: https://twitter.com/floriankugler\n---\n\n\n\nBoth iOS and OS X come with a wide array of audio APIs, ranging from very low level to very high level. The number of different APIs, developed over time as these platforms have grown and changed, can be quite overwhelming to say the least. This article gives a brief overview of the available APIs and the different purposes they serve.\n\n## Media Player Framework\n\nThe Media Player framework on iOS is a high-level API for audio and video playback, and includes a stock user interface you can drop into your app. You can use it to play back items in the user's iPod library, or to play local files or network streams.\n\nAdditionally, this framework contains APIs to query the user's media library, as well as to configure system audio controls like on the lock screen or in the control center.\n\n## AVFoundation\n\n`AVFoundation` is Apple's modern media framework that includes several APIs for different purposes and on different levels of abstraction. Some of these are modern Objective-C wrappers for lower-level C APIs. With a few exceptions, it's available both on iOS and OS X.\n\n### AVAudioSession\n\n`AVAudioSession` is specific to iOS and coordinates audio playback between apps, so that, for example, audio is stopped when a call comes in, or music playback stops when the user starts a movie. This API is needed to make sure an app behaves correctly in response to such events.\n\n### AVAudioPlayer\n\nThis high-level API gives you a simple interface to play audio from local files or memory. This is a headless audio player (i.e. no UI elements are provided), and it's very straightforward to use. It's not suitable for streaming audio from the network or for low-latency realtime audio applications. If those things are not a concern, this is probably the right choice. The audio player API also comes with a few extra features, such as looping, playback-level metering, etc.\n\n### AVAudioRecorder\n\nAs the counterpart to `AVAudioPlayer`, the audio recorder API is the simplest way to record audio straight to a file. Beyond the possibility to receive peak and average power values for a level meter, the API is very bare bones, but might just be what you need if your use case is simple.\n\n### AVPlayer\n\nThe `AVPlayer` API gives you more flexibility and control than the APIs mentioned above. Built around the `AVPlayerItem` and `AVAsset` classes, it gives you more granular access to assets, e.g. to pick a specific track. It also supports playlists via the `AVQueuePlayer` subclass, and lets you control whether the asset can be sent over AirPlay.\n\nA major difference compared to, for example, `AVAudioPlayer`, is `AVPlayer`'s out-of-the-box support for streaming assets from the network. This increases the complexity of playback state handling, but you can observe all state parameters using KVO. \n\n### AVAudioEngine\n\n`AVAudioEngine` is a modern Objective-C API for playback and recording. It provides a level of control for which you previously had to drop down to the C APIs of the Audio Toolbox framework (for example, with real-time audio tasks). The audio engine APIs are built to interface well with lower-level APIs, so you can still drop down to Audio Toolbox if you have to.\n\nThe basic concept of this API is to build up a graph of audio nodes, ranging from source nodes (players and microphones) and overprocessing nodes (mixers and effects) to destination nodes (hardware outputs). Each node has a certain number of input and output busses with well-defined data formats. This architecture makes it very flexible and powerful. And it even integrates with audio units.\n\n## Audio Unit Framework\n\nThe Audio Unit framework is a low-level API; all audio technologies on iOS are built on top of it. Audio units are plug-ins that process audio data. A chain of audio units is called an audio processing graph. \n\nYou may have to use audio units directly or write your own if you need very low latency (e.g. for VoIP or synthesized musical instruments), acoustic echo cancelation, mixing, or tonal equalization. But a lot of this can often be achieved with the `AVAudioEngine` API. If you have to write your own audio units, you can integrate them in an `AVAudioEngine` processing graph with the `AVAudioUnit` node.\n\n### Inter-App Audio\n\nThe Audio Unit API allows for Inter-App Audio on iOS. Audio streams (and MIDI commands) can be sent between apps. For example, an app can provide an audio effect or filter. Another app can then send its audio to the first app to apply the audio effect. The filtered audio is sent back to the originating app in real time. CoreAudioKit provides a simple UI for Inter-App Audio.\n\n\n## Other APIs\n\n### OpenAL\n\n[OpenAL](https://en.wikipedia.org/wiki/OpenAL) is a cross-platform API. It provides positional (3D) and low-latency audio services. It's mostly intended for cross-platform games. The API deliberately resembles the OpenGL API in style.\n\n### MIDI\n\nOn iOS, Core MIDI and CoreAudioKit can be used to make an app behave as a MIDI instrument. On OS X, Music Sequencing Services gives access to playing MIDI-based control and music data. Core MIDI Server gives server and driver support.\n\n### Even More\n\n- The most basic of all audio APIs on OS X is the `NSBeep()` function that simply plays the system sound.\n- On OS X, the `NSSound` class provides a simple API to play sounds, similar in concept to  `AVAudioPlayer`.\n- All notification APIs — local and remote notifications on iOS, `NSUserNotification` on OS X, and CloudKit notifications — can play sounds.\n- The Audio Toolbox framework is powerful, but very low level. It's historically C++ based, but most of its functionality is now available through the `AVFoundation` framework.\n- The QTKit and QuickTime frameworks are on their way out and should not be used for new development anymore. Use `AVFoundation` (and AVKit) instead.\n"
  },
  {
    "path": "2015-05-11-audio-dog-house.md",
    "content": "---\ntitle:  \"The Audio Processing Dog House\"\ncategory: \"24\"\ndate: \"2015-05-11 11:00:00\"\ntags: article\nstylesheets: \"issue-24/style.css\"\nauthor:\n  - name: Jack Schaedler\n    url: https://twitter.com/JackSchaedler\nillustrator:\n  - name: Jack Schaedler\n    url: https://twitter.com/JackSchaedler\n---\n\nI'm not sure if this concept is universal, but in North America, the archetypal project for a young and aspiring carpenter is the creation of a dog house; when children become curious about construction and want to fiddle around with hammers, levels, and saws, their parents will instruct them to make one. In many respects, the dog house is a perfect project for the enthusiastic novice. It's grand enough to be inspiring, but humble enough to preclude a sense of crushing defeat if the child happens to screw it up or lose interest halfway through. The dog house is appealing as an introductory project because it is a miniature \"Gesamtwerk.\" It requires design, planning, engineering, and manual craftsmanship. It's easy to tell when the project is complete. When Puddles can overnight in the dog house without becoming cold or wet, the project is a success.\n\n![](/images/issue-24/pic1.png)\n\nI'm certain that most earnest and curious developers — the kind that set aside their valuable time on evenings and weekends to read a periodical like objc.io — often find themselves in a situation where they're attempting to evaluate tools and understand new and difficult concepts without having an inspiring or meaningful project handy for application. If you're like me, you've probably experienced that peculiar sense of dread that follows the completion of a \"Hello World!\" tutorial. The jaunty phase of editor configuration and project setup comes to a disheartening climax when you realize that you haven't the foggiest idea what you actually want to _make_ with your new tools. What was previously an unyielding desire to learn Haskell, Swift, or C++ becomes tempered by the utter absence of a compelling project to keep you engaged and motivated.\n\nIn this article, I want to propose a \"dog house\" project for audio signal processing. I'm making an assumption (based on the excellent track record of objc.io) that the other articles in this issue will address your precise technical needs related to Xcode and Core Audio configuration. I'm viewing my role in this issue of objc.io as a platform-agnostic motivator, and a purveyor of fluffy signal processing theory. If you're excited about digital audio processing but haven't a clue where to begin, read on.\n\n## Before We Begin\n\nEarlier this year, I authored a 30-part interactive essay on basic signal processing. You can find it [here](https://jackschaedler.github.io/circles-sines-signals/index.html). I humbly suggest that you look it over before reading the rest of this article. It will help to explicate some of the basic terminology and concepts you might find confusing if you have a limited background in digital signal processing. If terms like \"Sample,\" \"Aliasing,\" or \"Frequency\" are foreign to you, that's totally OK, and this resource will help get you up to speed on the basics.\n\n## The Project\n\nAs an introductory project to learn audio signal processing, I suggest that you <b>write an application that can track the pitch of a _monophonic_ musical performance in real time</b>. \n\nThink of the game \"Rock Band\" and the algorithm that must exist in order to analyze and evaluate the singing player's vocal performance. This algorithm must listen to the device microphone and automatically compute the frequency at which the player is singing, in real time. Assuming that you have a plump opera singer at hand, we can only hope that the project will end up looking something like this:[^1a]\n\n![](/images/issue-24/pic2.png)\n\nI've italicized the word monophonic because it's an important qualifier. A musical performance is _monophonic_ if there is only ever a single note being played at any given time. A melodic line is monophonic. Harmonic and chordal performances are _not_ monophonic, but instead _polyphonic_. If you're singing, playing the trumpet, blowing on a tin whistle, or tapping on the keyboard of a Minimoog, you are performing a monophonic piece of music. These instruments do not allow for the production of two or more simultaneous notes. If you're playing a piano or guitar, it's quite likely that you are generating a polyphonic audio signal, unless you're taking great pains to ensure that only one string rings out at any given time.\n\nThe pitch detection techniques that we will discuss in this article are only suitable for _monophonic_ audio signals. If you're interested in the topic of polyphonic pitch detection, skip to the _resources_ section, where I've linked to some relevant literature. In general, monophonic pitch detection is considered something of a solved problem. Polyphonic pitch detection is still an active and energetic field of research.[^1b]\n\nI will not be providing you with code snippets in this article. Instead, I'll give you some introductory theory on pitch estimation, which should allow you to begin writing and experimenting with your own pitch tracking algorithms. It's easier than you might expect to quickly achieve convincing results! As long as you've got buffers of audio being fed into your application from the microphone or line-in, you should be able to start fiddling around with the algorithms and techniques described in this article immediately.\n\nIn the next section, I'll introduce the notion of wave _frequency_ and begin to dig into the problem of pitch detection in earnest.\n\n## Sound, Signals, and Frequency\n\n![](/images/issue-24/pic3.png)\n\nMusical instruments generate sound by rapidly vibrating. As an object vibrates, it generates a [longitudinal pressure wave](http://jackschaedler.github.io/circles-sines-signals/sound.html), which radiates into the surrounding air. When this pressure wave reaches your ear, your auditory system will interpret the fluctuations in pressure as a sound. Objects that vibrate regularly and periodically generate sounds which we interpret as tones or notes. Objects that vibrate in a non-regular or random fashion generate atonal or noisy sounds. The most simple tones are described by the sine wave.\n\n<svg id=\"sinecycle\" class=\"svgWithText\" width=\"100%\" height=\"100\"></svg>\n\nThis figure visualizes an abstract sinusoidal sound wave. The vertical axis of the figure refers to the amplitude of the wave (intensity of air pressure), and the horizontal axis represents the dimension of time. This sort of visualization is usually called a _waveform drawing_, and it allows us to understand how the amplitude and frequency of the wave changes over time. The taller the waveform, the louder the sound. The more tightly packed the peaks and troughs, the higher the frequency. \n\nThe frequency of any wave is measured in _hertz_. One hertz is defined as one _cycle_ per second. A _cycle_ is the smallest repetitive section of a waveform. If we knew that the width of the horizontal axis corresponded to a duration of one second, we could compute the frequency of this wave in hertz by simply counting the number of visible wave cycles.\n\n<svg id=\"sinecycle2\" class=\"svgWithText\" width=\"100%\" height=\"100\"></svg>\n\nIn the figure above, I've highlighted a single cycle of our sine wave using a transparent box. When we count the number of cycles that are completed by this waveform in one second, it becomes clear that the frequency of the wave is exactly 4 hertz, or four cycles per second. The wave below completes eight cycles per second, and therefore has a frequency of 8 hertz.  \n\n<svg id=\"sinecycle3\" class=\"svgWithText\" width=\"100%\" height=\"100\"></svg>\n\nBefore proceeding any further, we need some clarity around two terms I have been using interchangeably up to this point. _Pitch_ is an auditory sensation related to the human perception of sound. _Frequency_ is a physical, measurable property of a waveform. We relate the two concepts by noting that the pitch of a signal is very closely related to its frequency. For simple sinusoids, the pitch and frequency are more or less equivalent. For more complex waveforms, the pitch corresponds to the _fundamental_ frequency of the waveform (more on this later). Conflating the two concepts can get you into trouble. For example, given two sounds with the same fundamental frequency, humans will often perceive the louder sound to be higher in pitch. For the rest of this article, I will be sloppy and use the two terms interchangeably. If you find this topic interesting, continue your extracurricular studies [here](http://en.wikipedia.org/wiki/Pitch_%28music%29).\n\n\n## Pitch Detection\n\nStated simply, algorithmic pitch detection is the task of automatically computing the frequency of some arbitrary waveform. In essence, this all boils down to being able to robustly identify a single cycle within a given waveform. This is an exceptionally easy task for humans, but a difficult task for machines. The CAPTCHA mechanism works precisely because it's quite difficult to write algorithms that are capable of robustly identifying structure and patterns within arbitrary sets of sample data. I personally have no problem picking out the repeating pattern in a waveform using my eyeballs, and I'm sure you don't either. The trick is to figure out how we might program a computer to do the same thing quickly, in a real-time, performance-critical environment.[^1c]\n\n\n## The Zero-Crossing Method\n\nAs a starting point for algorithmic frequency detection, we might notice that any sine wave will cross the horizontal axis two times per cycle. If we count the number of zero-crossings that occur over a given time period, and then divide that number by two, we _should_ be able to easily compute the number of cycles present within a waveform. For example, in the figure below, we count eight zero-crossings over the duration of one second. This implies that there are four cycles present in the wave, and we can therefore deduce that the frequency of the signal is 4 hertz.\n\n<svg id=\"zerocrossings\" class=\"svgWithText\" width=\"100%\" height=\"100\"></svg>\n\nWe might begin to notice some problems with this approach when fractional numbers of cycles are present in the signal under analysis. For example, if the frequency of our waveform increases slightly, we will now count nine zero-crossings over the duration of our one-second window. This will lead us to incorrectly deduce that the frequency of the purple wave is 4.5 hertz, when it's really more like _4.6_ hertz.\n\n<svg id=\"zerocrossings2\" class=\"svgWithText\" width=\"100%\" height=\"100\"></svg>\n\nWe can alleviate this problem a bit by adjusting the size of our analysis window, performing some clever averaging, or introducing heuristics that remember the position of zero-crossings in previous windows and predict the position of future zero-crossings. I'd recommend playing around a bit with improvements to the naive counting approach until you feel comfortable working with a buffer full of audio samples. If you need some test audio to feed into your iOS device, you can load up the sine generator at the bottom of [this page](http://jackschaedler.github.io/circles-sines-signals/sound.html).\n\nWhile the zero-crossing approach might be workable for very simple signals, it will fail in more distressing ways for complex signals. As an example, take the signal depicted below. This wave still completes one cycle every 0.25 seconds, but the number of zero-crossings per cycle is considerably higher than what we saw for the sine wave. The signal produces six zero-crossings per cycle, even though the fundamental frequency of the signal is still 4 hertz.\n\n<svg id=\"zerocrossingscomplex\" class=\"svgWithText\" width=\"100%\" height=\"100\"></svg>\n\nWhile the zero-crossing approach isn't really ideal for use as a hyper-precise pitch tracking algorithm, it can still be incredibly useful as a quick and dirty way to roughly measure the amount of noise present in a signal. The zero-crossing approach is appropriate in this context because noisy signals will produce more zero-crossings per unit time than cleaner, more tonal sounds. Zero-crossing counting is often used in voice recognition software to distinguish between voiced and unvoiced segments of speech. Roughly speaking, voiced speech usually consists of vowels, where unvoiced speech is produced by consonants. However, some consonants, like the English \"Z,\" are voiced (think of saying \"zeeeeee\").\n\nBefore we move on to introducing a more robust approach to pitch detection, we first must understand what is meant by the term I bandied about in earlier sections. Namely, the _fundamental frequency_.\n\n## The Fundamental Frequency\n\nMost natural sounds and waveforms are _not_ pure sinusoids, but amalgamations of multiple sine waves. While [Fourier Theory](http://jackschaedler.github.io/circles-sines-signals/dft_introduction.html) is beyond the scope of this article, you must accept the fact that physical sounds are (modeled by) summations of many sinusoids, and each constituent sinusoid may differ in frequency and amplitude. When our algorithm is fed this sort of compound waveform, it must determine which sinusoid is acting as the _fundamental_ or foundational component of the sound and compute the frequency of _that_ wave.\n\nI like to think of sounds as compositions of spinning, circular forms. A sine wave can be described by a spinning circle, and more complex wave shapes can be created by chaining or summing together additional spinning circles.[^1d] Experiment with the visualization below by clicking on each of the four buttons to see how various compound waveforms can be composed using many individual sinusoids.\n\n<p id=\"phasorbuttons\" class=\"buttonholder\" style=\"text-align: center\"></p>\n<svg id=\"phasorSum2\" class=\"svgWithText\" width=\"100%\" height=\"300\" style=\"margin-left: 10px\"></svg>\n\nThe blue spinning circle at the center of the diagram represents the _fundamental_, and the additional orbiting circles describe _overtones_ of the fundamental. It's important to notice that one rotation of the blue circle corresponds precisely to one cycle in the generated waveform. In other words, every full rotation of the fundamental generates a single cycle in the resulting waveform.[^1e]\n\nI've again highlighted a single cycle of each waveform using a grey box, and I'd encourage you to notice that the fundamental frequency of all four waveforms is identical. Each has a fundamental frequency of 4 hertz. Even though there are multiple sinusoids present in the square, saw, and wobble waveforms, the fundamental frequency of the four waveforms is always tied to the blue sinusoidal component. The blue component acts as the foundation of the signal.\n\nIt's also very important to notice that the fundamental is not necessarily the largest or loudest component of a signal. If you take another look at the \"wobble\" waveform, you'll notice that the second overtone (orange circle) is actually the largest component of the signal. In spite of this rather dominant overtone, the fundamental frequency is still unchanged.[^1f]\n\nIn the next section, we'll revisit some university math, and then investigate another approach for fundamental frequency estimation that should be capable of dealing with these pesky compound waveforms.\n\n\n## The Dot Product and Correlation\n\nThe _dot product_ is probably the most commonly performed operation in audio signal processing. The dot product of two signals is easily defined in pseudo-code using a simple _for_ loop. Given two signals (arrays) of equal length, their dot product can be expressed as follows:\n\n```swift\nfunc dotProduct(signalA: [Float], signalB: [Float]) -> [Float] {\n    return map(zip(signalA, signalB), *)\n}\n```\n\nHidden in this rather pedestrian code snippet is a truly wonderful property. The dot product can be used to compute the similarity or _correlation_ between two signals. If the dot product of two signals resolves to a large value, you know that the two signals are positively correlated. If the dot product of two signals is zero, you know that the two signals are decorrelated — they are not similar. As always, it's best to scrutinize such a claim visually, and I'd like you to spend some time studying the figure below.\n\n<svg id=\"sigCorrelationInteractive\" class=\"svgWithText\" width=\"100%\" height=\"380\"></svg>\n<p class=\"controls\">\n  <label id=\"squareShift\" for=\"squareCorrelationOffset\">Shift</label><br/>\n  <input type=\"range\" min=\"0\" max=\"100\" value=\"0\" id=\"squareCorrelationOffset\" step=\"0.5\" oninput=\"updateSquareCorrelationOffset(value);\" onMouseDown=\"\" onMouseUp=\"\" style=\"width: 150px\">\n</p>\n\nThis visualization depicts the computation of the dot product of two different signals. On the topmost row, you will find a depiction of a square wave, which we'll call Signal A. On the second row, there is a sinusoidal waveform we'll refer to as Signal B. The waveform drawn on the bottommost row depicts the product of these two signals. This signal is generated by multiplying each point in Signal A with its vertically aligned counterpart in Signal B. At the very bottom of the visualization, we're displaying the final value of the dot product. The magnitude of the dot product corresponds to the integral, or the area underneath this third curve. \n\nAs you play with the slider at the bottom of the visualization, notice that the absolute value of the dot product will be larger when the two signals are correlated (tending to move up and down together), and smaller when the two signals are out of phase or moving in opposite directions. The more that Signal A behaves like Signal B, the larger the resulting dot product. Amazingly, the dot product allows us to easily compute the similarity between two signals.\n\nIn the next section, we'll apply the dot product in a clever way to identify cycles within our waveforms and devise a simple method for determining the fundamental frequency of a compound waveform.\n\n## Autocorrelation\n\n![](/images/issue-24/pic5.png)\n\nThe autocorrelation is like an auto portrait, or an autobiography. It's the correlation of a signal with _itself_. We compute the autocorrelation by computing the dot product of a signal with a copy of itself at various shifts or time _lags_. Let's assume that we have a compound signal that looks something like the waveform shown in the figure below.\n\n<svg id=\"autosignal\" class=\"svgWithText\" width=\"100%\" height=\"100\"></svg>\n\nWe compute the autocorrelation by making a copy of the signal and repeatedly shifting it alongside the original. For each shift (lag), we compute the dot product of the two signals and record this value into our _autocorrelation function_. The autocorrelation function is plotted on the third row of the following figure. For each possible lag, the height of the autocorrelation function tells us how much similarity there is between the original signal and its copy.\n\n<svg id=\"sigCorrelationInteractiveTwoSines\" class=\"svgWithText\" width=\"100%\" height=\"300\" style=\"\"></svg>\n<p class=\"controls\">\n  <label id=\"phaseShift\" for=\"simpleCorrelationOffset\">Lag: <b> -60</b></label><br/>\n  <input type=\"range\" min=\"0\" max=\"120\" value=\"0\" id=\"simpleCorrelationOffset\" step=\"1\" oninput=\"updateSimpleCorrelationOffset(value);\"\n  onMouseDown=\"\" onMouseUp=\"\" style=\"width: 150px\">\n</p>\n\nSlowly move the slider at the bottom of this figure to the right to explore the values of the autocorrelation function for various lags. I'd like you to pay particular attention to the position of the peaks (local maxima) in the autocorrelation function. For example, notice that the highest peak in the autocorrelation will always occur when there is no lag. Intuitively, this should make sense because a signal will always be maximally correlated with itself. More importantly, however, we should notice that the secondary peaks in the autocorrelation function occur when the signal is shifted by a multiple of one cycle. In other words, we get peaks in the autocorrelation every time that the copy is shifted or lagged by one full cycle, since it once again \"lines up\" with itself.\n\nThe trick behind this approach is to determine the distance between consecutive prominent peaks in the autocorrelation function. This distance will correspond precisely to the length of one waveform cycle. The longer the distance between peaks, the longer the wave cycle and the lower the frequency. The shorter the distance between peaks, the shorter the wave cycle and the higher the frequency. For our waveform, we can see that the distance between prominent peaks is 0.25 seconds. This means that our signal completes four cycles per second, and the fundamental frequency is 4 hertz — just as we expected from our earlier visual inspection.\n\n<svg id=\"autocorrelationinterpretation\" class=\"svgWithText\" width=\"100%\" height=\"150\"></svg>\n\nThe autocorrelation is a nifty signal processing trick for pitch estimation, but it has its drawbacks. One obvious problem is that the autocorrelation function tapers off at its left and right edges. The tapering is caused by fewer non-zero samples being used in the calculation of the dot product for extreme lag values. Samples that lie outside the original waveform are simply considered to be zero, causing the overall magnitude of the dot product to be attenuated. This effect is known as <i>biasing</i>, and can be addressed in a number of ways. In his excellent paper, <a href=\"http://miracle.otago.ac.nz/tartini/papers/A_Smarter_Way_to_Find_Pitch.pdf\">\"A Smarter Way to Find Pitch,\"</a> Philip McLeod devises a strategy that cleverly removes this biasing from the autocorrelation function in a non-obvious but very robust way. When you've played around a bit with a simple implementation of the autocorrelation, I would suggest reading through this paper to see how the basic method can be refined and improved.\n\nAutocorrelation as implemented in its naive form is an _O(N<sup>2</sup>)_ operation. This complexity class is less than desirable for an algorithm that we intend to run in real time. Thankfully, there is an efficient way to compute the autocorrelation in _O(N log(N))_ time. The theoretical justification for this algorithmic shortcut is far beyond the scope of this article, but if you're interested, you should know that it's possible to compute the autocorrelation function using two FFT (Fast Fourier Transform) operations. You can read more about this technique in the footnotes.[^1g] I would suggest writing the naive version first, and using this implementation as a ground truth to verify a fancier, FFT-based implementation. \n\n## Latency and the Waiting Game\n\n![](/images/issue-24/pic4.png)\n\nReal-time audio applications partition time into chunks or _buffers_. In the case of iOS and OS X development, Core Audio will deliver buffers of audio to your application from an input source like a microphone or input jack and expect you to regularly provide a buffer's worth of audio in the rendering callback. It may seem trivial, but it's important to understand the relationship of your application's audio buffer size to the sort of audio material you want to consider in your analysis algorithms.\n\nLet's walk through a simple thought experiment. Pretend that your application is operating at a sampling rate of 128 hertz,[^1h] and your application is being delivered buffers of 32 samples. If you want to be able to detect fundamental frequencies as low as 2 hertz, it will be necessary to collect two buffers worth of input samples before you've captured a whole cycle of a 2 hertz input wave.\n\n<svg id=\"buffer1\" class=\"svgWithText\" width=\"100%\" height=\"100\"></svg>\n<svg id=\"buffer2\" class=\"svgWithText\" width=\"100%\" height=\"100\"></svg>\n\nThe pitch detection techniques discussed in this article actually need _two or more_ cycles worth of input signal to be able to robustly detect a pitch. For our imaginary application, this means that we'd have to wait for two _more_ buffers of audio to be delivered to our audio input callback before being able to accurately report a pitch for this waveform.\n\n<svg id=\"buffer3\" class=\"svgWithText\" width=\"100%\" height=\"100\"></svg>\n\n<svg id=\"buffer4\" class=\"svgWithText\" width=\"100%\" height=\"100\"></svg>\n\nThis may seem like stating the obvious, but it's a very important point. A classic mistake when writing audio analysis algorithms is to create an implementation that works well for high-frequency signals, but performs poorly on low-frequency signals. This can occur for many reasons, but it's often caused by not working with a large enough analysis window — by not waiting to collect enough samples before performing your analysis. High-frequency signals are less likely to reveal this sort of problem because there are usually enough samples present in a single audio buffer to fully describe many cycles of a high-frequency input waveform.\n\n<svg id=\"buffer5\" class=\"svgWithText\" width=\"100%\" height=\"100\"></svg>\n\nThe best way to handle this situation is to push every incoming audio buffer into a secondary circular buffer. This circular buffer should be large enough to accommodate at least two full cycles of the lowest pitch you want to detect. Avoid the temptation to simply increase the buffer size of your application. This will cause the overall latency of your application to increase, even though you only require a larger buffer for particular analysis tasks.\n\nYou can reduce latency by choosing to exclude very bassy frequencies from your detectable range. For example, you'll probably be operating at a sample rate of 44,100 hertz in your OS X or iOS project, and if you want to detect pitches beneath 60 hertz, you'll need to collect at least 2,048 samples before performing the autocorrelation operation. If you don't care about pitches beneath 60 hertz, you can get away with an analysis buffer size of 1,024 samples.\n\nThe important takeaway from this section is that it's impossible to _instantly_ detect pitch. There's an inherent latency in any pitch tracking approach, and you must simply be willing to wait. The lower the frequencies you want to detect, the longer you'll have to wait. This tradeoff between frequency coverage and algorithmic latency is actually related to the Heisenberg Uncertainty Principle, and permeates all of signal processing theory. In general, the more you know about a signal's frequency content, the less you know about its placement in time.\n\n## References and Further Reading\n\nI hope that by now you have a sturdy enough theoretical toehold on the problem of fundamental frequency estimation to begin writing your own monophonic pitch tracker. Working from the cursory explanations in this article, you should be able to implement a simple monophonic pitch tracker and dig into some of the relevant academic literature with confidence. If not, I hope that you at least got a small taste for audio signal processing theory and enjoyed the visualizations and illustrations.\n\nThe approaches to pitch detection outlined in this article have been explored and refined to a great degree of finish by the academic signal processing community over the past few decades. In this article, we've only scratched the surface, and I suggest that you refine your initial implementations and explorations by digging deeper into two exceptional examples of monophonic pitch detectors: the SNAC and YIN algorithms.\n\nPhilip McLeod's SNAC pitch detection algorithm is a clever refinement of the autocorrelation method introduced in this article. McLeod has found a way to work around the inherent biasing of the autocorrelation function. His method is performant and robust. I highly recommend reading McLeod's paper titled <a href=\"http://miracle.otago.ac.nz/tartini/papers/A_Smarter_Way_to_Find_Pitch.pdf\">\"A Smarter Way to Find Pitch\"</a> if you want to learn more about monophonic pitch detection. It's one of the most approachable papers on the subject. There is also a wonderful tutorial and evaluation of McLeod's method available <a href=\"http://www.katjaas.nl/helmholtz/helmholtz.html\">here</a>. I <i>highly</i> recommend poking around this author's website. \n\nYIN was developed by Cheveigné and Kawahahara in the early 2000s, and remains a classic pitch estimation technique. It's often taught in graduate courses on audio signal processing. I'd definitely recommend reading <a href=\"http://audition.ens.fr/adc/pdf/2002_JASA_YIN.pdf\">the original paper</a> if you find the topic of pitch estimation interesting. Implementing your own version of YIN is a fun weekend task.\n\nIf you're interested in more advanced techniques for _polyphonic_ fundamental frequency estimation, I suggest that you begin by reading Anssi Klapuri's excellent Ph.D. thesis on [automatic music transcription](http://www.cs.tut.fi/sgn/arg/klap/phd/klap_phd.pdf). In his paper, he outlines a number of approaches to multiple fundamental frequency estimation, and gives a great overview of the entire automatic music transcription landscape.\n\nIf you're feeling inspired enough to start on your own dog house, feel free to [contact me](https://twitter.com/JackSchaedler) on Twitter with any questions, complaints, or comments about the content of this article. Happy building!\n\n![](/images/issue-24/pic6.png)\n\n<style type=\"text/css\">\n  .controls {\n    text-align: center;\n    font-size: 0.8em;\n  }\n\n  .svgWithText {\n    font-size: 0.7em;\n\n    user-select: none;\n    -moz-user-select: none;\n    -webkit-user-select: none;\n  }\n\n  .axis path,\n  .axis line {\n      fill: none;\n    stroke: #333333;\n    shape-rendering: crispEdges;\n  }\n  .gridAxis path,\n  .gridAxis line {\n      fill: none;\n    stroke: #dddddd;\n    stroke-width: 1;\n    shape-rendering: crispEdges;\n  }\n\n  .buttonholder {\n    padding: 10px;\n  }\n\n  .buttonholder button {\n    margin-left: 5px;\n    margin-top: 5px;\n    cursor: pointer;\n    padding: 5px 10px;\n    border-radius: 5px;\n    border: solid 1px #ccc;\n    color: #333;\n    background: #fff;\n    bottom: 20px;\n    left: 20px;\n    font-size: 0.8em;\n  }\n\n  .buttonholder button:hover {\n    border-color: #777;\n    color: #000;\n  }\n\n  .buttonholder button:focus {\n    outline: none;\n  }\n</style>\n\n<script src=\"//cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js\"></script>\n\n<script>\n  // sinecycle\n  (function() {\n    var canvasWidth = 600;\n    var canvasHeight = 100;\n    var MARGINS =\n      {\n        top: 0,\n        right: 0,\n        bottom: 0,\n        left: 0\n      };\n\n    var vis = d3.select('#sinecycle');\n\n    var plotWidth = canvasWidth - MARGINS.left - MARGINS.right;\n    var plotHeight = canvasHeight - MARGINS.top - MARGINS.bottom;\n\n    var xRangeTime = d3.scale.linear().range([0, canvasWidth]);\n    xRangeTime.domain([0, 8 * Math.PI]);\n\n    var yRangeTime = d3.scale.linear().range([canvasHeight, 0]);\n    yRangeTime.domain([-1, 1]);\n\n    var data = d3.range(0, 8 * Math.PI, 0.01);\n\n    var signal = d3.svg.line()\n      .x(function (d, i) { return xRangeTime(d) + 1; })\n      .y(function (d, i) { return yRangeTime(Math.sin(d)  * 0.9)} );\n\n    var xAxis = d3.svg.axis()\n      .scale(xRangeTime)\n      .tickSize(0)\n      .ticks(0)\n      .tickSubdivide(true);\n\n    var yAxis = d3.svg.axis()\n      .scale(yRangeTime)\n      .tickSize(0)\n      .ticks(0)\n      .orient('left')\n      .tickSubdivide(true);\n\n    vis.append('text')\n      .attr(\"text-anchor\", \"left\")\n      .attr(\"x\", 5)\n      .attr(\"y\", yRangeTime(-1))\n      .attr(\"stroke\", \"none\")\n      .attr(\"fill\", \"grey\")\n      .attr(\"font-size\", 11)\n      .text(\"Amplitude\");\n\n    vis.append('text')\n      .attr(\"text-anchor\", \"end\")\n      .attr(\"x\", xRangeTime(Math.PI * 8))\n      .attr(\"y\", yRangeTime(0.1))\n      .attr(\"stroke\", \"none\")\n      .attr(\"fill\", \"grey\")\n      .attr(\"font-size\", 11)\n      .text(\"Time\");\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeTime(0) + ')')\n      .style(\"opacity\", 0.35)\n      .call(xAxis);\n\n    vis.append('svg:g')\n      .attr('class', 'y axis')\n      .attr('transform', 'translate(0,0)')\n      .style(\"opacity\", 0.35)\n      .call(yAxis);\n\n    var path = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", \"steelblue\")\n      .attr(\"fill\", \"none\")\n      .attr(\"opacity\", 1.0)\n      .attr(\"d\", signal(data));\n  })();\n\n  // sinecycle2\n  (function() {\n    var canvasWidth = 600;\n    var canvasHeight = 100;\n    var MARGINS =\n      {\n        top: 0,\n        right: 0,\n        bottom: 0,\n        left: 0\n      };\n\n    var vis = d3.select('#sinecycle2');\n\n    var plotWidth = canvasWidth - MARGINS.left - MARGINS.right;\n    var plotHeight = canvasHeight - MARGINS.top - MARGINS.bottom;\n\n    var xRangeTime = d3.scale.linear().range([0, canvasWidth]);\n    xRangeTime.domain([0, 8 * Math.PI]);\n\n    var yRangeTime = d3.scale.linear().range([canvasHeight, 0]);\n    yRangeTime.domain([-1, 1]);\n\n    var data = d3.range(0, 8 * Math.PI, 0.01);\n\n    var signal = d3.svg.line()\n      .x(function (d, i) { return xRangeTime(d) + 1; })\n      .y(function (d, i) { return yRangeTime(Math.sin(d) * 0.9)} );\n\n    vis.append(\"svg:rect\")\n      .attr(\"fill\", \"grey\")\n      .style(\"opacity\", 0.10)\n      .attr(\"x\", 0)\n      .attr(\"y\", 0)\n      .attr(\"width\", xRangeTime(2*Math.PI))\n      .attr(\"height\", yRangeTime(-1) - yRangeTime(1));\n\n    var xAxis = d3.svg.axis()\n      .scale(xRangeTime)\n      .tickSize(10)\n      .tickValues([2*Math.PI, 4*Math.PI, 6*Math.PI, 8*Math.PI])\n      .tickFormat(function(d) { return (d / (8*Math.PI)) + \" s\"; })\n      .tickSubdivide(true);\n\n    var yAxis = d3.svg.axis()\n      .scale(yRangeTime)\n      .tickSize(0)\n      .ticks(0)\n      .orient('left')\n      .tickSubdivide(true);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeTime(0) + ')')\n      .style(\"opacity\", 0.35)\n      .call(xAxis);\n\n    vis.append('svg:g')\n      .attr('class', 'y axis')\n      .attr('transform', 'translate(0,0)')\n      .style(\"opacity\", 0.35)\n      .call(yAxis);\n\n    var path = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", \"steelblue\")\n      .attr(\"fill\", \"none\")\n      .attr(\"opacity\", 1.0)\n      .attr(\"d\", signal(data));\n  })();\n\n  // sinecycle3\n  (function() {\n\n    var canvasWidth = 600;\n    var canvasHeight = 100;\n    var MARGINS =\n      {\n        top: 0,\n        right: 0,\n        bottom: 0,\n        left: 0\n      };\n\n    var vis = d3.select('#sinecycle3');\n\n    var plotWidth = canvasWidth - MARGINS.left - MARGINS.right;\n    var plotHeight = canvasHeight - MARGINS.top - MARGINS.bottom;\n\n    var xRangeTime = d3.scale.linear().range([0, canvasWidth]);\n    xRangeTime.domain([0, 8 * Math.PI]);\n\n    var yRangeTime = d3.scale.linear().range([canvasHeight, 0]);\n    yRangeTime.domain([-1, 1]);\n\n    var data = d3.range(0, 8 * Math.PI, 0.01);\n\n    var signal = d3.svg.line()\n      .x(function (d, i) { return xRangeTime(d) + 1; })\n      .y(function (d, i) { return yRangeTime(Math.sin(2 * d) * 0.9)} );\n\n    vis.append(\"svg:rect\")\n      .attr(\"fill\", \"grey\")\n      .style(\"opacity\", 0.10)\n      .attr(\"x\", 0)\n      .attr(\"y\", 0)\n      .attr(\"width\", xRangeTime(Math.PI))\n      .attr(\"height\", yRangeTime(-1) - yRangeTime(1));\n\n    var xAxis = d3.svg.axis()\n      .scale(xRangeTime)\n      .tickSize(10)\n      .tickValues([2*Math.PI, 4*Math.PI, 6*Math.PI, 8*Math.PI])\n      .tickFormat(function(d) { return (d / (8*Math.PI)) + \" s\"; })\n      .tickSubdivide(true);\n\n    var yAxis = d3.svg.axis()\n      .scale(yRangeTime)\n      .tickSize(0)\n      .ticks(0)\n      .orient('left')\n      .tickSubdivide(true);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeTime(0) + ')')\n      .style(\"opacity\", 0.35)\n      .call(xAxis);\n\n    vis.append('svg:g')\n      .attr('class', 'y axis')\n      .attr('transform', 'translate(0,0)')\n      .style(\"opacity\", 0.35)\n      .call(yAxis);\n\n    var path = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", \"steelblue\")\n      .attr(\"fill\", \"none\")\n      .attr(\"opacity\", 1.0)\n      .attr(\"d\", signal(data));\n  })();\n\n  // zerocrossings\n  (function() {\n    var canvasWidth = 600;\n    var canvasHeight = 100;\n    var MARGINS =\n      {\n        top: 0,\n        right: 0,\n        bottom: 0,\n        left: 0\n      };\n\n    var vis = d3.select('#zerocrossings');\n\n    var plotWidth = canvasWidth - MARGINS.left - MARGINS.right;\n    var plotHeight = canvasHeight - MARGINS.top - MARGINS.bottom;\n\n    var xRangeTime = d3.scale.linear().range([0, canvasWidth]);\n    xRangeTime.domain([0, 8 * Math.PI]);\n\n    var yRangeTime = d3.scale.linear().range([canvasHeight, 0]);\n    yRangeTime.domain([-1, 1]);\n\n    var data = d3.range(0, 8 * Math.PI, 0.01);\n\n    var signal = d3.svg.line()\n      .x(function (d, i) { return xRangeTime(d) + 1; })\n      .y(function (d, i) { return yRangeTime(Math.sin(d) * 0.9)} );\n\n    vis.append(\"svg:rect\")\n      .attr(\"fill\", \"grey\")\n      .style(\"opacity\", 0.10)\n      .attr(\"x\", 0)\n      .attr(\"y\", 0)\n      .attr(\"width\", xRangeTime(2*Math.PI))\n      .attr(\"height\", yRangeTime(-1) - yRangeTime(1));\n\n    var xAxis = d3.svg.axis()\n      .scale(xRangeTime)\n      .tickSize(10)\n      .tickValues([2*Math.PI, 4*Math.PI, 6*Math.PI, 8*Math.PI])\n      .tickFormat(function(d) { return (d / (8*Math.PI)) + \" s\"; })\n      .tickSubdivide(true);\n\n    var yAxis = d3.svg.axis()\n      .scale(yRangeTime)\n      .tickSize(0)\n      .ticks(0)\n      .orient('left')\n      .tickSubdivide(true);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeTime(0) + ')')\n      .style(\"opacity\", 0.35)\n      .call(xAxis);\n\n    vis.append('svg:g')\n      .attr('class', 'y axis')\n      .attr('transform', 'translate(0,0)')\n      .style(\"opacity\", 0.35)\n      .call(yAxis);\n\n    var path = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", \"steelblue\")\n      .attr(\"fill\", \"none\")\n      .attr(\"opacity\", 1.0)\n      .attr(\"d\", signal(data));\n\n    vis.append(\"defs\").append(\"marker\")\n        .attr(\"id\", \"arrowhead\")\n        .attr(\"refX\", 5)\n        .attr(\"refY\", 2)\n        .attr(\"markerWidth\", 10)\n        .attr(\"markerHeight\", 10)\n        .attr(\"orient\", \"auto\")\n        .attr(\"fill\", \"black\")\n        .append(\"path\")\n            .attr(\"d\", \"M 0,0 V 4 L6,2 Z\");\n\n    for (var i = 0; i < 8; i++)\n    {\n      vis.append(\"circle\")\n        .attr(\"cx\", xRangeTime(Math.PI * (i + 1)) + 1)\n        .attr(\"cy\", yRangeTime(0))\n        .attr(\"r\", 4)\n        .attr(\"stroke-width\", 0)\n        .attr(\"stroke\", \"black\")\n        .style(\"opacity\", 0.5)\n        .attr(\"fill\", \"black\");\n\n      vis.append(\"line\")\n        .attr(\"x1\", xRangeTime(Math.PI * (i + 1)) + 1)\n        .attr(\"y1\", yRangeTime(1))\n        .attr(\"x2\", xRangeTime(Math.PI * (i + 1)) + 1)\n        .attr(\"y2\", yRangeTime(0) - 5)\n        .attr(\"stroke-width\", 2)\n        .attr(\"stroke\", \"black\")\n        .style(\"opacity\", 0.25)\n        .attr(\"marker-end\", \"url(#arrowhead)\");\n    }\n  })();\n\n  // zerocrossings2\n  (function() {\n    var canvasWidth = 600;\n    var canvasHeight = 100;\n    var MARGINS =\n      {\n        top: 0,\n        right: 0,\n        bottom: 0,\n        left: 0\n      };\n\n    var vis = d3.select('#zerocrossings2');\n\n    var plotWidth = canvasWidth - MARGINS.left - MARGINS.right;\n    var plotHeight = canvasHeight - MARGINS.top - MARGINS.bottom;\n\n    var xRangeTime = d3.scale.linear().range([0, canvasWidth]);\n    xRangeTime.domain([0, 8 * Math.PI]);\n\n    var yRangeTime = d3.scale.linear().range([canvasHeight, 0]);\n    yRangeTime.domain([-1, 1]);\n\n    var data = d3.range(0, 8 * Math.PI, 0.01);\n\n    var signal = d3.svg.line()\n      .x(function (d, i) { return xRangeTime(d) + 1; })\n      .y(function (d, i) { return yRangeTime(Math.sin(d * 1.22) * 0.9)} );\n\n    vis.append(\"svg:rect\")\n      .attr(\"fill\", \"grey\")\n      .style(\"opacity\", 0.10)\n      .attr(\"x\", 0)\n      .attr(\"y\", 0)\n      .attr(\"width\", xRangeTime(2 * 0.82 * Math.PI))\n      .attr(\"height\", yRangeTime(-1) - yRangeTime(1));\n\n    var xAxis = d3.svg.axis()\n      .scale(xRangeTime)\n      .tickSize(10)\n      .tickValues([2*Math.PI, 4*Math.PI, 6*Math.PI, 8*Math.PI])\n      .tickFormat(function(d) { return (d / (8*Math.PI)) + \" s\"; })\n      .tickSubdivide(true);\n\n    var yAxis = d3.svg.axis()\n      .scale(yRangeTime)\n      .tickSize(0)\n      .ticks(0)\n      .orient('left')\n      .tickSubdivide(true);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeTime(0) + ')')\n      .style(\"opacity\", 0.35)\n      .call(xAxis);\n\n    vis.append('svg:g')\n      .attr('class', 'y axis')\n      .attr('transform', 'translate(0,0)')\n      .style(\"opacity\", 0.35)\n      .call(yAxis);\n\n    var path = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", \"purple\")\n      .attr(\"fill\", \"none\")\n      .attr(\"opacity\", 0.6)\n      .attr(\"d\", signal(data));\n\n    vis.append(\"defs\").append(\"marker\")\n        .attr(\"id\", \"arrowhead\")\n        .attr(\"refX\", 5)\n        .attr(\"refY\", 2)\n        .attr(\"markerWidth\", 10)\n        .attr(\"markerHeight\", 10)\n        .attr(\"orient\", \"auto\")\n        .attr(\"fill\", \"black\")\n        .append(\"path\")\n            .attr(\"d\", \"M 0,0 V 4 L6,2 Z\");\n\n    for (var i = 0; i < 9; i++)\n    {\n      vis.append(\"circle\")\n        .attr(\"cx\", xRangeTime(0.82 * Math.PI * (i + 1)) + 1)\n        .attr(\"cy\", yRangeTime(0))\n        .attr(\"r\", 4)\n        .attr(\"stroke-width\", 0)\n        .attr(\"stroke\", \"black\")\n        .style(\"opacity\", 0.5)\n        .attr(\"fill\", \"black\");\n\n      vis.append(\"line\")\n        .attr(\"x1\", xRangeTime(0.82 * Math.PI * (i + 1)) + 1)\n        .attr(\"y1\", yRangeTime(1))\n        .attr(\"x2\", xRangeTime(0.82 * Math.PI * (i + 1)) + 1)\n        .attr(\"y2\", yRangeTime(0) - 5)\n        .attr(\"stroke-width\", 2)\n        .attr(\"stroke\", \"black\")\n        .style(\"opacity\", 0.25)\n        .attr(\"marker-end\", \"url(#arrowhead)\");\n    }\n  })();\n\n  // zerocrossingscomplex\n  (function() {\n    var canvasWidth = 600;\n    var canvasHeight = 100;\n    var MARGINS =\n      {\n        top: 0,\n        right: 0,\n        bottom: 0,\n        left: 0\n      };\n\n    var vis = d3.select('#zerocrossingscomplex');\n\n    var plotWidth = canvasWidth - MARGINS.left - MARGINS.right;\n    var plotHeight = canvasHeight - MARGINS.top - MARGINS.bottom;\n\n    var xRangeTime = d3.scale.linear().range([0, canvasWidth]);\n    xRangeTime.domain([0, 8 * Math.PI]);\n\n    var yRangeTime = d3.scale.linear().range([canvasHeight, 0]);\n    yRangeTime.domain([-1, 1]);\n\n    var data = d3.range(0, 8 * Math.PI, 0.01);\n\n    var signal = d3.svg.line()\n      .x(function (d, i) { return xRangeTime(d) + 1; })\n      .y(function (d, i) { return yRangeTime((Math.sin(d) + Math.sin(d*2) + Math.sin(d*3)) * 0.35) } );\n\n    vis.append(\"svg:rect\")\n      .attr(\"fill\", \"grey\")\n      .style(\"opacity\", 0.10)\n      .attr(\"x\", 0)\n      .attr(\"y\", 0)\n      .attr(\"width\", xRangeTime(2*Math.PI))\n      .attr(\"height\", yRangeTime(-1) - yRangeTime(1));\n\n    var xAxis = d3.svg.axis()\n      .scale(xRangeTime)\n      .tickSize(10)\n      .tickValues([2*Math.PI, 4*Math.PI, 6*Math.PI, 8*Math.PI])\n      .tickFormat(function(d) { return (d / (8*Math.PI)) + \" s\"; })\n      .tickSubdivide(true);\n\n    var yAxis = d3.svg.axis()\n      .scale(yRangeTime)\n      .tickSize(0)\n      .ticks(0)\n      .orient('left')\n      .tickSubdivide(true);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeTime(0) + ')')\n      .style(\"opacity\", 0.35)\n      .call(xAxis);\n\n    vis.append('svg:g')\n      .attr('class', 'y axis')\n      .attr('transform', 'translate(0,0)')\n      .style(\"opacity\", 0.35)\n      .call(yAxis);\n\n    var path = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", \"green\")\n      .attr(\"fill\", \"none\")\n      .attr(\"opacity\", 0.7)\n      .attr(\"d\", signal(data));\n\n    vis.append(\"defs\").append(\"marker\")\n        .attr(\"id\", \"arrowhead\")\n        .attr(\"refX\", 5)\n        .attr(\"refY\", 2)\n        .attr(\"markerWidth\", 10)\n        .attr(\"markerHeight\", 10)\n        .attr(\"orient\", \"auto\")\n        .attr(\"fill\", \"black\")\n        .append(\"path\")\n            .attr(\"d\", \"M 0,0 V 4 L6,2 Z\");\n\n    var zeroes = \n    [\n      Math.PI,\n      2 * Math.PI,\n      3 * Math.PI,\n      4 * Math.PI,\n      5 * Math.PI,\n      6 * Math.PI,\n      7 * Math.PI,\n      8 * Math.PI,\n    ];\n\n    var zero = Math.PI / 2;\n    for (var i = 0; i < 8; i++)\n    {\n      zeroes.push(zero);\n      zero += Math.PI;\n    }\n\n    zero = Math.PI / 2 + 0.5;\n    for (var i = 0; i < 4; i++)\n    {\n      zeroes.push(zero);\n      zero += Math.PI * 2;\n    }\n\n    zero = Math.PI + 1.0;\n    for (var i = 0; i < 4; i++)\n    {\n      zeroes.push(zero);\n      zero += Math.PI * 2;\n    }\n\n    for (var i = 0; i < zeroes.length; i++)\n    {\n      vis.append(\"circle\")\n        .attr(\"cx\", xRangeTime(zeroes[i]) + 1)\n        .attr(\"cy\", yRangeTime(0))\n        .attr(\"r\", 4)\n        .attr(\"stroke-width\", 0)\n        .attr(\"stroke\", \"black\")\n        .style(\"opacity\", 0.5)\n        .attr(\"fill\", \"black\");\n\n      vis.append(\"line\")\n        .attr(\"x1\", xRangeTime(zeroes[i]) + 1)\n        .attr(\"y1\", yRangeTime(1))\n        .attr(\"x2\", xRangeTime(zeroes[i]) + 1)\n        .attr(\"y2\", yRangeTime(0) - 5)\n        .attr(\"stroke-width\", 2)\n        .attr(\"stroke\", \"black\")\n        .style(\"opacity\", 0.25)\n        .attr(\"marker-end\", \"url(#arrowhead)\");\n    }\n  })();\n\n  // phasorSum2\n  (function() {\n    var canvasWidth = 300;\n    var canvasHeight = 300;\n    var MARGINS =\n      {\n        top: 0,\n        right: 0,\n        bottom: 0,\n        left: 0\n      };\n\n    var plotWidth = canvasWidth - MARGINS.left - MARGINS.right;\n    var plotHeight = canvasHeight - MARGINS.top - MARGINS.bottom;\n\n    var xRange = d3.scale.linear().range([MARGINS.left, plotWidth]);\n    var yRange = d3.scale.linear().range([plotHeight, MARGINS.top]);\n\n    xRange.domain([-1.25 * 2.0, 1.25 * 2.0]);\n    yRange.domain([-1.25 * 2.0, 1.25 * 2.0]);\n\n    var vis = d3.select('#phasorSum2');\n\n    var xAxis = d3.svg.axis()\n      .scale(xRange)\n      .tickSize(0)\n      .ticks(0)\n      .tickSubdivide(true);\n\n    var yAxis = d3.svg.axis()\n      .scale(yRange)\n      .tickSize(0)\n      .ticks(0)\n      .orient('left')\n      .tickSubdivide(true);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + (plotHeight / 2) + ')')\n      .style('opacity', 0.25)\n      .call(xAxis);\n     \n    vis.append('svg:g')\n      .attr('class', 'y axis')\n      .attr('transform', 'translate(' + plotWidth / 2 + ',0)')\n      .style('opacity', 0.25)\n      .call(yAxis);\n\n    var colorScale = d3.scale.category10();\n    colorScale.domain[d3.range(0, 10, 1)];\n\n    var vectors = [];\n    var frequencyVectors = [];\n    var circles = [];\n    var amplitudes = [];\n    var freqSamples = [];\n\n    for (var i = 0; i < 5; i++)\n    {\n      vectors.push(vis.append(\"line\")\n        .attr(\"stroke-width\", 2.0)\n        .attr(\"stroke\", colorScale(i))\n        .style(\"stroke-linecap\", \"round\")\n        .style('opacity', 1.0)\n        );\n\n      circles.push(vis.append('svg:circle')\n        .attr('stroke-width', 2.5)\n        .attr('stroke', colorScale(i))\n        .attr('fill', 'none')\n        .attr('opacity', 0.30)\n      );\n\n      amplitudes.push(i === 0 ? 0.5 : 0.0);\n    }\n\n    var sineProjection = vis.append(\"line\")\n      .attr(\"x1\", xRange(1))\n      .attr(\"y1\", yRange(0))\n      .attr(\"x2\", xRange(0))\n      .attr(\"y2\", yRange(0))\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", \"grey\")\n      .style(\"stroke-linecap\", \"round\")\n      .style(\"stroke-dasharray\", (\"3, 3\"))\n      .style(\"opacity\", 1.0);\n\n    var axisExtension = vis.append(\"line\")\n      .attr(\"x1\", xRange(2.5))\n      .attr(\"y1\", yRange(0))\n      .attr(\"x2\", 630)\n      .attr(\"y2\", yRange(0))\n      .attr(\"stroke-width\", 1.0)\n      .attr(\"stroke\", \"black\")\n      .style(\"opacity\", 0.5);\n\n    var path = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", \"grey\")\n      .attr(\"fill\", \"none\")\n      .style(\"opacity\", 0.75);\n\n    var traceCircle = vis.append('svg:circle')\n      .attr('cx', xRange(0))\n      .attr('cy', yRange(0))\n      .attr('r', 2)\n      .attr('stroke-width', 2.0)\n      .attr('stroke', 'grey')\n      .attr('fill', 'grey')\n      .attr('opacity', 1);\n\n    var time = 0.0;\n    var data = d3.range(0, 6 * Math.PI + 0.05, 0.05);\n\n    var xRangePlot = d3.scale.linear().range([xRange(0) + 150, xRange(0) + 450]);\n    xRangePlot.domain([0, 6 * Math.PI + 0.05]);\n\n    vis.append(\"line\")\n      .attr(\"x1\", xRangePlot(2 * Math.PI))\n      .attr(\"y1\", yRange(-0.1))\n      .attr(\"x2\", xRangePlot(2 * Math.PI))\n      .attr(\"y2\", yRange(0))\n      .attr(\"stroke-width\", 1.0)\n      .attr(\"stroke\", \"grey\");\n\n    vis.append(\"text\")\n      .attr(\"text-anchor\", \"middle\")\n      .attr(\"x\", xRangePlot(2 * Math.PI))\n      .attr(\"y\", yRange(-0.3))\n      .attr(\"font-size\", 10)\n      .attr(\"fill\", \"grey\")\n      .text(\"0.25 s\");\n\n    vis.append(\"line\")\n      .attr(\"x1\", xRangePlot(4 * Math.PI))\n      .attr(\"y1\", yRange(-0.1))\n      .attr(\"x2\", xRangePlot(4 * Math.PI))\n      .attr(\"y2\", yRange(0))\n      .attr(\"stroke-width\", 1.0)\n      .attr(\"stroke\", \"grey\");\n\n    vis.append(\"text\")\n      .attr(\"text-anchor\", \"middle\")\n      .attr(\"x\", xRangePlot(4 * Math.PI))\n      .attr(\"y\", yRange(-0.3))\n      .attr(\"font-size\", 10)\n      .attr(\"fill\", \"grey\")\n      .text(\"0.5 s\");\n\n    vis.append(\"svg:rect\")\n      .attr(\"fill\", \"grey\")\n      .style(\"opacity\", 0.10)\n      .attr(\"x\", xRangePlot(0))\n      .attr(\"y\", yRange(2.5))\n      .attr(\"width\", xRangePlot(2*Math.PI) - xRangePlot(0))\n      .attr(\"height\", yRange(-2.5) - yRange(2.5));\n\n    var sine = d3.svg.line()\n      .x(function (d, i) { return xRangePlot(d)})\n      .y(function (d, i) {\n\n        return yRange(\n           (\n             (Math.sin(d) * amplitudes[0])\n           + (Math.sin(d * 2) * amplitudes[1])\n           + (Math.sin(d * 3) * amplitudes[2])\n           + (Math.sin(d * 4) * amplitudes[3])\n           + (Math.sin(d * 5) * amplitudes[4])\n           )\n        );\n      });\n\n    function onSine()\n    {\n      amplitudes[0] = 1.0;\n      amplitudes[1] = 0.0;\n      amplitudes[2] = 0.0;\n      amplitudes[3] = 0.0;\n      amplitudes[4] = 0.0;\n    }\n\n    function onSquare()\n    {\n      amplitudes[0] = 1.0;\n      amplitudes[1] = 0.0;\n      amplitudes[2] = 0.5;\n      amplitudes[3] = 0.0;\n      amplitudes[4] = 0.25;\n    }\n\n    function onSaw()\n    {\n      amplitudes[0] = 1.0;\n      amplitudes[1] = 0.50;\n      amplitudes[2] = 0.4;\n      amplitudes[3] = 0.0;\n      amplitudes[4] = 0.0;\n    }\n\n    function onWobble()\n    {\n      amplitudes[0] = 0.5;\n      amplitudes[1] = 0.7;\n      amplitudes[2] = 0.3;\n      amplitudes[3] = 0.0;\n      amplitudes[4] = 0.0;\n    }\n\n    onSine();\n\n    d3.select('#phasorbuttons').insert(\"button\")\n      .style(\"height\", 25)\n      .text(\"Sine\")\n      .on(\"click\", onSine);\n\n    d3.select('#phasorbuttons').insert(\"button\")\n      .style(\"height\", 25)\n      .text(\"Square\")\n      .on(\"click\", onSquare);\n\n    d3.select('#phasorbuttons').insert(\"button\")\n      .style(\"height\", 25)\n      .text(\"Saw\")\n      .on(\"click\", onSaw);\n\n    d3.select('#phasorbuttons').insert(\"button\")\n      .style(\"height\", 25)\n      .text(\"Wobble\")\n      .on(\"click\", onWobble);\n\n    var xComponent = 0;\n    var yComponent = 0;\n\n    var cosComp = 0;\n    var sinComp = 0;\n\n    function draw() {\n\n      cosComp = 0;\n      sinComp = 0;\n\n      for (var i = 0; i < 5; i++)\n      {\n        var xStart = xRange(cosComp);\n        var yStart = yRange(sinComp);\n\n        cosComp += Math.cos(time * (i + 1)) * amplitudes[i];\n        sinComp += Math.sin(time * (i + 1)) * amplitudes[i];\n\n        xComponent = xRange(cosComp);\n        yComponent = yRange(sinComp);\n\n        vectors[i]\n          .attr('x1', xStart)\n          .attr('y1', yStart)\n          .attr('x2', xComponent)\n          .attr('y2', yComponent);\n\n        circles[i]\n          .attr('cx', xStart)\n          .attr('cy', yStart)\n          .attr('r', xRange(amplitudes[i]) - xRange(0))\n      }\n\n      var leftX = xComponent;//Math.min(xComponent3, xRange(0));\n\n      sineProjection\n        .attr('x1', xRangePlot(time))\n        .attr('y1', yComponent)\n        .attr('x2', leftX)\n        .attr('y2', yComponent)\n\n      path\n        .attr('d', sine(data));\n\n      traceCircle\n        .attr(\"cx\", xRangePlot(time))\n        .attr(\"cy\", yComponent);\n\n      time += 0.0125;\n      if (time > Math.PI * 6)\n      {\n        time = 0.0;\n      }\n    }\n\n    d3.timer(draw, 100);\n  })();\n\n  // sigCorrelationInteractive\n  (function() {\n\n    var canvasWidth = 590;\n    var canvasHeight = 250;\n    var MARGINS =\n      {\n        top: 0,\n        right: 0,\n        bottom: 0,\n        left: 0\n      };\n\n    plotWidth = canvasWidth - MARGINS.left - MARGINS.right,\n    plotHeight = canvasHeight - MARGINS.top - MARGINS.bottom;\n\n    var subPlotHeight = plotHeight / 3;\n\n    var xRangeCorr = d3.scale.linear().range([MARGINS.left, plotWidth]);\n    var yRangeCorr = d3.scale.linear().range([subPlotHeight, MARGINS.top]);\n\n    var yRangeCorr1 = d3.scale.linear().range([subPlotHeight * 2 + 20, subPlotHeight + 20]);\n    var yRangeCorr2 = d3.scale.linear().range([subPlotHeight * 3 + 40, subPlotHeight * 2 + 40]);\n\n    xRangeCorr.domain([0, 4 * Math.PI]);\n    yRangeCorr.domain([-1.25, 1.25]);\n    yRangeCorr1.domain([-1.25, 1.25]);\n    yRangeCorr2.domain([-1.25, 1.25]);\n\n    var signalPhase = -1;\n    var signalFreq = 1;\n\n    var xAxis = d3.svg.axis()\n      .scale(xRangeCorr)\n      .tickSize(0)\n      .ticks(0)\n      .tickSubdivide(true);\n\n    var xAxis1 = d3.svg.axis()\n      .scale(xRangeCorr)\n      .tickSize(0)\n      .ticks(0)\n      .tickSubdivide(true);\n\n    var xAxis2 = d3.svg.axis()\n      .scale(xRangeCorr)\n      .tickSize(0)\n      .ticks(0)\n      .tickSubdivide(true);\n\n    var vis = d3.select('#sigCorrelationInteractive');\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeCorr(0) + ')')\n      .style('opacity', 0.25)\n      .call(xAxis);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeCorr1(0) + ')')\n      .style('opacity', 0.25)\n      .call(xAxis1);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeCorr2(0) + ')')\n      .style('opacity', 0.25)\n      .call(xAxis2);\n\n    var color1 = \"black\";\n    var color2 = \"green\";\n    var color3 = \"rgb(86, 60, 50)\";\n\n    var corrSigPath1 = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", color1)\n      .attr(\"fill\", \"none\")\n      .attr(\"opacity\", 0.4);\n\n    var corrSigPath2 = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", color2)\n      .attr(\"fill\", \"none\")\n      .attr(\"opacity\", 0.3);\n\n    var corrSigPath3 = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", color3)\n      .attr(\"fill\", color3)\n      .attr(\"fill-opacity\", 0.20)\n      .attr(\"opacity\", 0.50);\n\n    var corrSig1 = d3.svg.line()\n      .x(function (d, i) { return xRangeCorr(d)})\n      .y(function (d, i) { return yRangeCorr(inputSignal(d + signalPhase)); });\n\n    var corrSig2 = d3.svg.line()\n      .x(function (d, i) { return xRangeCorr(d)})\n      .y(function (d, i) { return yRangeCorr1(Math.sin(d * signalFreq)); });\n\n    var corrSig3 = d3.svg.line()\n      .x(function (d, i) { return xRangeCorr(d)})\n      .y(function (d, i) { return yRangeCorr2(inputSignal(d + signalPhase) * Math.sin(d * signalFreq)); });\n\n    var corrSigData = d3.range(0, 4 * Math.PI, 0.05);\n\n    corrSigPath1.attr(\"d\", corrSig1(corrSigData));\n    corrSigPath2.attr(\"d\", corrSig2(corrSigData));\n    corrSigPath3.attr(\"d\", corrSig3(corrSigData));\n\n    var corrSigSampleData = d3.range(0, 4 * Math.PI, 4 * Math.PI / 30);\n\n    function inputSignal(d)\n    {\n      return Math.sin(d)\n              + 0.3 * Math.sin(d * 3)\n              //+ 0.25 * Math.sin(d * 5)\n              //+ 0.10 * Math.sin(d * 10)\n              ; \n    }\n\n    var samples1 = vis.selectAll(\".point1\")\n      .data(corrSigSampleData)\n      .enter().append(\"svg:circle\")\n        .attr(\"stroke\", \"none\")\n        .attr(\"fill\", color1)\n        .attr(\"cx\", function(d, i) { return xRangeCorr(d); })\n        .attr(\"cy\", function(d, i) { return yRangeCorr(inputSignal(d + signalPhase)); })\n        .attr(\"r\", function(d, i) { return 2.0 });\n\n    var samples2 = vis.selectAll(\".point2\")\n      .data(corrSigSampleData)\n      .enter().append(\"svg:circle\")\n        .attr(\"stroke\", \"none\")\n        .attr(\"fill\", color2)\n        .attr(\"cx\", function(d, i) { return xRangeCorr(d); })\n        .attr(\"cy\", function(d, i) { return yRangeCorr1(Math.sin(d * signalFreq)); })\n        .attr(\"r\", function(d, i) { return 2.0 });\n\n    var connectors = vis.selectAll(\".connectors\")\n      .data(corrSigSampleData)\n      .enter().append(\"line\")\n        .attr(\"x1\", function(d, i) { return xRangeCorr(d); })\n        .attr(\"y1\", function(d, i) { return yRangeCorr(inputSignal(d)); })\n        .attr(\"x2\", function(d, i) { return xRangeCorr(d); })\n        .attr(\"y2\", function(d, i) { return yRangeCorr2(inputSignal(d + signalPhase) * Math.sin(d * signalFreq)); })\n        .attr(\"stroke-width\", 1.0)\n        .attr(\"stroke\", \"grey\")\n        .style(\"opacity\", 0.20)\n        .style(\"stroke-dasharray\", (\"3, 3\"))\n        ;\n\n    var samples3 = vis.selectAll(\".point3\")\n      .data(corrSigSampleData)\n      .enter().append(\"svg:circle\")\n        .attr(\"stroke\", \"none\")\n        .attr(\"fill\", color3)\n        .attr(\"cx\", function(d, i) { return xRangeCorr(d); })\n        .attr(\"cy\", function(d, i) { return yRangeCorr2(inputSignal(d + signalPhase) * Math.sin(d * signalFreq)); })\n        .attr(\"r\", function(d, i) { return 2.0 });\n\n    var xRangeDotProduct = d3.scale.linear().range([140, 470]);\n    xRangeDotProduct.domain([-20, 20]);\n\n    var xAxisDotProduct = d3.svg.axis()\n      .scale(xRangeDotProduct)\n      .tickSize(4)\n      .ticks(10)\n      .tickSubdivide(true);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + (yRangeCorr2(0) + 60) + ')')\n      .style('opacity', 0.45)\n      .call(xAxisDotProduct);\n\n    var corrText = vis.append('text')\n      .attr(\"text-anchor\", \"middle\")\n      .attr(\"x\", xRangeDotProduct(0))\n      .attr(\"y\", (yRangeCorr2(0) + 90))\n      .attr(\"stroke\", \"none\")\n      .attr(\"fill\", \"#555\")\n      .attr(\"font-size\", 12)\n      .attr(\"font-weight\", \"bold\")\n      .text(\"Dot Product: \");\n\n    vis.append(\"defs\").append(\"marker\")\n        .attr(\"id\", \"arrowhead\")\n        .attr(\"refX\", 5)\n        .attr(\"refY\", 2)\n        .attr(\"markerWidth\", 10)\n        .attr(\"markerHeight\", 10)\n        .attr(\"orient\", \"auto\")\n        .attr(\"fill\", color3)\n        .append(\"path\")\n            .attr(\"d\", \"M 0,0 V 4 L6,2 Z\");\n\n    var dotProductLine = vis.append(\"line\")\n      .attr(\"x1\", xRangeDotProduct(0))\n      .attr(\"y1\", (yRangeCorr2(0) + 60))\n      .attr(\"x2\", xRangeDotProduct(0))\n      .attr(\"y2\", (yRangeCorr2(0) + 60))\n      .attr(\"stroke-width\", 2)\n      .attr(\"stroke\", color3)\n      //.attr(\"marker-end\", \"url(#arrowhead)\")\n      ;\n\n    var dotProductCircle = vis.append(\"svg:circle\")\n      .attr(\"cx\", xRangeDotProduct(0))\n      .attr(\"cy\", (yRangeCorr2(0) + 60))\n      .attr(\"stroke\", \"#eee\")\n      .attr(\"stroke-width\", 1.5)\n      .attr(\"fill\", color3)\n      .attr(\"r\", 3.0);\n\n    var sigText = vis.append('text')\n      .attr(\"text-anchor\", \"left\")\n      .attr(\"x\", 0)\n      .attr(\"y\", yRangeCorr(0) + 13)\n      .attr(\"stroke\", \"none\")\n      .attr(\"fill\", \"grey\")\n      .attr(\"font-size\", 11)\n      .text(\"Signal A\");\n\n    var sig2Text = vis.append('text')\n      .attr(\"text-anchor\", \"left\")\n      .attr(\"x\", 0)\n      .attr(\"y\", yRangeCorr1(0) + 13)\n      .attr(\"stroke\", \"none\")\n      .attr(\"fill\", \"grey\")\n      .attr(\"font-size\", 11)\n      .text(\"Signal B\");\n\n    var prodText = vis.append('text')\n      .attr(\"text-anchor\", \"left\")\n      .attr(\"x\", 0)\n      .attr(\"y\", 210)\n      .attr(\"stroke\", \"none\")\n      .attr(\"fill\", \"grey\")\n      .attr(\"font-size\", 11)\n      .text(\"A x B\");\n\n    var colorScale = d3.scale.category10();\n    colorScale.domain[d3.range(0, 10, 1)];\n\n    function draw() {\n      if (SQUARE_CORRELATION_OFFSET == signalPhase && SQUARE_CORRELATION_FREQ == signalFreq)\n        return;\n\n      signalPhase = SQUARE_CORRELATION_OFFSET;\n      signalFreq = SQUARE_CORRELATION_FREQ;\n\n      document.getElementById(\"squareShift\").innerHTML = \"Phase Shift: &nbsp; <b>\" + (signalPhase * 180 / Math.PI).toFixed(2) + \"°</b>\";\n\n      samples1.data(corrSigSampleData)\n        .attr(\"cx\", function(d, i) { return xRangeCorr(d); })\n        .attr(\"cy\", function(d, i) { return yRangeCorr(inputSignal(d + signalPhase)); });\n      \n      samples2.data(corrSigSampleData)\n        .attr(\"fill\", colorScale(signalFreq))\n        .attr(\"cx\", function(d, i) { return xRangeCorr(d); })\n        .attr(\"cy\", function(d, i) { return yRangeCorr1(Math.sin(d * signalFreq)); });\n\n      samples3.data(corrSigSampleData)\n        .attr(\"cx\", function(d, i) { return xRangeCorr(d); })\n        .attr(\"cy\", function(d, i) { return yRangeCorr2(inputSignal(d + signalPhase) * Math.sin(d * signalFreq)); });\n\n      connectors.data(corrSigSampleData)\n        .attr(\"x1\", function(d, i) { return xRangeCorr(d); })\n        .attr(\"y1\", function(d, i) { return yRangeCorr(inputSignal(d + signalPhase)); })\n        .attr(\"x2\", function(d, i) { return xRangeCorr(d); })\n        .attr(\"y2\", function(d, i) { return yRangeCorr2(inputSignal(d + signalPhase) * Math.sin(d * signalFreq)); });\n\n      corrSigPath1.attr(\"d\", corrSig1(corrSigData));\n      corrSigPath2\n        .attr(\"stroke\", colorScale(signalFreq))\n        .attr(\"d\", corrSig2(corrSigData));\n      corrSigPath3.attr(\"d\", corrSig3(corrSigData));\n\n      var dotProduct = 0;\n      for (i = 0; i < corrSigSampleData.length; i++)\n      {\n        var d = corrSigSampleData[i];\n        dotProduct += inputSignal(d) * Math.sin(d * signalFreq + signalPhase);\n      }\n\n      corrText.text(\"Dot Product: \" + dotProduct.toFixed(2));\n\n      dotProductLine\n        .attr(\"x2\", xRangeDotProduct(dotProduct));\n\n      dotProductCircle\n        .attr(\"cx\", xRangeDotProduct(dotProduct));\n    };\n\n    d3.timer(draw, 100);\n  })();\n  var SQUARE_CORRELATION_OFFSET     = 0.0;\n  function updateSquareCorrelationOffset(value) { SQUARE_CORRELATION_OFFSET = Math.PI * 2 * (value / 100); }\n  var SQUARE_CORRELATION_FREQ       = 1.0;\n\n  // autosignal\n  (function() {\n    var canvasWidth = 600;\n    var canvasHeight = 100;\n    var MARGINS =\n      {\n        top: 0,\n        right: 0,\n        bottom: 0,\n        left: 0\n      };\n\n    var vis = d3.select('#autosignal');\n\n    var plotWidth = canvasWidth - MARGINS.left - MARGINS.right;\n    var plotHeight = canvasHeight - MARGINS.top - MARGINS.bottom;\n\n    var xRangeTime = d3.scale.linear().range([0, canvasWidth]);\n    xRangeTime.domain([0, 8 * Math.PI]);\n\n    var yRangeTime = d3.scale.linear().range([canvasHeight, 0]);\n    yRangeTime.domain([-1, 1]);\n\n    var data = d3.range(0, 8 * Math.PI, 0.01);\n\n    var signal = d3.svg.line()\n      .x(function (d, i) { return xRangeTime(d) + 1; })\n      .y(function (d, i) { return yRangeTime((Math.sin(d) + Math.sin(2 * d)) * 0.5)} );\n\n    vis.append(\"svg:rect\")\n      .attr(\"fill\", \"grey\")\n      .style(\"opacity\", 0.10)\n      .attr(\"x\", 0)\n      .attr(\"y\", 0)\n      .attr(\"width\", xRangeTime(2*Math.PI))\n      .attr(\"height\", yRangeTime(-1) - yRangeTime(1));\n\n    var xAxis = d3.svg.axis()\n      .scale(xRangeTime)\n      .tickSize(10)\n      .tickValues([2*Math.PI, 4*Math.PI, 6*Math.PI, 8*Math.PI])\n      .tickFormat(function(d) { return (d / (8*Math.PI)) + \" s\"; })\n      .tickSubdivide(true);\n\n    var yAxis = d3.svg.axis()\n      .scale(yRangeTime)\n      .tickSize(0)\n      .ticks(0)\n      .orient('left')\n      .tickSubdivide(true);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeTime(0) + ')')\n      .style(\"opacity\", 0.35)\n      .call(xAxis);\n\n    vis.append('svg:g')\n      .attr('class', 'y axis')\n      .attr('transform', 'translate(0,0)')\n      .style(\"opacity\", 0.35)\n      .call(yAxis);\n\n    var path = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", \"steelblue\")\n      .attr(\"fill\", \"none\")\n      .attr(\"opacity\", 1.0)\n      .attr(\"d\", signal(data));\n  }) ();\n  \n  // sigCorrelationInteractiveTwoSines\n  (function() {\n\n    var vis = d3.select('#sigCorrelationInteractiveTwoSines');\n\n    var signal = [];\n    var numPoints = 20;\n    for (var i = 0; i < numPoints; i++)\n    {\n      var phase = (i / numPoints) * 2 * Math.PI;\n\n      signal.push(\n        (Math.sin(phase) + Math.sin(2 * phase)) * 0.5\n        );\n    }\n\n    signal = signal.concat(signal).concat(signal);\n\n    function sigval(index)\n    {\n      var adjustedIndex = index - signal.length;\n      if (adjustedIndex > 0 && adjustedIndex < signal.length)\n        { return signal[adjustedIndex]; }\n      else\n        return 0.0;\n    }\n\n    var autocorrelation = [];\n\n    for (var i = 0; i < 120; i++)\n    {\n      var val = 0;\n      for (var j = 0; j < signal.length; j++)\n      {\n        val += signal[j] * sigval(j + i);\n      }\n\n      autocorrelation.push(val);\n    }\n\n    autocorrelation.push(0);\n\n    var canvasWidth = 550;\n    var canvasHeight = 250;\n    var MARGINS =\n      {\n        top: 0,\n        right: 0,\n        bottom: 0,\n        left: 0\n      };\n\n    plotWidth = canvasWidth - MARGINS.left - MARGINS.right,\n    plotHeight = canvasHeight - MARGINS.top - MARGINS.bottom;\n\n    var subPlotHeight = plotHeight / 3;\n\n    var xRangeCorr = d3.scale.linear().range([MARGINS.left, plotWidth]);\n    var yRangeCorr = d3.scale.linear().range([subPlotHeight, MARGINS.top]);\n\n    var yRangeCorr1 = d3.scale.linear().range([subPlotHeight * 2 + 20, subPlotHeight + 20]);\n    var yRangeCorr2 = d3.scale.linear().range([subPlotHeight * 3 + 40, subPlotHeight * 2 + 40]);\n\n    xRangeCorr.domain([0, signal.length * 3]);\n    yRangeCorr.domain([-1.0, 1.0]);\n    yRangeCorr1.domain([-1.0, 1.0]);\n    yRangeCorr2.domain([-15.00, 15.00]);\n\n    vis.append(\"svg:rect\")\n      .attr(\"fill\", \"grey\")\n      .style(\"opacity\", 0.10)\n      .attr(\"x\", xRangeCorr(signal.length))\n      .attr(\"y\", 0)\n      .attr(\"width\", xRangeCorr(signal.length) / 3.0)\n      .attr(\"height\", 280);\n\n    var lag = 60;\n\n    var xAxis = d3.svg.axis()\n      .scale(xRangeCorr)\n      .tickSize(0)\n      .ticks(0)\n      .tickSubdivide(true);\n\n    var xAxis1 = d3.svg.axis()\n      .scale(xRangeCorr)\n      .tickSize(0)\n      .ticks(0)\n      .tickSubdivide(true);\n\n    var xAxis2 = d3.svg.axis()\n      .scale(xRangeCorr)\n      .tickSize(0)\n      .ticks(0)\n      .tickSubdivide(true);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeCorr(0) + ')')\n      .style('opacity', 0.25)\n      .call(xAxis);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeCorr1(0) + ')')\n      .style('opacity', 0.25)\n      .call(xAxis1);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeCorr2(0) + ')')\n      .style('opacity', 0.25)\n      .call(xAxis2);\n\n    var color1 = \"steelblue\";\n    var color2 = \"green\";\n    var color3 = \"#8c564b\";\n\n    var lag = 0;\n    var laggedSamples = [];\n\n    var corrSig1 = d3.svg.line()\n      .x(function (d, i) { return xRangeCorr(d + signal.length)})\n      .y(function (d, i) { return yRangeCorr(signal[i]); });\n\n    var corrSig2 = d3.svg.line()\n      .x(function (d, i) { return xRangeCorr(d + lag)})\n      .y(function (d, i) { return yRangeCorr1(signal[i]); });\n\n    var corrSig3 = d3.svg.line()\n      .x(function (d, i) { return xRangeCorr(i)})\n      .y(function (d, i) { return yRangeCorr2(autocorrelation[i]); });\n\n    var connectors = vis.selectAll(\".connectors\")\n      .data(d3.range(0, signal.length, 1))\n      .enter().append(\"line\")\n        .attr(\"x1\", function(d, i) { return xRangeCorr(d + lag); })\n        .attr(\"y1\", function(d, i) { return yRangeCorr(sigval(d + lag)); })\n        .attr(\"x2\", function(d, i) { return xRangeCorr(d + lag); })\n        .attr(\"y2\", function(d, i) { return yRangeCorr1(signal[d]); })\n        .attr(\"stroke-width\", 1.0)\n        .attr(\"stroke\", \"grey\")\n        .style(\"opacity\", 0.20)\n        .style(\"stroke-dasharray\", (\"5, 2\"));\n\n    function drawOriginalSignal()\n    { \n      var offset = signal.length;\n\n        vis.append('svg:path')\n          .attr(\"stroke-width\", 2.0)\n          .attr(\"stroke\", color1)\n          .attr(\"fill\", \"none\")\n          .attr(\"opacity\", 0.4)\n          .attr(\"d\", corrSig1(d3.range(0, signal.length, 1)));\n\n      for (var i = 0; i < signal.length; i++)\n      {\n        vis.append(\"svg:circle\")\n          .attr(\"stroke\", \"none\")\n          .attr(\"fill\", color1)\n          .attr(\"cx\", xRangeCorr(i + offset))\n          .attr(\"cy\", yRangeCorr(signal[i]))\n          .attr(\"r\", 2.0);\n      }\n    }\n\n    drawOriginalSignal();\n\n    function drawAutocorrelation()\n    { \n        vis.append('svg:path')\n          .attr(\"stroke-width\", 2.0)\n          .attr(\"stroke\", color3)\n          .attr(\"fill\", \"none\")\n          .attr(\"opacity\", 0.4)\n          .attr(\"d\", corrSig3(d3.range(0, autocorrelation.length, 1)));\n\n      for (var i = 0; i < autocorrelation.length; i++)\n      {\n        vis.append(\"svg:circle\")\n          .attr(\"stroke\", \"none\")\n          .attr(\"fill\", color3)\n          .attr(\"cx\", xRangeCorr(i))\n          .attr(\"cy\", yRangeCorr2(autocorrelation[i]))\n          .attr(\"r\", 2.0);\n      }\n    }\n\n    drawAutocorrelation();\n\n    for (var i = 0; i < signal.length; i++)\n    {\n      laggedSamples.push(\n        vis.append(\"svg:circle\")\n        .attr(\"stroke\", \"none\")\n        .attr(\"fill\", color2)\n        .attr(\"cx\", xRangeCorr(i + lag))\n        .attr(\"cy\", yRangeCorr1(signal[i]))\n        .attr(\"r\", 2.0));\n    }\n\n    var laggedPath = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", color1)\n      .attr(\"fill\", \"none\")\n      .attr(\"opacity\", 0.4)\n      .attr(\"d\", corrSig2(d3.range(0, signal.length, 1)));\n\n    function updateSignals()\n    {\n      for (var i = 0; i < laggedSamples.length; i++)\n      {\n        laggedSamples[i].attr(\"cx\", xRangeCorr(i + lag));\n      }\n\n      laggedPath\n        .attr(\"d\", corrSig2(d3.range(0, signal.length, 1)));\n\n    }\n\n    var AutoCorrLine = vis.append(\"line\")\n      .attr(\"x1\", xRangeCorr(lag))\n      .attr(\"y1\", yRangeCorr2(autocorrelation[lag]))\n      .attr(\"x2\", xRangeCorr(lag))\n      .attr(\"y2\", yRangeCorr(sigval(lag)))\n      .attr(\"stroke-width\", 2)\n      .attr(\"stroke\", \"grey\")\n      .style(\"opacity\", 0.5)\n      //.attr(\"marker-end\", \"url(#arrowhead)\")\n      ;\n\n    var AutoCorrCircle = vis.append(\"svg:circle\")\n      .attr(\"cx\", xRangeCorr(lag))\n      .attr(\"cy\", yRangeCorr2(autocorrelation[lag]))\n      .attr(\"stroke\", \"grey\")\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"fill\", \"none\")\n      .attr(\"r\", 4.0);\n\n    var sigText = vis.append('text')\n      .attr(\"text-anchor\", \"end\")\n      .attr(\"x\", xRangeCorr(180))\n      .attr(\"y\", yRangeCorr(0) + 13)\n      .attr(\"stroke\", \"none\")\n      .attr(\"fill\", \"#555\")\n      .attr(\"font-weight\", \"bold\")\n      .attr(\"font-size\", 11)\n      .text(\"Original Waveform, x(n)\");\n\n    vis.append(\"svg:rect\")\n      .attr(\"fill\", \"white\")\n      .style(\"opacity\", 1.0)\n      .attr(\"x\", xRangeCorr(150))\n      .attr(\"y\", yRangeCorr1(0) + 2)\n      .attr(\"width\", 100)\n      .attr(\"height\", 15);\n\n    var sig2Text = vis.append('text')\n      .attr(\"text-anchor\", \"end\")\n      .attr(\"x\", xRangeCorr(180))\n      .attr(\"y\", yRangeCorr1(0) + 13)\n      .attr(\"stroke\", \"none\")\n      .attr(\"fill\", \"#555\")\n      .attr(\"font-weight\", \"bold\")\n      .attr(\"font-size\", 11)\n      .text(\"Lagged Waveform, x(k + t)\");\n\n    var prodText = vis.append('text')\n      .attr(\"text-anchor\", \"end\")\n      .attr(\"x\", xRangeCorr(180))\n      .attr(\"y\", 265)\n      .attr(\"stroke\", \"none\")\n      .attr(\"fill\", \"#555\")\n      .attr(\"font-weight\", \"bold\")\n      .attr(\"font-size\", 11)\n      .text(\"Auto Correlation Function\");\n\n    var colorScale = d3.scale.category10();\n    colorScale.domain[d3.range(0, 10, 1)];\n\n    function draw() {\n      if (SIMPLE_CORRELATION_OFFSET == lag)\n      {\n        return;\n      }\n\n      lag = SIMPLE_CORRELATION_OFFSET;\n\n      updateSignals();\n\n      document.getElementById(\"phaseShift\").innerHTML = \"Lag: &nbsp; <b>\" + (lag - 60).toFixed(0) + \"</b>\";\n\n      connectors.data(d3.range(0, signal.length, 1))\n        .attr(\"x1\", function(d, i) { return xRangeCorr(d + lag); })\n        .attr(\"y1\", function(d, i) { return yRangeCorr(sigval(d + lag)); })\n        .attr(\"x2\", function(d, i) { return xRangeCorr(d + lag); })\n        .attr(\"y2\", function(d, i) { return yRangeCorr1(signal[d]); });\n\n      AutoCorrLine\n      .attr(\"x1\", xRangeCorr(lag))\n      .attr(\"y1\", yRangeCorr2(autocorrelation[lag]))\n      .attr(\"x2\", xRangeCorr(lag))\n      .attr(\"y2\", yRangeCorr(sigval(lag)))\n      //.attr(\"marker-end\", \"url(#arrowhead)\")\n      ;\n\n      AutoCorrCircle\n      .attr(\"cx\", xRangeCorr(lag))\n      .attr(\"cy\", yRangeCorr2(autocorrelation[lag]));\n    }\n\n    d3.timer(draw, 100);\n  }) ();\n  var SIMPLE_CORRELATION_OFFSET = 0.0;\n  function updateSimpleCorrelationOffset(value) { SIMPLE_CORRELATION_OFFSET = value * 1.0; }\n\n  // autocorrelationinterpretation\n  (function() {\n\n    var autocorrelation =\n    [\n    0,\n    0,\n     -0.20106356740693337,\n     -0.6900183776675255,\n     -1.3812274365218544,\n     -2.044255694669431,\n     -2.4068828084493017,\n     -2.2865494441493777,\n     -1.6878630685231943,\n     -0.8169335313590205,\n     0,\n     0.4618347265585053,\n     0.4378630685231944,\n     0.044543827480826276,\n     -0.388202163425436,\n     -0.45574430533056925,\n     0.1312274365218541,\n     1.3869390224613396,\n     2.9961485392816702,\n     4.400183776675253,\n     5.000000000000001,\n     4.400183776675253,\n     2.5940214044678034,\n     0.006902267126288608,\n     -2.6312274365218546,\n     -4.544255694669432,\n     -5.20196778032404,\n     -4.528555060817929,\n     -2.937863068523195,\n     -1.1720323361595355,\n     0,\n     0.10673592175799038,\n     -0.8121369314768054,\n     -2.1974617891877255,\n     -3.183287135300174,\n     -2.9557443053305694,\n     -1.118772563478146,\n     2.0838596672551537,\n     5.791233511156407,\n     8.800367553350505,\n     10.000000000000002,\n     8.800367553350505,\n     5.38910637634254,\n     0.7038229119201027,\n     -3.8812274365218546,\n     -7.044255694669432,\n     -7.997052752198776,\n     -6.770560677486481,\n     -4.1878630685231935,\n     -1.5271311409600505,\n     0,\n     -0.24836288304252463,\n     -2.0621369314768057,\n     -4.439467405856277,\n     -5.978372107174911,\n     -5.45574430533057,\n     -2.368772563478146,\n     2.7807803120489676,\n     8.586318483031143,\n     13.200551330025757,\n     15.000000000000002,\n     13.200551330025757,\n     8.586318483031143,\n     2.7807803120489676,\n     -2.368772563478146,\n     -5.45574430533057,\n     -5.978372107174911,\n     -4.439467405856277,\n     -2.0621369314768057,\n     -0.24836288304252463,\n     0,\n     -1.5271311409600505,\n     -4.1878630685231935,\n     -6.770560677486481,\n     -7.997052752198776,\n     -7.044255694669432,\n     -3.8812274365218546,\n     0.7038229119201027,\n     5.38910637634254,\n     8.800367553350505,\n     10.000000000000002,\n     8.800367553350505,\n     5.791233511156407,\n     2.0838596672551537,\n     -1.118772563478146,\n     -2.9557443053305694,\n     -3.183287135300174,\n     -2.1974617891877255,\n     -0.8121369314768054,\n     0.10673592175799038,\n     0,\n     -1.1720323361595355,\n     -2.937863068523195,\n     -4.528555060817929,\n     -5.20196778032404,\n     -4.544255694669432,\n     -2.6312274365218546,\n     0.006902267126288608,\n     2.5940214044678034,\n     4.400183776675253,\n     5.000000000000001,\n     4.400183776675253,\n     2.9961485392816702,\n     1.3869390224613396,\n     0.1312274365218541,\n     -0.45574430533056925,\n     -0.388202163425436,\n     0.044543827480826276,\n     0.4378630685231944,\n     0.4618347265585053,\n     0,\n     -0.8169335313590205,\n     -1.6878630685231943,\n     -2.2865494441493777,\n     -2.4068828084493017,\n     -2.044255694669431,\n     -1.3812274365218544,\n     -0.6900183776675255,\n     -0.20106356740693337,\n     0,\n     0\n    ]; \n\n    var canvasWidth = 600;\n    var canvasHeight = 150;\n    var MARGINS =\n      {\n        top: 0,\n        right: 0,\n        bottom: 0,\n        left: 0\n      };\n\n    var vis = d3.select('#autocorrelationinterpretation');\n\n    var plotWidth = canvasWidth - MARGINS.left - MARGINS.right;\n    var plotHeight = canvasHeight - MARGINS.top - MARGINS.bottom;\n\n    var xRangeTime = d3.scale.linear().range([0, canvasWidth]);\n    xRangeTime.domain([0, autocorrelation.length]);\n\n    var yRangeTime = d3.scale.linear().range([canvasHeight, 0]);\n    yRangeTime.domain([-18, 18]);\n\n    var data = autocorrelation;\n\n    var signal = d3.svg.line()\n      .x(function (d, i) { return xRangeTime(i); })\n      .y(function (d, i) { return yRangeTime(d); });\n\n    vis.append(\"svg:rect\")\n      .attr(\"fill\", \"grey\")\n      .style(\"opacity\", 0.10)\n      .attr(\"x\", xRangeTime(data.length / 2 - 0.5 ))\n      .attr(\"y\", 0)\n      .attr(\"width\", xRangeTime(data.length / 6))\n      .attr(\"height\", yRangeTime(-15) - yRangeTime(15));\n\n    var xAxis = d3.svg.axis()\n      .scale(xRangeTime)\n      .tickSize(10)\n      .tickValues([20, 40, 60, 80, 100])\n      .tickFormat(function(d) { return ((d / (120)) * 1.5 - 0.75).toFixed(2) + \" s\"; })\n      .tickSubdivide(true);\n\n    var yAxis = d3.svg.axis()\n      .scale(yRangeTime)\n      .tickSize(0)\n      .ticks(0)\n      .orient('left')\n      .tickSubdivide(true);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeTime(0) + ')')\n      .style(\"opacity\", 0.35)\n      .call(xAxis);\n\n    vis.append('svg:g')\n      .attr('class', 'y axis')\n      .attr('transform', 'translate(0,0)')\n      .style(\"opacity\", 0.35)\n      .call(yAxis);\n\n    var path = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", \"#8c564b\")\n      .attr(\"fill\", \"none\")\n      .attr(\"opacity\", 0.5)\n      .attr(\"d\", signal(data));\n\n    for (var i = 0; i < autocorrelation.length; i++)\n    {\n      vis.append(\"svg:circle\")\n          .attr(\"stroke\", \"none\")\n          .attr(\"fill\", \"#8c564b\")\n          .attr(\"cx\", xRangeTime(i))\n          .attr(\"cy\", yRangeTime(autocorrelation[i]))\n          .attr(\"r\", 2.5);  \n    }\n\n    vis.append(\"defs\").append(\"marker\")\n        .attr(\"id\", \"arrowhead\")\n        .attr(\"refX\", 5)\n        .attr(\"refY\", 2)\n        .attr(\"markerWidth\", 10)\n        .attr(\"markerHeight\", 10)\n        .attr(\"orient\", \"auto\")\n        .attr(\"fill\", \"black\")\n        .append(\"path\")\n            .attr(\"d\", \"M 0,0 V 4 L6,2 Z\");\n\n    vis.append(\"line\")\n      .attr(\"x1\", xRangeTime(data.length / 2 + 9))\n      .attr(\"y1\", yRangeTime(15))\n      .attr(\"x2\", xRangeTime(data.length / 2 + 1))\n      .attr(\"y2\", yRangeTime(15))\n      .attr(\"stroke-width\", 2)\n      .attr(\"stroke\", \"black\")\n      .style(\"opacity\", 0.25)\n      .attr(\"marker-end\", \"url(#arrowhead)\");\n\n    vis.append(\"line\")\n      .attr(\"x1\", xRangeTime(data.length / 2 + 9))\n      .attr(\"y1\", yRangeTime(15))\n      .attr(\"x2\", xRangeTime(data.length / 2 - 0.5) + xRangeTime(data.length / 6 - 1))\n      .attr(\"y2\", yRangeTime(15))\n      .attr(\"stroke-width\", 2)\n      .attr(\"stroke\", \"black\")\n      .style(\"opacity\", 0.25)\n      .attr(\"marker-end\", \"url(#arrowhead)\");\n\n    vis.append(\"svg:circle\")\n      .attr(\"stroke\", \"grey\")\n      .attr(\"stroke-width\", 2)\n      .attr(\"fill\", \"none\")\n      .attr(\"cx\", xRangeTime(60))\n      .attr(\"cy\", yRangeTime(autocorrelation[60]))\n      .attr(\"r\", 4.5); \n\n    vis.append(\"svg:circle\")\n      .attr(\"stroke\", \"grey\")\n      .attr(\"stroke-width\", 2)\n      .attr(\"fill\", \"none\")\n      .attr(\"cx\", xRangeTime(80))\n      .attr(\"cy\", yRangeTime(autocorrelation[80]))\n      .attr(\"r\", 4.5);\n  })();\n\n  // Buffer 1\n  (function() {\n    var longsignal = []; \n\n    for (var i = 0; i < 128; i++)\n    {\n      var phase = (i / 128) * 4 * Math.PI;\n      longsignal.push(Math.sin(phase));\n    }\n\n    var canvasWidth = 600;\n    var canvasHeight = 100;\n    var MARGINS =\n      {\n        top: 0,\n        right: 0,\n        bottom: 0,\n        left: 0\n      };\n\n    var vis = d3.select('#buffer1');\n\n    var plotWidth = canvasWidth - MARGINS.left - MARGINS.right;\n    var plotHeight = canvasHeight - MARGINS.top - MARGINS.bottom;\n\n    var xRangeTime = d3.scale.linear().range([0, canvasWidth]);\n    xRangeTime.domain([0, longsignal.length]);\n\n    var yRangeTime = d3.scale.linear().range([canvasHeight, 0]);\n    yRangeTime.domain([-1.6, 1.6]);\n\n    var data = longsignal;\n\n    var signal = d3.svg.line()\n      .x(function (d, i) { return xRangeTime(i); })\n      .y(function (d, i) { return yRangeTime(d); });\n\n    vis.append(\"svg:rect\")\n      .attr(\"fill\", \"grey\")\n      .style(\"opacity\", 0.10)\n      .attr(\"x\", xRangeTime(0))\n      .attr(\"y\", 0)\n      .attr(\"width\", xRangeTime(data.length / 4))\n      .attr(\"height\", 120);\n\n    var xAxis = d3.svg.axis()\n      .scale(xRangeTime)\n      .tickSize(10)\n      .tickValues([16, 32, 48, 64, 80, 96, 112, 128, 144, 160])\n      .tickFormat(function(d) { return ((d / (data.length))).toFixed(2) + \" s\"; })\n      .tickSubdivide(true);\n\n    var yAxis = d3.svg.axis()\n      .scale(yRangeTime)\n      .tickSize(0)\n      .ticks(0)\n      .orient('left')\n      .tickSubdivide(true);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeTime(0) + ')')\n      .style(\"opacity\", 0.35)\n      .call(xAxis);\n\n    vis.append('svg:g')\n      .attr('class', 'y axis')\n      .attr('transform', 'translate(0,0)')\n      .style(\"opacity\", 0.35)\n      .call(yAxis);\n\n    var path = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", \" steelblue\")\n      .attr(\"fill\", \"none\")\n      .attr(\"opacity\", 0.3)\n      .attr(\"d\", signal(data));\n\n    for (var i = 0; i < data.length / 4; i++)\n    {\n      vis.append(\"svg:circle\")\n          .attr(\"stroke\", \"none\")\n          .attr(\"fill\", \" steelblue\")\n          .attr(\"cx\", xRangeTime(i))\n          .attr(\"cy\", yRangeTime(data[i]))\n          .attr(\"r\", 2.5);  \n    }\n\n    vis.append('text')\n      .attr(\"text-anchor\", \"middle\")\n      .attr(\"x\", xRangeTime(16))\n      .attr(\"y\", 12)\n      .attr(\"stroke\", \"none\")\n      .attr(\"fill\", \"#555\")\n      .attr(\"font-size\", 12)\n      .attr(\"font-weight\", \"bold\")\n      .text(\"First Input Buffer\");\n  })();\n\n  // Buffer 2\n  (function() {\n\n    var longsignal = []; \n\n    for (var i = 0; i < 128; i++)\n    {\n      var phase = (i / 128) * 4 * Math.PI;\n      longsignal.push(Math.sin(phase));\n    }\n\n    var canvasWidth = 600;\n    var canvasHeight = 100;\n    var MARGINS =\n      {\n        top: 0,\n        right: 0,\n        bottom: 0,\n        left: 0\n      };\n\n    var vis = d3.select('#buffer2');\n\n    var plotWidth = canvasWidth - MARGINS.left - MARGINS.right;\n    var plotHeight = canvasHeight - MARGINS.top - MARGINS.bottom;\n\n    var xRangeTime = d3.scale.linear().range([0, canvasWidth]);\n    xRangeTime.domain([0, longsignal.length]);\n\n    var yRangeTime = d3.scale.linear().range([canvasHeight, 0]);\n    yRangeTime.domain([-1.6, 1.6]);\n\n    var data = longsignal;\n\n    var signal = d3.svg.line()\n      .x(function (d, i) { return xRangeTime(i); })\n      .y(function (d, i) { return yRangeTime(d); });\n\n    vis.append(\"svg:rect\")\n      .attr(\"fill\", \"grey\")\n      .style(\"opacity\", 0.10)\n      .attr(\"x\", xRangeTime(32))\n      .attr(\"y\", 0)\n      .attr(\"width\", xRangeTime(data.length / 4))\n      .attr(\"height\", 120);\n\n    var xAxis = d3.svg.axis()\n      .scale(xRangeTime)\n      .tickSize(10)\n      .tickValues([16, 32, 48, 64, 80, 96, 112, 128, 144, 160])\n      .tickFormat(function(d) { return ((d / (data.length))).toFixed(2) + \" s\"; })\n      .tickSubdivide(true);\n\n    var yAxis = d3.svg.axis()\n      .scale(yRangeTime)\n      .tickSize(0)\n      .ticks(0)\n      .orient('left')\n      .tickSubdivide(true);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeTime(0) + ')')\n      .style(\"opacity\", 0.35)\n      .call(xAxis);\n\n    vis.append('svg:g')\n      .attr('class', 'y axis')\n      .attr('transform', 'translate(0,0)')\n      .style(\"opacity\", 0.35)\n      .call(yAxis);\n\n    var path = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", \" steelblue\")\n      .attr(\"fill\", \"none\")\n      .attr(\"opacity\", 0.3)\n      .attr(\"d\", signal(data));\n\n    for (var i = 0; i < data.length / 2; i++)\n    {\n      vis.append(\"svg:circle\")\n          .attr(\"stroke\", \"none\")\n          .attr(\"fill\", \" steelblue\")\n          .attr(\"cx\", xRangeTime(i))\n          .attr(\"cy\", yRangeTime(data[i]))\n          .attr(\"r\", 2.5);  \n    }\n\n    vis.append('text')\n      .attr(\"text-anchor\", \"middle\")\n      .attr(\"x\", xRangeTime(48))\n      .attr(\"y\", 12)\n      .attr(\"stroke\", \"none\")\n      .attr(\"fill\", \"#555\")\n      .attr(\"font-size\", 12)\n      .attr(\"font-weight\", \"bold\")\n      .text(\"Second Input Buffer\");\n  })();\n\n  // Buffer 3\n  (function() {\n\n    var longsignal = []; \n\n    for (var i = 0; i < 128; i++)\n    {\n      var phase = (i / 128) * 4 * Math.PI;\n      longsignal.push(Math.sin(phase));\n    }\n\n    var canvasWidth = 600;\n    var canvasHeight = 100;\n    var MARGINS =\n      {\n        top: 0,\n        right: 0,\n        bottom: 0,\n        left: 0\n      };\n\n    var vis = d3.select('#buffer3');\n\n    var plotWidth = canvasWidth - MARGINS.left - MARGINS.right;\n    var plotHeight = canvasHeight - MARGINS.top - MARGINS.bottom;\n\n    var xRangeTime = d3.scale.linear().range([0, canvasWidth]);\n    xRangeTime.domain([0, longsignal.length]);\n\n    var yRangeTime = d3.scale.linear().range([canvasHeight, 0]);\n    yRangeTime.domain([-1.6, 1.6]);\n\n    var data = longsignal;\n\n    var signal = d3.svg.line()\n      .x(function (d, i) { return xRangeTime(i); })\n      .y(function (d, i) { return yRangeTime(d); });\n\n    vis.append(\"svg:rect\")\n      .attr(\"fill\", \"grey\")\n      .style(\"opacity\", 0.10)\n      .attr(\"x\", xRangeTime(64))\n      .attr(\"y\", 0)\n      .attr(\"width\", xRangeTime(data.length / 4))\n      .attr(\"height\", 120);\n\n    var xAxis = d3.svg.axis()\n      .scale(xRangeTime)\n      .tickSize(10)\n      .tickValues([16, 32, 48, 64, 80, 96, 112, 128, 144, 160])\n      .tickFormat(function(d) { return ((d / (data.length))).toFixed(2) + \" s\"; })\n      .tickSubdivide(true);\n\n    var yAxis = d3.svg.axis()\n      .scale(yRangeTime)\n      .tickSize(0)\n      .ticks(0)\n      .orient('left')\n      .tickSubdivide(true);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeTime(0) + ')')\n      .style(\"opacity\", 0.35)\n      .call(xAxis);\n\n    vis.append('svg:g')\n      .attr('class', 'y axis')\n      .attr('transform', 'translate(0,0)')\n      .style(\"opacity\", 0.35)\n      .call(yAxis);\n\n    var path = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", \" steelblue\")\n      .attr(\"fill\", \"none\")\n      .attr(\"opacity\", 0.3)\n      .attr(\"d\", signal(data));\n\n    for (var i = 0; i < 96; i++)\n    {\n      vis.append(\"svg:circle\")\n          .attr(\"stroke\", \"none\")\n          .attr(\"fill\", \" steelblue\")\n          .attr(\"cx\", xRangeTime(i))\n          .attr(\"cy\", yRangeTime(data[i]))\n          .attr(\"r\", 2.5);  \n    }\n\n    vis.append('text')\n      .attr(\"text-anchor\", \"middle\")\n      .attr(\"x\", xRangeTime(80))\n      .attr(\"y\", 12)\n      .attr(\"stroke\", \"none\")\n      .attr(\"fill\", \"#555\")\n      .attr(\"font-size\", 12)\n      .attr(\"font-weight\", \"bold\")\n      .text(\"Third Input Buffer\");\n  })();\n  \n  // Buffer 4\n  (function() {\n\n    var longsignal = []; \n\n    for (var i = 0; i < 128; i++)\n    {\n      var phase = (i / 128) * 4 * Math.PI;\n      longsignal.push(Math.sin(phase));\n    }\n\n    var canvasWidth = 600;\n    var canvasHeight = 100;\n    var MARGINS =\n      {\n        top: 0,\n        right: 0,\n        bottom: 0,\n        left: 0\n      };\n\n    var vis = d3.select('#buffer4');\n\n    var plotWidth = canvasWidth - MARGINS.left - MARGINS.right;\n    var plotHeight = canvasHeight - MARGINS.top - MARGINS.bottom;\n\n    var xRangeTime = d3.scale.linear().range([0, canvasWidth]);\n    xRangeTime.domain([0, longsignal.length]);\n\n    var yRangeTime = d3.scale.linear().range([canvasHeight, 0]);\n    yRangeTime.domain([-1.6, 1.6]);\n\n    var data = longsignal;\n\n    var signal = d3.svg.line()\n      .x(function (d, i) { return xRangeTime(i); })\n      .y(function (d, i) { return yRangeTime(d); });\n\n    vis.append(\"svg:rect\")\n      .attr(\"fill\", \"grey\")\n      .style(\"opacity\", 0.10)\n      .attr(\"x\", xRangeTime(96))\n      .attr(\"y\", 0)\n      .attr(\"width\", xRangeTime(data.length / 4))\n      .attr(\"height\", 120);\n\n    var xAxis = d3.svg.axis()\n      .scale(xRangeTime)\n      .tickSize(10)\n      .tickValues([16, 32, 48, 64, 80, 96, 112, 128, 144, 160])\n      .tickFormat(function(d) { return ((d / (data.length))).toFixed(2) + \" s\"; })\n      .tickSubdivide(true);\n\n    var yAxis = d3.svg.axis()\n      .scale(yRangeTime)\n      .tickSize(0)\n      .ticks(0)\n      .orient('left')\n      .tickSubdivide(true);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeTime(0) + ')')\n      .style(\"opacity\", 0.35)\n      .call(xAxis);\n\n    vis.append('svg:g')\n      .attr('class', 'y axis')\n      .attr('transform', 'translate(0,0)')\n      .style(\"opacity\", 0.35)\n      .call(yAxis);\n\n    var path = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", \" steelblue\")\n      .attr(\"fill\", \"none\")\n      .attr(\"opacity\", 0.3)\n      .attr(\"d\", signal(data));\n\n    for (var i = 0; i < data.length; i++)\n    {\n      vis.append(\"svg:circle\")\n          .attr(\"stroke\", \"none\")\n          .attr(\"fill\", \" steelblue\")\n          .attr(\"cx\", xRangeTime(i))\n          .attr(\"cy\", yRangeTime(data[i]))\n          .attr(\"r\", 2.5);  \n    }\n\n    vis.append('text')\n      .attr(\"text-anchor\", \"middle\")\n      .attr(\"x\", xRangeTime(112))\n      .attr(\"y\", 12)\n      .attr(\"stroke\", \"none\")\n      .attr(\"fill\", \"#555\")\n      .attr(\"font-size\", 12)\n      .attr(\"font-weight\", \"bold\")\n      .text(\"Fourth Input Buffer\");\n  })();\n  \n  // Buffer 5\n  (function() {\n\n    var longsignal = []; \n\n    for (var i = 0; i < 128; i++)\n    {\n      var phase = (i / 128) * 4 * Math.PI;\n      longsignal.push(Math.sin(4 * phase));\n    }\n\n    var canvasWidth = 600;\n    var canvasHeight = 100;\n    var MARGINS =\n      {\n        top: 0,\n        right: 0,\n        bottom: 0,\n        left: 0\n      };\n\n    var vis = d3.select('#buffer5');\n\n    var plotWidth = canvasWidth - MARGINS.left - MARGINS.right;\n    var plotHeight = canvasHeight - MARGINS.top - MARGINS.bottom;\n\n    var xRangeTime = d3.scale.linear().range([0, canvasWidth]);\n    xRangeTime.domain([0, longsignal.length]);\n\n    var yRangeTime = d3.scale.linear().range([canvasHeight, 0]);\n    yRangeTime.domain([-1.6, 1.6]);\n\n    var data = longsignal;\n\n    var signal = d3.svg.line()\n      .x(function (d, i) { return xRangeTime(i); })\n      .y(function (d, i) { return yRangeTime(d); });\n\n    vis.append(\"svg:rect\")\n      .attr(\"fill\", \"grey\")\n      .style(\"opacity\", 0.10)\n      .attr(\"x\", xRangeTime(0))\n      .attr(\"y\", 0)\n      .attr(\"width\", xRangeTime(data.length / 4))\n      .attr(\"height\", 120);\n\n    var xAxis = d3.svg.axis()\n      .scale(xRangeTime)\n      .tickSize(10)\n      .tickValues([16, 32, 48, 64, 80, 96, 112, 128, 144, 160])\n      .tickFormat(function(d) { return ((d / (data.length))).toFixed(2) + \" s\"; })\n      .tickSubdivide(true);\n\n    var yAxis = d3.svg.axis()\n      .scale(yRangeTime)\n      .tickSize(0)\n      .ticks(0)\n      .orient('left')\n      .tickSubdivide(true);\n\n    vis.append('svg:g')\n      .attr('class', 'x axis')\n      .attr('transform', 'translate(0,' + yRangeTime(0) + ')')\n      .style(\"opacity\", 0.35)\n      .call(xAxis);\n\n    vis.append('svg:g')\n      .attr('class', 'y axis')\n      .attr('transform', 'translate(0,0)')\n      .style(\"opacity\", 0.35)\n      .call(yAxis);\n\n    var path = vis.append('svg:path')\n      .attr(\"stroke-width\", 2.0)\n      .attr(\"stroke\", \"grey\")\n      .attr(\"fill\", \"none\")\n      .attr(\"opacity\", 0.3)\n      .attr(\"d\", signal(data));\n\n    for (var i = 0; i < data.length / 4; i++)\n    {\n      vis.append(\"svg:circle\")\n          .attr(\"stroke\", \"none\")\n          .attr(\"fill\", \"grey\")\n          .attr(\"cx\", xRangeTime(i))\n          .attr(\"cy\", yRangeTime(data[i]))\n          .attr(\"r\", 2.5);  \n    }\n\n    vis.append('text')\n      .attr(\"text-anchor\", \"middle\")\n      .attr(\"x\", xRangeTime(16))\n      .attr(\"y\", 12)\n      .attr(\"stroke\", \"none\")\n      .attr(\"fill\", \"#555\")\n      .attr(\"font-size\", 12)\n      .attr(\"font-weight\", \"bold\")\n      .text(\"First Input Buffer\");\n  })();\n</script>\n\n[^1a]: Monophonic pitch tracking is a useful technique to have in your signal processing toolkit. It lies at the heart of applied products like [Auto-Tune](http://en.wikipedia.org/wiki/Auto-Tune), games like \"Rock Band,\" guitar and instrument tuners, music transcription programs, audio-to-MIDI conversion software, and query-by-humming applications.\n\n[^1b]: Every year, the best audio signal processing researchers algorithmically battle it out in the [MIREX](http://www.music-ir.org/mirex/wiki/MIREX_HOME) (Music Information Retrieval Evaluation eXchange) competition. Researchers from around the world submit algorithms designed to automatically transcribe, tag, segment, and classify recorded musical performances. If you become enthralled with the topic of audio signal processing after reading this article, you might want to submit an algorithm to the 2015 MIREX \"K-POP Mood Classification\" competition round. If cover bands are more your thing, you might instead choose to submit an algorithm that can identify the original title from a recording of a cover band in the \"Audio Cover Song Identification\" competition (this is much more difficult than you might expect).\n\n[^1c]: I'm not sure if such a thing has been attempted, but I think an interesting weekend could be spent applying techniques in computer vision to the problem of pitch detection and automatic music transcription.\n\n[^1d]: It's baffling to me why young students aren't shown the beautiful correspondence between circular movement and the trigonometric functions in early education. I think that most students first encounter sine and cosine in relation to right triangles, and this is an unfortunate constriction of a more generally beautiful and harmonious way of thinking about these functions.\n\n[^1e]: You may be wondering if adding additional tones to a fundamental makes the resulting sound polyphonic. In the introductory section, I made a big fuss about excluding polyphonic signals from our allowed input, and now I'm asking you to consider waveforms that consist of many individual tones. As it turns out, pretty much every musical note is composed of a fundamental and [overtones](http://en.wikipedia.org/wiki/Overtone). Polyphony occurs only when you have _multiple fundamental_ frequencies present in a sound signal. I've written a bit about this topic [here](http://jackschaedler.github.io/circles-sines-signals/sound2.html) if you want to learn more.\n\n[^1f]: It's actually often the case that the fundamental frequency of a given note is quieter than its overtones. In fact, humans are able to perceive fundamental frequencies that do not even exist. This curious phenomenon is known as the [\"Missing Fundamental\"](http://en.wikipedia.org/wiki/Missing_fundamental) problem.\n\n[^1g]: The autocorrelation can be computed using an FFT and IFFT pair. In order to compute the style of autocorrelation shown in this article (linear autocorrelation), you must first [zero-pad](http://jackschaedler.github.io/circles-sines-signals/zeropadding.html) your signal by a factor of two before performing the FFT (if you fail to zero-pad the signal, you will end up implementing a so-called _circular_ autocorrelation). The formula for the linear autocorrelation can be expressed like this in MATLAB or Octave: `linear_autocorrelation = ifft(abs(fft(signal)) .^ 2);`\n\n[^1h]: This sample rate would be ridiculous for audio. I'm using it as a toy example because it makes for easy visualizations. The normal sampling rate for audio is 44,000 hertz. In fact, throughout this whole article I've chosen frequencies and sample rates that make for easy visualizing.\n"
  },
  {
    "path": "2015-05-11-editorial.md",
    "content": "---\ntitle:  \"Editorial\"\ncategory: \"24\"\ndate: \"2015-05-11 12:00:00\"\ntags: editorial\n---\n\nWelcome to issue 24 of objc.io. This month is all about audio.\n\nWe already featured two issues about [photos](/issues/21-camera-and-photos/) and [video](/issues/23-video/) this year, so it follows that this month's issue completes our media series with the missing piece: audio.\n\nJack Schaedler recently wrote a fantastic [introduction to signal processing](https://jackschaedler.github.io/circles-sines-signals/index.html), and he opens this issue with an article explaining the concepts behind the \"Hello World!\" of audio processing apps: [tracking the pitch of a monophonic musical performance](/issues/24-audio/audio-dog-house/). (The beautiful illustrations are his work too!)\n\nNext up, Chris writes about [functional signal processing with Swift](/issues/24-audio/functional-signal-processing/), and Aaron looks at the topic of audio from a different perspective: [sound design for products](/issues/24-audio/sound-design/). Finally, if you're new to the sometimes confusing multitude of audio frameworks on Apple's platforms, Daniel and Florian provide a [high-level API overview](/issues/24-audio/audio-api-overview/).\n\nBest from Berlin,\n\nChris, Daniel, and Florian.\n"
  },
  {
    "path": "2015-05-11-functional-signal-processing.md",
    "content": "---\ntitle:  \"Functional Signal Processing Using Swift\"\ncategory: \"24\"\ndate: \"2015-05-11 10:00:00\"\ntags: article\nauthor:\n  - name: Chris Liscio\n    url: https://twitter.com/liscio\n---\n\nAs a long-time Core Audio programmer, Apple's introduction of Swift left me both excited and confused. I was excited by the prospect of a modern language built with performance in mind, but I wasn't entirely sure how functional programming could apply to \"my world.\" Fortunately for me, many have [explored and conquered][faust] this problem set already, so I decided to apply some of what I learned from those projects to the Swift programming language.\n\nSignals\n-------\n\nThe basic unit of signal processing is, of course, a signal. In Swift, I would declare a signal as follows:\n\n```swift\npublic typealias Signal = Int -> SampleType\n```\n\nYou can think of the `Signal` type as a discrete function in time that returns the value of the signal at that instant in time. In most signal processing texts, this is often denoted as `x[t]`, so it fits my world view.\n\nLet's define a sine wave at a given frequency:\n\n```swift\npublic func sineWave(sampleRate: Int, frequency: ParameterType) -> Signal {\n    let phi = frequency / ParameterType(sampleRate)\n    return { i in\n        return SampleType(sin(2.0 * ParameterType(i) * phi * ParameterType(M_PI)))\n    }\n}\n```\n\nThe `sineWave` function returns a `Signal`, which itself is a function that converts sample indices into output samples. I refer to these \"inputless\" signals as generators, since they generate signals out of nothing. \n\nBut I thought we were talking about signal _processing_? How do we modify a signal?\n\nNo high-level discussion about signal processing would be complete without demonstrating the application of gain (or a volume control):\n\n```swift\npublic func scale(s: Signal, amplitude: ParameterType) -> Signal {\n    return { i in\n            return SampleType(s(i) * SampleType(amplitude))\n    }\n}\n```\n\nThe `scale` function takes an input `Signal` called `s`, and returns a new `Signal` with the scalar applied. Every call to the `scale`d signal would return the same output of `s(i)`, scaled by the supplied `amplitude`. Pretty straightforward stuff, right? Well, you can only go so far with this construct before it starts to get messy. Consider the following:\n\n```swift\npublic func mix(s1: Signal, s2: Signal) -> Signal {\n    return { i in\n        return s1(i) + s2(i)\n    }\n}\n```\n\nThis allows us to compose two signals down to a single signal. We can even compose arbitrary signals:\n\n```swift\npublic func mix(signals: [Signal]) -> Signal {\n    return { i in\n        return signals.reduce(SampleType(0)) { $0 + $1(i) }\n    }\n}\n```\n\nThis can get us pretty far; however, a `Signal` is limited to a single \"channel\" of audio, and certain audio effects require much more complex combinations of operations to happen at once.\n\nProcessing Blocks\n-----------------\n\nWhat if we were able to make connections between signals and processors in a more flexible way, matching up more closely with the way we think about signal processing? There are popular environments, such as [Max][max] and [PureData][pd], which compose signal processing \"blocks\" to create powerful sound effects and performance tools.\n\n[Faust][faust] is a functional programming language that was designed with this idea in mind, and it is an incredibly powerful tool for building highly complex (and performant!) signal processing code. Faust defines a set of operators that allows you to compose blocks (or processors) together in a way that mimics signal flow diagrams.\n\nSimilarly, I have created an environment that effectively works the same way.\n\nUsing our earlier definition of `Signal`, we can expand on this concept:\n\n```swift\npublic protocol BlockType {\n    typealias SignalType\n    var inputCount: Int { get }\n    var outputCount: Int { get }\n    var process: [SignalType] -> [SignalType] { get }\n                        \n    init(inputCount: Int, outputCount: Int, process: [SignalType] -> [SignalType])\n}\n```\n\nA `Block` has a number of inputs, a number of outputs, and a `process` function that transforms the `Signal`s on its inputs to a set of `Signal`s on its outputs. Blocks can have zero or more inputs, and zero or more outputs.\n\nTo compose blocks serially, you could do the following:\n\n```swift\npublic func serial<B: BlockType>(lhs: B, rhs: B) -> B {\n    return B(inputCount: lhs.inputCount, outputCount: rhs.outputCount, process: { inputs in\n        return rhs.process(lhs.process(inputs))\n    })\n}\n```\n\nThis function effectively takes the output of the `lhs` block as the input to the `rhs` block and returns the result. It's like connecting a wire between two blocks. Things get a little more interesting when you run blocks in parallel:\n\n```swift\npublic func parallel<B: BlockType>(lhs: B, rhs: B) -> B {\n    let totalInputs = lhs.inputCount + rhs.inputCount\n    let totalOutputs = lhs.outputCount + rhs.outputCount\n                \n    return B(inputCount: totalInputs, outputCount: totalOutputs, process: { inputs in\n        var outputs: [B.SignalType] = []\n                                    \n        outputs += lhs.process(Array(inputs[0..<lhs.inputCount]))\n        outputs += rhs.process(Array(inputs[lhs.inputCount..<lhs.inputCount+rhs.inputCount]))\n                                                            \n        return outputs\n    })\n}\n```\n\nA pair of blocks running in parallel combines inputs and outputs to create a larger block. Consider a pair of `Block`s that produces sine waves together to create a [DTMF tone][dtmf], or a stereo delay `Block` that is a composition of two single-channel delay `Block`s. This concept can be quite powerful in practice.\n\nBut what about a mixer? How would we achieve a single-channel result from multiple inputs? We can merge blocks together using the following function:\n\n```swift\npublic func merge<B: BlockType where B.SignalType == Signal>(lhs: B, rhs: B) -> B {\n    return B(inputCount: lhs.inputCount, outputCount: rhs.outputCount, process: { inputs in\n        let leftOutputs = lhs.process(inputs)\n        var rightInputs: [B.SignalType] = []\n\n        let k = lhs.outputCount / rhs.inputCount\n        for i in 0..<rhs.inputCount  {\n            var inputsToSum: [B.SignalType] = []\n            for j in 0..<k {\n                inputsToSum.append(leftOutputs[i+(rhs.inputCount*j)])\n            }\n            let summed = inputsToSum.reduce(NullSignal) { mix($0, $1) }\n            rightInputs.append(summed)\n        }\n\n        return rhs.process(rightInputs)\n    })\n}\n```\n\nTo borrow convention from Faust, inputs are multiplexed such that the inputs of the right-hand side block come from outputs on the left-hand side modulo the number of inputs. For instance, three stereo tracks with a total of six channels would go into a stereo output block such that output channels 0, 2, and 4 are mixed (i.e. added) into input channel 0, and 1, 3, and 5 are mixed into input channel 1.\n\nSimilarly, you can do the opposite and split the outputs of a block:\n\n```swift\npublic func split<B: BlockType>(lhs: B, rhs: B) -> B {\n    return B(inputCount: lhs.inputCount, outputCount: rhs.outputCount, process: { inputs in\n        let leftOutputs = lhs.process(inputs)\n        var rightInputs: [B.SignalType] = []\n        \n        // Replicate the channels from the lhs to each of the inputs\n        let k = lhs.outputCount\n        for i in 0..<rhs.inputCount {\n            rightInputs.append(leftOutputs[i%k])\n        }\n        \n        return rhs.process(rightInputs)\n    })\n}\n```\n\nAgain, a similar convention is used with the outputs such that one stereo block being fed into three stereo blocks (accepting six total channels) would result in channel 0 going into the inputs 0, 2, and 4, with channel 1 going into inputs 1, 3, and 5.\n\nOf course, we don't want to get stuck with having to write all of this with long-hand functions, so I came up with this collection of operators:\n\n```swift\n// Parallel\npublic func |-<B: BlockType>(lhs: B, rhs: B) -> B\n\n// Serial\npublic func --<B: BlockType>(lhs: B, rhs: B) -> B\n\n// Split\npublic func -<<B: BlockType>(lhs: B, rhs: B) -> B\n\n// Merge\npublic func >-<B: BlockType where B.SignalType == Signal>(lhs: B, rhs: B) -> B\n```\n\n(I'm not quite happy with the \"Parallel\" operator definition, as it looks an awful lot like the symbol for \"Perpendicular\" in geometry, but I digress. Feedback is obviously welcome.)\n\nNow, with these operators, you can build some interesting \"graphs\" of blocks and compose them together. For instance, consider this [DTMF tone][dtmf] generator:\n\n```swift\nlet dtmfFrequencies = [\n    ( 941.0, 1336.0 ),\n    \n    ( 697.0, 1209.0 ),\n    ( 697.0, 1336.0 ),\n    ( 697.0, 1477.0 ),\n    \n    ( 770.0, 1209.0 ),\n    ( 770.0, 1336.0 ),\n    ( 770.0, 1477.0 ),\n    \n    ( 852.0, 1209.0 ),\n    ( 852.0, 1336.0 ),\n    ( 852.0, 1477.0 ),\n]\n\nfunc dtmfTone(digit: Int, sampleRate: Int) -> Block {\n    assert( digit < dtmfFrequencies.count )\n    let (f1, f2) = dtmfFrequencies[digit]\n    \n    let f1Block = Block(inputCount: 0, outputCount: 1, process: { _ in [sineWave(sampleRate, f1)] })\n    let f2Block = Block(inputCount: 0, outputCount: 1, process: { _ in [sineWave(sampleRate, f2)] })\n    \n    return ( f1Block |- f2Block ) >- Block(inputCount: 1, outputCount: 1, process: { return $0 })\n}\n```\n\nThe `dtmfTone` function runs two parallel sine generators and merges them into an \"identity block,\" which just copies its input to its output. Remember, the return value of this function is itself a block, so you could now reference this block as part of a larger system.\n\nAs you can see, there is a lot of potential in this idea. By creating an environment in which we can build and describe increasingly complex systems with a more compact and understandable DSL (domain specific language), we can spend less time worrying about the details of each individual block and how everything fits together.\n\nPracticality\n------------\n\nIf I were starting a project today that required the best possible performance and most rich set of functionality, I would run straight to [Faust][faust] to get going. I highly recommend that you spend some time with Faust if you are interested in pursuing this idea of functional audio programming.\n\nWith that said, the practicality of my ideas above rests heavily on Apple's ability to advance its compiler such that it can identify patterns in the blocks we define and turn them into smarter output code. Effectively, Apple needs to get Swift compiling more like Haskell does, where functional programming patterns can be collapsed down into vectorized operations on a given target CPU.\n\nFrankly, I feel that Swift is in the right hands at Apple and we will see the powerful kinds of ideas I presented above become commonplace and performant in the future.\n\nFuture Work\n-----------\n\nI will keep this [\"Functional DSP\" project](http://github.com/liscio/FunctionalDSP) up at GitHub if you would like to follow along or contribute ideas as I play around with the concepts. I plan to investigate more complex blocks, such as those that require FFTs to calculate their output, or blocks that require \"memory\" in order to operate (such as FIR filters, etc.)\n\nBibliography\n------------\n\nWhile writing this article, I stumbled upon the following papers that I recommend you delve further into if you are interested in this area of research. There are many more out there, but in the limited time I had, these seemed like really good starting points.\n\n* Thielemann, H. (2004). Audio Processing using Haskell.\n* Cheng, E., & Hudak, P. (2009). Audio Processing and Sound Synthesis in Haskell.\n\n\n[faust]: http://sourceforge.net/projects/faudiostream/\n[max]: https://cycling74.com/products/max/\n[pd]: http://puredata.info\n[dtmf]: http://en.wikipedia.org/wiki/Dual-tone_multi-frequency_signaling\n"
  },
  {
    "path": "2015-05-11-sound-design.md",
    "content": "---\ntitle:  \"Play, Fail, Iterate: Sound Design for Products\"\ncategory: \"24\"\ndate: \"2015-05-11 9:00:00\"\ntags: article\nauthor:\n  - name: Aaron Day\n    url: https://twitter.com/rx_tx\n---\n\nOur world gets noisier every day. This is the case for all modalities, and not just sound. Alerts and cues blink, beep, jingle, and vibrate from an ever-expanding array of sources. If there is a “war” for our attention, the only guarantee is that there will be no winners. Consider over-compressed and dynamically limited music where “everything is as loud as everything else.” This can be an impressive and even enjoyable experience for a limited amount of time. Over a longer period, however, the listener is left fatigued. If we create a product where modalities are unnecessarily stacked, e.g. you are looking at it and it blinks and it beeps and it vibrates, we will get the same effect: overloaded perceptual bandwidth, fatigue, and frustration with the product.\n\nWe can do better. Let's reduce the collective load on our perceptual bandwidth. How? By starting with a careful integration of sound with the other aspects of interaction design. Use it where it's needed, and even then, with respect for the idea that a given sound or alert comprising sound and some visual or haptic element might be experienced by someone many thousands of times or more during his or her exposure to our product. \n\nSound design is part of the interaction design process, and not something to be tacked on at the end. Experiment early and often, or in this case: play, fail, iterate.\n\nHere are five guidelines and associated concepts that help me design and integrate sound into products — digital, physical, or otherwise. They can be used however you like: as a checklist, manifesto, guideline, etc.  \n\n## Sound design for products and interaction shall:\n\n### 1. Not annoy the person using it.\n\nThis should be obvious, but it is truly a wicked problem — what is annoying to one person might be just right for someone else. There are many ways to solve this problem! To start, create sounds that play at appropriate times and volumes and are mindful of people’s needs. Above all else: when in doubt, leave it out. Some of the best sound design is NO sound design. Look for opportunities to create silence and reflection. \n\n### 2. Help a person do what he or she wants to do or let him or her know that something has happened.\n\n“If I touch the interface and don’t feel or hear anything change, how will I know I have succeeded in doing what I wanted to do?” Sound, of course, fills this gap. That said, we should take care to make interactive sound as relevant, accurate, and non-intrusive as possible. Take the time to test, then tune the synchronization of audio and moving elements. When that isn’t possible, deemphasize the correlation. Unless sound *has* to be there, leave it out.\n\nThe graphic below is a mental model that might help in implementing this idea.\n\nThe user experience of a given interaction can be seen as the sum of the physical, graphical, and audio interfaces over time. The ratio of the different modalities changes over time according to context and interaction flow (see graphic). There are cases such as targeted interactions, e.g. looking directly at the GUI where the AUI (audio user interface) part of the venn diagram might be very small or nonexistent, whereas for an “eyes-free” interaction such as an incoming phone call, the AUI would be much more important. These rations change over time depending on the use case or cases, and in the end provide the basis for a user’s experience.\n\n![The user experience of an interaction can be seen as the sum of the physical, graphical, and audio interfaces over time](/images/issue-24/sum-of-interfaces.svg)\n\n### 3. Reproduce well on the target.\n\nMany technical problems have non-technical origins. Getting UI sound and related audio to sound good on hardware is no exception. Don’t pick your soundset by listening to it through a nice stereo system or, conversely, through the speakers of a Dell laptop in an echoey conference room. Decisions concerning selection of sounds and their relative merits within a design system should be made on the target hardware, i.e. test it on the device(s) you are developing for. You might say: “But sound coming out of a handheld device (medical device, automobile, etc.) sucks!” That is exactly why the decision about what goes into a build should be made on target hardware. \n\nRestated: confirm stakeholder buy-in and integrate sound into the beginning of the design process. Communicate the risk(s) of bad, inappropriate, or poorly implemented sound design before all the project’s capacity is spoken for.\n\n\n### 4. Reflect the product tone of voice in a unique way.\n\nSome things have a face, others a voice, and a limited few have an aura. Does your product have an aura?\n\n\n### 5. Be future-proof.\n\nTo hit a moving target, you have to aim ahead of it. \n\nLess-than-optimal audio hardware, such as tiny little speakers, underpowered amplifiers, extremely band-limited frequency response, etc. has been the hobgoblin of sound design for many products for a long time — especially mobile devices. There were mobile devices that had good sound hardware before the iPhone, but none that had the same impact. After the iPhone came out, user expectations went up. They are continuing to go up, and this should be reflected in whatever sound design you incorporate into your product. Design for the “late now” and the future. Furthermore, interfaces are getting smaller, to the point of disappearing altogether, e.g. wearables (see graphic). Sound design just got some new pants.\n\n\n![Size of physical display vs. importance of auditive and tactile display](/images/issue-24/size-vs-auditive.svg)\n\n\n## Is That All? \n\nNo. There is more, but that is a start. These guidelines are no guarantee for successful integration of sound into products, but they will definitely point the development and design process in the right direction.\n\n## In Conclusion\n\n * The proper mix of beautiful sound and well-timed silence will make for happier customers.\n * Sound design is part of interaction design — not something added “on top.”\n * Take the time to test and tune. When that isn’t possible, deemphasize the correlation.\n * When in doubt, leave it out.\n * Confirm stakeholder buy-in and integrate sound into the beginning of the design process.\n * Don’t let app store reviews rule your life! You will never make everyone happy all the time, especially with sound. \n * Play. Fail. Iterate.\n"
  },
  {
    "path": "README.md",
    "content": "# objc.io articles\n\nThis repository contains all available articles from [objc.io](http://www.objc.io).\n\nIf you find mistakes or have suggestions for improvements of the content, please don't hesitate to file issues or even better: send us a pull request!\n\nIf you're writing an article, see the page [How to write an article](https://github.com/objcio/articles/wiki/How-to-write-an-article) with some tips and links.\n"
  }
]