<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>More of a programming nerd than is strictly healthy. See also {nevyn.nu, thirdcog.eu, twitter}</description><title>Overooped</title><generator>Tumblr (3.0; @nevyn)</generator><link>http://overooped.com/</link><item><title>Faking generics in ObjC</title><description>&lt;p&gt;If you have every touched C++, Java or C# and then moved to Objective-C, you have at some point written one of the following in a source file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;NSArray/*&amp;lt;MyThing&amp;gt;*/ *_queuedThings;
NSDictionary *_thingMap; // contains MyThing
-(MAFuture*)fetchThing; // wraps a MyThing
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With &lt;a href="https://github.com/nevyn/TCTypeSafety"&gt;&lt;code&gt;TCTypeSafety&lt;/code&gt;&lt;/a&gt;, you can write pretend that you&amp;#8217;re writing in a language with generics and write:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@interface MyThing : NSObject
...
@end
TCMakeTypeSafe(MyThing)

(later...)
NSArray&amp;lt;MyThing&amp;gt; _queuedThings;&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Why?!&lt;/h2&gt;

&lt;p&gt;Assume that we have some kind of &lt;code&gt;Future&lt;/code&gt; class. Assume also that we have a factory that returns &lt;code&gt;Future&lt;/code&gt;s that wrap the asynchronous creation of a &lt;code&gt;MyThing&lt;/code&gt;. We might want to define the interface for such a factory like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@interface MyThingFactory : NSObject
- (TCFuture&amp;lt;MyThing&amp;gt; *)fetchLatestThing;
@end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Later, when we use the factory:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;MyThingFactory *fac = [MyThingFactory new];
NSString *thing = [fac fetchLatestThing].typedObject;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&amp;#8230; actually generates a compiler warning, since &lt;code&gt;typedObject&lt;/code&gt; returns &lt;code&gt;MyThing&lt;/code&gt;, not &lt;code&gt;NSString&lt;/code&gt;. (Note that we did not have to give &lt;code&gt;TCFuture&lt;/code&gt; knowledge of the &lt;code&gt;MyThing&lt;/code&gt; type to get this benefit, or modify it in any way except make it &lt;code&gt;TCTypeSafety&lt;/code&gt; compatible).&lt;/p&gt;

&lt;p&gt;This way, we can ensure that even though we have wrapped our &lt;code&gt;MyThing&lt;/code&gt;s in a &lt;code&gt;TCFuture&lt;/code&gt;, we haven&amp;#8217;t thrown away type safety, so that if we want to change the return type of &lt;code&gt;fetchLatestThing&lt;/code&gt;, we can just do so in the header and then fix all the compiler warnings, rather than going through every single usage of &lt;code&gt;fetchLatestThing&lt;/code&gt; and fix any now invalid assumptions on the return type.&lt;/p&gt;

&lt;p&gt;This is also useful for collections such as arrays and dictionaries:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;NSMutableArray&amp;lt;MyThing&amp;gt; *typedArray = (id)[NSMutableArray new];
[typedArray insertTypedObject:@"Not a MyThing" atIndex:0]; // compiler warning! NSString ≠ MyThing
NSNumber *last = typedArray.lastTypedObject; // compiler warning! NSNumber ≠ MyThing
NSLog(@"Last typed thing: %@", last);
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;What?!&lt;/h2&gt;

&lt;p&gt;The syntax for indicating protocol conformance of a variable is the same that you would use for template/generics specialization in other language. Also, the namespace for protocols is separate from the namespace for classes, so we can have a protocol with the same name as a class. So if we implement a protocol with getters and setters that take and return the type that we are interested in, we can get type safety. For example, we can create the protocol &lt;code&gt;MyThing&lt;/code&gt; as such:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@protocol MyThing
- (MyThing*)typedObjectAtIndex:(NSUInteger)index;
- (void)addTypedObject:(MyThing*)thing;
@end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We then need to add support for these methods to &lt;code&gt;NSArray&lt;/code&gt; and &lt;code&gt;NSMutableArray&lt;/code&gt;. However, the type we are specializing on is only a compile time hint and does not affect the type of the instance, so in the implementation, we can just say that these return &amp;#8216;id&amp;#8217;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@implementation NSArray (SPTypeSafety)
- (id)typedObjectAtIndex:(NSUInteger)index;
{
    return [self objectAtIndex:index];
}
@end

@implementation NSMutableArray (SPTypeSafety)
- (void)addTypedObject:(id)thing;
{
    [self addObject:thing];
}
@end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Tada! Write a macro that generates this protocol for you (like, say, &lt;code&gt;TCMakeTypeSafe&lt;/code&gt;), and you have instant type safety.&lt;/p&gt;

&lt;h2&gt;There must be downsides.&lt;/h2&gt;

&lt;p&gt;Absolutely. You can only &amp;#8220;specialize&amp;#8221; on a single class: you can&amp;#8217;t create some generic facility that would let you specialize both the key and the value of an NSDictionary. You have to use weird selectors such as &lt;code&gt;-[NSArray typedObjectAtIndex:]&lt;/code&gt;, since the protocol conformance sadly does not override the method signature for your array instance, and using &lt;code&gt;-[NSArray objectAtIndex:]&lt;/code&gt; will still give you type-unsafe return values.&lt;/p&gt;

&lt;p&gt;Worst of all, by applying the &lt;code&gt;TCMakeTypeSafe&lt;/code&gt; macro on your class, it will suddenly look like it has the interface of both a to-one accessor, &lt;code&gt;NSArray&lt;/code&gt;, &lt;code&gt;NSDictionary&lt;/code&gt;, and whatever else you add support for in &lt;code&gt;TCMakeTypeSafe&lt;/code&gt;. Thus, you wouldn&amp;#8217;t get compile time warnings if you changed your &lt;code&gt;NSArray&lt;/code&gt; into an &lt;code&gt;NSDictionary&lt;/code&gt;, as you normally would. I &lt;em&gt;think&lt;/em&gt; this trade-off is worth it, but I&amp;#8217;m not sure.&lt;/p&gt;

&lt;h2&gt;Where can I get it?&lt;/h2&gt;

&lt;p&gt;At &lt;a href="https://github.com/nevyn/TCTypeSafety"&gt;nevyn/TCTypeSafety&lt;/a&gt; at GitHub.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Update&lt;/b&gt;: turns out Jonathan Sterling had &lt;a href="http://www.jonmsterling.com/posts/2012-05-01-refinement-protocols-another-approach-to-typesafe-collections.html"&gt;almost exactly the same idea&lt;/a&gt; only a few days ago.&lt;/p&gt;</description><link>http://overooped.com/post/22516989979</link><guid>http://overooped.com/post/22516989979</guid><pubDate>Sun, 06 May 2012 16:51:00 +0200</pubDate><category>cocoa</category><category>flattr</category><category>objc</category><category>objectivec</category><category>coding</category></item><item><title>Low Verbosity KVO</title><description>&lt;h2&gt;DRY Cocoa: SPLowVerbosity&lt;/h2&gt;
&lt;p&gt;I love Objective-C. It&amp;#8217;s a very explicit language: there is no magic in the language. If you want such magic as distributed objects, model classes generated at runtime, generic change notification, or heck, even referenced counted memory management, you can implement it yourself in a library. It&amp;#8217;s also by convention a very verbose language. If the method name doesn&amp;#8217;t say exactly what the method does, the author is doing it wrong.&lt;/p&gt;

&lt;p&gt;However, there are some things we type over and over again every day, whose verbosity does not make the code more readable but only serves to pad the code file with useless characters. These are in particular array and dictionary literals, and formatted strings.&lt;/p&gt;

&lt;p&gt;ObjC wizard &lt;a href="http://jens.mooseyard.com/"&gt;Jens Alfke&lt;/a&gt; wrote &lt;a href="https://bitbucket.org/snej/myutilities"&gt;MYUtilities&lt;/a&gt; quite a while ago, and in particular &lt;a href="https://bitbucket.org/snej/myutilities/src/5f25fbb44fa6/CollectionUtils.h"&gt;CollectionUtils&lt;/a&gt;. This header defines a few very handy macros such as &lt;code&gt;$array(...)&lt;/code&gt;, &lt;code&gt;$dict(...)&lt;/code&gt; and &lt;code&gt;$sprintf(...)&lt;/code&gt;. &lt;code&gt;$array&lt;/code&gt; is simply a shorthand for &lt;code&gt;[NSArray arrayWithObjects:..., nil]&lt;/code&gt; (note the extra &lt;code&gt;nil&lt;/code&gt;, freeing you from typing that yourself every time, and also avoiding a possible crash). &lt;code&gt;$dict&lt;/code&gt; is &lt;code&gt;NSDictionary&lt;/code&gt;&amp;#8217;s constructor, with the arguments in the right order (key, value, key, value, …). &lt;code&gt;$sprintf&lt;/code&gt; is &lt;code&gt;[NSString stringWithFormat:]&lt;/code&gt;. It may seem trivial, but this simple header has saved me tons and tons of typing, and surely from some silly bugs as well.&lt;/p&gt;

&lt;p&gt;For more C++ friendliness, and to not have to depend on the rest of MYUtilities, &lt;a href="https://github.com/nevyn/SPSuccinct/blob/master/SPSuccinct/SPLowVerbosity.h"&gt;I wrote my own, called &lt;b&gt;SPLowVerbosity&lt;/b&gt;, with mostly the same things&lt;/a&gt;. The details are not interesting, so I won&amp;#8217;t list the code; click through if you are curious.&lt;/p&gt;

&lt;p&gt;This is a hack, and while it is very convenient, it is not pretty. Objective-C needs in-language literals for arrays, sets, dictionaries and number objects.&lt;/p&gt;

&lt;p&gt;I do believe that such literals are now inevitable, though. With ARC, what I wrote in the first paragraph is no longer entirely true. Dealloc now magically calls super. Memory management is implicit and part of the language, not explicit and manual. The sanctity of Objective-C&amp;#8217;s simplicity has been violated, parts of Cocoa has been moved into the language. Moving more parts of Cocoa into the language (e g exposing APIs saying &amp;#8216;this is the class that should be instantiated when I use a dictionary literal&amp;#8217;) no longer has a high religious price, and I bet we&amp;#8217;ll see it at next year&amp;#8217;s WWDC.&lt;/p&gt;

&lt;h2&gt;Object-Oriented KVO: SPKVONotificationCenter&lt;/h2&gt;
&lt;p&gt;During the past few years, my nose has started to really pick up on a code smell I didn&amp;#8217;t feel before: unencapsulated concepts. What I mean by that is an API concept or contract that is only visible in comments and documentation, thus only &lt;u&gt;implicit&lt;/u&gt; by knowledge of that documentation when you read code using the API, instead of exposing the concept as a construct the language can help you manage.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;Mutex locking as two separate functions that must always be paired. This is an easy to make mistake, and can be very difficult to fix in complexly branching code. Static analyzation can find such errors, but is in my opinion the wrong approach.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;UITableView&lt;/code&gt;&amp;#8217;s &lt;code&gt;beginUpdates&lt;/code&gt;/&lt;code&gt;endUpdates&lt;/code&gt; must always be called in a pair, and all the updates you make in that pair must match the changes you are doing to the data source.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NSNotificationCenter&lt;/code&gt; requires you to register for a notification with a {sender, receiver, name} triplet, and reuse that triplet to destroy the subscription. In practice what you have here is an implicit object that you must manage using an awkward object triplet, rather than an actual object.&lt;/li&gt;
&lt;li&gt;Key-Value Observing, with the same defect as with notifications, plus that callbacks are dispatched to the same instance method, forcing you to use the triplet plus a context pointer as the identifier again.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Instead of the above concepts being implicit and only visible in documentation, several of them can be represented as objects and closures, thus making it impossible to do wrong.&lt;/p&gt;

&lt;p&gt;The realization is not new, of course. It&amp;#8217;s quite common to use &lt;a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization"&gt;RAII&lt;/a&gt; patterns in C++ to encapsulate concepts in code, giving them an explicit start point and a managed (and deterministic) end point.&lt;/p&gt;

&lt;p&gt;But this is Objective-C, not C++, so we don&amp;#8217;t have RAII. We do have objects, however, and if we use them to actually represent our objects, we can clean up so much code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/nevyn/SPSuccinct/blob/master/SPSuccinct/SPKVONotificationCenter.h"&gt;&lt;b&gt;&lt;code&gt;SPKVOObservation&lt;/code&gt;&lt;/b&gt;&lt;/a&gt; is an object that represents a KVO subscription. When this object is created, you know you have your subscription. When this object disappears, you know the subscription is gone. By managing instances of this class like I would any other instance variable, I know I&amp;#8217;m following KVO&amp;#8217;s subscribe/unsubscribe contract. (With ARC, I don&amp;#8217;t even have to manage the ivar).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SPKVONotificationCenter&lt;/code&gt; also has dispatch to a given selector instead of the catch-all &lt;code&gt;-[NSObject observeValueForKeyPath:ofObject:change:context:]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;(As for solutions for the rest of the examples: a closure can be used for the mutex case if in the current scope, otherwise you might want RAII (which is possible with GNU extensions even in C). For the table view, I&amp;#8217;d prefer bindings, but doing to-many KVO and bindings can be really hard, so I doubt we&amp;#8217;ll be seeing it on iOS in the near future.)&lt;/p&gt;

&lt;h2&gt;DRY KVO: SPDepends&lt;/h2&gt;

&lt;p&gt;I love &lt;a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/"&gt;KVO&lt;/a&gt;. Basically every library I&amp;#8217;ve ever used has their own implementation of notifications on attribute changes. Such an implementation is of course not difficult, but because there is no generic system for it in other languages/platforms, you can&amp;#8217;t build a framework for working with change notifications generically. Recognizing that this is a generic concept, and then as good as building it into the language is genius, and has given us wonders such as bindings.&lt;/p&gt;

&lt;p&gt;Outside of their use in bindings where you have tool help, KVO&amp;#8217;s API is pretty bad, and working with it is almost painfully verbose (made worse by the single callback point, spreading your code out all over the file). What I really wanted to do was to unify the two above sections for a very simple, non-verbose and object oriented approach to KVO. The result is &lt;a href="https://github.com/nevyn/SPSuccinct/blob/master/SPSuccinct/SPDepends.h"&gt;&lt;b&gt;&lt;code&gt;SPDepends&lt;/code&gt;&lt;/b&gt;&lt;/a&gt;. Magic is taken to the next level; we are approaching dark arts. Whether what I&amp;#8217;m about to present is actually a good idea or not, I leave as an exercise for the reader. First, the header: &lt;!--Dashboard readers, this code is only visible on the blog entry. Head over to read it.--&gt;&lt;/p&gt;

&lt;script src="http://gist-it.appspot.com/github/nevyn/SPSuccinct/raw/master/SPSuccinct/SPDepends.h"&gt;&lt;/script&gt;&lt;p&gt;tl;dr: SPDepends lets you give it a list of {object, key path(s)} pairs that one of your properties depend on. When the value of any of the given key paths changes, a given closure is called, letting you recalculate the dependent property (or whatever you want to do). It&amp;#8217;s about as far as you can go with KVO without actually building bindings.&lt;/p&gt;

&lt;p&gt;There is one additional piece of magic related to memory management (this was before ARC). By providing an owner and associationName, the dependency is assigned as an associatedObject, so that it will automatically be cleaned up when the owner object dies. Defining a dependency again with the same name will also throw away the old dependency. If this is not desired, you can just not provide the association name, and instead manage the returned &lt;code&gt;SPKVOObservation&lt;/code&gt; object on your own.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;
&lt;script src="http://gist-it.appspot.com/github/nevyn/SPSuccinct/raw/master/SPSuccinct/main.m"&gt;&lt;/script&gt;&lt;p&gt;Output:&lt;/p&gt;
&lt;script src="https://gist.github.com/1074637.js?file=output.txt"&gt;&lt;/script&gt;&lt;p&gt;Note how short the $depends macro makes all your KVO work. The macro also defines a block-safe &lt;code&gt;self&lt;/code&gt; variable &lt;code&gt;selff&lt;/code&gt; that won&amp;#8217;t create a reference cycle.&lt;/p&gt;

&lt;p&gt;The equivalent classical KVO code would be many lines longer. However, this macro takes several steps away from how Cocoa code is normally written. Is the syntax too obscure? Is the code still readable for normal human beings?&lt;/p&gt;</description><link>http://overooped.com/post/7456709174</link><guid>http://overooped.com/post/7456709174</guid><pubDate>Sun, 10 Jul 2011 18:50:00 +0200</pubDate><category>cocoa</category><category>cocoadev</category><category>dev</category><category>ios</category><category>iosdev</category><category>kvo</category><category>mac</category><category>macdev</category><category>objc</category><category>faves</category><category>flattr</category></item><item><title>UIKit: Hide the keyboard without a reference to the currently focused text field</title><description>&lt;p&gt;Googling for answers to UIKit or iOS/iPhone programming problems today resembles googling for answers to JavaScript problems. There is so much horribly, horribly broken code out there, being promoted as the way to do things. 

Ranting aside, today&amp;#8217;s problem is writing your own view controller container, and noticing that focused UI elements don&amp;#8217;t dismiss their keyboard in response to viewWillDisappear: or similar. If this was MacOS, or AppStore approval didn&amp;#8217;t exist, you&amp;#8217;d just do [self.view.window.firstResponder resignFirstResponder]. You can&amp;#8217;t do that though, firstResponder is private on window, don&amp;#8217;t ask me why. &lt;a href="http://stackoverflow.com/questions/1823317/how-do-i-legally-get-the-current-first-responder-on-the-screen-on-an-iphone/2887690#2887690"&gt;cdyson37 on StackOverflow&lt;/a&gt; however, found the public API &lt;code&gt;-[UIView(UITextField) endEditing:]&lt;/code&gt; hidden in a category in UITextField.h, that supposedly looks recursively through the receiver&amp;#8217;s children for the first responder, and if found, resigns it. Perfect! A copy-pasteable code snippet for lazy googlers:

&lt;pre&gt;&lt;code&gt;[self.view endEditing:YES]&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;</description><link>http://overooped.com/post/3898655445</link><guid>http://overooped.com/post/3898655445</guid><pubDate>Wed, 16 Mar 2011 15:14:21 +0100</pubDate><category>iphone</category><category>iphonedev</category><category>cocoa</category><category>uikit</category><category>programming</category></item><item><title>Media keys hook in Mac OS X</title><description>&lt;p&gt;There is no nice (or is it even official?) way of detecting and handling the media keys on the user&amp;#8217;s keyboard. One can intercept events with type NSSystemDefined and subtype 8 in -[NSApplication sendEvents:], but this has major problem: any other applications that listen to the media keys will receive these events too. This means for example that if only Spotify is running and you want to pause your music, iTunes will start when you press play/pause. If you have VLC running in the background, it will also start playing.&lt;/p&gt;

&lt;p&gt;Apple has solved this problem internally by having their media key using applications cooperate and resign media key controls to the application that was in the foreground most recently. However, there is no way for third party applications to join this cooperation.&lt;/p&gt;

&lt;p&gt;I have implemented a workaround that does this using a CGEventTap in the Cocoa class &lt;a href="https://github.com/nevyn/SPMediaKeyTap"&gt;SPMediaKeyTap&lt;/a&gt;. The advantage of using an event tap instead of just intercepting the events in -[NSApplication sendEvent:] is that you have the power to throw the event away. This way, we can decide to &amp;#8220;own&amp;#8221; the media keys and be the only app that listens to them.&lt;/p&gt;

&lt;p&gt;This class is smart enough to resign the media key event tap whenever another application that we know will want to use the media keys becomes active, and keeps track of which media key using app was recently started.&lt;/p&gt;

&lt;p&gt;If everyone uses this class, and everyone add each other&amp;#8217;s bundle ID to that list of whitelisted bundle ID&amp;#8217;s, we&amp;#8217;ll get nice behavior from all apps. An even better solution would be if &lt;a href="http://www.openradar.me/8817368"&gt;Apple provided a way of acquiring the media keys&lt;/a&gt;.&lt;/p&gt;</description><link>http://overooped.com/post/2593597587</link><guid>http://overooped.com/post/2593597587</guid><pubDate>Tue, 04 Jan 2011 09:42:00 +0100</pubDate><category>faves</category><category>mac</category><category>macdev</category><category>cocoa</category><category>programming</category><category>spotify</category></item><item><title>Filtering a UITableView, and keyboard wonkiness</title><description>&lt;p&gt;Hey, another bug that took half a day to fix. That always deserves a blog entry.&lt;/p&gt;

&lt;p&gt;So I have a UITableView, and on it I have a tableHeaderView containing a UISearchBar. The UISearchBar filters the contents of the UITableView, and thus at every key press I have to reloadData (or more specifically, I -[UITableView reloadSections:withRowAnimation:]). For some reason, this calls setUserInteractionEnabled:NO on the UITableView, which in turn makes the UISearchBar&amp;#8217;s UIFieldEditor resignFirstResponder, which makes the keyboard collapse. After the reload, user interaction is restored, UISearchBar gets focus, and the keyboard comes up.&lt;/p&gt;

&lt;p&gt;This is rather embarrassing: I even subclassed UITableView, disabling setUserInteractionEnabled, before I realized that right there, before my eyes, is a UISearchBar delegate method called -[UISearchBarDelegate searchBarShouldEndEditing:]. Just add a bool ivar to the view controller saying whether the search bar may be resign key, set it to false before refreshing, refresh, and set it to true, and in the above delegate, return the ivar bool. Done, no more disappearing and reappearing keyboards.&lt;/p&gt;

&lt;p&gt;Oh, and the code:
&lt;/p&gt;&lt;pre&gt;&lt;code&gt;
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
	_searchBarMayResign = NO;
	[self.tableView reloadData]; // or equivalent
	_searchBarMayResign = YES;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;
{
	return _searchBarMayResign;
}
&lt;/code&gt;&lt;/pre&gt;</description><link>http://overooped.com/post/308091622</link><guid>http://overooped.com/post/308091622</guid><pubDate>Wed, 30 Dec 2009 17:15:00 +0100</pubDate><category>bugs</category><category>cocoa</category><category>iphone</category><category>flattr</category></item><item><title> The write-compile-test cycle is very very tedious when doing...</title><description>&lt;img src="http://24.media.tumblr.com/tumblr_krigxd4t9I1qz4vmko1_500.png"/&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt; The write-compile-test cycle is very very tedious when doing iPhone development on the device, because the “compile” step needs to include “install on device”, which can be very, very slow. It can take up to a minute, depending on how many apps you have installed and the current cycle of the moon. Imagine, then, doing tiny interface changes and you want to see how that tiny fix changes the UI (which sometimes you really need to do on the device to get a feel for it) — ten tries changing an animation delay could mean ten minutes of just waiting for installation. If you’re jailbroken, there is a&lt;strike&gt;n easier&lt;/strike&gt; faster way.&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;In Cydia, install ldid, rsync and ssh&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.translucentcode.org/mick/archives/000230.html"&gt;Follow this guide to install an ssh key pair&lt;/a&gt; on your iPhone, so that the script can install the app without asking for password.&lt;/li&gt;
&lt;li&gt;Add an additional build target to your app, and call it “Upload” or something.&lt;/li&gt;
&lt;li&gt;Make that build target depend on your real app (as in the picture above)&lt;/li&gt;
&lt;li&gt;Add a “run shell script” build phase, and give it this script:
&lt;pre&gt;&lt;code&gt;export DEVICE_NAME=Mishimazu.local
rsync -avz "${CONFIGURATION_BUILD_DIR}/${PROJECT}.app" root@${DEVICE_NAME}:/Applications/
ssh root@${DEVICE_NAME} ldid -s "/Applications/${PROJECT}.app/${PROJECT}"&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;Replace “Mishimazu” with the name of your iPhone.&lt;/li&gt;
&lt;li&gt;Change your active target to “Upload”, and build as usual.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;A few notes though.&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;This script does not launch the app, you’ll have to do that yourself.&lt;/li&gt;
&lt;li&gt;You don’t get the console routed to Xcode. Open up the Console in the Organizer for a workaround (not as good though)&lt;/li&gt;
&lt;li&gt;Xcode debugger won’t work&lt;/li&gt;
&lt;li&gt;File locations might have changed! You no longer have your private uuid bundle with your documents, but rather need to place documents and related things in /var/mobile. It’s possible NSSearchPathForDirectoriesInDomains will figure the right paths out for you, I haven’t tested; just make sure you’re aware of this&lt;/li&gt;
&lt;li&gt;You are no longer sandboxed. This might change assumptions you do in code&lt;/li&gt;


&lt;p&gt;In short, only use this deployment method for simple things, and install as usual when you need to really make sure things still work as they should, before a beta or appstore deploy. Of course, if you’re targeting the Cydia store or similar, that doesn’t apply.&lt;/p&gt; &lt;/ul&gt;</description><link>http://overooped.com/post/212929090</link><guid>http://overooped.com/post/212929090</guid><pubDate>Wed, 14 Oct 2009 17:41:00 +0200</pubDate><category>flattr</category><category>iphone</category><category>ios</category><category>development</category><category>iphonedev</category><category>cocoa</category></item><item><title>xib + subversion + automerge = pain</title><description>&lt;p&gt;Don&amp;#8217;t let Subversion automerge your xib files. This error isn&amp;#8217;t even on the freakin&amp;#8217; google:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;ibtool: some object IDs were duplicated&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;You can work around this by 1) telling subversion the file can&amp;#8217;t be merged by setting its mime type to application/octet-stream 2) requiring the file to be svn locked before you edit it for an exclusive lock. You can tell your subversion client to automatically give xib files these properties by adding a few lines to your ~/.subversion/config file.&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;code&gt;
## Under [miscellany]
enable-auto-props = yes

## under [auto-props]
*.nib = svn:mime-type=application/octet-stream;svn:needs-lock=*
*.xib = svn:mime-type=application/octet-stream;svn:needs-lock=*
&lt;/code&gt;&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;Of course, your merged xib is still beyond saving, so just fetch the previous version and redo all your changes! (also, &lt;a href="http://svnbook.red-bean.com/en/1.5/svn.advanced.locking.html"&gt;more on subversion locking&lt;/a&gt; and &lt;a href="http://blogs.open.collab.net/svn/2007/07/from-the-questi.html"&gt;exclusive checkouts&lt;/a&gt;)&lt;/p&gt;</description><link>http://overooped.com/post/112276157</link><guid>http://overooped.com/post/112276157</guid><pubDate>Sun, 24 May 2009 13:42:00 +0200</pubDate><category>cocoa</category><category>interfacebuilder</category><category>macdev</category><category>programming</category><category>subversion</category><category>flattr</category></item><item><title>NSFileHandle Considered Harmful [Updated]</title><description>&lt;p&gt;&lt;em&gt;&lt;u&gt;Update 20090627&lt;/u&gt;: This bug &lt;a href="http://openradar.me/6535050"&gt;has been fixed&lt;/a&gt; in 10.6. I&amp;#8217;d still only recommend using it for very simple cases, but at least it works now!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;NSFileHandle has a bug where the calling thread will lock up indefinitely if a data of size &amp;gt;4096 is requested. Reduced case:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
#import &lt;foundation&gt;

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSFileHandle *fh = [[NSFileHandle alloc] initWithFileDescriptor:fileno(fopen("/dev/random", "r"))];
    
    NSLog(@"Reading 4096 bytes... This will succeed.");
    [fh readDataOfLength:4096];
    NSLog(@"Reading 4097 bytes... This will lock for infinity");
    [fh readDataOfLength:4097];
    NSLog(@"This will never be printed.");
    
    [fh closeFile];
    [fh release];

    [pool drain];
    return 0;
}
&lt;/foundation&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(Warning: running this program might lock up your computer; killing the rogue process is difficult as NSFileHandle will read an infinite amount of data from /dev/random, swapping out your OS.)

&lt;/p&gt;&lt;p&gt;This is extra dangerous if your read length is dynamic, such as in a protocol where the first incoming bytes defines how long the upcoming chunk to read is.&lt;/p&gt;

&lt;p&gt;Solution: Use the C API for working with file descriptors instead, such as read() and write(). A file descriptor can be extracted with [NSFileHandle fileDescriptor] if you receive one from another API.&lt;/p&gt;

&lt;p&gt;(&lt;a href="http://openradar.me/6535050"&gt;This bug has been reported&lt;/a&gt;.)&lt;/p&gt;</description><link>http://overooped.com/post/73721688</link><guid>http://overooped.com/post/73721688</guid><pubDate>Wed, 28 Jan 2009 10:44:00 +0100</pubDate><category>cocoa</category><category>nsfilehandle</category><category>bugs</category></item><item><title>[Cocoa] Less code when working with delegates</title><description>&lt;p&gt;The usual pattern is to do this in your class that has a delegate:&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
-(void)frobnicate;
{
  ...
  BOOL extraJuicy = NO;
  if(delegate &amp;amp;&amp;amp; [delegate respondsToSelector:@selector(frobnicatorShouldAddJuice:)])
    extraJuicy = [delegate frobnicatorShouldAddJuice:self];
  if(extraJuicy)
    ...
  ...
}
&lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;for every delegation method, which gets old, fast.&lt;/p&gt;


&lt;p&gt;With &lt;a href="http://github.com/nevyn/delegationhelper/tree/master" title="nevyn's delegationhelper at master - GitHub"&gt;DelegationHelper&lt;/a&gt;, you can do this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
// Setup
@interface FrobnicatorDelegationHelper : DelegationHelper &lt;frobnicatordelegate&gt; {} @end

@interface Frobnicator... { FrobnicatorDelegationHelper *delegate; ... } ...@end

@implementation
-(void)setDelegate:(id&lt;frobnicatordelegate&gt;)delegate_
{
  delegate = [[DelegationHelper alloc] init];
  delegate.delegate = delegate_;
  
  [delegate setDefaultDelegationObject:BOOLobj(YES) forSelector:(frobnicatorShouldAddJuice:)];
}

// And then just...
-(void)frobnicate;
{
  ...
  if([delegate frobnicateShouldAddJuice:self])
    ...
  ...
}
&lt;/frobnicatordelegate&gt;&lt;/frobnicatordelegate&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Okay, the setup requires three extra lines and one line per selector (for defaults), but we&amp;#8217;ve cut down the actual 
delegate calls from four lines to one! So if you have more than a single delegate method for your class, the 
DelegationHelper will help you.&lt;/p&gt;

&lt;p&gt;For more samples, see &lt;a href="http://github.com/nevyn/delegationhelper/blob/e4801e9c50f784d072a5c9e9054885dd281bd7a3/main.m" title="main.m at e4801e9c50f784d072a5c9e9054885dd281bd7a3 from nevyn's delegationhelper - GitHub"&gt;main.m&lt;/a&gt; in &lt;a href="http://github.com/nevyn/delegationhelper/tree/master" title="nevyn's delegationhelper at master - GitHub"&gt;my repository&lt;/a&gt;.&lt;/p&gt;</description><link>http://overooped.com/post/72310958</link><guid>http://overooped.com/post/72310958</guid><pubDate>Thu, 22 Jan 2009 14:06:00 +0100</pubDate><category>cocoa</category><category>delegates</category></item><item><title>My brother needed some math homework help figuring out quadratic...</title><description>&lt;img src="http://25.media.tumblr.com/MLhBbCcsbiq1ds67zmSJnx8Po1_500.png"/&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;&lt;a href="http://flickr.com/photos/nevyn/2783672135/"&gt;My brother&lt;/a&gt; needed some math homework help figuring out quadratic equations. I really want to teach him programming and I’ve shown him some very basic C before, so together we &lt;a href="http://github.com/nevyn/myass/tree/master"&gt;threw together a quick app&lt;/a&gt; solve quadratic formulas with the pq-formula :) [note: he chose project name :P]&lt;/p&gt;

&lt;p&gt;(If you are a beta tester for any of &lt;a href="http://thirdcog.eu/"&gt;my apps&lt;/a&gt;, you can even &lt;a href="http://github.com/nevyn/myass/raw/5493b1a13322f8086c3119874220bf87e4eaf608/Releases/Leyory%201.0.zip"&gt;download and install the app&lt;/a&gt; and try it yourself!)&lt;/p&gt;</description><link>http://overooped.com/post/70513929</link><guid>http://overooped.com/post/70513929</guid><pubDate>Wed, 14 Jan 2009 22:15:00 +0100</pubDate><category>cocoa</category><category>iphone</category></item></channel></rss>

